1 IN: stack-checker.transforms.tests
2 USING: sequences stack-checker.transforms tools.test math kernel
3 quotations stack-checker stack-checker.errors accessors
4 combinators words arrays classes classes.tuple macros ;
6 MACRO: compose-n ( n word -- quot' ) <repetition> >quotation ;
8 : compose-n-test ( a b c -- x ) 2 \ + compose-n ;
10 { 6 } [ 1 2 3 compose-n-test ] unit-test
16 : cleave-test ( color -- r g b )
17 { [ r>> ] [ g>> ] [ b>> ] } cleave ;
19 { 1 3 } [ cleave-test ] must-infer-as
21 { 1 2 3 } [ 1 2 3 <color> cleave-test ] unit-test
23 { 1 2 3 } [ 1 2 3 <color> \ cleave-test def>> call ] unit-test
25 : 2cleave-test ( a b -- c d e ) { [ 2array ] [ + ] [ - ] } 2cleave ;
27 { { 1 2 } 3 -1 } [ 1 2 2cleave-test ] unit-test
29 { { 1 2 } 3 -1 } [ 1 2 \ 2cleave-test def>> call ] unit-test
31 : spread-test ( a b c -- d e f ) { [ sq ] [ neg ] [ recip ] } spread ;
33 { 16 -3 1/6 } [ 4 3 6 spread-test ] unit-test
35 { 16 -3 1/6 } [ 4 3 6 \ spread-test def>> call ] unit-test
37 [ fixnum instance? ] must-infer
39 : bad-new-test ( -- obj ) V{ } new ;
41 [ bad-new-test ] must-infer
43 [ bad-new-test ] must-fail
45 ! Corner case if macro expansion calls 'infer', found by Doug
48 \ smart-combo [ infer [ ] curry ] 1 define-transform
50 [ [ "a" "b" "c" ] smart-combo ] must-infer
52 [ [ [ "a" "b" ] smart-combo "c" ] smart-combo ] must-infer
54 : very-smart-combo ( quot -- ) smart-combo ; inline
56 [ [ "a" "b" "c" ] very-smart-combo ] must-infer
58 [ [ [ "a" "b" ] very-smart-combo "c" ] very-smart-combo ] must-infer
60 ! Caveat found by Doug
61 MACRO: curry-folding-test ( quot -- )
62 length \ drop <repetition> >quotation ;
64 { 3 0 } [ [ 1 2 3 ] curry-folding-test ] must-infer-as
65 { 3 0 } [ 1 [ 2 3 ] curry curry-folding-test ] must-infer-as
66 { 3 0 } [ [ 1 2 ] 3 [ ] curry compose curry-folding-test ] must-infer-as
68 [ [ curry curry-folding-test ] infer ]
69 [ T{ unknown-macro-input f curry-folding-test } = ] must-fail-with
71 : member?-test ( a -- ? ) { 1 2 3 10 7 58 } member? ;
73 { f } [ 1.0 member?-test ] unit-test
74 { t } [ \ member?-test def>> first [ member?-test ] all? ] unit-test
76 ! Macro expansion should throw its own type of error
79 \ bad-macro [ "OOPS" throw ] 0 define-transform
81 [ [ bad-macro ] infer ] [ [ transform-expansion-error? ] [ error>> "OOPS" = ] [ word>> \ bad-macro = ] tri and and ] must-fail-with
83 MACRO: two-params ( a b -- c ) + 1quotation ;
85 [ [ 3 two-params ] infer ] [ T{ unknown-macro-input f two-params } = ] must-fail-with