Skip to content

Commit

Permalink
[test] build test frame and add process_front_matter(author part) uni…
Browse files Browse the repository at this point in the history
…t test.
  • Loading branch information
zweix123 committed Oct 27, 2024
1 parent 97c1cb0 commit 6c0cd0c
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 0 deletions.
Empty file added test/.gitkeep
Empty file.
10 changes: 10 additions & 0 deletions test/regression-testing.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash

# TODO: Compatible with Windows operating system
jyyslice_md_path=$(dirname $(dirname $(readlink -f $0)))
export JYYSLICE_MD_PATH=$jyyslice_md_path

# unit
poetry run python unit/author.py

# TODO: other tests
135 changes: 135 additions & 0 deletions test/unit/author.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
# unit test for function process_front_matter
# process_front_matter input is st.content
# process_front_matter output is st.author_template


import os
import sys
import unittest

from jinja2 import Template # lib used in converter.py

# add project root to sys.path, otherwise, the import below will fail
sys.path = [os.environ["JYYSLICE_MD_PATH"]] + sys.path


import src.settings as st
from src.converter import process_front_matter

# 这里没有使用项目中的template, 而是针对其使用的模版变量专门测试
#! 假如未来模版内容修改, 当前单元测试无法感知, 导致作为回归测试的话会落后版本
AUTHOR_TEMPLATE_STRING = """
{{ author.name }}
{{ author.url }}
{% for department in departments %}
{{ department.name }}
{{ department.url }}
{{ department.img }}
{% endfor %}
"""


class TestProcessFrontMatter(unittest.TestCase):
def single_test(self, input: str, output: str):
"""
单次测试
Args:
input (str): 要测试的markdown头
output (str): 要测试的头部分的输出
"""
st.content = input
st.author_template = Template(AUTHOR_TEMPLATE_STRING) # init st.author_template
process_front_matter()
self.assertEqual(st.author_template, output)

def test_not_front_matter(self):
self.single_test("", "")

def test_common_json(self):
author_info_md = (
"""{
"author": {
"name": "蒋炎岩",
"url": "https://ics.nju.edu.cn/~jyy/"
},
"departments": [
{
"name": " 南京大学 ",
"url": "https://www.nju.edu.cn/main.htm",
"img": "./img/nju-logo.jpg"
},
{
"name": "计算机科学与技术系",
"url": "https://cs.nju.edu.cn/main.htm",
"img": "./img/njucs-logo.jpg"
},
{
"name": "计算机软件研究所",
"url": "https://www.nju.edu.cn/main.htm",
"img": "./img/ics-logo.png"
}
]
}"""
+ st.op_front_matter
)
expected_output = """
蒋炎岩
https://ics.nju.edu.cn/~jyy/
  南京大学  
https://www.nju.edu.cn/main.htm
./img/nju-logo.jpg
计算机科学与技术系
https://cs.nju.edu.cn/main.htm
./img/njucs-logo.jpg
计算机软件研究所
https://www.nju.edu.cn/main.htm
./img/ics-logo.png
"""

self.single_test(author_info_md, expected_output)

def test_common_yaml(self):
author_info_md = (
"""author:
name: 蒋炎岩
url: https://ics.nju.edu.cn/~jyy/
departments:
- name: " 南京大学 "
url: https://www.nju.edu.cn/main.htm,
img: ./img/nju-logo.jpg
- name: 计算机科学与技术系
url: https://cs.nju.edu.cn/main.htm,
img: ./img/njucs-logo.jpg
- name: 计算机软件研究所
url: https://www.nju.edu.cn/main.htm,
img: ./img/ics-logo.png"""
+ st.op_front_matter
)
expected_output = """
蒋炎岩
https://ics.nju.edu.cn/~jyy/
  南京大学  
https://www.nju.edu.cn/main.htm,
./img/nju-logo.jpg
计算机科学与技术系
https://cs.nju.edu.cn/main.htm,
./img/njucs-logo.jpg
计算机软件研究所
https://www.nju.edu.cn/main.htm,
./img/ics-logo.png
"""

self.single_test(author_info_md, expected_output)


if __name__ == "__main__":
unittest.main()

0 comments on commit 6c0cd0c

Please sign in to comment.