-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlexer-test.lisp
146 lines (137 loc) · 6.04 KB
/
lexer-test.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
(in-package :cl-user)
(defpackage lexer-test
(:use :cl
:prove
:elvis-parsley))
(in-package :lexer-test)
(plan 7)
(diag "Normal object test case")
(let ((normal-object (make-instance 'json-ast)))
(is (progn
(with-open-stream (stream (make-string-input-stream "{\"tk1\":1, \"tk2\":2.8, \"tk3\":\"tv3\", \"tk4\":true, \"tk5\":false, \"tk6\":null, \"tk7\":{\"sk1\":\"sv1\"}, \"tk8\":[1,2,3], \"tk9\":3., \"tk10\":432, \"tk11\":.14, \"tk12\":-1, \"tk13\":-.38, \"tk14\":-48.546, \"tk15\":+48.546, \"tk16\":+85, \"tk17\":+.916}"))
(lex normal-object stream))
(tokens normal-object))
'((:type :punctuation :value #\{)
(:type :string :value "tk1")
(:type :punctuation :value #\:)
(:type :number :value "1")
(:type :punctuation :value #\,)
(:type :string :value "tk2")
(:type :punctuation :value #\:)
(:type :number :value "2.8")
(:type :punctuation :value #\,)
(:type :string :value "tk3")
(:type :punctuation :value #\:)
(:type :string :value "tv3")
(:type :punctuation :value #\,)
(:type :string :value "tk4")
(:type :punctuation :value #\:)
(:type :keyword :value "true")
(:type :punctuation :value #\,)
(:type :string :value "tk5")
(:type :punctuation :value #\:)
(:type :keyword :value "false")
(:type :punctuation :value #\,)
(:type :string :value "tk6")
(:type :punctuation :value #\:)
(:type :keyword :value "null")
(:type :punctuation :value #\,)
(:type :string :value "tk7")
(:type :punctuation :value #\:)
(:type :punctuation :value #\{)
(:type :string :value "sk1")
(:type :punctuation :value #\:)
(:type :string :value "sv1")
(:type :punctuation :value #\})
(:type :punctuation :value #\,)
(:type :string :value "tk8")
(:type :punctuation :value #\:)
(:type :punctuation :value #\[)
(:type :number :value "1")
(:type :punctuation :value #\,)
(:type :number :value "2")
(:type :punctuation :value #\,)
(:type :number :value "3")
(:type :punctuation :value #\])
(:type :punctuation :value #\,)
(:type :string :value "tk9" )
(:type :punctuation :value #\:)
(:type :number :value "3.")
(:type :punctuation :value #\,)
(:type :string :value "tk10")
(:type :punctuation :value #\:)
(:type :number :value "432")
(:type :punctuation :value #\,)
(:type :string :value "tk11")
(:type :punctuation :value #\:)
(:type :number :value ".14")
(:type :punctuation :value #\,)
(:type :string :value "tk12")
(:type :punctuation :value #\:)
(:type :number :value "-1")
(:type :punctuation :value #\,)
(:type :string :value "tk13")
(:type :punctuation :value #\:)
(:type :number :value "-.38")
(:type :punctuation :value #\,)
(:type :string :value "tk14")
(:type :punctuation :value #\:)
(:type :number :value "-48.546")
(:type :punctuation :value #\,)
(:type :string :value "tk15")
(:type :punctuation :value #\:)
(:type :number :value "+48.546")
(:type :punctuation :value #\,)
(:type :string :value "tk16")
(:type :punctuation :value #\:)
(:type :number :value "+85")
(:type :punctuation :value #\,)
(:type :string :value "tk17")
(:type :punctuation :value #\:)
(:type :number :value "+.916")
(:type :punctuation :value #\}))))
(diag "Normal array test case")
(let ((normal-array (make-instance 'json-ast)))
(is (progn
(with-open-stream (stream (make-string-input-stream "[\"a1\",\"b2\",\"c3\"]"))
(lex normal-array stream))
(tokens normal-array))
'((:type :punctuation :value #\[)
(:type :string :value "a1")
(:type :punctuation :value #\,)
(:type :string :value "b2")
(:type :punctuation :value #\,)
(:type :string :value "c3")
(:type :punctuation :value #\]))))
(diag "Invalid number test case")
(let ((bad-number-object (make-instance 'json-ast)))
(is-error (with-open-stream (stream (make-string-input-stream "{\"tk1\":tasdf}"))
(lex bad-number-object stream))
'invalid-token))
(diag "Invalid keyword test cases")
(let ((bad-keyword-object (make-instance 'json-ast)))
(is-error (with-open-stream (stream (make-string-input-stream "{\"tk1\": trug}"))
(lex bad-keyword-object stream))
'invalid-token))
(diag "unterminated string")
(let ((unterminated-string-object (make-instance 'json-ast)))
(is-error (with-open-stream (stream (make-string-input-stream "{\"tk1\":\"tv1}"))
(lex unterminated-string-object stream))
'invalid-token))
;;TODO write "correct" response tests for lexer
;;This test is based on an error I found from my own testing
(diag "Unterminated object with number values (\"numbers are known as unquoted objects\")")
(let ((unterminated-object-unquoted (make-instance 'json-ast)))
(is (progn
(with-open-stream (stream (make-string-input-stream "{\"tk1\":1, \"tk2\":2"))
(lex unterminated-object-unquoted stream))
(tokens unterminated-object-unquoted))
'((:type :punctuation :value #\{) (:type :string :value "tk1") (:type :punctuation :value #\:) (:type :number :value "1") (:type :punctuation :value #\,) (:type :string :value "tk2") (:type :punctuation :value #\:) (:type :number :value "2"))))
(diag "Unterminated array of numbers")
(let ((unterminated-array-unquoted (make-instance 'json-ast)))
(is (progn
(with-open-stream (stream (make-string-input-stream "[1,2,3"))
(lex unterminated-array-unquoted stream))
(tokens unterminated-array-unquoted))
'((:type :punctuation :value #\[) (:type :number :value "1") (:type :punctuation :value #\,) (:type :number :value "2") (:type :punctuation :value #\,) (:type :number :value "3"))))
(finalize)