Skip to content

Commit 4d017cc

Browse files
Add tests for nullable types
1 parent 1968f08 commit 4d017cc

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

test/TestParser.cs

+52
Original file line numberDiff line numberDiff line change
@@ -439,5 +439,57 @@ public void TestDuplicateKeysInAnonymousObject()
439439
*/
440440
Assert.AreEqual(dictionary["hello"], "hell", "The parser stored an incorrect value for the duplicated key");
441441
}
442+
443+
private class SimpleModelWithNulls
444+
{
445+
public string AString { get; set; }
446+
public int? NullableInt { get; set; }
447+
public float? NullableFloat { get; set; }
448+
}
449+
450+
[TestMethod]
451+
public void FromJson_NullString_IsParsedCorrectly()
452+
{
453+
// Arrange
454+
var json = "{\"AString\": null}";
455+
456+
// Act
457+
var actual = json.FromJson<SimpleModelWithNulls>();
458+
459+
// Assert
460+
Assert.IsNull(actual.AString);
461+
}
462+
463+
[DataTestMethod]
464+
[DataRow(null)]
465+
[DataRow(5)]
466+
[DataRow(-1)]
467+
public void FromJson_NullableInt_IsParsedCorrectly(int? value)
468+
{
469+
// Arrange
470+
var json = "{\"NullableInt\": " + (value?.ToString() ?? "null") + "}";
471+
472+
// Act
473+
var actual = json.FromJson<SimpleModelWithNulls>();
474+
475+
// Assert
476+
Assert.AreEqual(value, actual.NullableInt);
477+
}
478+
479+
[DataTestMethod]
480+
[DataRow(null)]
481+
[DataRow(3.0f)]
482+
[DataRow(2.5f)]
483+
public void FromJson_NullableFloat_IsParsedCorrectly(float? value)
484+
{
485+
// Arrange
486+
var json = "{\"NullableFloat\": " + (value?.ToString(System.Globalization.CultureInfo.InvariantCulture) ?? "null") + "}";
487+
488+
// Act
489+
var actual = json.FromJson<SimpleModelWithNulls>();
490+
491+
// Assert
492+
Assert.AreEqual(value, actual.NullableFloat);
493+
}
442494
}
443495
}

0 commit comments

Comments
 (0)