1 USING: arrays math parser tools.test kernel generic words
2 io.streams.string namespaces classes effects source-files assocs
3 sequences strings io.files io.pathnames definitions
4 continuations sorting classes.tuple compiler.units debugger
5 vocabs vocabs.loader accessors eval combinators lexer
6 vocabs.parser words.symbol multiline source-files.errors
7 tools.crossref grouping sets ;
11 [ "1\n[\n2\n[\n3\n]\n4\n]\n5" eval( -- a b c ) ]
15 [ "t t f f" eval( -- ? ? ? ? ) ]
19 [ "\"hello world\"" eval( -- string ) ]
23 [ "\"\\n\\r\\t\\\\\"" eval( -- string ) ]
28 """#!/usr/bin/env factor
29 "hello world" """ eval( -- string )
34 "IN: parser.tests : hello ( -- str ) \"hello world\" ;"
35 eval( -- ) "USE: parser.tests hello" eval( -- string )
38 [ "IN: parser.tests : \" ( -- n ) 123 ;" eval( -- ) ]
39 [ error>> invalid-word-name? ] must-fail-with
40 [ "IN: parser.tests : \"asdf ( -- n ) 123 ;" eval( -- ) ]
41 [ error>> invalid-word-name? ] must-fail-with
42 [ "IN: parser.tests : 123 ( -- n ) 123 ;" eval( -- ) ]
43 [ error>> invalid-word-name? ] must-fail-with
46 [ "! This is a comment, people." eval( -- ) ]
52 [ "\"\\u000020\"" eval( -- string ) ]
56 [ "\"\\u000027\"" eval( -- string ) ]
59 ! Test EOL comments in multiline strings.
60 { "Hello" } [ "#! This calls until-eol.\n\"Hello\"" eval( -- string ) ] unit-test
62 { word } [ \ f class-of ] unit-test
64 ! Test stack effect parsing
66 : effect-parsing-test ( a b -- c ) + ;
69 "effect-parsing-test" "parser.tests" lookup-word
70 \ effect-parsing-test eq?
73 { T{ effect f { "a" "b" } { "c" } f } }
74 [ \ effect-parsing-test "declared-effect" word-prop ] unit-test
76 : baz ( a b -- * ) 2array throw ;
79 [ \ baz "declared-effect" word-prop terminated?>> ]
82 { } [ "IN: parser.tests USE: math : effect-parsing-test ( a b -- d ) - ;" eval( -- ) ] unit-test
85 "effect-parsing-test" "parser.tests" lookup-word
86 \ effect-parsing-test eq?
89 { T{ effect f { "a" "b" } { "d" } f } }
90 [ \ effect-parsing-test "declared-effect" word-prop ] unit-test
92 [ "IN: parser.tests : missing-- ( a b ) ;" eval( -- ) ] must-fail
95 { 2 } [ "IN: parser.tests : \0. ( -- x ) 2 ; \0." eval( -- n ) ] unit-test
99 "IN: parser.tests USING: math prettyprint ; SYNTAX: foo 2 2 + . ;" eval( -- )
101 { } [ "USE: parser.tests foo" eval( -- ) ] unit-test
103 "IN: parser.tests USING: math prettyprint ; : foo ( -- ) 2 2 + . ;" eval( -- )
106 "USE: parser.tests \\ foo" eval( -- word )
107 "foo" "parser.tests" lookup-word eq?
110 ! parse-tokens should do the right thing on EOF
111 [ "USING: kernel" eval( -- ) ]
112 [ error>> T{ unexpected { want "token" } } = ] must-fail-with
117 "IN: parser.tests : smudge-me ( -- ) ;" <string-reader> "foo"
120 "foo" source-file definitions>> first cardinality
123 { t } [ "smudge-me" "parser.tests" lookup-word >boolean ] unit-test
126 "IN: parser.tests : smudge-me-more ( -- ) ;" <string-reader> "foo"
130 { t } [ "smudge-me-more" "parser.tests" lookup-word >boolean ] unit-test
131 { f } [ "smudge-me" "parser.tests" lookup-word >boolean ] unit-test
134 "IN: parser.tests USING: math strings ; GENERIC: smudge-me ( a -- b ) M: integer smudge-me ; M: string smudge-me ;" <string-reader> "foo"
137 "foo" source-file definitions>> first cardinality
141 "IN: parser.tests USING: arrays ; M: array smudge-me ;" <string-reader> "bar"
144 "bar" source-file definitions>> first cardinality
148 "IN: parser.tests USING: math strings ; GENERIC: smudge-me ( a -- b ) M: integer smudge-me ;" <string-reader> "foo"
151 "foo" source-file definitions>> first cardinality
155 array "smudge-me" "parser.tests" lookup-word order member-eq?
159 integer "smudge-me" "parser.tests" lookup-word order member-eq?
163 string "smudge-me" "parser.tests" lookup-word order member-eq?
167 "IN: parser.tests USE: math 2 2 +" <string-reader> "a"
172 "a" <pathname> \ + usage member?
176 "IN: parser.tests USE: math 2 2 -" <string-reader> "a"
181 "a" <pathname> \ + usage member?
185 "a" source-files get delete-at
187 "IN: parser.tests DEFER: x : y ( -- ) x ; : x ( -- ) y ;"
188 <string-reader> "a" parse-stream drop
192 "a" source-files get delete-at
195 "IN: parser.tests : x ( -- ) ; : y ( -- * ) 3 throw ; this is an error"
196 <string-reader> "a" parse-stream
197 ] [ source-file-error? ] must-fail-with
200 "y" "parser.tests" lookup-word >boolean
204 "IN: parser.tests : x ( -- ) ;"
205 <string-reader> "a" parse-stream drop
206 "y" "parser.tests" lookup-word
209 ! Test new forward definition logic
211 "IN: axx : axx ( -- ) ;"
212 <string-reader> "axx" parse-stream drop
216 "USE: axx IN: bxx : bxx ( -- ) ; : cxx ( -- ) axx bxx ;"
217 <string-reader> "bxx" parse-stream drop
220 ! So we move the bxx word to axx...
222 "IN: axx : axx ( -- ) ; : bxx ( -- ) ;"
223 <string-reader> "axx" parse-stream drop
226 { t } [ "bxx" "axx" lookup-word >boolean ] unit-test
228 ! And reload the file that uses it...
230 "USE: axx IN: bxx ( -- ) : cxx ( -- ) axx bxx ;"
231 <string-reader> "bxx" parse-stream drop
234 ! And hope not to get a forward-error!
236 ! Turning a generic into a non-generic could cause all
239 "IN: ayy USE: kernel GENERIC: ayy ( a -- b ) M: object ayy ;"
240 <string-reader> "ayy" parse-stream drop
244 "IN: ayy USE: kernel : ayy ( -- ) ;"
245 <string-reader> "ayy" parse-stream drop
249 "IN: azz TUPLE: my-class ; GENERIC: a-generic ( a -- b )"
250 <string-reader> "azz" parse-stream drop
254 "USE: azz M: my-class a-generic ;"
255 <string-reader> "azz-2" parse-stream drop
259 "IN: azz GENERIC: a-generic ( a -- b )"
260 <string-reader> "azz" parse-stream drop
264 "USE: azz USE: math M: integer a-generic ;"
265 <string-reader> "azz-2" parse-stream drop
269 "IN: parser.tests : <bogus-error> ( -- ) ; : bogus ( -- error ) <bogus-error> ;"
270 <string-reader> "bogus-error" parse-stream drop
274 "IN: parser.tests TUPLE: bogus-error ; C: <bogus-error> bogus-error : bogus ( -- error ) <bogus-error> ;"
275 <string-reader> "bogus-error" parse-stream drop
278 ! Problems with class predicates -vs- ordinary words
280 "IN: parser.tests TUPLE: killer ;"
281 <string-reader> "removing-the-predicate" parse-stream drop
285 "IN: parser.tests GENERIC: killer? ( a -- b )"
286 <string-reader> "removing-the-predicate" parse-stream drop
290 "killer?" "parser.tests" lookup-word >boolean
294 "IN: parser.tests TUPLE: another-pred-test ; GENERIC: another-pred-test? ( a -- b )"
295 <string-reader> "removing-the-predicate" parse-stream
296 ] [ error>> error>> error>> redefine-error? ] must-fail-with
299 "IN: parser.tests TUPLE: class-redef-test ; TUPLE: class-redef-test ;"
300 <string-reader> "redefining-a-class-1" parse-stream
301 ] [ error>> error>> error>> redefine-error? ] must-fail-with
304 "IN: parser.tests TUPLE: class-redef-test ; SYMBOL: class-redef-test"
305 <string-reader> "redefining-a-class-2" parse-stream drop
309 "IN: parser.tests TUPLE: class-redef-test ; SYMBOL: class-redef-test : class-redef-test ( -- ) ;"
310 <string-reader> "redefining-a-class-3" parse-stream drop
311 ] [ error>> error>> error>> redefine-error? ] must-fail-with
314 "IN: parser.tests TUPLE: class-fwd-test ;"
315 <string-reader> "redefining-a-class-3" parse-stream drop
319 "IN: parser.tests \\ class-fwd-test"
320 <string-reader> "redefining-a-class-3" parse-stream drop
321 ] [ error>> error>> error>> no-word-error? ] must-fail-with
324 "IN: parser.tests TUPLE: class-fwd-test ; SYMBOL: class-fwd-test"
325 <string-reader> "redefining-a-class-3" parse-stream drop
329 "IN: parser.tests \\ class-fwd-test"
330 <string-reader> "redefining-a-class-3" parse-stream drop
331 ] [ error>> error>> error>> no-word-error? ] must-fail-with
334 "IN: parser.tests : foo ( -- ) ; TUPLE: foo ;"
335 <string-reader> "redefining-a-class-4" parse-stream drop
336 ] [ error>> error>> error>> redefine-error? ] must-fail-with
339 "IN: parser.tests : foo ( x y -- z ) 1 2 ; : bar ( a -- b ) ;" eval( -- )
343 "IN: parser.tests : foo ( x y -- z) 1 2 ; : bar ( a -- b ) ;" eval( -- )
347 "IN: parser.tests USE: kernel PREDICATE: foo < object ;" eval( -- )
351 "foo" "parser.tests" lookup-word last-word eq?
356 "redefining-a-class-5" forget-source
357 "redefining-a-class-6" forget-source
358 "redefining-a-class-7" forget-source
359 ] with-compilation-unit
364 "IN: parser.tests TUPLE: foo ; GENERIC: foo ( a -- b )"
365 <string-reader> "redefining-a-class-5" parse-stream drop
369 "IN: parser.tests M: f foo ;"
370 <string-reader> "redefining-a-class-6" parse-stream drop
373 [ f ] [ f "foo" "parser.tests" lookup-word execute ] unit-test
376 "IN: parser.tests TUPLE: foo ; GENERIC: foo ( a -- b )"
377 <string-reader> "redefining-a-class-5" parse-stream drop
380 [ f ] [ f "foo" "parser.tests" lookup-word execute ] unit-test
383 "IN: parser.tests TUPLE: foo ; GENERIC: foo ( a -- b )"
384 <string-reader> "redefining-a-class-7" parse-stream drop
387 [ f ] [ f "foo" "parser.tests" lookup-word execute ] unit-test
390 "IN: parser.tests TUPLE: foo ;"
391 <string-reader> "redefining-a-class-7" parse-stream drop
394 [ t ] [ "foo" "parser.tests" lookup-word symbol? ] unit-test
397 [ "vocab:parser/test/assert-depth.factor" run-file ] must-fail
401 "IN: parser.tests DEFER: d-f-s d-f-s SYMBOL: d-f-s d-f-s"
402 <string-reader> "d-f-s-test" parse-stream drop
406 "IN: parser.tests DEFER: d-f-s d-f-s FORGET: d-f-s SYMBOL: d-f-s d-f-s"
407 <string-reader> "d-f-s-test" parse-stream drop
411 "IN: parser.tests DEFER: d-f-s d-f-s SYMBOL: d-f-s d-f-s"
412 <string-reader> "d-f-s-test" parse-stream drop
417 [ "this-better-not-exist" forget-vocab ] with-compilation-unit
421 "USE: this-better-not-exist" eval( -- )
424 [ ": foo ;" eval( -- ) ] [ error>> error>> no-current-vocab-error? ] must-fail-with
426 { 92 } [ "CHAR: \\" eval( -- n ) ] unit-test
427 { 92 } [ "CHAR: \\\\" eval( -- n ) ] unit-test
432 "USING: math arrays kernel ;"
433 "GENERIC: change-combination ( obj a -- b )"
434 "M: integer change-combination 2drop 1 ;"
435 "M: array change-combination 2drop 2 ;"
436 } "\n" join <string-reader> "change-combination-test" parse-stream drop
442 "USING: math arrays kernel ;"
443 "GENERIC# change-combination 1 ( obj a -- b )"
444 "M: integer change-combination 2drop 1 ;"
445 "M: array change-combination 2drop 2 ;"
446 } "\n" join <string-reader> "change-combination-test" parse-stream drop
450 "change-combination" "parser.tests" lookup-word
451 "methods" word-prop assoc-size
456 "IN: parser.tests DEFER: twice-fails FORGET: twice-fails MIXIN: twice-fails"
457 <string-reader> "twice-fails-test" parse-stream drop
462 "IN: parser.tests : staging-problem-test-1 ( -- a ) 1 ; : staging-problem-test-2 ( -- a ) staging-problem-test-1 ;"
463 <string-reader> "staging-problem-test" parse-stream
466 { t } [ "staging-problem-test-1" "parser.tests" lookup-word >boolean ] unit-test
468 { t } [ "staging-problem-test-2" "parser.tests" lookup-word >boolean ] unit-test
471 "IN: parser.tests << : staging-problem-test-1 ( -- a ) 1 ; >> : staging-problem-test-2 ( -- a ) staging-problem-test-1 ;"
472 <string-reader> "staging-problem-test" parse-stream
475 { t } [ "staging-problem-test-1" "parser.tests" lookup-word >boolean ] unit-test
477 { t } [ "staging-problem-test-2" "parser.tests" lookup-word >boolean ] unit-test
479 [ "DEFER: blahy" eval( -- ) ] [ error>> error>> no-current-vocab-error? ] must-fail-with
482 "IN: parser.tests SYNTAX: blahy ; FORGET: blahy" eval( -- )
484 error>> staging-violation?
487 ! Bogus error message
490 [ "IN: parser.tests USE: kernel TUPLE: blahy < tuple ; : blahy ( -- ) ; TUPLE: blahy < tuple ; : blahy ( -- ) ;" eval( -- ) ]
491 [ error>> error>> def>> \ blahy eq? ] must-fail-with
493 [ "CHAR: \\u9999999999999" eval( -- n ) ] must-fail
497 { a } [ a ] unit-test
498 { b } [ b ] unit-test
499 { c } [ c ] unit-test
503 { } [ "IN: parser.tests GENERIC: blah ( -- )" eval( -- ) ] unit-test
504 { } [ "IN: parser.tests SYMBOLS: blah ;" eval( -- ) ] unit-test
506 { f } [ \ blah generic? ] unit-test
507 { t } [ \ blah symbol? ] unit-test
511 [ "IN: parser.tests SINGLETONS: blah1 blah1 blah1 ;" eval( -- ) ]
512 [ error>> error>> def>> \ blah1 eq? ]
515 IN: qualified.tests.foo
518 IN: qualified.tests.bar
521 IN: qualified.tests.baz
524 QUALIFIED: qualified.tests.foo
525 QUALIFIED: qualified.tests.bar
526 { 1 2 3 } [ qualified.tests.foo:x qualified.tests.bar:x x ] unit-test
528 QUALIFIED-WITH: qualified.tests.bar p
529 { 2 } [ p:x ] unit-test
531 RENAME: x qualified.tests.baz => y
532 { 3 } [ y ] unit-test
534 FROM: qualified.tests.baz => x ;
535 { 3 } [ x ] unit-test
536 { 3 } [ y ] unit-test
538 EXCLUDE: qualified.tests.bar => x ;
539 { 3 } [ x ] unit-test
540 { 4 } [ y ] unit-test
544 ! Replace : def with something in << >>
546 "IN: parser.tests : was-once-a-word-bug ( -- ) ;"
547 <string-reader> "was-once-a-word-test" parse-stream
550 { t } [ "was-once-a-word-bug" "parser.tests" lookup-word >boolean ] unit-test
553 "IN: parser.tests USE: words << \"was-once-a-word-bug\" \"parser.tests\" create-word [ ] ( -- ) define-declared >>"
554 <string-reader> "was-once-a-word-test" parse-stream
557 { t } [ "was-once-a-word-bug" "parser.tests" lookup-word >boolean ] unit-test */
559 ! Replace : def with DEFER:
561 "IN: parser.tests : is-not-deferred ( -- ) ;"
562 <string-reader> "is-not-deferred" parse-stream
565 { t } [ "is-not-deferred" "parser.tests" lookup-word >boolean ] unit-test
566 { f } [ "is-not-deferred" "parser.tests" lookup-word deferred? ] unit-test
569 "IN: parser.tests DEFER: is-not-deferred"
570 <string-reader> "is-not-deferred" parse-stream
573 { t } [ "is-not-deferred" "parser.tests" lookup-word >boolean ] unit-test
574 { t } [ "is-not-deferred" "parser.tests" lookup-word deferred? ] unit-test
576 ! Forward-reference resolution case iterated using list in the wrong direction
578 "IN: parser.tests.forward-ref-1 DEFER: x DEFER: y"
579 <string-reader> "forward-ref-1" parse-stream
583 "IN: parser.tests.forward-ref-2 DEFER: x DEFER: y"
584 <string-reader> "forward-ref-2" parse-stream
588 "IN: parser.tests.forward-ref-3 FROM: parser.tests.forward-ref-1 => x y ; FROM: parser.tests.forward-ref-2 => x y ; : z ( -- ) x y ;"
589 <string-reader> "forward-ref-3" parse-stream
593 "z" "parser.tests.forward-ref-3" lookup-word def>> [ vocabulary>> ] map all-equal?
597 "FROM: parser.tests.forward-ref-1 => x y ; FROM: parser.tests.forward-ref-2 => x y ; IN: parser.tests.forward-ref-3 : x ( -- ) ; : z ( -- ) x y ;"
598 <string-reader> "forward-ref-3" parse-stream
602 "z" "parser.tests.forward-ref-3" lookup-word def>> [ vocabulary>> ] map all-equal?
606 "IN: parser.tests.forward-ref-3 FROM: parser.tests.forward-ref-1 => x y ; FROM: parser.tests.forward-ref-2 => x y ; : z ( -- ) x y ;"
607 <string-reader> "forward-ref-3" parse-stream
611 "z" "parser.tests.forward-ref-3" lookup-word def>> [ vocabulary>> ] map all-equal?
615 "USE: kernel dup" <string-reader> "unuse-test" parse-stream
619 "dup" <string-reader> "unuse-test" parse-stream
620 ] [ error>> error>> error>> no-word-error? ] must-fail-with
623 "USE: kernel UNUSE: kernel dup" <string-reader> "unuse-test" parse-stream
624 ] [ error>> error>> error>> no-word-error? ] must-fail-with
626 { } [ [ "vocabs.loader.test.l" forget-vocab ] with-compilation-unit ] unit-test
629 [ "vocabs.loader.test.l" use-vocab ] must-fail
630 [ f ] [ "vocabs.loader.test.l" manifest get search-vocab-names>> in? ] unit-test
631 [ ] [ "vocabs.loader.test.l" unuse-vocab ] unit-test
632 [ f ] [ "vocabs.loader.test.l" manifest get search-vocab-names>> in? ] unit-test
635 ! Test cases for #183
636 [ "SINGLETON: 33" <string-reader> "class identifier test" parse-stream ]
637 [ error>> lexer-error? ] must-fail-with
639 [ ": 44 ( -- ) ;" <string-reader> "word identifier test" parse-stream ]
640 [ error>> lexer-error? ] must-fail-with
642 [ "GENERIC: 33 ( -- )" <string-reader> "generic identifier test" parse-stream ]
643 [ error>> lexer-error? ] must-fail-with
647 { private? } use-first-word?