-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[test] build test frame and add process_front_matter(author part) uni…
…t test.
- Loading branch information
Showing
3 changed files
with
145 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |