From 8cfd527f31adc166fac0446e68919b0f826b41e6 Mon Sep 17 00:00:00 2001 From: Daimona Eaytoy Date: Sat, 7 Apr 2018 12:42:17 +0200 Subject: [PATCH] Reinforce parser tests Some of them are actually too simple, and may be unuseful in tricky situations. This patch adds a lot of test cases to provide an (almost) bombproof safety with future patches. Depends-On: I0bb1ed0109af66997e238b532d342d82d4c4ae19 Change-Id: I274ef306775c36be20acb662353f6537ff3f1a33 --- tests/parserTests/arith.t | 28 ++++++++++++++++++- tests/parserTests/cast.t | 2 +- tests/parserTests/comment.t | 5 +++- tests/parserTests/contains-all.t | 2 +- tests/parserTests/contains-any.t | 2 +- tests/parserTests/contains.r | 1 + tests/parserTests/contains.t | 1 + tests/parserTests/expn.t | 2 +- tests/parserTests/float.t | 2 +- tests/parserTests/get-matches.r | 1 + tests/parserTests/get-matches.t | 4 +++ tests/parserTests/in.t | 2 +- tests/parserTests/mwexamples-arithmetic.r | 1 + tests/parserTests/mwexamples-arithmetic.t | 8 ++++++ tests/parserTests/mwexamples-bools.r | 1 + tests/parserTests/mwexamples-bools.t | 13 +++++++++ tests/parserTests/mwexamples-comparisons.r | 1 + tests/parserTests/mwexamples-comparisons.t | 20 +++++++++++++ tests/parserTests/mwexamples-functions.r | 1 + tests/parserTests/mwexamples-functions.t | 22 +++++++++++++++ tests/parserTests/mwexamples-keywords.r | 1 + tests/parserTests/mwexamples-keywords.t | 9 ++++++ tests/parserTests/mwexamples-lists.r | 1 + tests/parserTests/mwexamples-lists.t | 10 +++++++ tests/parserTests/ord.t | 9 +++++- tests/parserTests/shortcircuit-and.r | 1 + tests/parserTests/shortcircuit-and.t | 2 ++ .../{shortcircuit.r => shortcircuit-or.r} | 0 .../{shortcircuit.t => shortcircuit-or.t} | 0 tests/parserTests/whitespace.r | 1 - 30 files changed, 143 insertions(+), 10 deletions(-) create mode 100644 tests/parserTests/contains.r create mode 100644 tests/parserTests/contains.t create mode 100644 tests/parserTests/get-matches.r create mode 100644 tests/parserTests/get-matches.t create mode 100644 tests/parserTests/mwexamples-arithmetic.r create mode 100644 tests/parserTests/mwexamples-arithmetic.t create mode 100644 tests/parserTests/mwexamples-bools.r create mode 100644 tests/parserTests/mwexamples-bools.t create mode 100644 tests/parserTests/mwexamples-comparisons.r create mode 100644 tests/parserTests/mwexamples-comparisons.t create mode 100644 tests/parserTests/mwexamples-functions.r create mode 100644 tests/parserTests/mwexamples-functions.t create mode 100644 tests/parserTests/mwexamples-keywords.r create mode 100644 tests/parserTests/mwexamples-keywords.t create mode 100644 tests/parserTests/mwexamples-lists.r create mode 100644 tests/parserTests/mwexamples-lists.t create mode 100644 tests/parserTests/shortcircuit-and.r create mode 100644 tests/parserTests/shortcircuit-and.t rename tests/parserTests/{shortcircuit.r => shortcircuit-or.r} (100%) rename tests/parserTests/{shortcircuit.t => shortcircuit-or.t} (100%) delete mode 100644 tests/parserTests/whitespace.r diff --git a/tests/parserTests/arith.t b/tests/parserTests/arith.t index e3c42e32c..1f88e9dfb 100644 --- a/tests/parserTests/arith.t +++ b/tests/parserTests/arith.t @@ -1 +1,27 @@ -(1 + 1 == 2) & (5 - 3 = 2) & (2 * 3 = 6) & (10 / 2 = 5) & (10 % 7 = 3) & (2 ** 4 = 16) +(1 + 1 === 2) & +(1.5 + 1.5 === 3.0) & +(2.5 + 1 === 3.5) & +(0 + 1 === 1) & +(2.5 + 0 === 2.5) & +(5 - 3 === 2) & +(5 - 3.5 === 1.5) & +(5.5 - 3.5 === 2.0) & +(1 - 0 === 1) & +(2.5 - 0 === 2.5) & +(2 * 3 === 6) & +(2 * 3.5 === 7.0) & +(2.5 * 3.5 === 8.75) & +(2.5 * 0 === 0.0) & +(10 / 2 === 5) & +(10 / 2.5 === 4.0) & +(18 / 36 === 0.5) & +(0 / 36 === 0) & +(12.5 / 2.5 === 5.0) & +(10.5 / 2.5 === 4.2) & +(10 % 7 === 3) & +(10.48762 % 7 === 3) & +(10 % 7.123576 === 3) & +(2 ** 4 === 16) & +(2.5 ** 2 === 6.25) & +(2.5 ** 0 === 1.0) & +(1000 ** 0 === 1) \ No newline at end of file diff --git a/tests/parserTests/cast.t b/tests/parserTests/cast.t index 0ec2720ef..c0bc317cd 100644 --- a/tests/parserTests/cast.t +++ b/tests/parserTests/cast.t @@ -1 +1 @@ -(string(1) === "1") & (int("1") === 1) & (float(1) === 1.0) +(string(1) === "1") & (int("1") === 1) & (float(1) === 1.0) & bool(1) & !bool(0) diff --git a/tests/parserTests/comment.t b/tests/parserTests/comment.t index 2ddf58293..3252b87a3 100644 --- a/tests/parserTests/comment.t +++ b/tests/parserTests/comment.t @@ -1 +1,4 @@ -1 /* a */ == /* b */ "1" /* c */ +1 /* a */ == /* b */ "1" /* c */ /* & + +1/0 +*/ diff --git a/tests/parserTests/contains-all.t b/tests/parserTests/contains-all.t index 21026e42d..f8b81b24b 100644 --- a/tests/parserTests/contains-all.t +++ b/tests/parserTests/contains-all.t @@ -1 +1 @@ -contains_all("the foo is on the bar", "foo", "is on", "bar") \ No newline at end of file +contains_all("the foo is on the bar", "foo", "is on", "bar") & !(contains_all(['foo', 'bar', 'hey'], 'foo', 'bar', 'sup')) & contains_all([1, 2, 3], '1', '2', '3') \ No newline at end of file diff --git a/tests/parserTests/contains-any.t b/tests/parserTests/contains-any.t index 6fee9e8d6..56f1c8bde 100644 --- a/tests/parserTests/contains-any.t +++ b/tests/parserTests/contains-any.t @@ -1 +1 @@ -contains_any("like anyone else", "else", "someone") & contains_any("street fighter", "fight") +contains_any("like anyone else", "else", "someone") & contains_any("street fighter", "fight") & !(contains_any('My foo is cute', 'bar', 'wtf')) & contains_any([[1], [2,3]], 1) diff --git a/tests/parserTests/contains.r b/tests/parserTests/contains.r new file mode 100644 index 000000000..f629599c3 --- /dev/null +++ b/tests/parserTests/contains.r @@ -0,0 +1 @@ +MATCH \ No newline at end of file diff --git a/tests/parserTests/contains.t b/tests/parserTests/contains.t new file mode 100644 index 000000000..cac43d24e --- /dev/null +++ b/tests/parserTests/contains.t @@ -0,0 +1 @@ +"quux" contains "ux" & ['1', 'foo'] contains '1' & !('fo obar' contains 'foo') & ['foo'] contains 'f' \ No newline at end of file diff --git a/tests/parserTests/expn.t b/tests/parserTests/expn.t index bd39c3869..17849182c 100644 --- a/tests/parserTests/expn.t +++ b/tests/parserTests/expn.t @@ -1,2 +1,2 @@ /* In filter language, the exponentiation is left-associative */ -(2 ** 3 ** 2) == 64 +(2 ** 3 ** 2) === 64 diff --git a/tests/parserTests/float.t b/tests/parserTests/float.t index 792f19d65..e31067e0b 100644 --- a/tests/parserTests/float.t +++ b/tests/parserTests/float.t @@ -1 +1 @@ -(5 / 2 = 2.5) & (5. / 2 = 2.5) & (5 / 2. = 2.5) & (int(.5) = 0) +(5 / 2 === 2.5) & (int(5 / 2) === 2) & (5. / 2 === 2.5) & (5 / 2. === 2.5) & (int(.5) === 0) diff --git a/tests/parserTests/get-matches.r b/tests/parserTests/get-matches.r new file mode 100644 index 000000000..f629599c3 --- /dev/null +++ b/tests/parserTests/get-matches.r @@ -0,0 +1 @@ +MATCH \ No newline at end of file diff --git a/tests/parserTests/get-matches.t b/tests/parserTests/get-matches.t new file mode 100644 index 000000000..88e163b46 --- /dev/null +++ b/tests/parserTests/get-matches.t @@ -0,0 +1,4 @@ +/* More complete tests for get_matches are in AbuseFilterParserTest.php */ +a := get_matches('I am a (dog|cat)', 'What did you say?'); +get_matches('The (truth|pineapple) is (?:rarely)? pure and (nee*v(ah|er) sh?imple)', 'The truth is rarely pure and never simple, Wilde said') == ['The truth is rarely pure and never simple', 'truth', 'never simple', 'er'] & +a === [false, false] \ No newline at end of file diff --git a/tests/parserTests/in.t b/tests/parserTests/in.t index 467639ce6..73d480d32 100644 --- a/tests/parserTests/in.t +++ b/tests/parserTests/in.t @@ -1 +1 @@ -"foo" in "foobar" & "quux" contains "ux" +"foo" in "foobar" & '1' in ['1', 'foo'] & !('foo' in 'fo obar') & 'f' in ['foo'] diff --git a/tests/parserTests/mwexamples-arithmetic.r b/tests/parserTests/mwexamples-arithmetic.r new file mode 100644 index 000000000..f629599c3 --- /dev/null +++ b/tests/parserTests/mwexamples-arithmetic.r @@ -0,0 +1 @@ +MATCH \ No newline at end of file diff --git a/tests/parserTests/mwexamples-arithmetic.t b/tests/parserTests/mwexamples-arithmetic.t new file mode 100644 index 000000000..7cfaf4afb --- /dev/null +++ b/tests/parserTests/mwexamples-arithmetic.t @@ -0,0 +1,8 @@ +/* Examples from [[mw:Extension:AbuseFilter/Rules format#Arithmetic]] */ + +1 + 1 === 2 & +2 * 2 === 4 & +12 / 24 === 0.5 & +24 / 12 === 2 & +9 ** 2 === 81 & +6 % 5 === 1 \ No newline at end of file diff --git a/tests/parserTests/mwexamples-bools.r b/tests/parserTests/mwexamples-bools.r new file mode 100644 index 000000000..f629599c3 --- /dev/null +++ b/tests/parserTests/mwexamples-bools.r @@ -0,0 +1 @@ +MATCH \ No newline at end of file diff --git a/tests/parserTests/mwexamples-bools.t b/tests/parserTests/mwexamples-bools.t new file mode 100644 index 000000000..a8e885395 --- /dev/null +++ b/tests/parserTests/mwexamples-bools.t @@ -0,0 +1,13 @@ +/* Examples from [[mw:Extension:AbuseFilter/Rules format#Boolean operations]] */ + +(1 | 1) & +(1 | 0) & +!(0 | 0) & +(1 & 1) & +!(1 & 0) & +!(0 & 0) & +!(1 ^ 1) & +(1 ^ 0) & +!(0 ^ 0) & +!(!1) + diff --git a/tests/parserTests/mwexamples-comparisons.r b/tests/parserTests/mwexamples-comparisons.r new file mode 100644 index 000000000..f629599c3 --- /dev/null +++ b/tests/parserTests/mwexamples-comparisons.r @@ -0,0 +1 @@ +MATCH \ No newline at end of file diff --git a/tests/parserTests/mwexamples-comparisons.t b/tests/parserTests/mwexamples-comparisons.t new file mode 100644 index 000000000..7d0ada0dd --- /dev/null +++ b/tests/parserTests/mwexamples-comparisons.t @@ -0,0 +1,20 @@ +/* Examples from [[mw:Extension:AbuseFilter/Rules format#Simple comparisons]] */ + +!(1 == 2) & +(1 <= 2) & +!(1 >= 2) & +(1 != 2) & +(1 < 2) & +!(1 > 2) & +(2 = 2) & +('' == false) & +!('' === false) & +(1 == true) & +!(1 === true) & +(['1','2','3'] == ['1','2','3']) & +([1,2,3] === [1,2,3]) & +(['1','2','3'] == [1,2,3]) & +!(['1','2','3'] === [1,2,3]) & +([1,1,''] == [true, true, false]) & +!(['1'] == '1') + diff --git a/tests/parserTests/mwexamples-functions.r b/tests/parserTests/mwexamples-functions.r new file mode 100644 index 000000000..f629599c3 --- /dev/null +++ b/tests/parserTests/mwexamples-functions.r @@ -0,0 +1 @@ +MATCH \ No newline at end of file diff --git a/tests/parserTests/mwexamples-functions.t b/tests/parserTests/mwexamples-functions.t new file mode 100644 index 000000000..ea5bf1b1e --- /dev/null +++ b/tests/parserTests/mwexamples-functions.t @@ -0,0 +1,22 @@ +/* Examples from [[mw:Extension:AbuseFilter/Rules format#Functions]] */ + +length( "Wikipedia" ) === 9 & +lcase( "WikiPedia" ) === 'wikipedia' & +ccnorm( "w1k1p3d14" ) === 'WIKIPEDIA' & +ccnorm( "ωɨƙɩᑭƐƉ1α" ) === 'WIKIPEDIA' & +ccnorm_contains_any( "w1k1p3d14", "wiKiP3D1A", "foo", "bar" ) === true & +ccnorm_contains_any( "w1k1p3d14", "foo", "bar", "baz" ) === false & +ccnorm_contains_any( "w1k1p3d14 is 4w3s0me", "bar", "baz", "some" ) === true & +ccnorm( "ìíîïĩїį!ľ₤ĺľḷĿ" ) === 'IIIIIII!LLLLLL' & +norm( "!!ω..ɨ..ƙ..ɩ..ᑭᑭ..Ɛ.Ɖ@@1%%α!!" ) === 'WIKIPEDAIA' & +norm( "F00 B@rr" ) === 'FOBAR' & +rmdoubles( "foobybboo" ) === 'fobybo' & +specialratio( "Wikipedia!" ) === 0.1 & +count( "foo", "foofooboofoo" ) === 3 & +count( "foo,bar,baz" ) === 3 & +rmspecials( "FOOBAR!!1" ) === 'FOOBAR1' & +rescape( "abc* (def)" ) === 'abc\* \(def\)' & +str_replace( "foobarbaz", "bar", "-" ) === 'foo-baz' & +ip_in_range( "127.0.10.0", "127.0.0.0/12" ) === true & +contains_any( "foobar", "x", "y", "f" ) === true & +get_matches( "(foo?ba+r) is (so+ good)", "fobaaar is soooo good to eat" ) === ['fobaaar is soooo good', 'fobaaar', 'soooo good'] \ No newline at end of file diff --git a/tests/parserTests/mwexamples-keywords.r b/tests/parserTests/mwexamples-keywords.r new file mode 100644 index 000000000..f629599c3 --- /dev/null +++ b/tests/parserTests/mwexamples-keywords.r @@ -0,0 +1 @@ +MATCH \ No newline at end of file diff --git a/tests/parserTests/mwexamples-keywords.t b/tests/parserTests/mwexamples-keywords.t new file mode 100644 index 000000000..d6ca07d46 --- /dev/null +++ b/tests/parserTests/mwexamples-keywords.t @@ -0,0 +1,9 @@ +/* Examples from [[mw:Extension:AbuseFilter/Rules format#Keywords]] */ + +("1234" like "12?4") & +("1234" like "12*") & +("foo" in "foobar") & +("o" in ["foo", "bar"]) & +("foo" regex "\w+") & +("a\b" regex "a\\\\b") & +("a\b" regex "a\x5C\x5Cb") \ No newline at end of file diff --git a/tests/parserTests/mwexamples-lists.r b/tests/parserTests/mwexamples-lists.r new file mode 100644 index 000000000..f629599c3 --- /dev/null +++ b/tests/parserTests/mwexamples-lists.r @@ -0,0 +1 @@ +MATCH \ No newline at end of file diff --git a/tests/parserTests/mwexamples-lists.t b/tests/parserTests/mwexamples-lists.t new file mode 100644 index 000000000..904413b30 --- /dev/null +++ b/tests/parserTests/mwexamples-lists.t @@ -0,0 +1,10 @@ +/* Examples from [[mw:Extension:AbuseFilter/Rules format#Lists]] */ + +a_list := [ 5, 6, 7, 10]; +a_list[0] == 5 & +length(a_list) == 4 & +string(a_list) == "5\n6\n7\n10\n" & +5 in a_list == true & +'5' in a_list == true & +'5\n6' in a_list == true & +1 in a_list == true \ No newline at end of file diff --git a/tests/parserTests/ord.t b/tests/parserTests/ord.t index a82aafa72..6c94f4b6e 100644 --- a/tests/parserTests/ord.t +++ b/tests/parserTests/ord.t @@ -1 +1,8 @@ -(1 > 0) & (0 < 1) & (2 >= 2) & (2 <= 2) +1 > 0 & +0 < 1 & +2 >= 2 & +2 <= 2 & +0.1 < 0.2 & +0.001 <= 0.001 & +-0.01 < 0.01 & +0 >= -0.0001 diff --git a/tests/parserTests/shortcircuit-and.r b/tests/parserTests/shortcircuit-and.r new file mode 100644 index 000000000..33a8a8051 --- /dev/null +++ b/tests/parserTests/shortcircuit-and.r @@ -0,0 +1 @@ +NOT_MATCH \ No newline at end of file diff --git a/tests/parserTests/shortcircuit-and.t b/tests/parserTests/shortcircuit-and.t new file mode 100644 index 000000000..d87746de1 --- /dev/null +++ b/tests/parserTests/shortcircuit-and.t @@ -0,0 +1,2 @@ +/* The division by zero should not be executed and not crash the filter */ +false & 1/0 \ No newline at end of file diff --git a/tests/parserTests/shortcircuit.r b/tests/parserTests/shortcircuit-or.r similarity index 100% rename from tests/parserTests/shortcircuit.r rename to tests/parserTests/shortcircuit-or.r diff --git a/tests/parserTests/shortcircuit.t b/tests/parserTests/shortcircuit-or.t similarity index 100% rename from tests/parserTests/shortcircuit.t rename to tests/parserTests/shortcircuit-or.t diff --git a/tests/parserTests/whitespace.r b/tests/parserTests/whitespace.r deleted file mode 100644 index 4736e0800..000000000 --- a/tests/parserTests/whitespace.r +++ /dev/null @@ -1 +0,0 @@ -MATCH