diff --git a/scripts/test/ISISPowderSampleDetailsTest.py b/scripts/test/ISISPowderSampleDetailsTest.py index f7001513251b8252ba041ba5a0183db4d4d9ccd9..dd2abd0fa10e6d23003e7c9f5524144329ee11f4 100644 --- a/scripts/test/ISISPowderSampleDetailsTest.py +++ b/scripts/test/ISISPowderSampleDetailsTest.py @@ -217,58 +217,64 @@ class ISISPowderSampleDetailsTest(unittest.TestCase): chemical_formula_two = 'V' expected_number_density = 1.2345 - # Redirect std out to a capture object - std_out_buffer = get_std_out_buffer_obj() - sys.stdout = std_out_buffer + old_std_out = sys.stdout + # Wrap in try finally so we always restore std out if any exception is thrown + try: + # Redirect std out to a capture object + std_out_buffer = get_std_out_buffer_obj() + sys.stdout = std_out_buffer + + sample_details_obj = sample_details.SampleDetails(height=expected_height, radius=expected_radius, + center=expected_center) + # Test with most defaults set + sample_details_obj.print_sample_details() + captured_std_out_default = std_out_buffer.getvalue() + assertRegex(self, captured_std_out_default, "Height: " + str(float(expected_height))) + assertRegex(self, captured_std_out_default, "Radius: " + str(float(expected_radius))) + assertRegex(self, captured_std_out_default, "Center X:" + str(float(expected_center[0]))) + assertRegex(self, captured_std_out_default, "Material has not been set") + + # Test with material set but not number density + sys.stdout = std_out_buffer = get_std_out_buffer_obj() + sample_details_obj.set_material(chemical_formula=chemical_formula) + sample_details_obj.print_sample_details() + captured_std_out_material_default = std_out_buffer.getvalue() + assertRegex(self, captured_std_out_material_default, "Material properties:") + assertRegex(self, captured_std_out_material_default, "Chemical formula: " + chemical_formula) + assertRegex(self, captured_std_out_material_default, "Number Density: Set from elemental properties") + + # Test with material and number density + sys.stdout = std_out_buffer = get_std_out_buffer_obj() + sample_details_obj.reset_sample_material() + sample_details_obj.set_material(chemical_formula=chemical_formula_two, + number_density=expected_number_density) + sample_details_obj.print_sample_details() + captured_std_out_material_set = std_out_buffer.getvalue() + assertRegex(self, captured_std_out_material_set, "Chemical formula: " + chemical_formula_two) + assertRegex(self, captured_std_out_material_set, "Number Density: " + str(expected_number_density)) + + # Test with no material properties set - we can reuse buffer from previous test + assertRegex(self, captured_std_out_material_default, "Absorption cross section: Calculated by Mantid") + assertRegex(self, captured_std_out_material_default, "Scattering cross section: Calculated by Mantid") + assertRegex(self, captured_std_out_material_default, "Note to manually override these call") + + expected_abs_x_section = 2.13 + expected_scattering_x_section = 5.32 + + # Test with material set + sys.stdout = std_out_buffer = get_std_out_buffer_obj() + sample_details_obj.set_material_properties(absorption_cross_section=expected_abs_x_section, + scattering_cross_section=expected_scattering_x_section) + sample_details_obj.print_sample_details() + captured_std_out_material_props = std_out_buffer.getvalue() + assertRegex(self, captured_std_out_material_props, "Absorption cross section: " + + str(expected_abs_x_section)) + assertRegex(self, captured_std_out_material_props, "Scattering cross section: " + + str(expected_scattering_x_section)) + finally: + # Ensure std IO is restored. Do NOT remove this line as all std out will pipe into our buffer otherwise + sys.stdout = old_std_out - sample_details_obj = sample_details.SampleDetails(height=expected_height, radius=expected_radius, - center=expected_center) - # Test with most defaults set - sample_details_obj.print_sample_details() - captured_std_out_default = std_out_buffer.getvalue() - assertRegex(self, captured_std_out_default, "Height: " + str(float(expected_height))) - assertRegex(self, captured_std_out_default, "Radius: " + str(float(expected_radius))) - assertRegex(self, captured_std_out_default, "Center X:" + str(float(expected_center[0]))) - assertRegex(self, captured_std_out_default, "Material has not been set") - - # Test with material set but not number density - sys.stdout = std_out_buffer = get_std_out_buffer_obj() - sample_details_obj.set_material(chemical_formula=chemical_formula) - sample_details_obj.print_sample_details() - captured_std_out_material_default = std_out_buffer.getvalue() - assertRegex(self, captured_std_out_material_default, "Material properties:") - assertRegex(self, captured_std_out_material_default, "Chemical formula: " + chemical_formula) - assertRegex(self, captured_std_out_material_default, "Number Density: Set from elemental properties") - - # Test with material and number density - sys.stdout = std_out_buffer = get_std_out_buffer_obj() - sample_details_obj.reset_sample_material() - sample_details_obj.set_material(chemical_formula=chemical_formula_two, number_density=expected_number_density) - sample_details_obj.print_sample_details() - captured_std_out_material_set = std_out_buffer.getvalue() - assertRegex(self, captured_std_out_material_set, "Chemical formula: " + chemical_formula_two) - assertRegex(self, captured_std_out_material_set, "Number Density: " + str(expected_number_density)) - - # Test with no material properties set - we can reuse buffer from previous test - assertRegex(self, captured_std_out_material_default, "Absorption cross section: Calculated by Mantid") - assertRegex(self, captured_std_out_material_default, "Scattering cross section: Calculated by Mantid") - assertRegex(self, captured_std_out_material_default, "Note to manually override these call") - - expected_abs_x_section = 2.13 - expected_scattering_x_section = 5.32 - - # Test with material set - sys.stdout = std_out_buffer = get_std_out_buffer_obj() - sample_details_obj.set_material_properties(absorption_cross_section=expected_abs_x_section, - scattering_cross_section=expected_scattering_x_section) - sample_details_obj.print_sample_details() - captured_std_out_material_props = std_out_buffer.getvalue() - assertRegex(self, captured_std_out_material_props, "Absorption cross section: " + str(expected_abs_x_section)) - assertRegex(self, captured_std_out_material_props, "Scattering cross section: " + - str(expected_scattering_x_section)) - - # Ensure std IO is restored. Do NOT remove this line as all std out will pipe into our buffer otherwise - sys.stdout = sys.__stdout__ def get_std_out_buffer_obj(): # Because of the way that strings and bytes