forked from tali713/esxml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathesxml-form-tests.el
161 lines (152 loc) · 5.32 KB
/
esxml-form-tests.el
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
;; tests for field-set
(require 'kv)
(require 'db)
(require 'uuid)
(defvar my-test-db nil
"Test database variable.")
(defmacro esxml-form-field-set--test-defaults (&rest body)
(declare (debug (&rest form)))
`(let ((db-hash-do-not-save t))
(let* ((uuid-str (uuid-string))
(my-test-db
(db-make
`(db-hash :filename
,(format "/tmp/esxml-form-test-db-%s" uuid-str))))
(fs
(esxml-form
(:db my-test-db :db-key "username")
(username
:regex "[A-Za-z]+"
:db-key t
:db-check (= "name" $))
(key
:regex "[A-Za-z0-9=]+"
:html :textarea)))
(fs-check ; specified string check
(esxml-form
(:db my-test-db :db-key "username")
(username
:regex "[A-Za-z]+"
:check-failure "use only letters"
:db-key t
:db-check (= "name" $))
(key
:regex "[A-Za-z0-9=]+"
:html :textarea)))
(fs-check-choose ; specified list check
(esxml-form
(:db my-test-db :db-key "username")
(username
:regex "[A-Za-z]+"
:check-failure
((:regex "use only letters")
(:db-check "choose a different username"))
:db-key t
:db-check (= "name" $))
(key
:regex "[A-Za-z0-9=]+"
:html :textarea))))
,@body)))
(ert-deftest esxml-form-structure ()
(esxml-form-field-set--test-defaults
(should (listp (esxml-form-fields fs)))
(should (equal '(username key)
(kvalist->keys (esxml-form-fields fs))))
(should (listp (cdr (assoc 'username (esxml-form-fields fs)))))))
(ert-deftest esxml--field-check ()
(esxml-form-field-set--test-defaults
(let* ((fields (esxml-form-fields fs))
(username-field (cdr (assoc 'username fields))))
(should-not (esxml--field-check username-field "NicFerrier"))
(should
(eq :regex
(esxml--field-check username-field "!NicFerrier"))))))
(ert-deftest esxml-field-set-check ()
(esxml-form-field-set--test-defaults
(let ((params '(("username" . "nic") ; these match the fields
("key" . "ssh-dss sadwqqwdqdqd="))))
(let (my-test-db) ; don't do db validation
(should-not (esxml-field-set-check fs params))
;; Now with an invalid field
(let ((params
'(("username" . "!nic")
("key" . "ssh-dss sadwqqwdqdqd="))))
(should
(equal
'((username "!nic" "the content of the field was wrong"))
(esxml-field-set-check fs params)))
;; With invalid field and specific message
(should
(equal
'((username "!nic" "use only letters"))
(esxml-field-set-check fs-check params)))))
;; Now do one with db-validation
(let ((db-valid (esxml-field-set-check fs params)))
(should (eq nil db-valid)))
;; And now add a row first and then do it
(db-put "001" '((name . "nic")
(key . "311ndknd1")) my-test-db)
(should (esxml-field-set-check fs params))
;; Now one with a check map
(should
(equal
'((username "nic" "choose a different username"))
(esxml-field-set-check fs-check-choose params))))))
(ert-deftest esxml-form-test-db ()
"Test that the db is there."
(esxml-form-field-set--test-defaults
(should my-test-db)))
(ert-deftest esxml-field-set->esxml ()
(esxml-form-field-set--test-defaults
;; Test that the HTML is constructed properly.
(should
(equal
'(fieldset ()
(label ()
"username: "
(input ((name . "username")
(type . "text"))))
(label ()
"key: "
(textarea ((name . "key")))))
(esxml-field-set->esxml fs)))
;; Test it all renders properly
(should
(equal
(concat
"<fieldset>"
"<label>username: <input name=\"username\" type=\"text\"/></label>"
"<label>key: <textarea name=\"key\"/></label>"
"</fieldset>")
(esxml-to-xml (esxml-field-set->esxml fs))))
;; Test the structure with values
(let* (my-test-db ; bind the db to nothing
(params '(("username" . "test")))
(errors (esxml-field-set-check fs params)))
(should
(equal
'(fieldset ()
(label ()
"username: "
(input ((name . "username")
(type . "text")
(value . "test"))))
(label ()
"key: "
(textarea ((name . "key")))
(div ((class . "error")) "the content of the field was wrong")))
(esxml-field-set->esxml fs params errors))))))
(ert-deftest esxml-form-save ()
(esxml-form-field-set--test-defaults
(let* ((params '(("username" . "test001")
("key" . "wadkwqdnwdNJNSJNJSw")
("doit" . "doit"))) ; last one is fake button or something
(errors (esxml-field-set-check fs params)))
(unless errors (esxml-form-save fs params))
(let ((value (db-get "test001" my-test-db)))
(should
(equal
value
'(("username" . "test001")
("key" . "wadkwqdnwdNJNSJNJSw"))))))))
;; ends