From 922a5fcf5a4d41b12b2b001cb1100225b8687d73 Mon Sep 17 00:00:00 2001 From: Jonathan Shimwell Date: Fri, 26 Aug 2022 10:19:39 +0100 Subject: [PATCH] used escape chars to match regex --- openmc/mesh.py | 4 ++-- tests/unit_tests/test_mesh_to_vtk.py | 11 ++++++++--- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/openmc/mesh.py b/openmc/mesh.py index 288d1527c4..1b37a42728 100644 --- a/openmc/mesh.py +++ b/openmc/mesh.py @@ -231,8 +231,8 @@ class StructuredMesh(MeshBase): # check that the data sets are appropriately sized for label, dataset in datasets.items(): errmsg = ( - f"The size of the dataset '{label}' ({dataset.size}) " - f"should be equal to the number of mesh cells ({self.num_mesh_cells})" + f"The size of the dataset '{label}' ({dataset.size}) should be" + f" equal to the number of mesh cells ({self.num_mesh_cells})" ) if isinstance(dataset, np.ndarray): if not dataset.size == self.num_mesh_cells: diff --git a/tests/unit_tests/test_mesh_to_vtk.py b/tests/unit_tests/test_mesh_to_vtk.py index a6bed7035b..2c23a52985 100644 --- a/tests/unit_tests/test_mesh_to_vtk.py +++ b/tests/unit_tests/test_mesh_to_vtk.py @@ -28,6 +28,7 @@ spherical_mesh.r_grid = np.linspace(1, 2, num=30) spherical_mesh.phi_grid = np.linspace(0, np.pi, num=50) spherical_mesh.theta_grid = np.linspace(0, np.pi / 2, num=30) + @pytest.mark.parametrize("mesh", [cylinder_mesh, regular_mesh, rectilinear_mesh, spherical_mesh]) def test_write_data_to_vtk(mesh, tmpdir): # BUILD @@ -43,7 +44,7 @@ def test_write_data_to_vtk(mesh, tmpdir): # read file reader = vtk.vtkStructuredGridReader() - reader.SetFileName(filename) + reader.SetFileName(str(filename)) reader.Update() # check name of datasets @@ -58,6 +59,7 @@ def test_write_data_to_vtk(mesh, tmpdir): assert nps.vtk_to_numpy(array1).size == data.size assert nps.vtk_to_numpy(array2).size == data.size + @pytest.mark.parametrize("mesh", [cylinder_mesh, regular_mesh, rectilinear_mesh, spherical_mesh]) def test_write_data_to_vtk_size_mismatch(mesh): """Checks that an error is raised when the size of the dataset @@ -71,9 +73,12 @@ def test_write_data_to_vtk_size_mismatch(mesh): right_size = mesh.num_mesh_cells data = np.random.random(right_size + 1) + # Error message has \ in to escape characters that are otherwise recognized + # by regex. These are needed to make the test string match the error message + # string when using the match argument as that uses regular expression expected_error_msg = ( - f"The size of the dataset 'label' ({len(data)}) should be equal to " - f"the number of mesh cells ({mesh.num_mesh_cells})" + f"The size of the dataset 'label' \({len(data)}\) should be equal to " + f"the number of mesh cells \({mesh.num_mesh_cells}\)" ) with pytest.raises(RuntimeError, match=expected_error_msg): mesh.write_data_to_vtk(filename="out.vtk", datasets={"label": data})