-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest-short-format.red
233 lines (204 loc) · 5.74 KB
/
test-short-format.red
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
Red []
do %format.red ; only needed for ordinal-suffix right now.
do %short-format.red
tests: context [
parse-test: function [
input [string!]
][
print "parse-as-short-format"
print [tab "INPUT: ^/^-^-" mold input]
res: parse-as-short-format input
print [tab "OUTPUT:"]
either object? res [print [tab tab trim/lines mold res]][
foreach item res [print [tab tab trim/lines mold item]]
]
]
parse-specs: [
""
":"
"::"
"^^:"
"^^:^^:"
"test"
":*.*d"
":20.10d"
"\t:*.*\n"
"\t:20.10d\n"
":+*.*"
":<*.*"
":>*.5"
":>"
":5"
":.5"
":0*.*"
": *.*"
":+<>0_*.*" ; multi flags
"0:*.*"
"Color :s, number1 :d, number2 :05, float :5.2.\n"
":/"
":/5"
"/a"
"/1"
"/num:20.10"
"/abc xyz"
"/abc:xyz"
"/a/b/c:xyz"
"/(code here)xyz"
":º"
":$"
":¤"
":>'fixed"
":'money"
"/num:+<>Z_5.2'general"
"/abc:'ordinal xyz"
"/abc:'hex:<5.2"
"/abc:'hex:xyz"
"/a/b/c:'binary/key-x"
"/(code here):'base-64 "
"/(1 + 1)"
":/(1 + 1)"
": /(1 + 1) :"
"Color: :<10, number1/ :3, http://:05, float: :<5.2"
]
run-parse-tests: does [
print ""
foreach spec parse-specs [parse-test dbg: spec]
]
;------------------------------------------------------
apply-test: function [
input [string!] "Spec as string to be parsed"
value
][
print "apply-test"
print [tab "INPUT: " mold input]
print [tab "VALUE: " trim/lines mold value]
res: short-form input value
print [tab "OUTPUT:" mold res]
]
apply-specs: compose/only [
"" 123.456
":" 123.456
"^^:^^:" 123.456
"^^:" 123.456
"test" 123.456
":*.*d" 123.456
":20.10d" 123.456
"\t:*.*\n" 123.456
"\t:20.10d\n" 123.456
":+*.*" 123.456
":<*.*" 123.456
":>*.2" 123.456
":<10" 123.456
":>10" 123.456
":10" 123.456
":/5" 123.456 ; produces "123.456123.456" This is the single-value multi-placeholder question
":.5" 123.456
":07.1" 123.456
":010.1" 123.456 ; This is confusing, with 0 as a flag
":00.1" 123.456 ; This is confusing, with 0 as a flag
":0007.1" 123.456 ; This is confusing, with 0 as a flag
":015.4" 123.456789 ; This is confusing, with 0 as a flag
":Z10.1" 123.456
":Z0.1" 123.456
":Z007.1" 123.456
":Z15.4" 123.456789
":_*.*" 123.456
":+<>0_*.*" 123.456
"0:*.*" 123.456
":10" 123.456%
":<10" 123.456%
":+10" 123.456%
":5.1" 123.456%
":5.2" 123.456%
":10.3" 123.456%
":10.4" -123.456%
":2.2" 1.2
":10.4 :8.2 :5.0" -123.456%
":8.2" -10.5
":Z8.2" -10.5
":<Z8.2" -10.5 ; produces "-10.5000" ; this matches printf's approach
":<Z8.2" -12345678.5
":<8.2" -10.5
":º" 1
":º" 2
":º" 3
":º" 4
":º" 15
":º" 123
":$" 123
":¤" 123
":/5" 123.456
":/pi" 123.456
":/a" 123.456
":/" 123.456
"::" 123.456
":/pi" 123.456
"/system/words/pi" 123.456
":/system/words/pi" 123.456
": /system/words/pi" 123.456
"/system/words/dot" 123.456
":/system/words/dot" 123.456
": /system/words/dot" 123.456
"/now/time" 123.456
"/(1 + 1)" 123.456
":/(1 + 1)" 123.456
": /(1 + 1) :" 123.456
"Color :<10, number1 :3, number2 :05, float :<5.2.\n" ["Red" 2 3 -45.6]
; colon-slash escapes
"Color: :<10, number1/ :3, http://:2, float: :<5.2" ["Red" 3 8080 -45.6]
; "Color _<10, number1 _3, number2 _05, float _<5.2.\n" ["Red" 2 3 -45.6]
; "Color =<10, number1 =3, number2 =05, float =<5.2.\n" ["Red" 2 3 -45.6]
; "Color &<10, number1 &3, number2 &05, float &<5.2.\n" ["Red" 2 3 -45.6]
; "Color @<10, number1 @3, number2 @05, float @<5.2.\n" ["Red" 2 3 -45.6]
; "Color !<10, number1 !3, number2 !05, float !<5.2.\n" ["Red" 2 3 -45.6]
; Composite syntax?
;"Color :(general):| idx3 :(/3 acct):| num2 :(/N2 :< general):| pi :(system/words/pi :< fixed):| :((1 + 1)): :(now/time):"
"Color :'general | idx3 /3:'money | num2 /N2:<'general | pi /system/words/pi:<'fixed | /(1 + 1) /now/time" [
"Red" n2 2 3 n4 -45.6
]
"Color :<5| idx3 /3:Z3| num2 /N2:<5| pi /system/words/pi:<5.2| /(1 + 1) /now/time" [
"Red" n2 2 3 n4 -45.6
]
;"Color Red | idx3 003| num2 2 | pi 3.14 | 2"
"Color :<5| idx3 /3:Z3| num2 /N2:<5| pi /system/words/pi:<5.2| /(1 + 1):z3 |/now/time/precise:10|/fn" (compose [
"Red" n2 2 3 n4 -45.6 fn (does [42])
])
; named fields in an block
"First: /first:<8| Last: /last:8| phone: /phoneX | /3" [
first: "Gregg" last: "Irwin" phone: #208.461.9999
]
; named paths in an block
"First: /name/first:<8| Last: /name/last:8| phone: /name/phoneX | /3" [
name: [first: "Gregg" last: "Irwin" phone: #208.461.9999]
]
; named fields in an object
"First: /first:<8| Last: /last:8| phone: /phoneX | /3" (context [
first: "Gregg" last: "Irwin" phone: #208.461.9999
])
; named paths in an object
"First: /name/first:<8| Last: /name/last:8| phone: /name/phoneX | /3" (context [
name: context [first: "Gregg" last: "Irwin" phone: #208.461.9999]
])
; named fields in a map
"First: /first:<8| Last: /last:8| phone: /phoneX | /3" #(
first: "gregg" last: "irwin" phone: #208.461.0000
)
; named paths in a map. Nicer escapes
"First: /name/first:<8| Last: /name/last:8| phone: /name/phoneX | /3" #(
name: #(first: "gregg" last: "irwin" phone: #208.461.0000)
)
]
run-apply-tests: does [
print ""
foreach [spec val] apply-specs [apply-test dbg: spec val]
]
]
test-apply: func [str val][
apply-short-format parse-as-short-format str val
]
tests/run-parse-tests
print '--------------------------------------
tests/run-apply-tests
print '--------------------------------------
test-apply ":5" 123.456
halt