]> gitweb.factorcode.org Git - factor.git/blob - basis/cocoa/cocoa-tests.factor
cocoa.subclassing: new METHOD: syntax cleans up class definitions
[factor.git] / basis / cocoa / cocoa-tests.factor
1 USING: cocoa cocoa.messages cocoa.subclassing cocoa.types
2 compiler.test kernel namespaces cocoa.classes cocoa.runtime
3 tools.test memory compiler.units math core-graphics.types ;
4 FROM: alien.c-types => int void ;
5 IN: cocoa.tests
6
7 CLASS: {
8     { +superclass+ "NSObject" }
9     { +name+ "Foo" }
10 }
11
12 METHOD: void foo: NSRect rect [
13     gc rect "x" set
14 ] ;
15
16 : test-foo ( -- )
17     Foo -> alloc -> init
18     dup 1.0 2.0 101.0 102.0 <CGRect> -> foo:
19     -> release ;
20
21 test-foo
22
23 [ 1.0 ] [ "x" get CGRect-x ] unit-test
24 [ 2.0 ] [ "x" get CGRect-y ] unit-test
25 [ 101.0 ] [ "x" get CGRect-w ] unit-test
26 [ 102.0 ] [ "x" get CGRect-h ] unit-test
27
28 CLASS: {
29     { +superclass+ "NSObject" }
30     { +name+ "Bar" }
31 }
32
33 METHOD: NSRect bar [ test-foo "x" get ] ;
34
35 Bar [
36     -> alloc -> init
37     dup -> bar "x" set
38     -> release
39 ] compile-call
40
41 [ 1.0 ] [ "x" get CGRect-x ] unit-test
42 [ 2.0 ] [ "x" get CGRect-y ] unit-test
43 [ 101.0 ] [ "x" get CGRect-w ] unit-test
44 [ 102.0 ] [ "x" get CGRect-h ] unit-test
45
46 ! Make sure that we can add methods
47 CLASS: {
48     { +superclass+ "NSObject" }
49     { +name+ "Bar" }
50 }
51
52 METHOD: NSRect bar [ test-foo "x" get ]
53
54 METHOD: int babb: int x [ x sq ] ;
55
56 [ 144 ] [
57     Bar [
58         -> alloc -> init
59         dup 12 -> babb:
60         swap -> release
61     ] compile-call
62 ] unit-test