Skip to content

Commit 155c08f

Browse files
GarkGarciarocky
authored andcommitted
Extracted the code that loads the data from disk to a separate module
1 parent 778039c commit 155c08f

File tree

6 files changed

+34
-69
lines changed

6 files changed

+34
-69
lines changed

test/test_general_yaml_sanity.py

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
# -*- coding: utf-8 -*-
22

3-
from mathics_scanner.generate.build_tables import DEFAULT_DATA_DIR
4-
import yaml
3+
from util import yaml_data
54
import unicodedata
65

7-
def check_attr_is_invertible(yaml_data: dict, attr: str):
6+
def check_attr_is_invertible(attr: str):
87
for v in yaml_data.values():
98
if attr in v:
109
attr_v = v[attr]
@@ -17,12 +16,12 @@ def check_attr_is_invertible(yaml_data: dict, attr: str):
1716
), f"{attr_vs} all have the same {attr} field set to {attr_v}"
1817

1918

20-
def check_has_attr(yaml_data: dict, attr: str):
19+
def check_has_attr(attr: str):
2120
for k, v in yaml_data.items():
2221
assert attr in v, f"{k} has no {attr} attribute"
2322

2423

25-
def check_wl_unicode_name(yaml_data: dict):
24+
def check_wl_unicode_name():
2625
for k, v in yaml_data.items():
2726
wl = v["wl-unicode"]
2827

@@ -49,7 +48,7 @@ def check_wl_unicode_name(yaml_data: dict):
4948
), f"{k} has wl-unicode-name set to {real_name} but it should be {expected_name}"
5049

5150

52-
def check_unicode_name(yaml_data: dict):
51+
def check_unicode_name():
5352
for k, v in yaml_data.items():
5453
# Hack to skip characters that are correct but that doesn't show up in
5554
# unicodedata.name
@@ -79,19 +78,16 @@ def check_unicode_name(yaml_data: dict):
7978

8079

8180
def test_general_yaml_sanity():
82-
with open(DEFAULT_DATA_DIR / "named-characters.yml", "r") as yaml_file:
83-
yaml_data = yaml.load(yaml_file, Loader=yaml.FullLoader)
84-
85-
# Check if required attributes are in place
86-
check_has_attr(yaml_data, "wl-unicode")
87-
check_has_attr(yaml_data, "is-letter-like")
88-
check_has_attr(yaml_data, "has-unicode-inverse")
89-
90-
# Check if attributes that should be invertible are in fact invertible
91-
check_attr_is_invertible(yaml_data, "wl-unicode")
92-
check_attr_is_invertible(yaml_data, "esc-alias")
93-
94-
# Check the consistency of the unicode names in the table
95-
check_wl_unicode_name(yaml_data)
96-
check_unicode_name(yaml_data)
81+
# Check if required attributes are in place
82+
check_has_attr("wl-unicode")
83+
check_has_attr("is-letter-like")
84+
check_has_attr("has-unicode-inverse")
85+
86+
# Check if attributes that should be invertible are in fact invertible
87+
check_attr_is_invertible("wl-unicode")
88+
check_attr_is_invertible("esc-alias")
89+
90+
# Check the consistency of the unicode names in the table
91+
check_wl_unicode_name()
92+
check_unicode_name()
9793

test/test_has_unicode_inverse_sanity.py

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
11
# -*- coding: utf-8 -*-
22

3-
from mathics_scanner.generate.build_tables import DEFAULT_DATA_DIR
4-
import yaml
5-
import json
6-
7-
def check_has_unicode_inverse_sanity(yaml_data: dict, json_data: dict):
8-
"""
9-
Checks if the "has-unicode-inverse" data is self-consistant
10-
"""
3+
from util import yaml_data, json_data
114

5+
def test_has_unicode_inverse_sanity():
126
inverses = set()
137

148
for k, v in yaml_data.items():
@@ -32,10 +26,3 @@ def check_has_unicode_inverse_sanity(yaml_data: dict, json_data: dict):
3226
any(v["wl-unicode"] == wl and v.get("unicode-equivalent") == uni and v["has-unicode-inverse"] for v in yaml_data.values())
3327
), f"key {uni} is in unicode-to-wl-dict but there is not corresponding entry in the YAML table"
3428

35-
36-
def test_has_unicode_inverse_sanity():
37-
with open(DEFAULT_DATA_DIR / "named-characters.yml", "r") as yaml_file, open(DEFAULT_DATA_DIR / "characters.json", "r") as json_file:
38-
yaml_data = yaml.load(yaml_file, Loader=yaml.FullLoader)
39-
json_data = json.load(json_file)
40-
check_has_unicode_inverse_sanity(yaml_data, json_data)
41-

test/test_letterlikes_sanity.py

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
from mathics_scanner.generate.build_tables import DEFAULT_DATA_DIR
2-
import yaml
3-
import json
1+
from util import yaml_data, json_data
42

5-
def check_letterlikes_sanity(yaml_data: dict, json_data: dict):
3+
def test_letterlikes_sanity():
64
letterlikes = json_data["letterlikes"]
75

86
yaml_llc = [v["is-letter-like"] for v in yaml_data.values()].count(True)
@@ -12,10 +10,3 @@ def check_letterlikes_sanity(yaml_data: dict, json_data: dict):
1210
yaml_llc == json_llc
1311
), f"the YAML table has {yaml_llc} letter-like characters but the JSON files lists {json_llc} letterlike characters"
1412

15-
16-
def test_letterlikes_sanity():
17-
with open(DEFAULT_DATA_DIR / "named-characters.yml", "r") as yaml_file, open(DEFAULT_DATA_DIR / "characters.json", "r") as json_file:
18-
yaml_data = yaml.load(yaml_file, Loader=yaml.FullLoader)
19-
json_data = yaml.load(json_file)
20-
check_letterlikes_sanity(yaml_data, json_data)
21-

test/test_roundtrip.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
from mathics_scanner.generate.build_tables import DEFAULT_DATA_DIR
21
from mathics_scanner.characters import replace_wl_with_plain_text as wl_to_unicode
32
from mathics_scanner.characters import replace_unicode_with_wl as unicode_to_wl
4-
import yaml
5-
import json
3+
from util import yaml_data, json_data
64

75

8-
def check_roundtrip(yaml_data: dict, json_data: dict):
6+
def test_roundtrip():
97
wl_to_unicode_dict = json_data["wl-to-unicode-dict"]
108
unicode_to_wl_dict = json_data["unicode-to-wl-dict"]
119

@@ -30,9 +28,3 @@ def check_roundtrip(yaml_data: dict, json_data: dict):
3028
unicode_to_wl_dict[uni] == wl
3129
), f"key {k} unicode {uni}, {wl_to_unicode[uni]}"
3230

33-
34-
def test_roundtrip():
35-
with open(DEFAULT_DATA_DIR / "named-characters.yml", "r") as yaml_file, open(DEFAULT_DATA_DIR / "characters.json", "r") as json_file:
36-
yaml_data = yaml.load(yaml_file, Loader=yaml.FullLoader)
37-
json_data = json.load(json_file)
38-
check_roundtrip(yaml_data, json_data)

test/test_wl_to_ascii.py

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
# -*- coding: utf-8 -*-
22

3-
from mathics_scanner.generate.build_tables import DEFAULT_DATA_DIR
4-
from mathics_scanner.characters import replace_wl_with_plain_text
5-
import yaml
6-
import json
3+
from util import yaml_data
74

85
def wl_to_ascii(wl_input: str) -> str:
96
return replace_wl_with_plain_text(wl_input, use_unicode=False)
107

118
def is_ascii(s: str) -> bool:
129
return all(ord(c) < 127 for c in s)
1310

14-
def check_wl_to_ascii(yaml_data: dict):
11+
def test_wl_to_ascii():
1512
for k, v in yaml_data.items():
1613
wl = v["wl-unicode"]
1714

@@ -28,10 +25,3 @@ def check_wl_to_ascii(yaml_data: dict):
2825
uni == ascii_c
2926
), f"{k}'s unicode equivalent could be used as it's ASCII equivalent but it isn't"
3027

31-
32-
33-
def test_wl_to_ascii():
34-
with open(DEFAULT_DATA_DIR / "named-characters.yml", "r") as yaml_file:
35-
yaml_data = yaml.load(yaml_file, Loader=yaml.FullLoader)
36-
check_wl_to_ascii(yaml_data)
37-

test/util.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from mathics_scanner.generate.build_tables import DEFAULT_DATA_DIR
2+
import yaml
3+
import json
4+
5+
with open(DEFAULT_DATA_DIR / "named-characters.yml", "r") as yaml_file:
6+
yaml_data = yaml.load(yaml_file, Loader=yaml.FullLoader)
7+
8+
with open(DEFAULT_DATA_DIR / "characters.json", "r") as json_file:
9+
json_data = json.load(json_file)

0 commit comments

Comments
 (0)