@@ -18,72 +18,11 @@ describe("TestConfigSetCodec", function()
1818 _G .Inflate = savedInflate
1919 end )
2020
21- -- Mirrors the serialisation logic in ConfigTabClass:OpenExportConfigSetPopup.
22- local function encodeConfigSet (configSet , configTab )
23- local xmlNode = { elem = " ConfigSet" , attrib = { title = configSet .title } }
24- for k , v in pairs (configSet .input ) do
25- if v ~= configTab :GetDefaultState (k , type (v )) then
26- local node = { elem = " Input" , attrib = { name = k } }
27- if type (v ) == " number" then
28- node .attrib .number = tostring (v )
29- elseif type (v ) == " boolean" then
30- node .attrib .boolean = tostring (v )
31- else
32- node .attrib .string = tostring (v )
33- end
34- table.insert (xmlNode , node )
35- end
36- end
37- for k , v in pairs (configSet .placeholder ) do
38- local node = { elem = " Placeholder" , attrib = { name = k } }
39- if type (v ) == " number" then
40- node .attrib .number = tostring (v )
41- else
42- node .attrib .string = tostring (v )
43- end
44- table.insert (xmlNode , node )
45- end
46- local xmlText = common .xml .ComposeXML (xmlNode )
47- return common .base64 .encode (Deflate (xmlText )):gsub (" +" , " -" ):gsub (" /" , " _" )
48- end
49-
50- -- Mirrors the deserialisation logic in ConfigTabClass:OpenImportConfigSetPopup.
51- local function decodeConfigSet (code , configTab , name )
52- local xmlText = Inflate (common .base64 .decode (code :gsub (" -" , " +" ):gsub (" _" , " /" )))
53- if not xmlText or # xmlText == 0 then
54- return nil , " decode failed"
55- end
56- local parsedXML , errMsg = common .xml .ParseXML (xmlText )
57- if errMsg or not parsedXML or not parsedXML [1 ] or parsedXML [1 ].elem ~= " ConfigSet" then
58- return nil , errMsg or " invalid config set code"
59- end
60- local xmlConfigSet = parsedXML [1 ]
61- local newConfigSet = configTab :NewConfigSet (nil , name or xmlConfigSet .attrib .title or " Imported" )
62- for _ , child in ipairs (xmlConfigSet ) do
63- if child .elem == " Input" and child .attrib .name then
64- if child .attrib .number then
65- newConfigSet .input [child .attrib .name ] = tonumber (child .attrib .number )
66- elseif child .attrib .boolean then
67- newConfigSet .input [child .attrib .name ] = child .attrib .boolean == " true"
68- elseif child .attrib .string then
69- newConfigSet .input [child .attrib .name ] = child .attrib .string
70- end
71- elseif child .elem == " Placeholder" and child .attrib .name then
72- if child .attrib .number then
73- newConfigSet .placeholder [child .attrib .name ] = tonumber (child .attrib .number )
74- elseif child .attrib .string then
75- newConfigSet .placeholder [child .attrib .name ] = child .attrib .string
76- end
77- end
78- end
79- return newConfigSet , nil
80- end
81-
8221 it (" export produces a non-empty base64 code" , function ()
8322 local configTab = build .configTab
8423 local configSet = configTab .configSets [configTab .activeConfigSetId ]
8524 configSet .input [" usePowerCharges" ] = true
86- local code = encodeConfigSet (configSet , configTab )
25+ local code = configTab : EncodeConfigSet (configSet )
8726 assert .is_not_nil (code )
8827 assert .is_true (# code > 0 )
8928 end )
@@ -92,8 +31,8 @@ describe("TestConfigSetCodec", function()
9231 local configTab = build .configTab
9332 local configSet = configTab .configSets [configTab .activeConfigSetId ]
9433 configSet .input [" usePowerCharges" ] = true
95- local code = encodeConfigSet (configSet , configTab )
96- local imported , err = decodeConfigSet (code , configTab , " Test" )
34+ local code = configTab : EncodeConfigSet (configSet )
35+ local imported , err = configTab : DecodeConfigSet (code , " Test" )
9736 assert .is_nil (err )
9837 assert .is_not_nil (imported )
9938 assert .are .equal (true , imported .input [" usePowerCharges" ])
@@ -105,8 +44,8 @@ describe("TestConfigSetCodec", function()
10544 -- Use 75 to avoid matching any boss-level placeholder (83/84/85),
10645 -- which would cause GetDefaultState skip export.
10746 configSet .input [" enemyLevel" ] = 75
108- local code = encodeConfigSet (configSet , configTab )
109- local imported , err = decodeConfigSet (code , configTab , " Test" )
47+ local code = configTab : EncodeConfigSet (configSet )
48+ local imported , err = configTab : DecodeConfigSet (code , " Test" )
11049 assert .is_nil (err )
11150 assert .is_not_nil (imported )
11251 assert .are .equal (75 , imported .input [" enemyLevel" ])
@@ -117,8 +56,8 @@ describe("TestConfigSetCodec", function()
11756 local configSet = configTab .configSets [configTab .activeConfigSetId ]
11857 -- "Uber" is non-default (default is "Pinnacle" from defaultIndex=3)
11958 configSet .input [" enemyIsBoss" ] = " Uber"
120- local code = encodeConfigSet (configSet , configTab )
121- local imported , err = decodeConfigSet (code , configTab , " Test" )
59+ local code = configTab : EncodeConfigSet (configSet )
60+ local imported , err = configTab : DecodeConfigSet (code , " Test" )
12261 assert .is_nil (err )
12362 assert .is_not_nil (imported )
12463 assert .are .equal (" Uber" , imported .input [" enemyIsBoss" ])
@@ -130,8 +69,8 @@ describe("TestConfigSetCodec", function()
13069 configSet .input [" usePowerCharges" ] = true
13170 configSet .input [" enemyLevel" ] = 75
13271 configSet .input [" enemyIsBoss" ] = " Uber"
133- local code = encodeConfigSet (configSet , configTab )
134- local imported , err = decodeConfigSet (code , configTab , " Multi" )
72+ local code = configTab : EncodeConfigSet (configSet )
73+ local imported , err = configTab : DecodeConfigSet (code , " Multi" )
13574 assert .is_nil (err )
13675 assert .is_not_nil (imported )
13776 assert .are .equal (true , imported .input [" usePowerCharges" ])
@@ -143,8 +82,8 @@ describe("TestConfigSetCodec", function()
14382 local configTab = build .configTab
14483 local configSet = configTab .configSets [configTab .activeConfigSetId ]
14584 configSet .title = " Original Title"
146- local code = encodeConfigSet (configSet , configTab )
147- local imported , err = decodeConfigSet (code , configTab , " Custom Name" )
85+ local code = configTab : EncodeConfigSet (configSet )
86+ local imported , err = configTab : DecodeConfigSet (code , " Custom Name" )
14887 assert .is_nil (err )
14988 assert .is_not_nil (imported )
15089 assert .are .equal (" Custom Name" , imported .title )
@@ -153,25 +92,25 @@ describe("TestConfigSetCodec", function()
15392 it (" export and re-import of an unmodified config set succeeds" , function ()
15493 local configTab = build .configTab
15594 local configSet = configTab .configSets [configTab .activeConfigSetId ]
156- local code = encodeConfigSet (configSet , configTab )
95+ local code = configTab : EncodeConfigSet (configSet )
15796 assert .is_true (# code > 0 )
158- local imported , err = decodeConfigSet (code , configTab , " Imported" )
97+ local imported , err = configTab : DecodeConfigSet (code , " Imported" )
15998 assert .is_nil (err )
16099 assert .is_not_nil (imported )
161100 assert .are .equal (" Imported" , imported .title )
162101 end )
163102
164103 it (" returns error for garbage input" , function ()
165104 local configTab = build .configTab
166- local _ , err = decodeConfigSet (" !!!not_valid!!!" , configTab , " Test" )
105+ local _ , err = configTab : DecodeConfigSet (" !!!not_valid!!!" , " Test" )
167106 assert .is_not_nil (err )
168107 end )
169108
170109 it (" returns error when decoded XML root is not a ConfigSet" , function ()
171110 local configTab = build .configTab
172111 local xmlText = " <PathOfBuilding></PathOfBuilding>"
173112 local code = common .base64 .encode (Deflate (xmlText )):gsub (" +" , " -" ):gsub (" /" , " _" )
174- local _ , err = decodeConfigSet (code , configTab , " Test" )
113+ local _ , err = configTab : DecodeConfigSet (code , " Test" )
175114 assert .is_not_nil (err )
176115 end )
177116end )
0 commit comments