]> gitweb.factorcode.org Git - factor.git/blob - extra/python/python-tests.factor
build: add a make clean target, delete overlooked factor binary in make clean
[factor.git] / extra / python / python-tests.factor
1 USING: accessors alien arrays assocs calendar continuations destructors
2 destructors.private fry kernel math memory namespaces python python.errors
3 python.ffi python.objects sequences strings tools.test ;
4 IN: python
5
6 : py-test ( result quot -- )
7     '[ python-dll-loaded? [ _ [ _ with-destructors ] unit-test ] when ] call ; inline
8
9 ! None testing
10 { t } [
11     "builtins" "None" py-import-from <none> =
12 ] py-test
13
14 ! Destructors
15 { 1 } [ 33 >py drop always-destructors get length ] py-test
16
17 { 1 } [ f >py drop always-destructors get length ] py-test
18
19 ! The tuple steals the reference properly now.
20 { 1 } [ 33 >py <1py-tuple> drop always-destructors get length ] py-test
21
22 { 1 } [ { } >py drop always-destructors get length ] py-test
23
24 { 1 } [ V{ 1 2 3 4 } >py drop always-destructors get length ] py-test
25
26 { 2 } [
27     99 >py V{ 1 2 3 4 } >py 2drop always-destructors get length
28 ] py-test
29
30 { 1 } [
31     { { { 33 } } } >py drop always-destructors get length
32 ] py-test
33
34 { } [ 123 <alien> unsteal-ref ] unit-test
35
36 { t } [ Py_GetVersion string? ] py-test
37
38 [ "os" ] [ "os" py-import PyModule_GetName ] py-test
39
40 { t } [
41     "os" py-import "getpid" getattr
42     { } >py call-object py> 0 >
43 ] py-test
44
45 { t } [ Py_IsInitialized ] py-test
46
47 ! py-importing
48 [ { "ModuleNotFoundError" "No module named 'kolobi'" f } ] [
49     [ "kolobi" py-import ] [
50         [ type>> ] [ message>> ] [ traceback>> ] tri 3array
51     ] recover
52 ] py-test
53
54 ! setattr
55 { 73 } [
56     "sys" py-import "testit" [ 73 >py setattr ] [ getattr py> ] 2bi
57 ] py-test
58
59 ! Tuples
60 { 2 } [ 2 <py-tuple> py-tuple-size ] py-test
61
62 : py-datepy> ( py-obj -- timestamp )
63     { "year" "month" "day" } [ getattr py> ] with map
64     first3 0 0 0 instant <timestamp> ;
65
66 ! Lists
67 { t } [ V{ 4 8 15 16 23 42 } dup >py py> = ] py-test
68
69 ! ! Datetimes
70 { t } [
71     [ py-datepy> ] "date" py-type-dispatch get set-at
72     "datetime" py-import "date" getattr "today" getattr
73     { } >py call-object py>
74     today instant >>gmt-offset =
75 ] py-test
76
77 ! Unicode
78 { "غثههح" } [
79     "os.path" py-import "basename" getattr { "غثههح" } >py call-object py>
80 ] py-test
81
82 ! Instance variables
83 { 7 } [
84     "datetime" py-import "timedelta" getattr
85     { 7 } >py call-object "days" getattr py>
86 ] py-test
87
88 ! Create a dictonary
89 { 0 } [ <py-dict> py-dict-size ] py-test
90
91 ! Dictionary with object keys
92 { 1 } [
93     <py-dict> dup 0 >py 33 >py py-dict-set-item py-dict-size
94 ] py-test
95
96 ! Dictionary with string keys
97 { 1 } [
98     <py-dict> [ "foo" 33 >py py-dict-set-item-string ] [ py-dict-size ] bi
99 ] py-test
100
101 ! Get dictionary items
102 { 33 } [
103     <py-dict> "tjaba"
104     [ 33 >py  py-dict-set-item-string ]
105     [ py-dict-get-item-string py> ] 2bi
106 ] py-test
107
108 ! Nest dicts
109 { 0 } [
110     <py-dict> "foo"
111     [ <py-dict> py-dict-set-item-string ]
112     [ py-dict-get-item-string ] 2bi
113     py-dict-size
114 ] py-test
115
116 ! Nested tuples
117 { 3 } [
118     1 <py-tuple> dup 0 3 <py-tuple> py-tuple-set-item
119     0 py-tuple-get-item py-tuple-size
120 ] py-test
121
122 ! Round tripping!
123 { { "foo" { 99 77 } } }
124 [ { "foo" { 99 77 } } >py py> ] py-test
125
126 { H{ { "foo" "bar" } { 3 4 } } } [
127     H{ { "foo" "bar" } { 3 4 } } >py py>
128 ] py-test
129
130 ! Kwargs
131 { 2014 10 22 } [
132     "datetime" py-import "date" getattr
133     { } >py H{ { "year" 2014 } { "month" 10 } { "day" 22 } } >py
134     call-object-full py>
135     [ year>> ] [ month>> ] [ day>> ] tri
136 ] py-test
137
138 ! Modules
139 { t } [
140     "os" py-import PyModule_GetDict dup Py_IncRef &Py_DecRef py-dict-size 100 >
141 ] py-test
142
143 ! CFunctions
144 { t } [
145     1234 <alien> "foo" f <PyMethodDef>
146     ml_flags>> METH_VARARGS METH_KEYWORDS bitor =
147 ] unit-test
148
149 { f 3 } [
150     1234 <alien> <py-cfunction>
151     [ "__doc__" getattr py> ] [ PyCFunction_GetFlags ] bi
152 ] py-test
153
154 { "cfunction" } [
155     1234 <alien> <py-cfunction>
156     ! Force nursery flush
157     10000 [ 1000 0xff <array> drop ] times
158     "__name__" getattr py>
159 ] py-test
160
161 { 3 } [
162     1234 <alien> <py-cfunction> drop always-destructors get length
163 ] py-test
164
165 ! Callbacks
166 : py-map ( -- alien )
167     "builtins" "map" py-import-from ;
168
169 : py-list ( -- alien )
170     "builtins" "list" py-import-from ;
171
172 : py-list-call ( alien -- seq )
173     py-list swap 1array array>py-tuple f call-object-full py> ;
174
175 : py-map-call ( alien-cb -- seq )
176     [
177         <py-cfunction> py-map swap { 1 2 } >py 2array array>py-tuple f
178         call-object-full
179     ] with-callback py-list-call ;
180
181 : always-33-fun ( -- alien )
182     [ 3drop 33 >py ] PyCallback ;
183
184 { V{ 33 33 } } [ always-33-fun py-map-call ] py-test
185
186 : id-fun ( -- alien )
187     [ drop nip py> first >py ] PyCallback ;
188
189 { V{ 1 2 } } [ id-fun py-map-call ] py-test