|
| 1 | +import yaml |
| 2 | + |
| 3 | +from waveform_editor.annotations import Annotations |
| 4 | +from waveform_editor.yaml_parser import LineNumberYamlLoader |
| 5 | + |
| 6 | + |
| 7 | +def test_empty(): |
| 8 | + """Test if empty annotations returns empty list.""" |
| 9 | + annotations = Annotations() |
| 10 | + assert annotations == [] |
| 11 | + |
| 12 | + |
| 13 | +def test_add(): |
| 14 | + """Test adding error to the annotations instance.""" |
| 15 | + annotations = Annotations() |
| 16 | + test_messages = ["error message", "warning message"] |
| 17 | + line_numbers = [0, 5] |
| 18 | + annotations.add(line_numbers[0], test_messages[0]) |
| 19 | + annotations.add(line_numbers[1], test_messages[1], is_warning=True) |
| 20 | + |
| 21 | + assert annotations == [ |
| 22 | + { |
| 23 | + "row": line_numbers[0], |
| 24 | + "column": 0, |
| 25 | + "text": test_messages[0], |
| 26 | + "type": "error", |
| 27 | + }, |
| 28 | + { |
| 29 | + "row": line_numbers[1], |
| 30 | + "column": 0, |
| 31 | + "text": test_messages[1], |
| 32 | + "type": "warning", |
| 33 | + }, |
| 34 | + ] |
| 35 | + |
| 36 | + |
| 37 | +def test_add_annotations(): |
| 38 | + """Test adding annotations to the annotations.""" |
| 39 | + annotations1 = Annotations() |
| 40 | + annotations2 = Annotations() |
| 41 | + test_messages = ["error message", "warning message"] |
| 42 | + line_numbers = [0, 5] |
| 43 | + annotations1.add(line_numbers[0], test_messages[0]) |
| 44 | + annotations2.add(line_numbers[1], test_messages[1], is_warning=True) |
| 45 | + |
| 46 | + annotations1.add_annotations(annotations2) |
| 47 | + |
| 48 | + assert annotations1 == [ |
| 49 | + { |
| 50 | + "row": line_numbers[0], |
| 51 | + "column": 0, |
| 52 | + "text": test_messages[0], |
| 53 | + "type": "error", |
| 54 | + }, |
| 55 | + { |
| 56 | + "row": line_numbers[1], |
| 57 | + "column": 0, |
| 58 | + "text": test_messages[1], |
| 59 | + "type": "warning", |
| 60 | + }, |
| 61 | + ] |
| 62 | + |
| 63 | + |
| 64 | +def test_add_yaml_error(): |
| 65 | + """Test adding YAML parsing error to annotations.""" |
| 66 | + annotations = Annotations() |
| 67 | + try: |
| 68 | + yaml.load(",", Loader=LineNumberYamlLoader) |
| 69 | + except yaml.YAMLError as e: |
| 70 | + annotations.add_yaml_error(e) |
| 71 | + |
| 72 | + assert annotations[0]["type"] == "error" |
| 73 | + assert "," in annotations[0]["text"] |
| 74 | + assert annotations[0]["row"] == 0 |
| 75 | + assert annotations[0]["column"] == 0 |
| 76 | + |
| 77 | + |
| 78 | +def test_suggest(): |
| 79 | + """Test suggestions for misspelled words.""" |
| 80 | + annotations = Annotations() |
| 81 | + keywords = ["start", "end", "duration"] |
| 82 | + assert annotations.suggest("starrt", keywords) == "Did you mean 'start'?\n" |
| 83 | + assert annotations.suggest("ennnd", keywords) == "Did you mean 'end'?\n" |
| 84 | + assert annotations.suggest("durasdtion", keywords) == "Did you mean 'duration'?\n" |
| 85 | + assert annotations.suggest("asdf", keywords) == "" |
0 commit comments