This repository was archived by the owner on Apr 8, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhtmlEntities_spec.lua
More file actions
120 lines (105 loc) · 4.13 KB
/
htmlEntities_spec.lua
File metadata and controls
120 lines (105 loc) · 4.13 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
local htmlEntities = require("htmlEntities")
describe("htmlEntities.ASCII_HEX, htmlEntities.ASCII_DEC", function()
describe("Same HEX and DEC should output the same entitie.", function()
for hex = 33, 255 do
local dec = ("%02X"):format(hex)
it(('HEX: "%s" DEC: "%s"'):format(hex, dec), function()
assert.are.equal(htmlEntities.ASCII_HEX(hex), htmlEntities.ASCII_DEC(dec))
end)
end
end)
end)
describe("htmlEntities.decode", function()
describe("Ignore empty html entities.", function()
assert.equals(htmlEntities.decode("&#;"), "&#;")
end)
describe("Should only decode the HTML entity string.", function()
assert.are.equal(htmlEntities.decode("a=1&b=2&c=3&d=4"), "a=1&b=2&c=3&d=4")
end)
-- might need to remove beleow
it("should decode HTML entities", function()
assert.are.equal(
"<script>alert('Hello World!')</script>",
htmlEntities.decode("<script>alert('Hello World!')</script>")
)
end)
it("should not decode ASCII entities if decodeASCII is false", function()
assert.are.equal(
"Commandcracker",
htmlEntities.decode(
"Commandcracker",
false
)
)
end)
it("should decode ASCII entities to characters if decodeASCII is true", function()
assert.are.equal(
"Commandcracker",
htmlEntities.decode("Commandcracker", true)
)
end)
end)
-- TODO: fix
describe("Entity should be the same after encode and decode.", function()
for entity, char in pairs(htmlEntities.gethtmlEntities_table()) do
local decodedEntity = htmlEntities.decode(entity)
local reencodedEntity = htmlEntities.encode(decodedEntity)
it(
('entity: "%s" char "%s" decodedEntity: "%s" reencodedEntity: "%s"'):format(
entity,
char,
decodedEntity,
reencodedEntity
),
function()
assert.are.equal(reencodedEntity, decodedEntity) -- assert.are.equal(reencodedEntity, entity)
assert.are.equal(char, decodedEntity)
end
)
end
end)
-- might be junk
describe("htmlEntities.encode", function()
-- might need to remove beleow
--[[
it("should encode special characters to HTML entities", function()
assert.are.equal(
"<script>alert('Hello World!')</script>",
htmlEntities.encode("<script>alert('Hello World!')</script>")
)
end)
]]
--[[
it("should not encode ASCII characters if encodeASCII is false", function()
assert.are.equal(
"Commandcracker",
htmlEntities.encode("Commandcracker", false)
)
end)
]]
it("should encode ASCII characters to numeric entities if encodeASCII is true", function()
assert.are.equal(
"Commandcracker",
htmlEntities.encode("Commandcracker", true)
)
end)
end)
describe("htmlEntities.escape", function()
it("Should only escape special characters & < >", function()
assert.are.equal("Test &<>", htmlEntities.escape("Test &<>"))
end)
describe("Quotes enabled", function()
local input = "Test " .. '"' .. "&'"
local expectedOutput = "Test "&'"
it("Should escape quotes when enabled", function()
assert.are.equal(expectedOutput, htmlEntities.escape(input))
end)
it("Escape quotes by default", function()
assert.are.equal(htmlEntities.escape(input, true), htmlEntities.escape(input))
end)
end)
it("Should not escape quotes when disabled", function()
local quotes = 'Test "' .. "'"
assert.are.equal(quotes, htmlEntities.escape(quotes, false))
end)
end)