-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathvalidate.test
More file actions
149 lines (144 loc) · 3.68 KB
/
validate.test
File metadata and controls
149 lines (144 loc) · 3.68 KB
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
if {[lsearch [namespace children] ::tcltest] == -1} {
package require tcltest
namespace import ::tcltest::test
}
::tcltest::loadTestedCommands
# Options
test validate-1.1 {basic usage, valid} -body { #<<<
parse_args::parse_args {-rating 1.2} {
-rating {-validate {string is double -strict}}
}
set rating
} -cleanup {
unset -nocomplain rating
} -result 1.2
#>>>
test validate-1.2 {basic usage, invalid} -body { #<<<
list [catch {
parse_args::parse_args {-rating x1.2} {
-rating {-validate {string is double -strict}}
}
} r o] $r [dict get $o -errorcode]
} -cleanup {
unset -nocomplain r o
} -result {1 {Validation failed for "-rating": 0} {PARSE_ARGS VALIDATION -rating}}
#>>>
test validate-1.3 {basic usage, blank} -body { #<<<
list [catch {
parse_args::parse_args {-rating ""} {
-rating {-validate {string is double -strict}}
}
} r o] $r [dict get $o -errorcode]
} -cleanup {
unset -nocomplain r o
} -result {1 {Validation failed for "-rating": 0} {PARSE_ARGS VALIDATION -rating}}
#>>>
test validate-1.4 {basic usage, missing} -body { #<<<
parse_args::parse_args {} {
-rating {-validate {string is double -strict}}
}
info exists rating
} -cleanup {
unset -nocomplain rating
} -result 0
#>>>
test validate-2.1 {validator throws error} -body { #<<<
parse_args::parse_args {-rating 0} {
-rating {-validate {tcl::mathop::+ 1}}
}
set rating
} -cleanup {
unset -nocomplain rating
} -result 0
#>>>
test validate-2.2 {validator throws error} -body { #<<<
set outcome [list [catch {
parse_args::parse_args {-rating ""} {
-rating {-validate {tcl::mathop::+ 1}}
}
} r o] \
[regexp {^Validation failed for "-rating": (cannot|can't) use (non-numeric|empty) string as (right )?operand of "\+"$} $r] \
[dict get $o -errorcode]]
set outcome
} -cleanup {
unset -nocomplain r o
} -result {1 1 {PARSE_ARGS VALIDATION -rating}} -match regexp
#>>>
test validate-3.1 {validator with apply, throws error} -body { #<<<
list [catch {
parse_args::parse_args {-rating ""} {
-rating {-validate {apply {
val {
if {[string length $val] < 3} {
error "Must be at least 3 characters long"
}
return 1
}
} }}
}
} r o] $r [dict get $o -errorcode]
} -cleanup {
unset -nocomplain r o
} -result {1 {Validation failed for "-rating": Must be at least 3 characters long} {PARSE_ARGS VALIDATION -rating}}
#>>>
test validate-3.2 {validator with apply, returns blank} -body { #<<<
parse_args::parse_args {-rating foo} {
-rating {-validate {apply {
val {
if {[string length $val] < 3} {
error "Must be at least 3 characters long"
}
return ""
}
} }}
}
set rating
} -cleanup {
unset -nocomplain rating
} -result foo
#>>>
test validate-3.3 {validator with apply, throws error} -body { #<<<
list [catch {
parse_args::parse_args {-rating ""} {
-rating {-validate {apply {
val {
if {[string length $val] < 3} {
return "Must be at least 3 characters long"
}
}
} }}
}
} r o] $r [dict get $o -errorcode]
} -cleanup {
unset -nocomplain r o
} -result {1 {Validation failed for "-rating": Must be at least 3 characters long} {PARSE_ARGS VALIDATION -rating}}
#>>>
test validate-3.4 {blank validator} -body { #<<<
parse_args::parse_args {-rating foo} {
-rating {-validate {}}
}
set rating
} -cleanup {
unset -nocomplain rating
} -result foo
#>>>
test validate-3.5 {local vars} -body { #<<<
set ref foo
parse_args::parse_args {-rating foo} {
-rating {-validate {apply {
val {
upvar 1 ref ref
expr {$val eq $ref}
}
}}}
}
set rating
} -cleanup {
unset -nocomplain rating
} -result foo
#>>>
# Positional params
# cleanup
::tcltest::cleanupTests
return
# vim: ft=tcl foldmethod=marker foldmarker=<<<,>>> ts=4 shiftwidth=4