factor/extra/constructors/constructors-tests.factor

62 lines
1.3 KiB
Factor

! Copyright (C) 2009 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license.
USING: accessors calendar combinators.short-circuit
constructors eval initializers kernel math tools.test ;
IN: constructors.tests
TUPLE: stock-spread stock spread timestamp ;
CONSTRUCTOR: stock-spread ( stock spread -- stock-spread )
now >>timestamp ;
SYMBOL: AAPL
[ t ] [
AAPL 1234 <stock-spread>
{
[ stock>> AAPL eq? ]
[ spread>> 1234 = ]
[ timestamp>> timestamp? ]
} 1&&
] unit-test
TUPLE: ct1 a ;
TUPLE: ct2 < ct1 b ;
TUPLE: ct3 < ct2 c ;
TUPLE: ct4 < ct3 d ;
CONSTRUCTOR: ct1 ( a -- obj )
[ 1 + ] change-a ;
CONSTRUCTOR: ct2 ( a b -- obj )
[ 1 + ] change-a ;
CONSTRUCTOR: ct3 ( a b c -- obj )
[ 1 + ] change-a ;
CONSTRUCTOR: ct4 ( a b c d -- obj )
[ 1 + ] change-a ;
[ 1001 ] [ 1000 <ct1> a>> ] unit-test
[ 2 ] [ 0 0 <ct2> a>> ] unit-test
[ 3 ] [ 0 0 0 <ct3> a>> ] unit-test
[ 4 ] [ 0 0 0 0 <ct4> a>> ] unit-test
[
"""USE: constructors
IN: constructors.tests
TUPLE: foo a b ;
CONSTRUCTOR: foo ( a a -- obj ) ;""" eval( -- )
] [
error>> repeated-constructor-parameters?
] must-fail-with
[
"""USE: constructors
IN: constructors.tests
TUPLE: foo a b ;
CONSTRUCTOR: foo ( a c -- obj ) ;""" eval( -- )
] [
error>> unknown-constructor-parameters?
] must-fail-with