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