]> gitweb.factorcode.org Git - factor.git/blob - extra/python/syntax/syntax-tests.factor
core: Add words/unwords/unwords-as and use them.
[factor.git] / extra / python / syntax / syntax-tests.factor
1 USING: accessors arrays assocs continuations destructors destructors.private
2 fry io.files.temp kernel math namespaces python python.ffi
3 python.modules.__builtin__ python.modules.argparse python.modules.datetime
4 python.modules.os python.modules.os.path python.modules.sys
5 python.modules.time python.objects python.syntax sets splitting tools.test
6 unicode ;
7 QUALIFIED-WITH: sequences s
8 IN: python.syntax.tests
9
10 : py-test ( result quot -- )
11     '[ python-dll-loaded? [ _ [ _ with-destructors ] unit-test ] when ] call ; inline
12
13 : long-py-test ( result quot -- )
14     '[ python-dll-loaded? [ _ [ _ with-destructors ] long-unit-test ] when ] call ; inline
15
16 { t } [ getpid py> integer? ] py-test
17
18 ! Automatic tuple unpacking
19 [ "hello.doc" ] [ "/some/path/hello.doc" >py basename py> ] py-test
20
21 [ { "hello" ".doc" } ] [
22     "hello.doc" >py splitext 2array [ py> ] s:map
23 ] py-test
24
25 [ ] [ 0 >py sleep ] py-test
26
27 ! Module variables are bound as zero-arg functions
28 { t } [ $path py> s:sequence? ] py-test
29
30 { t } [ $path len int py> 5 > ] py-test
31
32 [ 10 ] [ 10 >py range len py> ] py-test
33
34 ! Callables
35 { t } [
36     "os" py-import "getpid" getattr
37     [ callable ] [ PyCallable_Check 1 = ] bi and
38 ] py-test
39
40 ! Reference counting
41 { 1 } [ 3 <py-tuple> getrefcount py> ] py-test
42
43 { -1 } [
44     H{ { "foo" 33 } { "bar" 44 } } >py
45     [ "foo" py-dict-get-item-string getrefcount py> ]
46     [
47         '[
48             500 [ _ "foo" py-dict-get-item-string drop ] times
49         ] with-destructors
50     ]
51     [ "foo" py-dict-get-item-string getrefcount py> ] tri -
52 ] py-test
53
54 { -1 } [
55     "abcd" >py <1py-tuple>
56     [ 0 py-tuple-get-item getrefcount py> ]
57     [
58         [ 100 [ swap 0 py-tuple-get-item drop ] with times ] with-destructors
59     ]
60     [ 0 py-tuple-get-item getrefcount py> ] tri -
61 ] py-test
62
63 { t } [
64     6 <py-tuple>
65     [ getrefcount py> 1 - ]
66     [ always-destructors get [ alien>> = ] with s:count ] bi =
67 ] py-test
68
69 { t } [
70     "python-file" temp-file >py "wb" >py open
71     [ tell ] [ fileno ] [ close ] tri
72     [ py> integer? ] both?
73 ] py-test
74
75 ! Method chaining
76 { t } [
77     "hello there" >py title 20 >py zfill "00" >py startswith py>
78 ] py-test
79
80 [ { "hello" "=" "there" } ] [
81     "hello=there" >py "=" >py partition 3array [ py> ] s:map
82 ] py-test
83
84 ! Introspection
85 PY-METHODS: func =>
86     func_code ( func -- code ) ;
87
88 PY-METHODS: code =>
89     co_argcount ( code -- n ) ;
90
91 { 1 } [ $splitext $func_code $co_argcount py> ] py-test
92
93 ! Change sys.path
94 { t } [
95     $path "test" >py [ append ] [ drop py> ] [ remove ] 2tri
96     "test" swap in?
97 ] py-test
98
99 ! Support for kwargs
100 [ "datetime.timedelta(4, 10800)" ] [
101     H{ { "hours" 99 } } >py timedelta repr py>
102 ] py-test
103
104 ! Kwargs in methods
105 { t } [
106     [
107         ArgumentParser dup
108         "--foo" >py H{ { "help" "badger" } } >py add_argument
109         format_help py>
110     ] with-destructors [ blank? ] s:trim words "badger" swap in?
111 ] py-test
112
113 { t } [
114     [ 987 >py basename ] [ traceback>> ] recover s:length 0 >
115 ] py-test
116
117 ! Test if exceptions leak references. If so, the test will leak a few
118 ! hundred megs of memory. Enough to be noticed but not to slow down
119 ! the tests too much.
120 { } [
121     100000 [
122         [ [ 987 >py basename drop ] ignore-errors ] with-destructors
123     ] times
124 ] long-py-test
125
126 ! Another leaky test
127 { } [
128     1000000 [
129         [ { 9 8 7 6 5 4 3 2 1 } >py ] with-destructors drop
130     ] times
131 ] long-py-test
132
133 ! Working with types
134 PY-QUALIFIED-FROM: types => UnicodeType ( -- ) ;
135
136 { "unicode" } [
137     types:$UnicodeType $__name__ py>
138 ] py-test
139
140 ! Make callbacks
141 PY-QUALIFIED-FROM: __builtin__ =>
142     None ( -- )
143     map ( func seq -- seq' )
144     reduce ( func seq -- seq' ) ;
145
146 { V{ 1 2 3 } } [
147     __builtin__:$None { 1 2 3 } >py __builtin__:map py>
148 ] py-test
149
150 : double-fun ( -- alien )
151     [ drop s:first 2 * ] quot>py-callback ;
152
153 { V{ 2 4 16 2 4 68 } } [
154     double-fun [
155         { 1 2 8 1 2 34 } >py __builtin__:map py>
156     ] with-quot>py-cfunction
157 ] py-test
158
159 : reduce-func ( -- alien )
160     [ drop s:first2 + ] quot>py-callback ;
161
162 { 48 } [
163     reduce-func [
164         { 1 2 8 1 2 34 } >py __builtin__:reduce py>
165     ] with-quot>py-cfunction
166 ] py-test