Skip to content

Commit

Permalink
tests: various tests
Browse files Browse the repository at this point in the history
  • Loading branch information
outloudvi committed Aug 23, 2024
1 parent b38984e commit e361be9
Show file tree
Hide file tree
Showing 8 changed files with 218 additions and 43 deletions.
23 changes: 23 additions & 0 deletions tests/cli/conf_err_no_path.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from mw2fcitx.tweaks.moegirl import tweaks

exports = {
"source": {
"kwargs": {
"title_limit": 50,
"output": "test_result.txt"
}
},
"tweaks":
tweaks,
"converter": {
"use": "opencc",
"kwargs": {}
},
"generator": [{
"use": "rime",
"kwargs": {
"name": "err_local",
"output": "err_local.dict.yml"
}
}]
}
24 changes: 24 additions & 0 deletions tests/cli/conf_local.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from mw2fcitx.tweaks.moegirl import tweaks

exports = {
"source": {
"file_path": "tests/cli/test.txt",
"kwargs": {
"title_limit": 50,
"output": "test_result.txt"
}
},
"tweaks":
tweaks,
"converter": {
"use": "opencc",
"kwargs": {}
},
"generator": [{
"use": "rime",
"kwargs": {
"name": "e2etest_local",
"output": "test_local_result.dict.yml"
}
}]
}
23 changes: 23 additions & 0 deletions tests/cli/conf_single_generator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from mw2fcitx.tweaks.moegirl import tweaks

exports = {
"source": {
"api_path": "https://zh.wikipedia.org/w/api.php",
"kwargs": {
"title_limit": 50,
"output": "titles.txt"
}
},
"tweaks":
tweaks,
"converter": {
"use": "opencc",
"kwargs": {}
},
"generator": {
"use": "rime",
"kwargs": {
"output": "moegirl.dict.yml"
}
}
}
1 change: 1 addition & 0 deletions tests/cli/test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
初音未来
31 changes: 31 additions & 0 deletions tests/cli/test_conf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from mw2fcitx.main import inner_main
import pytest


def test_inner_main_name_without_py():
inner_main(['-c', 'tests/cli/conf_one'])


def test_inner_main_name_with_py():
inner_main(['-c', 'tests/cli/conf_one.py'])


def test_single_generator():
inner_main(['-c', 'tests/cli/conf_single_generator'])


def test_local():
inner_main(['-c', 'tests/cli/conf_local'])
with open("test_local_result.dict.yml", "r") as f:
assert (f.read() == """---
name: e2etest_local
version: "0.1"
sort: by_weight
...
初音未来 chu yin wei lai
""")


def test_err_no_path():
with pytest.raises(SystemExit):
inner_main(['-c', 'tests/cli/conf_err_no_path'])
9 changes: 0 additions & 9 deletions tests/cli/test_conf_one.py

This file was deleted.

34 changes: 0 additions & 34 deletions tests/lib/test_is_libime_used.py

This file was deleted.

116 changes: 116 additions & 0 deletions tests/lib/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
from mw2fcitx.utils import is_libime_used, sanitize, smart_rewrite


def test_is_libime_used():
assert (
is_libime_used({}) == False
)

assert (
is_libime_used({
"generator": [{
"use": "rime",
"kwargs": {
"output": "1.yml"
}
}]
}) == False
)

assert (
is_libime_used({
"generator": [{
"use": "rime",
"kwargs": {
"output": "1.yml"
}
}, {
"use": "pinyin",
"kwargs": {
"output": "1.dict"
}
}]
}) == True
)


def test_sanitize():
def test():
pass
assert (
sanitize(test) == "[func test]"
)

assert (sanitize(lambda x: x) == "[func <lambda>]")

assert (sanitize({
"a": [1, "b"],
"c": {
"d": None
}
}) == {
"a": ['1', "b"],
"c": {
"d": "[<class 'NoneType'>]"
}
})


def test_smart_rewrite():
assert (
smart_rewrite(
{
"generator": [],
"source": {
"file_path": []
}
}
) == {
"generator": [],
"source": {
"file_path": []
}
}
)

assert (
smart_rewrite(
{
"generator": {
"use": "rime",
"kwargs": {
"output": "moegirl.dict.yml"
}
},
"source": {
"file_path": []
}
}
) == {
"generator": [{
"use": "rime",
"kwargs": {
"output": "moegirl.dict.yml"
}
}],
"source": {
"file_path": []
}
}
)

assert (
smart_rewrite(
{
"generator": [],
"source": {
"file_path": "1.txt"
}
}
) == {
"generator": [],
"source": {
"file_path": ["1.txt"]
}
}
)

0 comments on commit e361be9

Please sign in to comment.