aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSimen Heggestøyl2016-08-28 18:36:27 +0200
committerSimen Heggestøyl2016-08-28 18:36:27 +0200
commit91734c6bd33bab47c443ca23e6948e3a7900856b (patch)
treecb46a29b681d5d45f6497091352ac758e7854161
parent7fcce24e75b8281621a0b8816dc58cbdc05fdc91 (diff)
downloademacs-91734c6bd33bab47c443ca23e6948e3a7900856b.tar.gz
emacs-91734c6bd33bab47c443ca23e6948e3a7900856b.zip
Add tests for dom.el
* test/lisp/dom-tests.el: New file with tests for dom.el.
-rw-r--r--test/lisp/dom-tests.el201
1 files changed, 201 insertions, 0 deletions
diff --git a/test/lisp/dom-tests.el b/test/lisp/dom-tests.el
new file mode 100644
index 00000000000..ca6bfbf84b5
--- /dev/null
+++ b/test/lisp/dom-tests.el
@@ -0,0 +1,201 @@
1;;; dom-tests.el --- Tests for dom.el -*- lexical-binding: t; -*-
2
3;; Copyright (C) 2016 Free Software Foundation, Inc.
4
5;; Author: Simen Heggestøyl <simenheg@gmail.com>
6;; Keywords:
7
8;; GNU Emacs is free software: you can redistribute it and/or modify
9;; it under the terms of the GNU General Public License as published by
10;; the Free Software Foundation, either version 3 of the License, or
11;; (at your option) any later version.
12
13;; GNU Emacs is distributed in the hope that it will be useful,
14;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16;; GNU General Public License for more details.
17
18;; You should have received a copy of the GNU General Public License
19;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
20
21;;; Commentary:
22
23;;
24
25;;; Code:
26
27(require 'dom)
28(require 'ert)
29(require 'subr-x)
30
31(defun dom-tests--tree ()
32 "Return a DOM tree for testing."
33 (dom-node "html" nil
34 (dom-node "head" nil
35 (dom-node "title" nil
36 "Test"))
37 (dom-node "body" nil
38 (dom-node "div" '((class . "foo")
39 (style . "color: red;"))
40 (dom-node "p" '((id . "bar"))
41 "foo"))
42 (dom-node "div" '((title . "2nd div"))
43 "bar"))))
44
45(ert-deftest dom-tests-tag ()
46 (let ((dom (dom-tests--tree)))
47 (should (equal (dom-tag dom) "html"))
48 (should (equal (dom-tag (car (dom-children dom))) "head"))))
49
50(ert-deftest dom-tests-attributes ()
51 (let ((dom (dom-tests--tree)))
52 (should-not (dom-attributes dom))
53 (should (equal (dom-attributes (dom-by-class dom "foo"))
54 '((class . "foo") (style . "color: red;"))))))
55
56(ert-deftest dom-tests-children ()
57 (let ((dom (dom-tests--tree)))
58 (should (equal (mapcar #'dom-tag (dom-children dom))
59 '("head" "body")))
60 (should (equal (dom-tag (dom-children (dom-children dom)))
61 "title"))))
62
63(ert-deftest dom-tests-non-text-children ()
64 (let ((dom (dom-tests--tree)))
65 (should (equal (dom-children dom) (dom-non-text-children dom)))
66 (should-not (dom-non-text-children
67 (dom-children (dom-children dom))))))
68
69(ert-deftest dom-tests-set-attributes ()
70 (let ((dom (dom-tests--tree))
71 (attributes '((xmlns "http://www.w3.org/1999/xhtml"))))
72 (should-not (dom-attributes dom))
73 (dom-set-attributes dom attributes)
74 (should (equal (dom-attributes dom) attributes))))
75
76(ert-deftest dom-tests-set-attribute ()
77 (let ((dom (dom-tests--tree))
78 (attr 'xmlns)
79 (value "http://www.w3.org/1999/xhtml"))
80 (should-not (dom-attributes dom))
81 (dom-set-attribute dom attr value)
82 (should (equal (dom-attr dom attr) value))))
83
84(ert-deftest dom-tests-attr ()
85 (let ((dom (dom-tests--tree)))
86 (should-not (dom-attr dom 'id))
87 (should (equal (dom-attr (dom-by-id dom "bar") 'id) "bar"))))
88
89(ert-deftest dom-tests-text ()
90 (let ((dom (dom-tests--tree)))
91 (should (string-empty-p (dom-text dom)))
92 (should (equal (dom-text (dom-by-tag dom "title")) "Test"))))
93
94(ert-deftest dom-tests-texts ()
95 (let ((dom (dom-tests--tree)))
96 (should (equal (dom-texts dom) "Test foo bar"))
97 (should (equal (dom-texts dom ", ") "Test, foo, bar"))))
98
99(ert-deftest dom-tests-child-by-tag ()
100 (let ((dom (dom-tests--tree)))
101 (should (equal (dom-child-by-tag dom "head")
102 (car (dom-children dom))))
103 (should-not (dom-child-by-tag dom "title"))))
104
105(ert-deftest dom-tests-by-tag ()
106 (let ((dom (dom-tests--tree)))
107 (should (= (length (dom-by-tag dom "div")) 2))
108 (should-not (dom-by-tag dom "article"))))
109
110(ert-deftest dom-tests-strings ()
111 (let ((dom (dom-tests--tree)))
112 (should (equal (dom-strings dom) '("Test" "foo" "bar")))
113 (should (equal (dom-strings (dom-children dom)) '("Test")))))
114
115(ert-deftest dom-tests-by-class ()
116 (let ((dom (dom-tests--tree)))
117 (should (equal (dom-tag (dom-by-class dom "foo")) "div"))
118 (should-not (dom-by-class dom "bar"))))
119
120(ert-deftest dom-tests-by-style ()
121 (let ((dom (dom-tests--tree)))
122 (should (equal (dom-tag (dom-by-style dom "color")) "div"))
123 (should-not (dom-by-style dom "width"))))
124
125(ert-deftest dom-tests-by-id ()
126 (let ((dom (dom-tests--tree)))
127 (should (equal (dom-tag (dom-by-id dom "bar")) "p"))
128 (should-not (dom-by-id dom "foo"))))
129
130(ert-deftest dom-tests-elements ()
131 (let ((dom (dom-tests--tree)))
132 (should (equal (dom-elements dom 'class "foo")
133 (dom-by-class dom "foo")))
134 (should (equal (dom-attr (dom-elements dom 'title "2nd") 'title)
135 "2nd div"))))
136
137(ert-deftest dom-tests-remove-node ()
138 (let ((dom (dom-tests--tree)))
139 (should-not (dom-remove-node dom dom))
140 (should (= (length (dom-children dom)) 2))
141 (dom-remove-node dom (car (dom-children dom)))
142 (should (= (length (dom-children dom)) 1))
143 (dom-remove-node dom (car (dom-children dom)))
144 (should-not (dom-children dom))))
145
146(ert-deftest dom-tests-parent ()
147 (let ((dom (dom-tests--tree)))
148 (should-not (dom-parent dom dom))
149 (should (equal (dom-parent dom (car (dom-children dom))) dom))))
150
151(ert-deftest dom-tests-previous-sibling ()
152 (let ((dom (dom-tests--tree)))
153 (should-not (dom-previous-sibling dom dom))
154 (let ((children (dom-children dom)))
155 (should (equal (dom-previous-sibling dom (cadr children))
156 (car children))))))
157
158(ert-deftest dom-tests-append-child ()
159 (let ((dom (dom-tests--tree)))
160 (should (equal (mapcar #'dom-tag (dom-children dom))
161 '("head" "body")))
162 (dom-append-child dom (dom-node "feet"))
163 (should (equal (mapcar #'dom-tag (dom-children dom))
164 '("head" "body" "feet")))))
165
166(ert-deftest dom-tests-add-child-before ()
167 "Test `dom-add-child-before'.
168Tests the cases of adding a new first-child and mid-child. Also
169checks that an attempt to add a new node before a non-existent
170child results in an error."
171 (let ((dom (dom-tests--tree)))
172 (should (equal (mapcar #'dom-tag (dom-children dom))
173 '("head" "body")))
174 (dom-add-child-before dom (dom-node "neck")
175 (dom-child-by-tag dom "body"))
176 (should (equal (mapcar #'dom-tag (dom-children dom))
177 '("head" "neck" "body")))
178 (dom-add-child-before dom (dom-node "hat"))
179 (should (equal (mapcar #'dom-tag (dom-children dom))
180 '("hat" "head" "neck" "body")))
181 (should-error (dom-add-child-before dom (dom-node "neck")
182 (dom-by-id dom "bar")))))
183
184(ert-deftest dom-tests-ensure-node ()
185 (let ((node (dom-node "foo")))
186 (should (equal (dom-ensure-node '("foo")) node))
187 (should (equal (dom-ensure-node '(("foo"))) node))
188 (should (equal (dom-ensure-node '("foo" nil)) node))
189 (should (equal (dom-ensure-node '(("foo") nil)) node))))
190
191(ert-deftest dom-tests-pp ()
192 (let ((node (dom-node "foo" nil "")))
193 (with-temp-buffer
194 (dom-pp node)
195 (should (equal (buffer-string) "(\"foo\" nil\n \"\")")))
196 (with-temp-buffer
197 (dom-pp node t)
198 (should (equal (buffer-string) "(\"foo\" nil)")))))
199
200(provide 'dom-tests)
201;;; dom-tests.el ends here