1 IN: smalltalk.parser.tests
2 USING: smalltalk.parser smalltalk.ast peg.ebnf tools.test accessors
3 io.files io.encodings.ascii kernel ;
6 test = <foreign parse-smalltalk Character>
9 [ CHAR: a ] [ "a" test-Character ] unit-test
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 ]
24 test = <foreign parse-smalltalk Identifier>
27 [ "OrderedCollection" ] [ "OrderedCollection" test-Identifier ] unit-test
30 test = <foreign parse-smalltalk Literal>
33 [ nil ] [ "nil" test-Literal ] unit-test
34 [ 123 ] [ "123" test-Literal ] unit-test
35 [ HEX: deadbeef ] [ "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
56 EBNF: test-FormalBlockArgumentDeclarationList
57 test = <foreign parse-smalltalk FormalBlockArgumentDeclarationList>
60 [ V{ "x" "y" "elt" } ] [ ":x :y :elt" test-FormalBlockArgumentDeclarationList ] unit-test
63 test = <foreign parse-smalltalk Operand>
66 [ { 123 15.6 { t f } } ] [ "#(123 15.6 (true false))" test-Operand ] unit-test
67 [ T{ ast-name f "x" } ] [ "x" test-Operand ] unit-test
70 test = <foreign parse-smalltalk Expression>
73 [ self ] [ "self" test-Expression ] unit-test
74 [ { 123 15.6 { t f } } ] [ "#(123 15.6 (true false))" test-Expression ] unit-test
75 [ T{ ast-name f "x" } ] [ "x" test-Expression ] unit-test
76 [ T{ ast-message-send f 5 "print" { } } ] [ "5 print" test-Expression ] unit-test
77 [ T{ ast-message-send f T{ ast-message-send f 5 "squared" { } } "print" { } } ] [ "5 squared print" test-Expression ] unit-test
78 [ T{ ast-message-send f 2 "+" { 2 } } ] [ "2+2" test-Expression ] unit-test
82 T{ ast-message-send f 3 "factorial" { } }
84 { T{ ast-message-send f 4 "factorial" { } } }
87 [ "3 factorial + 4 factorial" test-Expression ] unit-test
92 T{ ast-message-send f 3 "factorial" { } }
100 [ "(3 factorial + 4) factorial" test-Expression ] unit-test
101 EBNF: test-FinalStatement
102 test = <foreign parse-smalltalk FinalStatement>
105 [ T{ ast-return f T{ ast-name f "value" } } ] [ "value" test-FinalStatement ] unit-test
106 [ T{ ast-return f T{ ast-name f "value" } } ] [ "^value" test-FinalStatement ] unit-test
107 [ T{ ast-return f T{ ast-assignment f T{ ast-name f "value" } 5 } } ] [ "value:=5" test-FinalStatement ] unit-test
109 EBNF: test-LocalVariableDeclarationList
110 test = <foreign parse-smalltalk LocalVariableDeclarationList>
113 [ T{ ast-local-variables f { "i" "j" } } ] [ " | i j |" test-LocalVariableDeclarationList ] unit-test
116 EBNF: test-KeywordMessageSend
117 test = <foreign parse-smalltalk KeywordMessageSend>
120 [ T{ ast-message-send f T{ ast-name f "x" } "foo:bar:" { 1 2 } } ]
121 [ "x foo:1 bar:2" test-KeywordMessageSend ] unit-test
126 T{ ast-message-send f
127 T{ ast-message-send f 3 "factorial" { } }
129 { T{ ast-message-send f 4 "factorial" { } } }
135 [ "3 factorial + 4 factorial between: 10 and: 100" test-KeywordMessageSend ] unit-test
137 [ ] [ "vocab:smalltalk/parser/test.st" ascii file-contents parse-smalltalk drop ] unit-test