diff --git a/brukerapi/jcampdx.py b/brukerapi/jcampdx.py index 1fc89e8..8b41a81 100644 --- a/brukerapi/jcampdx.py +++ b/brukerapi/jcampdx.py @@ -704,6 +704,12 @@ def __add__(self, other): def __getitem__(self, key): return self.params[key] + def __iter__(self): + return iter(self.params) + + def __len__(self): + return len(self.params) + def __contains__(self, item): return item in self.params diff --git a/test/test_jcampdx.py b/test/test_jcampdx.py index 8a0e0b0..3717fca 100644 --- a/test/test_jcampdx.py +++ b/test/test_jcampdx.py @@ -48,6 +48,26 @@ def test_jcampdx(test_jcampdx_data): assert value_ref == value_test +def test_jcampdx_iteration_and_length_follow_its_mapping_interface(tmp_path): + path = tmp_path / "visu_pars" + path.write_text( + "##TITLE=Parameter List\n" + "##JCAMPDX=4.24\n" + "##DATATYPE=Parameter Values\n" + "##$VisuCoreDim=2\n" + "##END=\n" + ) + jcamp = JCAMPDX(path) + + assert list(jcamp) == list(jcamp.keys()) + assert len(jcamp) == len(jcamp.keys()) + assert dict(jcamp) == {key: jcamp[key] for key in jcamp} + + jcamp.unload() + assert list(jcamp) == [] + assert len(jcamp) == 0 + + def test_a_string_is_read_without_its_delimiters(): """Spec 2.2: `<...>` delimits a string, so the brackets are not its value.