-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for (de)serializing CLR boolean values. (#83)
This should match the Valve implementation precisely. Fixes #81.
- Loading branch information
Showing
7 changed files
with
106 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
"object" | ||
{ | ||
"test1_false" "0" | ||
"test2_true" "1" | ||
"test3_oob" "2" | ||
} |
5 changes: 5 additions & 0 deletions
5
ValveKeyValue/ValveKeyValue.Test/Test Data/Text/boolean_serialization.vdf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
"object" | ||
{ | ||
"test1_false" "0" | ||
"test2_true" "1" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using NUnit.Framework; | ||
|
||
namespace ValveKeyValue.Test | ||
{ | ||
class BooleanTestCase | ||
{ | ||
[Test] | ||
public void DynamicDeserialization() | ||
{ | ||
using var stream = TestDataHelper.OpenResource("Text.boolean.vdf"); | ||
var data = KVSerializer.Create(KVSerializationFormat.KeyValues1Text).Deserialize(stream); | ||
|
||
Assert.Multiple(() => | ||
{ | ||
Assert.That((bool)data["test1_false"], Is.False, "test1_false"); | ||
Assert.That((bool)data["test2_true"], Is.True, "test2_true"); | ||
Assert.That((bool)data["test3_oob"], Is.True, "test3_oob"); | ||
}); | ||
} | ||
|
||
[Test] | ||
public void StronglyTypedDeserialization() | ||
{ | ||
using var stream = TestDataHelper.OpenResource("Text.boolean.vdf"); | ||
var data = KVSerializer.Create(KVSerializationFormat.KeyValues1Text).Deserialize<SerializedType>(stream); | ||
|
||
Assert.Multiple(() => | ||
{ | ||
Assert.That(data.test1_false, Is.False, nameof(data.test1_false)); | ||
Assert.That(data.test2_true, Is.True, nameof(data.test2_true)); | ||
Assert.That(data.test3_oob, Is.True, nameof(data.test3_oob)); | ||
}); | ||
} | ||
|
||
[Test] | ||
public void DynamicSerialization() | ||
{ | ||
var data = new KVObject("object", new[] | ||
{ | ||
new KVObject("test1_false", false), | ||
new KVObject("test2_true", true), | ||
}); | ||
|
||
var expected = TestDataHelper.ReadTextResource("Text.boolean_serialization.vdf"); | ||
|
||
using var ms = new MemoryStream(); | ||
KVSerializer.Create(KVSerializationFormat.KeyValues1Text).Serialize(ms, data); | ||
var text = Encoding.UTF8.GetString(ms.ToArray()); | ||
|
||
Assert.That(text, Is.EqualTo(expected)); | ||
} | ||
|
||
[Test] | ||
public void StronglyTypedSerialization() | ||
{ | ||
var data = new | ||
{ | ||
test1_false = false, | ||
test2_true = true, | ||
}; | ||
|
||
var expected = TestDataHelper.ReadTextResource("Text.boolean_serialization.vdf"); | ||
|
||
using var ms = new MemoryStream(); | ||
KVSerializer.Create(KVSerializationFormat.KeyValues1Text).Serialize(ms, data, "object"); | ||
var text = Encoding.UTF8.GetString(ms.ToArray()); | ||
|
||
Assert.That(text, Is.EqualTo(expected)); | ||
} | ||
|
||
class SerializedType | ||
{ | ||
public bool test1_false { get; set; } | ||
public bool test2_true { get; set; } | ||
public bool test3_oob { get; set; } | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters