Skip to content

Commit 9488582

Browse files
author
hungtu
committed
Issue 82 replace regex match by regex search for detection in the middle of the string
1 parent 4c3d82a commit 9488582

2 files changed

Lines changed: 32 additions & 10 deletions

File tree

rivescript/parser.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -535,13 +535,13 @@ def check_syntax(self, cmd, line):
535535
if parts[0] == "begin" and len(parts) > 1:
536536
return "The 'begin' label takes no additional arguments, should be verbatim '> begin'"
537537
elif parts[0] == "topic":
538-
match = re.match(RE.name_syntax, line)
539-
if match:
538+
search = re.search(RE.name_syntax, line)
539+
if search:
540540
return "Topics should be lowercased and contain only numbers and letters"
541541
elif parts[0] == "object":
542-
match = re.match(RE.name_syntax, line)
543-
if match:
544-
return "Objects can only contain numbers and letters"
542+
search = re.search(RE.name_syntax, line)
543+
if search:
544+
return "Objects can only contain numbers and letters" # TODO Acceptance of uppercase letter?
545545
elif cmd == '+' or cmd == '%' or cmd == '@':
546546
# + Trigger, % Previous, @ Redirect
547547
# This one is strict. The triggers are to be run through the regexp engine,
@@ -585,12 +585,12 @@ def check_syntax(self, cmd, line):
585585

586586
# In UTF-8 mode, most symbols are allowed.
587587
if self.utf8:
588-
match = re.match(RE.utf8_trig, line)
589-
if match:
588+
search = re.search(RE.utf8_trig, line)
589+
if search:
590590
return "Triggers can't contain uppercase letters, backslashes or dots in UTF-8 mode."
591591
else:
592-
match = re.match(RE.trig_syntax, line)
593-
if match:
592+
search = re.search(RE.trig_syntax, line)
593+
if search:
594594
return "Triggers may only contain lowercase letters, numbers, and these symbols: ( | ) [ ] * _ # @ { } < > ="
595595
elif cmd == '-' or cmd == '^' or cmd == '/':
596596
# - Trigger, ^ Continue, / Comment

tests/test_format_message.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,26 @@ def test_format_triggers(self):
2828
2929
""")
3030
self.reply("hi there", "hi there")
31-
self.reply("hi here", "hi here")
31+
self.reply("hi here", "hi here")
32+
33+
def test_invalid_character_raise_exception(self):
34+
self.assertRaises(Exception, self.new, """
35+
+ $hello
36+
- hi
37+
""") # This test passes with `match`, which only check at the beginning
38+
self.assertRaises(Exception, self.new, """
39+
+ hello$
40+
- hi
41+
""") # This test does not pass because the beginning is good, no $
42+
self.assertRaises(Exception, self.new, """
43+
> topic Greetings
44+
+ hello
45+
- hi
46+
<topics
47+
""")
48+
self.assertRaises(Exception, self.new, """
49+
> object hash %perl
50+
my ($rs, $args) = @_;
51+
my $method = shift @{$args};
52+
<object
53+
""") # Test for character violation in object, no %

0 commit comments

Comments
 (0)