@@ -50,6 +50,20 @@ def isfloat(s):
5050 return False
5151
5252
53+ def are_files_same (file1 , file2 ):
54+ """Assert that two files have (approximately) the same numerical
55+ values."""
56+ for f1_row , f2_row in zip (file1 , file2 ):
57+ f1_arr = f1_row .split ()
58+ f2_arr = f2_row .split ()
59+ assert len (f1_arr ) == len (f2_arr )
60+ for idx , _ in enumerate (f1_arr ):
61+ if isfloat (f1_arr [idx ]) and isfloat (f2_arr [idx ]):
62+ assert np .isclose (float (f1_arr [idx ]), float (f2_arr [idx ]))
63+ else :
64+ assert f1_arr [idx ] == f2_arr [idx ]
65+
66+
5367class TestApp :
5468 @pytest .fixture
5569 def setup (self ):
@@ -106,9 +120,9 @@ def test_morph_outputs(self, setup, tmp_path):
106120 for file in common :
107121 with open (tmp_succinct .joinpath (file )) as gf :
108122 with open (test_saving_succinct .joinpath (file )) as tf :
109- generated = filter (ignore_path , gf )
110- target = filter (ignore_path , tf )
111- assert all ( x == y for x , y in zip ( generated , target ) )
123+ actual = filter (ignore_path , gf )
124+ expected = filter (ignore_path , tf )
125+ are_files_same ( actual , expected )
112126
113127 # Save multiple verbose morphs
114128 tmp_verbose = tmp_path .joinpath ("verbose" )
@@ -147,9 +161,9 @@ def test_morph_outputs(self, setup, tmp_path):
147161 for file in common :
148162 with open (tmp_verbose .joinpath (file )) as gf :
149163 with open (test_saving_verbose .joinpath (file )) as tf :
150- generated = filter (ignore_path , gf )
151- target = filter (ignore_path , tf )
152- assert all ( x == y for x , y in zip ( generated , target ) )
164+ actual = filter (ignore_path , gf )
165+ expected = filter (ignore_path , tf )
166+ are_files_same ( actual , expected )
153167
154168 def test_morphsqueeze_outputs (self , setup , tmp_path ):
155169 # The file squeeze_morph has a squeeze and stretch applied
@@ -182,19 +196,9 @@ def test_morphsqueeze_outputs(self, setup, tmp_path):
182196 # Check squeeze morph generates the correct output
183197 with open (sqr ) as mf :
184198 with open (target_file ) as tf :
185- morphed = filter (ignore_path , mf )
186- target = filter (ignore_path , tf )
187- for m , t in zip (morphed , target ):
188- m_row = m .split ()
189- t_row = t .split ()
190- assert len (m_row ) == len (t_row )
191- for idx , _ in enumerate (m_row ):
192- if isfloat (m_row [idx ]) and isfloat (t_row [idx ]):
193- assert np .isclose (
194- float (m_row [idx ]), float (t_row [idx ])
195- )
196- else :
197- assert m_row [idx ] == t_row [idx ]
199+ actual = filter (ignore_path , mf )
200+ expected = filter (ignore_path , tf )
201+ are_files_same (actual , expected )
198202
199203 def test_morphfuncy_outputs (self , tmp_path ):
200204 def quadratic (x , y , a0 , a1 , a2 ):
@@ -215,16 +219,6 @@ def quadratic(x, y, a0, a1, a2):
215219
216220 with open (testdata_dir .joinpath ("funcy_target.cgr" )) as tf :
217221 with open (tmp_path .joinpath ("funcy_target.cgr" )) as gf :
218- generated = filter (ignore_path , gf )
219- target = filter (ignore_path , tf )
220- for m , t in zip (generated , target ):
221- m_row = m .split ()
222- t_row = t .split ()
223- assert len (m_row ) == len (t_row )
224- for idx , _ in enumerate (m_row ):
225- if isfloat (m_row [idx ]) and isfloat (t_row [idx ]):
226- assert np .isclose (
227- float (m_row [idx ]), float (t_row [idx ])
228- )
229- else :
230- assert m_row [idx ] == t_row [idx ]
222+ actual = filter (ignore_path , gf )
223+ expected = filter (ignore_path , tf )
224+ are_files_same (actual , expected )
0 commit comments