|
1 | | -import unittest |
2 | | - |
3 | 1 | import hbreader |
| 2 | +import pytest |
4 | 3 | import yaml |
5 | 4 |
|
6 | 5 | from linkml_runtime.utils.yamlutils import DupCheckYamlLoader, TypedNode |
|
13 | 12 | """ |
14 | 13 |
|
15 | 14 |
|
16 | | -class Issue6TestCase(unittest.TestCase): |
17 | | - def test_loc_function(self): |
18 | | - inp = yaml.load(hbreader.hbread(inp_yaml), DupCheckYamlLoader) |
19 | | - self.assertEqual('File "<unicode string>", line 3, col 8: ', TypedNode.yaml_loc(inp["foo"]["x"])) |
20 | | - self.assertEqual('File "<unicode string>", line 3, col 8', TypedNode.yaml_loc(inp["foo"]["x"], suffix="")) |
21 | | - self.assertEqual('File "<unicode string>", line 4, col 8: ', TypedNode.yaml_loc(inp["foo"]["y"])) |
22 | | - self.assertEqual( |
23 | | - 'File "<unicode string>", line 4, col 8I yam that I yam', |
24 | | - TypedNode.yaml_loc(inp["foo"]["y"], suffix=inp["foo"]["y"]), |
25 | | - ) |
26 | | - self.assertEqual('File "<unicode string>", line 5, col 8: ', TypedNode.yaml_loc(inp["foo"]["z"])) |
27 | | - |
28 | | - with self.assertWarns(DeprecationWarning) as cm: |
29 | | - self.assertEqual('File "<unicode string>", line 3, col 8', TypedNode.loc(inp["foo"]["x"])) |
30 | | - self.assertEqual("Call to deprecated method loc. (Use yaml_loc instead)", cm.warning.args[0]) |
31 | | - |
32 | | - self.assertEqual("", TypedNode.yaml_loc(None)) |
33 | | - self.assertEqual("", TypedNode.yaml_loc("abc")) |
34 | | - self.assertEqual("", TypedNode.yaml_loc(["a", "b"])) |
35 | | - |
36 | | - |
37 | | -if __name__ == "__main__": |
38 | | - unittest.main() |
| 15 | +def test_loc_function() -> None: |
| 16 | + """Test the TypedNode.yaml_loc function.""" |
| 17 | + inp = yaml.load(hbreader.hbread(inp_yaml), DupCheckYamlLoader) |
| 18 | + assert TypedNode.yaml_loc(inp["foo"]["x"]) == 'File "<unicode string>", line 3, col 8: ' |
| 19 | + assert TypedNode.yaml_loc(inp["foo"]["x"], suffix="") == 'File "<unicode string>", line 3, col 8' |
| 20 | + assert TypedNode.yaml_loc(inp["foo"]["y"]) == 'File "<unicode string>", line 4, col 8: ' |
| 21 | + assert ( |
| 22 | + TypedNode.yaml_loc(inp["foo"]["y"], suffix=inp["foo"]["y"]) |
| 23 | + == 'File "<unicode string>", line 4, col 8I yam that I yam' |
| 24 | + ) |
| 25 | + assert TypedNode.yaml_loc(inp["foo"]["z"]) == 'File "<unicode string>", line 5, col 8: ' |
| 26 | + |
| 27 | + |
| 28 | +def test_yaml_loc_warning() -> None: |
| 29 | + """Test that a warning is emitted when using the `loc` method.""" |
| 30 | + inp = yaml.load(hbreader.hbread(inp_yaml), DupCheckYamlLoader) |
| 31 | + with pytest.warns(DeprecationWarning) as warning_list: |
| 32 | + assert TypedNode.loc(inp["foo"]["x"]) == 'File "<unicode string>", line 3, col 8' |
| 33 | + assert len(warning_list) == 1 |
| 34 | + assert str(warning_list[0].message) == "Call to deprecated method loc. (Use yaml_loc instead)" |
| 35 | + |
| 36 | + |
| 37 | +@pytest.mark.parametrize("loc", [None, "abc", ["a", "b"]]) |
| 38 | +def test_yaml_loc_empty_str(loc) -> None: |
| 39 | + """Test yaml_loc values that translate to an empty string.""" |
| 40 | + assert TypedNode.yaml_loc(loc) == "" |
0 commit comments