aboutsummaryrefslogtreecommitdiff
path: root/stdlib/test/test/lux/lexer.lux
diff options
context:
space:
mode:
Diffstat (limited to 'stdlib/test/test/lux/lexer.lux')
-rw-r--r--stdlib/test/test/lux/lexer.lux133
1 files changed, 133 insertions, 0 deletions
diff --git a/stdlib/test/test/lux/lexer.lux b/stdlib/test/test/lux/lexer.lux
new file mode 100644
index 000000000..d0b17fe4b
--- /dev/null
+++ b/stdlib/test/test/lux/lexer.lux
@@ -0,0 +1,133 @@
+## Copyright (c) Eduardo Julian. All rights reserved.
+## This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
+## If a copy of the MPL was not distributed with this file,
+## You can obtain one at http://mozilla.org/MPL/2.0/.
+
+(;module:
+ [lux #- not]
+ (lux (control monad)
+ (codata [io])
+ (data error)
+ [test #- fail assert]
+ lexer))
+
+## [Tests]
+(test: "Lexer end works"
+ (test-all (should-pass (run end ""))
+ (should-fail (run end "YOLO"))))
+
+(test: "Simple text lexers"
+ (test-all (match (#;Right "YO")
+ (run (this "YO") "YOLO"))
+ (should-fail (run (this "YO") "MEME"))))
+
+(test: "Char lexers"
+ (test-all (match (#;Right #"Y")
+ (run (this-char #"Y") "YOLO"))
+ (should-fail (run (this-char #"Y") "MEME"))
+ (match (#;Right #"Y")
+ (run (char-range #"X" #"Z") "YOLO"))
+ (should-fail (run (char-range #"X" #"Z") "MEME"))
+ (match (#;Right #"Y")
+ (run upper "YOLO"))
+ (should-fail (run upper "meme"))
+ (match (#;Right #"y")
+ (run lower "yolo"))
+ (should-fail (run lower "MEME"))
+ (match (#;Right #"1")
+ (run digit "1"))
+ (should-fail (run digit " "))
+ (match (#;Right #"7")
+ (run oct-digit "7"))
+ (should-fail (run oct-digit "8"))
+ (match (#;Right #"A")
+ (run any "A"))
+ (should-fail (run any ""))))
+
+(test: "Combinators"
+ (test-all (match (#;Right [#"Y" #"O"])
+ (run (seq any any) "YOLO"))
+ (should-fail (run (seq any any) "Y"))
+ (match+ (#;Left #"0")
+ (should-pass (run (alt digit upper) "0")))
+ (match+ (#;Right #"A")
+ (should-pass (run (alt digit upper) "A")))
+ (should-fail (run (alt digit upper) "a"))
+ (should-pass (run (not (alt digit upper)) "a"))
+ (should-fail (run (not (alt digit upper)) "A"))
+ (match (#;Right #"0")
+ (run (either digit upper) "0"))
+ (match (#;Right #"A")
+ (run (either digit upper) "A"))
+ (should-fail (run (either digit upper) "a"))
+ (match (#;Right #"A")
+ (run alpha "A"))
+ (match (#;Right #"a")
+ (run alpha "a"))
+ (should-fail (run alpha "1"))
+ (match (#;Right #"A")
+ (run alpha-num "A"))
+ (match (#;Right #"a")
+ (run alpha-num "a"))
+ (match (#;Right #"1")
+ (run alpha-num "1"))
+ (should-fail (run alpha-num " "))
+ (match (#;Right #"1")
+ (run hex-digit "1"))
+ (match (#;Right #"a")
+ (run hex-digit "a"))
+ (match (#;Right #"A")
+ (run hex-digit "A"))
+ (should-fail (run hex-digit " "))
+ (match (#;Right #" ")
+ (run space " "))
+ (should-fail (run space "8"))
+ (match (#;Right #"C")
+ (run (one-of "ABC") "C"))
+ (should-fail (run (one-of "ABC") "D"))
+ (match (#;Right #"D")
+ (run (none-of "ABC") "D"))
+ (should-fail (run (none-of "ABC") "C"))
+ (match (#;Right #"D")
+ (run (satisfies (lambda [c] true)) "D"))
+ (should-fail (run (satisfies (lambda [c] false)) "C"))
+ (match (#;Right "0123456789ABCDEF")
+ (run (many' hex-digit) "0123456789ABCDEF yolo"))
+ (should-fail (run (many' hex-digit) "yolo"))
+ (match (#;Right "")
+ (run (some' hex-digit) "yolo"))
+ ))
+
+(test: "Yet more combinators..."
+ (test-all (should-fail (run (fail "Well, it really SHOULD fail...") "yolo"))
+ (should-fail (run (assert false "Well, it really SHOULD fail...") "yolo"))
+ (should-pass (run (assert true "GO, GO, GO!") "yolo"))
+ (match (^ (#;Right (list #"0" #"1" #"2" #"3" #"4" #"5" #"6" #"7" #"8" #"9" #"A" #"B" #"C" #"D" #"E" #"F")))
+ (run (many hex-digit) "0123456789ABCDEF yolo"))
+ (should-fail (run (many hex-digit) "yolo"))
+ (match (^ (#;Right (list)))
+ (run (some hex-digit) "yolo"))
+ (match (^ (#;Right (list #"0" #"1" #"2" #"3" #"4" #"5" #"6" #"7" #"8" #"9" #"A" #"B" #"C" #"D" #"E" #"F")))
+ (run (exactly +16 hex-digit) "0123456789ABCDEF yolo"))
+ (match (^ (#;Right (list #"0" #"1" #"2")))
+ (run (exactly +3 hex-digit) "0123456789ABCDEF yolo"))
+ (should-fail (run (exactly +17 hex-digit) "0123456789ABCDEF yolo"))
+ (match (^ (#;Right (list #"0" #"1" #"2" #"3" #"4" #"5" #"6" #"7" #"8" #"9" #"A" #"B" #"C" #"D" #"E" #"F")))
+ (run (at-most +16 hex-digit) "0123456789ABCDEF yolo"))
+ (match (^ (#;Right (list #"0" #"1" #"2")))
+ (run (at-most +3 hex-digit) "0123456789ABCDEF yolo"))
+ (match (^ (#;Right (list #"0" #"1" #"2" #"3" #"4" #"5" #"6" #"7" #"8" #"9" #"A" #"B" #"C" #"D" #"E" #"F")))
+ (run (at-most +17 hex-digit) "0123456789ABCDEF yolo"))
+ (match (^ (#;Right (list #"0" #"1" #"2" #"3" #"4" #"5" #"6" #"7" #"8" #"9" #"A" #"B" #"C" #"D" #"E" #"F")))
+ (run (between +0 +16 hex-digit) "0123456789ABCDEF yolo"))
+ (match (^ (#;Right (list #"0" #"1" #"2" #"3" #"4" #"5" #"6" #"7" #"8" #"9" #"A" #"B" #"C" #"D" #"E" #"F")))
+ (run (between +3 +16 hex-digit) "0123456789ABCDEF yolo"))
+ (should-fail (run (between +17 +100 hex-digit) "0123456789ABCDEF yolo"))
+ (match (^ (#;Right (list #"0" #"1" #"2" #"3" #"4" #"5" #"6" #"7" #"8" #"9" #"A" #"B" #"C" #"D" #"E" #"F")))
+ (run (between +15 +20 hex-digit) "0123456789ABCDEF yolo"))
+ (match (#;Right (#;Some #"1")) (run (opt hex-digit) "123abc"))
+ (match (#;Right #;None) (run (opt hex-digit) "yolo"))
+ (match (^ (#;Right (list #"0" #"1" #"2" #"3" #"4" #"5" #"6" #"7" #"8" #"9" #"a" #"b" #"c" #"d" #"e" #"f")))
+ (run (sep-by space hex-digit) "0 1 2 3 4 5 6 7 8 9 a b c d e f YOLO"))
+ (match (#;Right "yolo") (run get-input "yolo"))
+ ))