1 USING: smalltalk.parser smalltalk.ast peg.ebnf tools.test accessors
2 io.files io.encodings.ascii kernel multiline ;
3 IN: smalltalk.parser.tests
5 EBNF: test-Character [=[
6 test = <foreign parse-smalltalk Character>
9 { CHAR: a } [ "a" test-Character ] unit-test
11 EBNF: test-Comment [=[
12 test = <foreign parse-smalltalk Comment>
15 { T{ ast-comment f "Hello, this is a comment." } }
16 [ "\"Hello, this is a comment.\"" test-Comment ]
19 { T{ ast-comment f "Hello, \"this\" is a comment." } }
20 [ "\"Hello, \"\"this\"\" is a comment.\"" test-Comment ]
23 EBNF: test-Identifier [=[
24 test = <foreign parse-smalltalk Identifier>
27 { "OrderedCollection" } [ "OrderedCollection" test-Identifier ] unit-test
29 EBNF: test-Literal [=[
30 test = <foreign parse-smalltalk Literal>
33 { nil } [ "nil" test-Literal ] unit-test
34 { 123 } [ "123" test-Literal ] unit-test
35 { 0xdeadbeef } [ "16rdeadbeef" test-Literal ] unit-test
36 { -123 } [ "-123" test-Literal ] unit-test
37 { 1.2 } [ "1.2" test-Literal ] unit-test
38 { -1.24 } [ "-1.24" test-Literal ] unit-test
39 { 12.4e7 } [ "12.4e7" test-Literal ] unit-test
40 { 12.4e-7 } [ "12.4e-7" test-Literal ] unit-test
41 { -12.4e7 } [ "-12.4e7" test-Literal ] unit-test
42 { CHAR: x } [ "$x" test-Literal ] unit-test
43 { "Hello, world" } [ "'Hello, world'" test-Literal ] unit-test
44 { "Hello, 'funny' world" } [ "'Hello, ''funny'' world'" test-Literal ] unit-test
45 { T{ symbol f "foo" } } [ "#foo" test-Literal ] unit-test
46 { T{ symbol f "+" } } [ "#+" test-Literal ] unit-test
47 { T{ symbol f "at:put:" } } [ "#at:put:" test-Literal ] unit-test
48 { T{ symbol f "Hello world" } } [ "#'Hello world'" test-Literal ] unit-test
49 { B{ 1 2 3 4 } } [ "#[1 2 3 4]" test-Literal ] unit-test
50 { { nil t f } } [ "#(nil true false)" test-Literal ] unit-test
51 { { nil { t f } } } [ "#(nil (true false))" test-Literal ] unit-test
52 { T{ ast-block f { } { } { } } } [ "[]" test-Literal ] unit-test
53 { T{ ast-block f { "x" } { } { T{ ast-return f T{ ast-name f "x" } } } } } [ "[ :x|^x]" test-Literal ] unit-test
54 { T{ ast-block f { } { } { T{ ast-return f self } } } } [ "[^self]" test-Literal ] unit-test
62 { receiver T{ ast-name { name "i" } } }
69 [ "[ :i | i print ]" test-Literal ] unit-test
76 [ "[5. self]" test-Literal ] unit-test
78 EBNF: test-FormalBlockArgumentDeclarationList [=[
79 test = <foreign parse-smalltalk FormalBlockArgumentDeclarationList>
82 { V{ "x" "y" "elt" } } [ ":x :y :elt" test-FormalBlockArgumentDeclarationList ] unit-test
84 EBNF: test-Operand [=[
85 test = <foreign parse-smalltalk Operand>
88 { { 123 15.6 { t f } } } [ "#(123 15.6 (true false))" test-Operand ] unit-test
89 { T{ ast-name f "x" } } [ "x" test-Operand ] unit-test
91 EBNF: test-Expression [=[
92 test = <foreign parse-smalltalk Expression>
95 { self } [ "self" test-Expression ] unit-test
96 { { 123 15.6 { t f } } } [ "#(123 15.6 (true false))" test-Expression ] unit-test
97 { T{ ast-name f "x" } } [ "x" test-Expression ] unit-test
98 { T{ ast-message-send f 5 "print" { } } } [ "5 print" test-Expression ] unit-test
99 { T{ ast-message-send f T{ ast-message-send f 5 "squared" { } } "print" { } } } [ "5 squared print" test-Expression ] unit-test
100 { T{ ast-message-send f 2 "+" { 2 } } } [ "2+2" test-Expression ] unit-test
103 T{ ast-message-send f
104 T{ ast-message-send f 3 "factorial" { } }
106 { T{ ast-message-send f 4 "factorial" { } } }
109 [ "3 factorial + 4 factorial" test-Expression ] unit-test
112 T{ ast-message-send f
113 T{ ast-message-send f 3 "factorial" { } }
115 { T{ ast-message-send f 4 "factorial" { } } }
118 [ " 3 factorial + 4 factorial" test-Expression ] unit-test
121 T{ ast-message-send f
122 T{ ast-message-send f 3 "factorial" { } }
124 { T{ ast-message-send f 4 "factorial" { } } }
127 [ " 3 factorial + 4 factorial " test-Expression ] unit-test
130 T{ ast-message-send f
131 T{ ast-message-send f
132 T{ ast-message-send f 3 "factorial" { } }
140 [ "(3 factorial + 4) factorial" test-Expression ] unit-test
153 { selector "ifTrue:ifFalse:" }
156 T{ ast-block { body { "HI" } } }
157 T{ ast-block { body { "BYE" } } }
165 [ "((1 < 10) ifTrue: [ 'HI' ] ifFalse: [ 'BYE' ]) print" test-Expression ] unit-test
172 T{ ast-message f "sqrt" }
173 T{ ast-message f "+" { 2 } }
178 [ "12 sqrt; + 2" test-Expression ] unit-test
182 { receiver T{ ast-message-send f 12 "sqrt" } }
185 T{ ast-message f "+" { 1 } }
186 T{ ast-message f "+" { 2 } }
191 [ "12 sqrt + 1; + 2" test-Expression ] unit-test
195 { receiver T{ ast-message-send f 12 "squared" } }
198 T{ ast-message f "to:" { 100 } }
199 T{ ast-message f "sqrt" }
204 [ "12 squared to: 100; sqrt" test-Expression ] unit-test
207 T{ ast-message-send f
208 T{ ast-message-send f 1 "+" { 2 } }
213 [ "1+2*3" test-Expression ] unit-test
219 { receiver { T{ ast-block { body { "a" } } } } }
227 [ "(#(['a']) at: 0) value" test-Expression ] unit-test
229 EBNF: test-FinalStatement [=[
230 test = <foreign parse-smalltalk FinalStatement>
233 { T{ ast-name f "value" } } [ "value" test-FinalStatement ] unit-test
234 { T{ ast-return f T{ ast-name f "value" } } } [ "^value" test-FinalStatement ] unit-test
235 { T{ ast-assignment f T{ ast-name f "value" } 5 } } [ "value:=5" test-FinalStatement ] unit-test
237 EBNF: test-LocalVariableDeclarationList [=[
238 test = <foreign parse-smalltalk LocalVariableDeclarationList>
241 { T{ ast-local-variables f { "i" "j" } } } [ " | i j |" test-LocalVariableDeclarationList ] unit-test
244 { T{ ast-message-send f T{ ast-name f "x" } "foo:bar:" { 1 2 } } }
245 [ "x foo:1 bar:2" test-Expression ] unit-test
250 T{ ast-message-send f
251 T{ ast-message-send f 3 "factorial" { } }
253 { T{ ast-message-send f 4 "factorial" { } } }
259 [ "3 factorial + 4 factorial between: 10 and: 100" test-Expression ] unit-test
261 { T{ ast-sequence f { } { 1 2 } } } [ "1. 2" parse-smalltalk ] unit-test
263 { T{ ast-sequence f { } { 1 2 } } } [ "1. 2." parse-smalltalk ] unit-test
266 T{ ast-sequence f { }
270 { superclass "Object" }
276 [ "class Test [|a|]" parse-smalltalk ] unit-test
279 T{ ast-sequence f { }
283 { superclass "Object" }
289 { superclass "Test1" }
295 [ "class Test1 [|a|]. class Test2 extends Test1 [|b|]" parse-smalltalk ] unit-test
297 [ "class Foo []. Tests blah " parse-smalltalk ] must-not-fail
299 [ "vocab:smalltalk/parser/test.st" ascii file-contents parse-smalltalk ] must-not-fail
301 [ "_abc_" parse-smalltalk ] must-not-fail