Skip to content

Commit

Permalink
Merge pull request TotallyNotRobots#63 from TotallyNotRobots/gonzobot…
Browse files Browse the repository at this point in the history
…+factoid-prefix

Add prefixes to listfacts output
  • Loading branch information
linuxdaemon authored Dec 20, 2019
2 parents c7a525d + 2ff17d7 commit f6dbd49
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 6 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Added
- Add the factoid character in the listfacts output
### Changed
- Cleaned up the timeformat API and implementation (#32)
### Fixed
Expand All @@ -14,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fix .urban handling numbers in the query
- Fix the possible `-0` in weather data
- Fix random truncations of search result URLs
- Fix listfacts not listing some facts
### Removed
- Removed rottentomatoes plugin as the API has been removed

Expand Down
14 changes: 8 additions & 6 deletions plugins/factoids.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
import string
from collections import defaultdict

from sqlalchemy import Table, Column, String, PrimaryKeyConstraint, and_
from sqlalchemy import Column, PrimaryKeyConstraint, String, Table, and_

from cloudbot import hook
from cloudbot.util import database, colors, web
from cloudbot.util import colors, database, web
from cloudbot.util.formatting import gen_markdown_table, get_text_list
from cloudbot.util.web import NoPasteException

Expand Down Expand Up @@ -215,14 +215,16 @@ def listfactoids(notice, chan):
reply_text = []
reply_text_length = 0
for word in sorted(factoid_cache[chan].keys()):
added_length = len(word) + 2
text = FACTOID_CHAR + word
added_length = len(text) + 2
if reply_text_length + added_length > 400:
notice(", ".join(reply_text))
reply_text = []
reply_text_length = 0
else:
reply_text.append(word)
reply_text_length += added_length

reply_text.append(text)
reply_text_length += added_length

notice(", ".join(reply_text))


Expand Down
30 changes: 30 additions & 0 deletions tests/plugin_tests/test_factoids.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import importlib
import itertools
import string
from textwrap import dedent

from mock import MagicMock, call, patch
Expand Down Expand Up @@ -106,3 +108,31 @@ def test_clear_facts():
""").strip()

assert compiled.params == {'chan_1': '#example'}


def test_list_facts(mock_db):
from plugins import factoids

factoids.table.create(mock_db.engine, checkfirst=True)

names = [
''.join(c) for c in itertools.product(string.ascii_lowercase, repeat=2)
]

for name in names:
factoids.add_factoid(
mock_db.session(),
name.lower(),
'#chan',
name,
'nick',
)

notice = MagicMock()
factoids.listfactoids(notice, '#chan')

text = ', '.join(call[1][0] for call in notice.mock_calls)

assert text == ', '.join(
'?' + name for name in sorted(names + ['commands'])
)

0 comments on commit f6dbd49

Please sign in to comment.