Skip to content

Commit

Permalink
Fixing types in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bowencode committed Jun 27, 2023
1 parent 2bbeaf5 commit 83e58b0
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,19 @@ public class JsonDictionaryDataItemTest
public void ConvertNumber()
{
var data = new Dictionary<string, object?>
{
{ "integer", 1 },
{ "doubleAsNumber1", 1.0 },
{ "doubleAsNumber2", 1.1 },
};
{
{ "integer", 1 },
{ "long", int.MaxValue * 2L },
{ "doubleAsNumber1", 1.0 },
{ "doubleAsNumber2", 1.1 },
};

var output = new JsonDictionaryDataItem(data);

Assert.IsTrue(output.GetValue("integer")?.GetType()==typeof(int));
Assert.IsTrue(output.GetValue("doubleAsNumber1")?.GetType()==typeof(double));
Assert.IsTrue(output.GetValue("doubleAsNumber2")?.GetType()==typeof(double));
Assert.IsTrue(output.GetValue("integer")?.GetType() == typeof(int));
Assert.IsTrue(output.GetValue("long")?.GetType() == typeof(long));
Assert.IsTrue(output.GetValue("doubleAsNumber1")?.GetType() == typeof(double));
Assert.IsTrue(output.GetValue("doubleAsNumber2")?.GetType() == typeof(double));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public async Task ReadAsync_WithSingleObjectFile_ReadsValues()
{
counter++;
CollectionAssert.AreEquivalent(new[] { "id", "name" }, dataItem.GetFieldNames().ToArray());
Assert.IsInstanceOfType(dataItem.GetValue("id"), typeof(double));
Assert.IsInstanceOfType(dataItem.GetValue("id"), typeof(int));
Assert.IsNotNull(dataItem.GetValue("name"));
}

Expand All @@ -85,9 +85,9 @@ public async Task ReadAsync_WithSingleObjectsFolder_ReadsValuesInOrder()
counter++;
CollectionAssert.AreEquivalent(new[] { "id", "name" }, dataItem.GetFieldNames().ToArray());
object? value = dataItem.GetValue("id");
Assert.IsInstanceOfType(value, typeof(double));
Assert.IsInstanceOfType(value, typeof(int));
Assert.IsNotNull(dataItem.GetValue("name"));
var current = (double?)value ?? int.MaxValue;
var current = (int?)value ?? int.MaxValue;
Assert.IsTrue(current > lastId);
lastId = current;
}
Expand All @@ -109,7 +109,7 @@ public async Task ReadAsync_WithArraysFolder_ReadsValues()
{
counter++;
CollectionAssert.AreEquivalent(new[] { "id", "name" }, dataItem.GetFieldNames().ToArray());
Assert.IsInstanceOfType(dataItem.GetValue("id"), typeof(double));
Assert.IsInstanceOfType(dataItem.GetValue("id"), typeof(int));
Assert.IsNotNull(dataItem.GetValue("name"));
}

Expand All @@ -130,7 +130,7 @@ public async Task ReadAsync_WithMixedObjectsFolder_ReadsValues()
{
counter++;
CollectionAssert.AreEquivalent(new[] { "id", "name" }, dataItem.GetFieldNames().ToArray());
Assert.IsInstanceOfType(dataItem.GetValue("id"), typeof(double));
Assert.IsInstanceOfType(dataItem.GetValue("id"), typeof(int));
Assert.IsNotNull(dataItem.GetValue("name"));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,11 @@ public IEnumerable<string> GetFieldNames()
return element.GetString();
case JsonValueKind.Number:
{
if (IsInteger(element.ToString()))
if (IsInteger(element.GetRawText()))
return element.GetInt32();
else
return element.GetDouble();
if (IsLong(element.GetRawText()))
return element.GetInt64();
return element.GetDouble();
}
case JsonValueKind.True:
return true;
Expand All @@ -64,9 +65,14 @@ private static JsonDictionaryDataItem GetChildObject(JsonElement element)
return new JsonDictionaryDataItem(element.EnumerateObject().ToDictionary(p => p.Name, p => (object?)p.Value));
}

static bool IsInteger(string ?value)
private static bool IsInteger(string? value)
{
return int.TryParse(value, out _);
}

private static bool IsLong(string? value)
{
return long.TryParse(value, out _);
}
}
}

0 comments on commit 83e58b0

Please sign in to comment.