11# Copyright 2024-2025, Battelle Energy Alliance, LLC All Rights Reserved.
2+ import io
23from unittest import TestCase
4+ from pathlib import Path
35import pytest
46
57import montepy
@@ -129,26 +131,6 @@ def test_validator(self):
129131 with self .assertRaises (montepy .errors .IllegalState ):
130132 surf .validate ()
131133
132- def test_surface_is_reflecting_setter (self ):
133- in_str = "1 PZ 0.0"
134- card = Input ([in_str ], BlockType .SURFACE )
135- surf = Surface (card )
136- surf .is_reflecting = True
137- self .assertTrue (surf .is_reflecting )
138- self .verify_export (surf )
139- with self .assertRaises (TypeError ):
140- surf .is_reflecting = 1
141-
142- def test_surface_is_white_bound_setter (self ):
143- in_str = "1 PZ 0.0"
144- card = Input ([in_str ], BlockType .SURFACE )
145- surf = Surface (card )
146- surf .is_white_boundary = True
147- self .assertTrue (surf .is_white_boundary )
148- self .verify_export (surf )
149- with self .assertRaises (TypeError ):
150- surf .is_white_boundary = 1
151-
152134 def test_surface_constants_setter (self ):
153135 in_str = "1 PZ 0.0"
154136 card = Input ([in_str ], BlockType .SURFACE )
@@ -315,24 +297,36 @@ def test_cylinder_location_setter(self):
315297 with self .assertRaises (ValueError ):
316298 surf .coordinates = [3 , 4 , 5 ]
317299
318- def verify_export (_ , surf ):
319- output = surf .format_for_mcnp_input ((6 , 3 , 0 ))
320- print ("Surface output" , output )
321- new_surf = Surface ("\n " .join (output ))
322- assert surf .number == new_surf .number , "Material number not preserved."
323- assert len (surf .surface_constants ) == len (
324- new_surf .surface_constants
325- ), "number of surface constants not kept."
326- for old_const , new_const in zip (
327- surf .surface_constants , new_surf .surface_constants
328- ):
329- assert old_const == pytest .approx (new_const )
330- assert surf .is_reflecting == new_surf .is_reflecting
331- assert surf .is_white_boundary == new_surf .is_white_boundary
332- if surf .old_periodic_surface :
333- assert surf .old_periodic_surface == new_surf .old_periodic_surface
334- if surf .old_transform_number :
335- assert surf .old_transform_number == new_surf ._old_transform_number
300+
301+ def verify_export (surf ):
302+ output = surf .format_for_mcnp_input ((6 , 3 , 0 ))
303+ print ("Surface output" , output )
304+ new_surf = Surface ("\n " .join (output ))
305+ verify_equiv_surf (surf , new_surf )
306+
307+
308+ def verify_equiv_surf (surf , new_surf ):
309+ assert surf .number == new_surf .number , "Material number not preserved."
310+ assert len (surf .surface_constants ) == len (
311+ new_surf .surface_constants
312+ ), "number of surface constants not kept."
313+ for old_const , new_const in zip (surf .surface_constants , new_surf .surface_constants ):
314+ assert old_const == pytest .approx (new_const )
315+ assert surf .is_reflecting == new_surf .is_reflecting
316+ assert surf .is_white_boundary == new_surf .is_white_boundary
317+ if surf .old_periodic_surface :
318+ assert surf .old_periodic_surface == new_surf .old_periodic_surface
319+ if surf .old_transform_number :
320+ assert surf .old_transform_number == new_surf ._old_transform_number
321+
322+
323+ def verify_prob_export (problem , surf ):
324+ with io .StringIO () as fh :
325+ problem .write_problem (fh )
326+ fh .seek (0 )
327+ new_problem = montepy .read_input (fh )
328+ new_surf = new_problem .surfaces [surf .number ]
329+ verify_equiv_surf (surf , new_surf )
336330
337331
338332@pytest .mark .parametrize (
@@ -345,3 +339,41 @@ def test_surface_clone(surf_str):
345339 new_surf = surf .clone ()
346340 assert surf .surface_type == new_surf .surface_type
347341 assert surf .surface_constants == new_surf .surface_constants
342+
343+
344+ @pytest .fixture
345+ def simple_problem (scope = "module" ):
346+ return montepy .read_input (Path ("tests" ) / "inputs" / "test.imcnp" )
347+
348+
349+ @pytest .fixture
350+ def cp_simple_problem (simple_problem ):
351+ return simple_problem .clone ()
352+
353+
354+ @pytest .mark .parametrize (
355+ "in_str, expected" ,
356+ [("1 PZ 0.0" , True ), ("*1 PZ 0.0" , False ), (" *1 PZ 0.0" , False )],
357+ )
358+ def test_surface_is_reflecting_setter (cp_simple_problem , in_str , expected ):
359+ surf = Surface (in_str )
360+ surf .is_reflecting = expected
361+ assert surf .is_reflecting == expected
362+ cp_simple_problem .surfaces .append (surf )
363+ verify_prob_export (cp_simple_problem , surf )
364+ with pytest .raises (TypeError ):
365+ surf .is_reflecting = 1
366+
367+
368+ @pytest .mark .parametrize (
369+ "in_str, expected" ,
370+ [("1 PZ 0.0" , True ), ("+1 PZ 0.0" , False ), (" +1 PZ 0.0" , False )],
371+ )
372+ def test_surface_is_white_bound_setter (cp_simple_problem , in_str , expected ):
373+ surf = Surface (in_str )
374+ surf .is_white_boundary = expected
375+ assert surf .is_white_boundary == expected
376+ cp_simple_problem .surfaces .append (surf )
377+ verify_prob_export (cp_simple_problem , surf )
378+ with pytest .raises (TypeError ):
379+ surf .is_white_boundary = 1
0 commit comments