forked from cgiffard/Downsize
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
143 lines (115 loc) · 7.37 KB
/
test.js
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
var downsize = require("./"),
chai = require("chai"),
mocha = require("mocha");
chai.should();
describe("Word-wise truncation", function () {
it("should be able to truncate across nested tags", function () {
downsize("<p>this is a <strong>test of word downsizing</strong></p>", {words: 5})
.should.equal("<p>this is a <strong>test of</strong></p>");
});
it("should be able to truncate even if a single quote is found inside a string of double quotes or vice-versa", function () {
downsize('<p><img src="/someUrl.jpg" alt="Let\'s get in!"></p><p>hello world</p>', { words: 1 })
.should.equal('<p><img src="/someUrl.jpg" alt="Let\'s get in!"></p><p>hello</p>');
});
it("should be able to naively balance HTML markup", function () {
downsize("<p><p><p><p>this is a <strong>test of word downsizing</strong> some stuff</p>", {words: 5})
.should.equal("<p><p><p><p>this is a <strong>test of</strong></p></p></p></p>");
});
it("should be able to naively balance HTML markup", function () {
downsize("<p><p><p><p>this is a <strong>test of word downsizing</strong> some stuff</p>", {words: 5})
.should.equal("<p><p><p><p>this is a <strong>test of</strong></p></p></p></p>");
});
it("should ignore erroneously unescaped carets", function () {
downsize("<p>this < is a <strong>test of word downsizing</strong> some stuff</p>", {words: 5})
.should.equal("<p>this < is a <strong>test of</strong></p>");
downsize("<p>this < is a > test < test > <strong>test of word downsizing</strong> some stuff</p>", {words: 5})
.should.equal("<p>this < is a > test < test</p>");
});
it("should ignore comments in markup, and carets in comments", function () {
downsize("<p>this <!-- is a > test < test --> <strong>test of word downsizing</strong> some stuff</p>", {words: 2})
.should.equal("<p>this <!-- is a > test < test --> <strong>test</strong></p>");
});
it("should understand implicitly void tags, and not attempt to close them", function () {
downsize("<p>test <img src=\"blah.jpg\"> <strong>stuffo</strong> some stuff</p>", {words: 2})
.should.equal("<p>test <img src=\"blah.jpg\"> <strong>stuffo</strong></p>");
});
it("should understand self-closing tags, and not attempt to close them", function () {
downsize("<p>test <random selfclosing /> <strong>stuffo</strong> some stuff</p>", {words: 2})
.should.equal("<p>test <random selfclosing /> <strong>stuffo</strong></p>");
});
it("should understand self-closing tags, even marked up poorly.", function () {
downsize("<p>test <random selfclosing / > <strong>stuffo</strong> some stuff</p>", {words: 2})
.should.equal("<p>test <random selfclosing / > <strong>stuffo</strong></p>");
});
it("should close unknown tags appropriately", function () {
downsize("<p>test <unknown> <strong>stuffo</strong> some stuff</p>", {words: 2})
.should.equal("<p>test <unknown> <strong>stuffo</strong></unknown></p>");
});
it("should permit unescaped carets inside double-quoted strings", function () {
downsize("<p>test string <img \"<stuffo>\"> <strong>stuffo</strong> some stuff</p>", {words: 3})
.should.equal("<p>test string <img \"<stuffo>\"> <strong>stuffo</strong></p>");
});
it("should permit unescaped carets inside single-quoted strings", function () {
downsize("<p>test string <img '<stuffo>'> <strong>stuffo</strong> some stuff</p>", {words: 3})
.should.equal("<p>test string <img '<stuffo>'> <strong>stuffo</strong></p>");
});
it("should properly recognised manually closed elements, and do not re-close elements", function () {
downsize("<p>tag closing test</p><p>There should only</p><p>be one terminating para</p>", {words: 7})
.should.equal("<p>tag closing test</p><p>There should only</p><p>be</p>");
});
it("should properly handle unicode languages", function () {
downsize("Рэпудёандаэ конжыквуюнтюр эю прё, нэ квуй янжольэнж квюальизквюэ чадипжкёнг. Ед кюм жкрипта", {words: 3})
.should.equal("Рэпудёандаэ конжыквуюнтюр эю");
});
it("should properly handle unicode languages across nested tags", function () {
downsize("<p>Рэпудёандаэ конжыквуюнтюр эю прё, <span>нэ квуй янжольэнж квюальизквюэ</span> чадипжкёнг. Ед кюм жкрипта</p>", {words: 3})
.should.equal("<p>Рэпудёандаэ конжыквуюнтюр эю</p>");
});
it("should properly properly character-truncate across tag boundries", function () {
downsize("<p>abcdefghij</p><p>klmnop</p><p>qrs</p>", {characters: 15})
.should.equal("<p>abcdefghij</p><p>klmno</p>");
});
it("should not have trailing empty tags", function () {
downsize("<p>there are five words here</p><i>what</i>", {words: 5})
.should.equal("<p>there are five words here</p>");
});
it("should await the end of the containing paragraph", function () {
downsize("<p>there are more than seven words in this paragraph</p><p>this is unrelated</p>", {words: 7, contextualTags: ["p", "ul", "ol", "pre", "blockquote"]})
.should.equal("<p>there are more than seven words in this paragraph</p>");
});
it("should await the end of the containing unordered list", function () {
downsize("<ul><li>item one</li><li>item two</li><li>item three</li></ul><p>paragraph</p>", {words: 5, contextualTags: ["p", "ul", "ol", "pre", "blockquote"]})
.should.equal("<ul><li>item one</li><li>item two</li><li>item three</li></ul>");
});
it("should await the end of the containing paragraph", function () {
downsize("<p>there are many more than seven characters in this paragraph</p><p>this is unrelated</p>", {characters: 7, contextualTags: ["p", "ul", "ol", "pre", "blockquote"]})
.should.equal("<p>there are many more than seven characters in this paragraph</p>");
});
});
describe("Appending", function () {
it("should properly append an ellipsis where required", function () {
downsize("<p>abcdefghij</p><p>klmnop</p><p>qrs</p>", {characters: 15, append: "..."})
.should.equal("<p>abcdefghij</p><p>klmno...</p>");
downsize("<p>here's some text.</p>", {words: 2, append: "... (read more)"})
.should.equal("<p>here's some... (read more)</p>");
});
it("should not append an ellipsis where not required", function () {
downsize("<p>here's some text.</p>", {words: 5, append: "..."})
.should.equal("<p>here's some text.</p>");
});
});
describe("Performance", function () {
var perfTestSeed = "";
for (var i = 0; i < 1000000; i++) {
perfTestSeed += "<p>word truncate performance test</p>\n";
}
describe("truncate five words from a four-million word corpus one hundred thousand times", function () {
it("benchmark time should be under twenty seconds", function () {
var startTime = Date.now();
for (i = 0; i < 100000; i++) {
downsize(perfTestSeed, {"words": 5});
}
(Date.now() - startTime).should.be.lte(20*1000);
});
});
});