Skip to content

Commit

Permalink
tools.memories, test: fix SopelIdentifierMemory.setdefault() behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
dgw committed Oct 21, 2023
1 parent 1d5ecea commit 7ead271
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
3 changes: 3 additions & 0 deletions sopel/tools/memories.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,9 @@ def __contains__(self, key):
def __setitem__(self, key: Optional[str], value):
super().__setitem__(self._make_key(key), value)

def setdefault(self, key: str, default=None):
return super().setdefault(self._make_key(key), default)

def __delitem__(self, key: str):
super().__delitem__(self._make_key(key))

Expand Down
20 changes: 20 additions & 0 deletions test/tools/test_tools_memories.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,26 @@ def test_sopel_identifier_memory_channel_str():
assert memory['#AdminChannel'] == test_value


def test_sopel_identifier_memory_setdefault():
memory = memories.SopelIdentifierMemory()
assert 'foo' not in memory

memory.setdefault('Foo')
assert len(memory) == 1
assert 'foo' in memory
assert 'fOO' in memory
assert memory['fOo'] is None

memory.setdefault('bAr', 'DEFAULT')
assert len(memory) == 2
assert 'FoO' in memory
assert 'BaR' in memory
assert memory['Bar'] == 'DEFAULT'

assert memory.setdefault('baR', 'different') == 'DEFAULT'
assert len(memory) == 2 # still, that is


def test_sopel_identifier_memory_copy():
memory = memories.SopelIdentifierMemory()
memory['SomeCamelCase'] = True
Expand Down

0 comments on commit 7ead271

Please sign in to comment.