|
| 1 | +using System.Text; |
| 2 | + |
| 3 | +namespace ValveKeyValue.Test.TextKV3 |
| 4 | +{ |
| 5 | + class SkipHeaderTestCase |
| 6 | + { |
| 7 | + [Test] |
| 8 | + public void DeserializesWithoutHeader() |
| 9 | + { |
| 10 | + var options = new KVSerializerOptions { SkipHeader = true }; |
| 11 | + var kv = KVSerializer.Create(KVSerializationFormat.KeyValues3Text); |
| 12 | + var data = kv.Deserialize("{\n\tkey = \"value\"\n}\n", options); |
| 13 | + |
| 14 | + Assert.That((string)data["key"], Is.EqualTo("value")); |
| 15 | + } |
| 16 | + |
| 17 | + [Test] |
| 18 | + public void DeserializesWithoutHeaderReturnsEmptyHeader() |
| 19 | + { |
| 20 | + var options = new KVSerializerOptions { SkipHeader = true }; |
| 21 | + var kv = KVSerializer.Create(KVSerializationFormat.KeyValues3Text); |
| 22 | + var data = kv.Deserialize("{\n\tkey = \"value\"\n}\n", options); |
| 23 | + |
| 24 | + Assert.Multiple(() => |
| 25 | + { |
| 26 | + Assert.That(data.Header, Is.Not.Null); |
| 27 | + Assert.That(data.Header.Encoding.Name, Is.Null); |
| 28 | + Assert.That(data.Header.Format.Name, Is.Null); |
| 29 | + }); |
| 30 | + } |
| 31 | + |
| 32 | + [Test] |
| 33 | + public void SerializesWithoutHeader() |
| 34 | + { |
| 35 | + var options = new KVSerializerOptions { SkipHeader = true }; |
| 36 | + var kv = KVSerializer.Create(KVSerializationFormat.KeyValues3Text); |
| 37 | + var root = KVObject.Collection(); |
| 38 | + root.Add("key", "value"); |
| 39 | + var doc = new KVDocument(null!, null!, root); |
| 40 | + |
| 41 | + string text; |
| 42 | + using (var ms = new MemoryStream()) |
| 43 | + { |
| 44 | + kv.Serialize(ms, doc, options); |
| 45 | + ms.Seek(0, SeekOrigin.Begin); |
| 46 | + using var reader = new StreamReader(ms); |
| 47 | + text = reader.ReadToEnd(); |
| 48 | + } |
| 49 | + |
| 50 | + Assert.That(text, Does.Not.StartWith("<!--")); |
| 51 | + Assert.That(text, Is.EqualTo("{\n\tkey = \"value\"\n}\n")); |
| 52 | + } |
| 53 | + |
| 54 | + [Test] |
| 55 | + public void RoundTripsWithoutHeader() |
| 56 | + { |
| 57 | + var options = new KVSerializerOptions { SkipHeader = true }; |
| 58 | + var kv = KVSerializer.Create(KVSerializationFormat.KeyValues3Text); |
| 59 | + var root = KVObject.Collection(); |
| 60 | + root.Add("key", "value"); |
| 61 | + root.Add("number", 42); |
| 62 | + var doc = new KVDocument(null!, null!, root); |
| 63 | + |
| 64 | + using var ms = new MemoryStream(); |
| 65 | + kv.Serialize(ms, doc, options); |
| 66 | + ms.Seek(0, SeekOrigin.Begin); |
| 67 | + var data = kv.Deserialize(ms, options); |
| 68 | + |
| 69 | + Assert.Multiple(() => |
| 70 | + { |
| 71 | + Assert.That((string)data["key"], Is.EqualTo("value")); |
| 72 | + Assert.That((int)data["number"], Is.EqualTo(42)); |
| 73 | + }); |
| 74 | + } |
| 75 | + |
| 76 | + [Test] |
| 77 | + public void DeserializeWithoutHeaderFailsWhenHeaderPresent() |
| 78 | + { |
| 79 | + var options = new KVSerializerOptions { SkipHeader = true }; |
| 80 | + var kv = KVSerializer.Create(KVSerializationFormat.KeyValues3Text); |
| 81 | + var input = "<!-- kv3 encoding:text:version{e21c7f3c-8a33-41c5-9977-a76d3a32aa0d} format:generic:version{7412167c-06e9-4698-aff2-e63eb59037e7} -->\n{\n\tkey = \"value\"\n}\n"; |
| 82 | + |
| 83 | + Assert.That(() => kv.Deserialize(input, options), Throws.Exception); |
| 84 | + } |
| 85 | + |
| 86 | + [Test] |
| 87 | + public void DeserializeWithHeaderFailsWhenHeaderMissing() |
| 88 | + { |
| 89 | + var kv = KVSerializer.Create(KVSerializationFormat.KeyValues3Text); |
| 90 | + var input = "{\n\tkey = \"value\"\n}\n"; |
| 91 | + |
| 92 | + Assert.That(() => kv.Deserialize(input), Throws.Exception.TypeOf<InvalidDataException>()); |
| 93 | + } |
| 94 | + |
| 95 | + [Test] |
| 96 | + public void SerializesWithHeaderByDefault() |
| 97 | + { |
| 98 | + var kv = KVSerializer.Create(KVSerializationFormat.KeyValues3Text); |
| 99 | + var root = KVObject.Collection(); |
| 100 | + root.Add("key", "value"); |
| 101 | + var doc = new KVDocument(null!, null!, root); |
| 102 | + |
| 103 | + string text; |
| 104 | + using (var ms = new MemoryStream()) |
| 105 | + { |
| 106 | + kv.Serialize(ms, doc); |
| 107 | + ms.Seek(0, SeekOrigin.Begin); |
| 108 | + using var reader = new StreamReader(ms); |
| 109 | + text = reader.ReadToEnd(); |
| 110 | + } |
| 111 | + |
| 112 | + Assert.That(text, Does.StartWith("<!-- kv3 encoding:")); |
| 113 | + } |
| 114 | + } |
| 115 | +} |
0 commit comments