Skip to content

Commit

Permalink
tests: update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
BitterB0NG0 committed Nov 20, 2024
1 parent 775467f commit fc19ee9
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 117 deletions.
85 changes: 21 additions & 64 deletions src/pymcnp/files/inp/_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -920,10 +920,6 @@ def build(self):
return o

def build_test(self):
o = ''

o += 'def test_init_valid(self):\n'

t = ['value']

if self.has_suffix:
Expand All @@ -932,6 +928,8 @@ def build_test(self):
if self.has_designator:
t.append('designator')

o = ''
o += 'def test_valid(self):\n'
o += f' for {", ".join(t)} in self.VALID_EXAMPLES:\n'

if self.attribute.type.startswith('types'):
Expand All @@ -947,13 +945,26 @@ def build_test(self):
if self.has_suffix:
o += ' suffix = pymcnp.utils.types.McnpInteger(suffix)\n'

o += f' obj = pymcnp.inp.Cell{self.name}({", ".join(t)})\n'
if self.attribute.type == 'str':
q = 'value'
else:
q = 'value.to_mcnp()'

if self.has_suffix and self.has_designator:
o += f' obj = pymcnp.inp.Cell{self.name}.from_mcnp(f"{self.mnemonic}{{suffix.to_mcnp()}}:{{designator.to_mcnp()}}={{{q}}}")\n'
elif self.has_suffix:
o += f' obj = pymcnp.inp.Cell{self.name}.from_mcnp(f"{self.mnemonic}{{suffix.to_mcnp()}}={{{q}}}")\n'
elif self.has_designator:
o += f' obj = pymcnp.inp.Cell{self.name}.from_mcnp(f"{self.mnemonic}:{{designator.to_mcnp()}}={{{q}}}")\n'
else:
o += f' obj = pymcnp.inp.Cell{self.name}.from_mcnp(f"{self.mnemonic}={{{q}}}")\n'

o += '\n'
o += f' assert obj.keyword == pymcnp.inp.CellKeyword.{self.enum}\n'
o += f' assert obj.{self.attribute.name} == value\n'
o += ' assert obj.value == value\n'
o += '\n'
o += 'def test_init_invalid(self):\n'
o += 'def test_invalid(self):\n'
o += f' for {", ".join(t)} in self.INVALID_EXAMPLES:\n'
o += ' with pytest.raises(pymcnp.utils.errors.McnpError):\n'

Expand All @@ -970,75 +981,21 @@ def build_test(self):
if self.has_suffix:
o += ' suffix = pymcnp.utils.types.McnpInteger(suffix)\n'

o += f' pymcnp.inp.Cell{self.name}({", ".join(t)})\n'
o += '\n'
o += 'def test_fromMcnp_valid(self):\n'
o += f' for {", ".join(t)} in self.VALID_EXAMPLES:\n'

if self.attribute.type.startswith('types'):
o += f' value = pymcnp.utils.{self.attribute.type}(value)\n'
elif self.attribute.type == 'str':
o += ' value = value\n'
else:
o += f' value = {self.attribute.type}(value)\n'

if self.has_designator:
o += ' designator = pymcnp.utils.types.Designator(designator)\n'

if self.has_suffix:
o += ' suffix = pymcnp.utils.types.McnpInteger(suffix)\n'

if self.attribute.type == 'str':
q = 'value'
else:
q = 'value.to_mcnp()'

if self.has_suffix and self.has_designator:
o += f' obj = pymcnp.inp.Cell{self.name}.from_mcnp(f"{self.mnemonic}{{suffix.to_mcnp()}}:{{designator.to_mcnp()}}={{{q}}}")\n'
o += f' pymcnp.inp.Cell{self.name}.from_mcnp(f"{self.mnemonic}{{suffix.to_mcnp()}}:{{designator.to_mcnp()}}={{{q}}}")\n'
elif self.has_suffix:
o += f' obj = pymcnp.inp.Cell{self.name}.from_mcnp(f"{self.mnemonic}{{suffix.to_mcnp()}}={{{q}}}")\n'
o += f' pymcnp.inp.Cell{self.name}.from_mcnp(f"{self.mnemonic}{{suffix.to_mcnp()}}={{{q}}}")\n'
elif self.has_designator:
o += f' obj = pymcnp.inp.Cell{self.name}.from_mcnp(f"{self.mnemonic}:{{designator.to_mcnp()}}={{{q}}}")\n'
o += f' pymcnp.inp.Cell{self.name}.from_mcnp(f"{self.mnemonic}:{{designator.to_mcnp()}}={{{q}}}")\n'
else:
o += f' obj = pymcnp.inp.Cell{self.name}.from_mcnp(f"{self.mnemonic}={{{q}}}")\n'
o += f' pymcnp.inp.Cell{self.name}.from_mcnp(f"{self.mnemonic}={{{q}}}")\n'

o += '\n'
o += f' assert obj.keyword == pymcnp.inp.CellKeyword.{self.enum}\n'
o += f' assert obj.{self.attribute.name} == value\n'
o += ' assert obj.value == value\n'
o += '\n'
# o += f'def test_fromMcnp_invalid(self):\n'
# o += f' for {", ".join(t)} in self.INVALID_EXAMPLES:\n'
# o += f' with pytest.raises(Excpetion):\n'

# if self.attribute.type.startswith('types'):
# o += f' value = pymcnp.utils.{self.attribute.type}(value)\n'
# elif self.attribute.type == 'str':
# o += f' value = value\n'
# else:
# o += f' value = {self.attribute.type}(value)\n'

# if self.has_designator:
# o += f' designator = pymcnp.utils.types.Designator(designator)\n'

# if self.has_suffix:
# o += f' suffix = pymcnp.utils.types.McnpInteger(suffix)\n'

# if self.attribute.type == 'str':
# q = 'value'
# else:
# q = 'value.to_mcnp()'

# if self.has_suffix and self.has_designator:
# o += f' obj = pymcnp.inp.Cell{self.name}.from_mcnp(f"{self.mnemonic}{{suffix.to_mcnp()}}:{{designator.to_mcnp()}}={{{q}}}")\n'
# elif self.has_suffix:
# o += f' obj = pymcnp.inp.Cell{self.name}.from_mcnp(f"{self.mnemonic}{{suffix.to_mcnp()}}={{{q}}}")\n'
# elif self.has_designator:
# o += f' obj = pymcnp.inp.Cell{self.name}.from_mcnp(f"{self.mnemonic}:{{designator.to_mcnp()}}={{{q}}}")\n'
# else:
# o += f' obj = pymcnp.inp.Cell{self.name}.from_mcnp(f"{self.mnemonic}={{{q}}}")\n'

# o += f'\n'

if _DEBUG_CELL_TEST:
debug_print(o)
Expand Down
62 changes: 9 additions & 53 deletions tests/test_cell.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,37 +40,18 @@ class Test_CellGeometry:

INVALID_EXAMPLES = []

def test_init_valid(self):
for formula in self.VALID_EXAMPLES:
obj = pymcnp.inp.CellGeometry(formula)

assert obj.formula == formula

def test_init_invalid(self):
for geometry in self.INVALID_EXAMPLES:
with pytest.raises(pymcnp.utils.pymcnp.utils.errors.McnpError) as err:
pymcnp.inp.CellGeometry(geometry)

assert (
err.value.code
== pymcnp.utils.pymcnp.utils.errors.MCNPSemanticCodes.INVALID_CELL_GEOMETRY
)

def test_fromMcnp_valid(self):
def test_valid(self):
for formula in self.VALID_EXAMPLES:
obj = pymcnp.inp.CellGeometry.from_mcnp(f'{formula}')

assert obj.formula == formula

def test_fromMcnp_invalid(self):
def test_invalid(self):
for formula in self.INVALID_EXAMPLES:
with pytest.raises(pymcnp.utils.pymcnp.utils.errors.McnpError) as err:
pymcnp.inp.CellGeometry.from_mcnp(f'{formula}')

assert (
err.value.code
== pymcnp.utils.pymcnp.utils.errors.MCNPSemanticCodes.INVALID_CELL_GEOMETRY
)
assert err.value.code == pymcnp.utils.errors.McnpCode.INVALID_CELL_GEOMETRY


class Test_CellKeyword:
Expand All @@ -94,32 +75,7 @@ class Test_CellKeyword:
'beam',
]

def test_init_valid(self):
assert pymcnp.inp.CellKeyword('imp') == pymcnp.inp.CellKeyword.IMP
assert pymcnp.inp.CellKeyword('vol') == pymcnp.inp.CellKeyword.VOL
assert pymcnp.inp.CellKeyword('pwt') == pymcnp.inp.CellKeyword.PWT
assert pymcnp.inp.CellKeyword('ext') == pymcnp.inp.CellKeyword.EXT
assert pymcnp.inp.CellKeyword('fcl') == pymcnp.inp.CellKeyword.FCL
assert pymcnp.inp.CellKeyword('wwn') == pymcnp.inp.CellKeyword.WWN
assert pymcnp.inp.CellKeyword('dxc') == pymcnp.inp.CellKeyword.DXC
assert pymcnp.inp.CellKeyword('nonu') == pymcnp.inp.CellKeyword.NONU
assert pymcnp.inp.CellKeyword('pd') == pymcnp.inp.CellKeyword.PD
assert pymcnp.inp.CellKeyword('tmp') == pymcnp.inp.CellKeyword.TMP
assert pymcnp.inp.CellKeyword('u') == pymcnp.inp.CellKeyword.U
assert pymcnp.inp.CellKeyword('trcl') == pymcnp.inp.CellKeyword.TRCL
assert pymcnp.inp.CellKeyword('lat') == pymcnp.inp.CellKeyword.LAT
assert pymcnp.inp.CellKeyword('fill') == pymcnp.inp.CellKeyword.FILL
assert pymcnp.inp.CellKeyword('elpt') == pymcnp.inp.CellKeyword.ELPT
assert pymcnp.inp.CellKeyword('cosy') == pymcnp.inp.CellKeyword.COSY
assert pymcnp.inp.CellKeyword('bflcl') == pymcnp.inp.CellKeyword.BFIELD
assert pymcnp.inp.CellKeyword('unc') == pymcnp.inp.CellKeyword.UNC

def test_init_invalid(self):
for keyword in self.INVALID_EXAMPLES:
with pytest.raises(ValueError):
pymcnp.inp.CellKeyword(keyword)

def test_fromMcnp_valid(self):
def test_valid(self):
assert pymcnp.inp.CellKeyword.from_mcnp('imp') == pymcnp.inp.CellKeyword.IMP
assert pymcnp.inp.CellKeyword.from_mcnp('vol') == pymcnp.inp.CellKeyword.VOL
assert pymcnp.inp.CellKeyword.from_mcnp('pwt') == pymcnp.inp.CellKeyword.PWT
Expand All @@ -139,11 +95,13 @@ def test_fromMcnp_valid(self):
assert pymcnp.inp.CellKeyword.from_mcnp('bflcl') == pymcnp.inp.CellKeyword.BFIELD
assert pymcnp.inp.CellKeyword.from_mcnp('unc') == pymcnp.inp.CellKeyword.UNC

def test_fromMcnp_invalid(self):
def test_invalid(self):
for keyword in self.INVALID_EXAMPLES:
with pytest.raises(pymcnp.utils.errors.McnpError):
with pytest.raises(pymcnp.utils.errors.McnpError) as err:
pymcnp.inp.CellKeyword.from_mcnp(f'{keyword}')

assert err.value.code == pymcnp.utils.errors.McnpCode.UNRECOGNIZED_KEYWORD


class Test_CellImp:
"""
Expand Down Expand Up @@ -261,9 +219,7 @@ class Test_CellDxc:
]

INVALID_EXAMPLES = [
(1.5, 3, 'e'),
(5.9, 8, '@'),
(-0.5, 2, '_'),
(-1.0, 2, '_'),
]

exec(pymcnp.inp.cell._CellDxcFactory.build_test())
Expand Down

0 comments on commit fc19ee9

Please sign in to comment.