Skip to content

Commit

Permalink
chore: additional test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
bobbyg603 committed Oct 1, 2024
1 parent fe41821 commit bf25e12
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 12 deletions.
70 changes: 68 additions & 2 deletions BugSplatDotNetStandard.Test/JsonObject.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using BugSplatDotNetStandard.Http;
using System.Collections.Generic;
using BugSplatDotNetStandard.Http;
using NUnit.Framework;

namespace Tests
Expand All @@ -11,7 +12,7 @@ public class JsonObjectTest
public void JsonObject_GetValue_ShouldReturnValueForTopLevelKey()
{
var expected = "https://bugsplat.com";
var json = $@"{{ ""url"": ""{ expected }"" }}";
var json = $@"{{ ""url"": ""{expected}"" }}";
var obj = new JsonObject(json);

var result = obj.GetValue("url");
Expand All @@ -31,4 +32,69 @@ public void JsonObject_GetValue_ShouldReturnValueForNestedKey()
Assert.AreEqual(expected, result);
}
}

[TestFixture]
public class JsonSerializerTest
{
[Test]
public void JsonSerializer_Serialize_ShouldReturnNull()
{
var result = JsonSerializer.Serialize(null);

Assert.AreEqual("null", result);
}

[Test]
public void JsonSerializer_Serialize_ShouldEscapeChars()
{
var key = "key";
var value = "\"\\\b\f\n\r\t";
var expectedValue = "\"\\\"\\\\\\b\\f\\n\\r\\t\"";
var expected = $@"{{""{key}"":""{expectedValue}""}}";
var dictionary = new Dictionary<string, string>()
{
{ key, value }
};

var result = JsonSerializer.Serialize(dictionary);

Assert.AreEqual(expected, result);
}

[Test]
public void JsonSerializer_Serialize_ShouldEncodeSpecialChars()
{
var key = "key";
var value = "你好"; // "Hello" in Chinese
var expectedValue = "\\u4F60\\u597D";
var expected = $@"{{""{key}"":""{expectedValue}""}}";
var dictionary = new Dictionary<string, string>()
{
{ key, value }
};

var result = JsonSerializer.Serialize(dictionary);

Assert.AreEqual(expected, result);
}

[Test]
public void JsonSerializer_Serialize_ShouldConvertDictionaryToJsonString()
{
var attributeKey0 = "key0";
var attributeValue0 = "value0";
var attributeKey1 = "key1";
var attributeValue1 = "value0";
var expected = $@"{{""{attributeKey0}"":""{attributeValue0}"",""{attributeKey1}"":""{attributeValue1}""}}";
var dictionary = new Dictionary<string, string>()
{
{ attributeKey0, attributeValue0 },
{ attributeKey1, attributeValue1 },
};

var result = JsonSerializer.Serialize(dictionary);

Assert.AreEqual(expected, result);
}
}
}
10 changes: 0 additions & 10 deletions BugSplatDotNetStandard/Http/JsonObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,6 @@ public string GetValue(params string[] path)
var key = string.Join("/", path);
return root.XPathSelectElement($"//{key}").Value;
}

public override string ToString()
{
return json;
}

public static JsonObject Create(Dictionary<string, string> dictionary)
{
return new JsonObject(JsonSerializer.Serialize(dictionary));
}
}

public static class JsonSerializer
Expand Down

0 comments on commit bf25e12

Please sign in to comment.