Skip to content

Commit 1e2213e

Browse files
author
hungtu
committed
aichaos#94 Add unit test for sorting trigger with weights tag. Minor update on comment
1 parent ef599b6 commit 1e2213e

2 files changed

Lines changed: 22 additions & 5 deletions

File tree

rivescript/sorting.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class TriggerObj(object):
3232
pound: Number of numeric wildcards (``#``)
3333
under: Number of alphabetical wildcards (``_``)
3434
option: Number of optional tags ("[man]" in "hey [man]"), assume that the template is properly formatted
35+
is_empty: Boolean variable indicating whether the trigger has non-zero wordcount
3536
"""
3637

3738
def __init__(self, pattern, index, weight, inherit = sys.maxsize):
@@ -109,9 +110,10 @@ def sort_trigger_set(triggers, exclude_previous=True, say=None):
109110

110111
trigger_object_list.append(TriggerObj(pattern, index, weight, inherit))
111112

112-
# Priority order of sorting criteria: weight, inherit, star, pound, under, option, wordcount, len, alphabet
113+
# Priority order of sorting criteria:
114+
# weight, inherit, is_empty, star, pound, under, option, wordcount, len, alphabet
113115
sorted_list = sorted(trigger_object_list,
114-
key=attrgetter('weight', 'inherit', "is_empty", 'star', 'pound',
116+
key=attrgetter('weight', 'inherit', 'is_empty', 'star', 'pound',
115117
'under', 'option', 'wordcount', 'len', 'alphabet'))
116118
return [triggers[item.index] for item in sorted_list]
117119

tests/test_sorting.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@ def test_sorting_triggers(self):
6161
6262
+ _ _
6363
- 17
64+
65+
+ ho _{weight=100}
66+
- 18
67+
68+
+ ho _
69+
- 19
6470
""")
6571

6672
sorted_triggers = {trig[0]:position for position, trig in enumerate(self.rs._brain.master._sorted["topics"]['random'])}
@@ -81,10 +87,11 @@ def test_sorting_triggers(self):
8187
# 5) Sorted by number of wildcard triggers
8288
self.assertLess(sorted_triggers['hi *'], sorted_triggers['* you *'])
8389

84-
# 6) The `super catch all` (only single star `*` or `[*]`) should be last
85-
self.assertGreaterEqual(sorted_triggers['*'], max(sorted_triggers.values())-1)
90+
# 6) The `super catch all` (only single star `*` or `[*]`) should be the last two
91+
third_last_position = max(sorted_triggers.values())-2
92+
self.assertLess(third_last_position, sorted_triggers['*'])
8693
self.assertLess(sorted_triggers['hi [*]'], sorted_triggers['*'])
87-
self.assertGreaterEqual(sorted_triggers['[*]'], max(sorted_triggers.values())-1)
94+
self.assertLess(third_last_position, sorted_triggers['[*]'])
8895
self.assertLess(sorted_triggers['[*] hi [*]'], sorted_triggers['[*]'])
8996
self.assertLess(sorted_triggers['[*] hi *'], sorted_triggers['*'])
9097
self.assertLess(sorted_triggers['hi [*]'], sorted_triggers['[*]'])
@@ -102,3 +109,11 @@ def test_sorting_triggers(self):
102109
# 9) Among the triggers with text, the order of wildcard priority still holds
103110
self.assertLess(sorted_triggers['hi _'], sorted_triggers['hi *'])
104111
self.assertLess(sorted_triggers['hi _'], sorted_triggers['hi [*]'])
112+
113+
# 10) Among the triggers with text, the order of wildcard priority still holds
114+
self.assertLess(sorted_triggers['hi _'], sorted_triggers['hi *'])
115+
self.assertLess(sorted_triggers['hi _'], sorted_triggers['hi [*]'])
116+
117+
# 11) Making sure that the weight tag is taken into account
118+
self.assertLess(sorted_triggers['ho _{weight=100}'], sorted_triggers['hi _'])
119+
self.assertLess(sorted_triggers['hi _'], sorted_triggers['ho _'])

0 commit comments

Comments
 (0)