Skip to content

Commit df3cbc3

Browse files
committed
Test that we serialize to kv1 without flags and arrays
1 parent 1e110a4 commit df3cbc3

File tree

5 files changed

+164
-4
lines changed

5 files changed

+164
-4
lines changed

ValveKeyValue/ValveKeyValue.Test/Test Data/TextKV3/array.kv3

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@
2323
#[
2424
11 FF
2525
],
26-
resource:"hello.world"
26+
resource:"hello.world",
27+
"""
28+
multiline
29+
string
30+
""",
31+
-69.420
2732
]
2833
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
"root"
2+
{
3+
"arrayValue"
4+
{
5+
"0" "a"
6+
"1" "b"
7+
}
8+
"arrayOnSingleLine"
9+
{
10+
"0" "16.7551"
11+
"1" "20.3763"
12+
"2" "19.6448"
13+
}
14+
"arrayNoSpace"
15+
{
16+
"0" "1.3763"
17+
"1" "19.6448"
18+
}
19+
"arrayMixedTypes"
20+
{
21+
"0" "a"
22+
"1" "1"
23+
"2" "True"
24+
"3" "False"
25+
"4" ""
26+
"5"
27+
{
28+
"foo" "bar"
29+
}
30+
"6"
31+
{
32+
"0" "1"
33+
"1" "3"
34+
"2" "3"
35+
"3" "7"
36+
}
37+
"7" "hello.world"
38+
"8" "multiline
39+
string"
40+
"9" "-69.42"
41+
}
42+
"test" "success"
43+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
"root"
2+
{
3+
"foo" "bar"
4+
"bar" "foo"
5+
"uppercase" "foo"
6+
"flaggedNumber" "-1234"
7+
"multipleFlags" "cool value"
8+
"soundEvent" "event sound"
9+
"noFlags" "5"
10+
"flaggedObject"
11+
{
12+
"1" "test1"
13+
"2" "test2"
14+
"3"
15+
{
16+
"0" "test3"
17+
}
18+
"4" "test4"
19+
}
20+
"test" "success"
21+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
using System.IO;
2+
using NUnit.Framework;
3+
4+
namespace ValveKeyValue.Test.TextKV3
5+
{
6+
class Kv3ToKv1TestCase
7+
{
8+
[Test]
9+
public void SerializesAndDropsFlags()
10+
{
11+
using var stream = TestDataHelper.OpenResource("TextKV3.flagged_value.kv3");
12+
var expected = TestDataHelper.ReadTextResource("TextKV3.flagged_value_kv1.vdf");
13+
14+
var kv1 = KVSerializer.Create(KVSerializationFormat.KeyValues1Text);
15+
var kv3 = KVSerializer.Create(KVSerializationFormat.KeyValues3Text);
16+
var data = kv3.Deserialize(stream);
17+
18+
data.Add(new KVObject("test", "success"));
19+
20+
string text;
21+
using (var ms = new MemoryStream())
22+
{
23+
kv1.Serialize(ms, data);
24+
25+
ms.Seek(0, SeekOrigin.Begin);
26+
using var reader = new StreamReader(ms);
27+
text = reader.ReadToEnd();
28+
}
29+
30+
Assert.That(text, Is.EqualTo(expected));
31+
}
32+
33+
[Test]
34+
public void SerializesArraysToObjects()
35+
{
36+
using var stream = TestDataHelper.OpenResource("TextKV3.array.kv3");
37+
var expected = TestDataHelper.ReadTextResource("TextKV3.array_kv1.vdf");
38+
39+
var kv1 = KVSerializer.Create(KVSerializationFormat.KeyValues1Text);
40+
var kv3 = KVSerializer.Create(KVSerializationFormat.KeyValues3Text);
41+
var data = kv3.Deserialize(stream);
42+
43+
data.Add(new KVObject("test", "success"));
44+
45+
string text;
46+
using (var ms = new MemoryStream())
47+
{
48+
kv1.Serialize(ms, data);
49+
50+
ms.Seek(0, SeekOrigin.Begin);
51+
using var reader = new StreamReader(ms);
52+
text = reader.ReadToEnd();
53+
}
54+
55+
System.Console.WriteLine(text);
56+
57+
Assert.That(text, Is.EqualTo(expected));
58+
}
59+
}
60+
}

ValveKeyValue/ValveKeyValue/Serialization/KeyValues1/KV1TextSerializer.cs

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Globalization;
34
using System.IO;
45
using System.Text;
6+
using System.Xml.Linq;
57
using ValveKeyValue.Abstraction;
68

79
namespace ValveKeyValue.Serialization.KeyValues1
@@ -23,6 +25,7 @@ public KV1TextSerializer(Stream stream, KVSerializerOptions options)
2325
readonly KVSerializerOptions options;
2426
readonly TextWriter writer;
2527
int indentation = 0;
28+
Stack<int> arrayCount = new();
2629

2730
public void Dispose()
2831
{
@@ -38,9 +41,26 @@ public void OnObjectEnd()
3841
public void OnKeyValuePair(string name, KVValue value)
3942
=> WriteKeyValuePair(name, value);
4043

41-
public void OnArrayStart(string name, KVFlag flag) => throw new NotImplementedException();
42-
public void OnArrayValue(KVValue value) => throw new NotImplementedException();
43-
public void OnArrayEnd() => throw new NotImplementedException();
44+
public void OnArrayStart(string name, KVFlag flag)
45+
{
46+
WriteStartObject(name);
47+
arrayCount.Push(0);
48+
}
49+
50+
public void OnArrayValue(KVValue value)
51+
{
52+
var count = arrayCount.Pop();
53+
54+
WriteKeyValuePair(count.ToString(), value);
55+
56+
arrayCount.Push(count + 1);
57+
}
58+
59+
public void OnArrayEnd()
60+
{
61+
WriteEndObject();
62+
arrayCount.Pop();
63+
}
4464

4565
public void DiscardCurrentObject()
4666
{
@@ -49,6 +69,15 @@ public void DiscardCurrentObject()
4969

5070
void WriteStartObject(string name)
5171
{
72+
if (name == null)
73+
{
74+
var count = arrayCount.Pop();
75+
76+
name = count.ToString();
77+
78+
arrayCount.Push(count + 1);
79+
}
80+
5281
WriteIndentation();
5382
WriteText(name);
5483
WriteLine();
@@ -68,6 +97,8 @@ void WriteEndObject()
6897

6998
void WriteKeyValuePair(string name, IConvertible value)
7099
{
100+
// TODO: Handle true, false, null value types
101+
71102
WriteIndentation();
72103
WriteText(name);
73104
writer.Write('\t');

0 commit comments

Comments
 (0)