Skip to content

Commit 57552e5

Browse files
committed
pymcnp/inp: added metaprogrammed tests
1 parent 9f9b0c5 commit 57552e5

File tree

11 files changed

+7626
-622
lines changed

11 files changed

+7626
-622
lines changed
Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import re
22
import pathlib
33

4-
import _data
4+
import inp_data
55

66

77
# UTILITIES #
@@ -30,11 +30,6 @@ def SNAKE(name: str) -> str:
3030
return name.lower()
3131

3232

33-
def FILE(name: str) -> str:
34-
name = re.sub('/', '_', name)
35-
return name.lower()
36-
37-
3833
# ATTRIBUTE FUNCTIONS #
3934
def ATTRS_REGEX(element):
4035
def _REGEX(element):
@@ -261,5 +256,5 @@ def build_element(element, parent_name, path_dir, depth):
261256
file.write(ELEMENT(element, parent_name, depth - 1))
262257

263258

264-
for card in _data.cards.options:
265-
build_element(card, '', pathlib.Path(__file__).parent / 'inp', 3)
259+
for card in inp_data.cards.options:
260+
build_element(card, '', pathlib.Path(__file__).parent.parent / 'src/pymcnp/inp', 3)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ def __init__(
202202
),
203203
AttributeScheme(
204204
name='stretch',
205-
type='str',
205+
type='types.String',
206206
description='Cell exponential transform stretching specifier',
207207
),
208208
),

scripts/inp_tests.py

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
import re
2+
import pathlib
3+
import itertools
4+
5+
import inp_data
6+
7+
8+
def CAMEL(name: str) -> str:
9+
name = re.sub('/', '_', name)
10+
if name:
11+
return name[0].upper() + name[1:]
12+
else:
13+
return ''
14+
15+
16+
def SNAKE(name: str) -> str:
17+
name = re.sub('/', '_', name)
18+
return name.lower()
19+
20+
21+
def TEST(element, mod, parent_name):
22+
return f'''
23+
class Test_{CAMEL(parent_name)}{CAMEL(element.name)}:
24+
"""
25+
Tests ``{CAMEL(parent_name)}{CAMEL(element.name)}``.
26+
"""
27+
28+
class Test_FromMcnp(_Test_FromMcnp):
29+
"""
30+
Tests ``{CAMEL(parent_name)}{CAMEL(element.name)}.from_mcnp``.
31+
"""
32+
33+
element = pymcnp.inp.{f"{mod}." if mod else ""}{CAMEL(element.name)}
34+
EXAMPLES_VALID = []
35+
EXAMPLES_INVALID = []
36+
'''[1:]
37+
38+
39+
path = pathlib.Path(__file__).parent / '..' / 'tests'
40+
41+
42+
def get_tests(element, mod, parent_name):
43+
if element.options:
44+
return [TEST(element, mod, parent_name)] + list(
45+
itertools.chain(
46+
*(
47+
get_tests(
48+
option, mod + ('.' if mod else '') + SNAKE(element.name), element.name
49+
)
50+
for option in element.options
51+
)
52+
)
53+
)
54+
else:
55+
return [TEST(element, mod, parent_name)]
56+
57+
58+
for card in inp_data.cards.options:
59+
tests = get_tests(card, '', '')
60+
61+
res = f'''
62+
import pymcnp
63+
64+
import pytest
65+
66+
67+
class _Test_FromMcnp:
68+
"""
69+
Tests ``McnpElement_.from_mcnp``.
70+
"""
71+
72+
element: pymcnp.utils._object.McnpElement_
73+
EXAMPLE_VALID: list[str]
74+
EXAMPLE_INVALID: list[str]
75+
76+
def test_valid(self):
77+
"""
78+
Tests ``EXAMPLES_VALID``.
79+
"""
80+
81+
for example in self.EXAMPLES_VALID:
82+
self.element.from_mcnp(example)
83+
84+
def test_invalid(self):
85+
"""
86+
Tests ``EXAMPLES_INVALID``.
87+
"""
88+
89+
for example in self.EXAMPLES_INVALID:
90+
with pytest.raises(pymcnp.utils.errors.InpError):
91+
self.element.from_mcnp(example)
92+
93+
94+
{'\n\n'.join(tests)}
95+
'''[1:]
96+
97+
path_file = pathlib.Path(__file__).parent.parent / 'tests' / f'test_{card.name}.py'
98+
with path_file.open('w') as file:
99+
file.write(res)

0 commit comments

Comments
 (0)