Skip to content

Commit d232e50

Browse files
physikerweltmarijnh
authored andcommitted
Add tests for html auto completion
1 parent 96e836d commit d232e50

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed

test/html-hint-test.js

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
// CodeMirror, copyright (c) by Marijn Haverbeke and others
2+
// Distributed under an MIT license: http://codemirror.net/LICENSE
3+
4+
(function() {
5+
var Pos = CodeMirror.Pos;
6+
7+
namespace = "html-hint_";
8+
9+
testData =[
10+
{
11+
name: "html-element",
12+
value: "<htm",
13+
list: ["<html"]
14+
},
15+
{
16+
name: "element-close",
17+
value: "<a href='#a'>\n</",
18+
list: ["</a>"]
19+
},
20+
{
21+
name: "linkref-attribute",
22+
value: "<link hreflang='z",
23+
from: Pos(0,"<link hreflang=".length),
24+
list: ["'zh'","'za'","'zu'"]
25+
},
26+
{
27+
name: "html-completion",
28+
value: "<html>\n",
29+
list: ["<head","<body","</html>"]
30+
}
31+
];
32+
33+
34+
function escapeHtmlList(o) {
35+
return '<code>' +
36+
JSON.stringify(o.list,null,2)
37+
.replace(/</g, "&lt;")
38+
.replace(/>/g, "&gt;") +
39+
'</code>'
40+
;
41+
}
42+
43+
44+
function test(name, spec) {
45+
testCM(name, function(cm) {
46+
cm.setValue(spec.value);
47+
cm.setCursor(spec.cursor);
48+
var completion = CodeMirror.hint.html(cm);
49+
if (!deepCompare(completion.list, spec.list))
50+
throw new Failure("Wrong completion results. Got" +
51+
escapeHtmlList(completion) +" but expected" +
52+
escapeHtmlList(spec));
53+
eqCharPos(completion.from, spec.from,'from-failed');
54+
eqCharPos(completion.to, spec.to, 'to-failed');
55+
}, {
56+
value: spec.value,
57+
mode: spec.mode || "text/html"
58+
});
59+
}
60+
61+
testData.forEach(function (value) {
62+
// Use sane defaults
63+
var lines = value.value.split(/\n/);
64+
value.to = value.pos || Pos(lines.length-1, lines[lines.length-1].length);
65+
value.from = value.from || Pos(lines.length-1,0);
66+
value.cursor = value.cursor || value.to;
67+
var name = value.name ||value.value;
68+
test(name,value) });
69+
70+
71+
72+
function deepCompare(a, b) {
73+
if (a === b) return true;
74+
if (!(a && typeof a === "object") ||
75+
!(b && typeof b === "object")) return false;
76+
var array = Array.isArray(a);
77+
if (Array.isArray(b) !== array) return false;
78+
if (array) {
79+
if (a.length !== b.length) return false;
80+
for (var i = 0; i < a.length; i++) if (!deepCompare(a[i], b[i])) return false
81+
} else {
82+
for (var p in a) if (!(p in b) || !deepCompare(a[p], b[p])) return false;
83+
for (var p in b) if (!(p in a)) return false
84+
}
85+
return true
86+
}
87+
})();

test/index.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
<script src="../addon/dialog/dialog.js"></script>
1616
<script src="../addon/edit/matchbrackets.js"></script>
1717
<script src="../addon/hint/sql-hint.js"></script>
18+
<script src="../addon/hint/xml-hint.js"></script>
19+
<script src="../addon/hint/html-hint.js"></script>
1820
<script src="../addon/comment/comment.js"></script>
1921
<script src="../addon/mode/simple.js"></script>
2022
<script src="../mode/css/css.js"></script>
@@ -148,6 +150,7 @@ <h2>Test Suite</h2>
148150
<script src="sql-hint-test.js"></script>
149151
<script src="sublime_test.js"></script>
150152
<script src="vim_test.js"></script>
153+
<script src="html-hint-test.js"></script>
151154
<script>
152155
window.onload = runHarness;
153156
CodeMirror.on(window, 'hashchange', runHarness);

0 commit comments

Comments
 (0)