Skip to content

Commit f024a97

Browse files
mkusakagnachman
andauthored
Add _name method to Trigger base class (#595)
* Add _name method to Trigger base class Trigger.encode calls self._name() but the base class did not define it, causing AttributeError on unrecognized trigger types returned by decode_trigger. * Add tests for Trigger base class _name method Verify that: - Trigger base class has _name attribute - Trigger._name() returns "Trigger" - Trigger.encode does not crash on bare Trigger instances - Unrecognized trigger types can round-trip through decode/encode --------- Co-authored-by: George Nachman <gnachman@gmail.com>
1 parent c220d3e commit f024a97

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

api/library/python/iterm2/iterm2/triggers.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,8 @@ def enabled(self, value: bool):
137137
self.__enabled = value
138138

139139
@staticmethod
140+
def _name():
141+
return "Trigger"
140142
def deserialize(regex: str, param: str, instant: bool, enabled: bool) -> 'Trigger':
141143
return Trigger(regex, param, instant, enabled)
142144

api/library/python/iterm2/tests/test_triggers.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,49 @@
11
"""Tests for iterm2.triggers module."""
2+
from iterm2.triggers import Trigger, decode_trigger
3+
4+
5+
class TestTriggerBaseName:
6+
"""Tests for the Trigger base class _name method."""
7+
8+
def test_trigger_base_has_name(self):
9+
"""Test that Trigger base class defines _name."""
10+
assert hasattr(Trigger, '_name')
11+
12+
def test_trigger_base_name_returns_trigger(self):
13+
"""Test that Trigger._name() returns 'Trigger'."""
14+
assert Trigger._name() == "Trigger"
15+
16+
def test_trigger_instance_encode_does_not_crash(self):
17+
"""Test that encode works on a bare Trigger instance.
18+
19+
decode_trigger returns a bare Trigger for unrecognized trigger
20+
types, and calling .encode on it should not raise AttributeError.
21+
"""
22+
trigger = Trigger(regex="test", param="", instant=False, enabled=True)
23+
encoded = trigger.encode
24+
assert encoded["action"] == "Trigger"
25+
assert encoded["regex"] == "test"
26+
assert encoded["parameter"] == ""
27+
assert encoded["partial"] is False
28+
assert encoded["disabled"] is False
29+
30+
def test_unrecognized_trigger_roundtrip(self):
31+
"""Test that an unrecognized trigger type can round-trip through
32+
decode_trigger and encode without crashing."""
33+
encoded = {
34+
"action": "SomeFutureTrigger",
35+
"regex": "pattern",
36+
"parameter": "param_value",
37+
"partial": True,
38+
"disabled": False,
39+
}
40+
trigger = decode_trigger(encoded)
41+
assert isinstance(trigger, Trigger)
42+
43+
result = trigger.encode
44+
assert result["regex"] == "pattern"
45+
assert result["partial"] is True
46+
assert result["disabled"] is False
247
from iterm2.triggers import (
348
Trigger,
449
AlertTrigger,

0 commit comments

Comments
 (0)