forked from zatherz/luanxml
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtest.lua
More file actions
162 lines (140 loc) · 3.92 KB
/
test.lua
File metadata and controls
162 lines (140 loc) · 3.92 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
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
162
local nxml = require("nxml")
---@param a string?
---@param b string?
local function assert_eq(a, b)
assert(a == b, ("'%s' == '%s'"):format(a, b))
end
---@param a string?
---@param valid table<string, true>
local function assert_contained(a, valid)
local s = "{"
for k, _ in pairs(valid) do
s = s .. "'" .. k .. "', "
if a == k then
return
end
end
assert(false, ("'%s' ∈ %s}"):format(a, s))
end
local tree = nxml.parse(
[[<Entity name="hi"> <LuaComponent script_source_file="hamis_code.lua"> </LuaComponent> <DamageModelComponent hp="9999"> </DamageModelComponent> </Entity>]]
)
print(tree.attr.name)
for element in tree:each_of("LuaComponent") do
print(element.attr["script_source_file"])
end
for element in tree:each_child() do
print(element)
end
print(tree:first_of("LuaComponent").attr["script_source_file"])
print(tostring(tree))
print(nxml.tostring(tree, true))
tree:add_child(nxml.parse("<Entity />"))
print(tree)
local dup_name = nxml.parse([[<Entity name="a" name="b" />]])
assert(dup_name.attr.name == "a")
---@type {[string]: string}
local vfs = {}
---@param filename string
---@return string?
local function read(filename)
return vfs[filename]
end
---@param filename string
---@return bool
local function exists(filename)
return vfs[filename] ~= nil
end
---@param filename string
---@param content string
local function write(filename, content)
vfs[filename] = content
end
for _ = 1, 10 do
print("")
end
print("===============================")
for _ = 1, 10 do
print("")
end
local dmc = nxml.new_element("DamageModelComponent", { hp = "0.01" })
local base = nxml.new_element("Base", { file = "enemy" }, { dmc })
local hamis = nxml.new_element("Entity", { name = "hamis" }, { base })
local enemy = nxml.new_element(
"Entity",
{ name = "enemy" },
{ nxml.new_element("DamageModelComponent", { hp = "999", max_hp = "2" }) }
)
enemy:create_children({
LifetimeComponent = {
lifetime = 300,
},
})
vfs.enemy = tostring(enemy)
hamis:expand_base(read, exists)
assert(hamis:first_of("DamageModelComponent"):get("hp") == "0.01")
assert(hamis:first_of("DamageModelComponent"):get("max_hp") == "2")
print(hamis)
local evil_hamis = hamis:clone()
evil_hamis:first_of("DamageModelComponent"):set("hp", -1)
assert(hamis:first_of("DamageModelComponent"):get("hp") == "0.01")
assert(enemy:first_of("LifetimeComponent"):get("lifetime") == "300")
---@return string
local function arbitrary_str()
local s = ""
for _ = 1, math.random(10) do
s = s .. string.char(math.random(26) + 0x60)
end
return s
end
---@return table<string, string>
local function arbitrary_table()
---@type table<string, string>
local t = {}
for _ = 1, math.random(10) do
t[arbitrary_str()] = arbitrary_str()
end
return t
end
---@param n integer? 1
---@return element
local function arbitrary_el(n)
n = n or 1
local children = {}
if math.random(1, math.floor(n)) == math.floor(n) then
for _ = 1, math.random(10) do
table.insert(children, arbitrary_el(n * 2))
end
end
return nxml.new_element(arbitrary_str(), arbitrary_table(), children)
end
--[[
---@type any
local sock = require("socket")
for _ = 1, 10 do
local el = arbitrary_el(1)
print(el)
local start = sock.gettime()
to_string_internal(el, false, "\t", "", {})
local fin = sock.gettime()
print(fin - start, "old")
start = sock.gettime()
to_string_internal2(el, false, "\t", "")
fin = sock.gettime()
print(fin - start, "new")
end
]]
-- print(arbitrary_el())
arbitrary_el()
write("test.xml", [[<Entity name="fish"/>]])
for content in nxml.edit_file("test.xml", read, write) do
content:set("name", "banana")
end
assert_eq(nxml.parse(read("test.xml") or ""):get("name"), "banana")
local valid = {
['<Foo a="2"b="3"><Bar c="3"d="4"/></Foo>'] = true,
--['<Foo b="3"a="2"><Bar c="3"d="4"/></Foo>'] = true,
['<Foo a="2"b="3"><Bar d="4"c="3"/></Foo>'] = true,
['<Foo b="3"a="2"><Bar d="4"c="3"/></Foo>'] = true,
}
assert_contained(nxml.tostring(nxml.parse('<Foo a="2" b="3"> <Bar c="3" d="4" /> </Foo>'), true), valid)