Skip to content

Commit

Permalink
Adding New Idictionary tests and fixing test name and input for type …
Browse files Browse the repository at this point in the history
…Mismatch (dotnet#38660)

* Adding New Idictionary tests and fixing test name and inout for type mismatch

* Changing name of the variable
  • Loading branch information
Anipik authored Jun 20, 2019
1 parent 055052f commit 36678a0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
23 changes: 17 additions & 6 deletions src/System.Text.Json/tests/Serialization/DictionaryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -776,16 +776,27 @@ public static void HashtableFail()
[Fact]
public static void DeserializeDictionaryWithDuplicateKeys()
{
// Non-generic IDictionary case.
IDictionary iDictionary = JsonSerializer.Parse<IDictionary>(@"{""Hello"":""World"", ""Hello"":""NewValue""}");
Assert.Equal("NewValue", iDictionary["Hello"].ToString());

// Generic IDictionary case.
IDictionary<string, string> iNonGenericDictionary = JsonSerializer.Parse<IDictionary<string, string>>(@"{""Hello"":""World"", ""Hello"":""NewValue""}");
Assert.Equal("NewValue", iNonGenericDictionary["Hello"]);

IDictionary<string, object> iNonGenericObjectDictionary = JsonSerializer.Parse<IDictionary<string, object>>(@"{""Hello"":""World"", ""Hello"":""NewValue""}");
Assert.Equal("NewValue", iNonGenericObjectDictionary["Hello"].ToString());

// Strongly-typed IDictionary<,> case.
Dictionary<string, string> deserialize = JsonSerializer.Parse<Dictionary<string, string>>(@"{""Hello"":""World"", ""Hello"":""NewValue""}");
Assert.Equal("NewValue", deserialize["Hello"]);
Dictionary<string, string> dictionary = JsonSerializer.Parse<Dictionary<string, string>>(@"{""Hello"":""World"", ""Hello"":""NewValue""}");
Assert.Equal("NewValue", dictionary["Hello"]);

deserialize = JsonSerializer.Parse<Dictionary<string, string>>(@"{""Hello"":""World"", ""myKey"" : ""myValue"", ""Hello"":""NewValue""}");
Assert.Equal("NewValue", deserialize["Hello"]);
dictionary = JsonSerializer.Parse<Dictionary<string, string>>(@"{""Hello"":""World"", ""myKey"" : ""myValue"", ""Hello"":""NewValue""}");
Assert.Equal("NewValue", dictionary["Hello"]);

// Weakly-typed IDictionary case.
Dictionary<string, object> deserializeObject = JsonSerializer.Parse<Dictionary<string, object>>(@"{""Hello"":""World"", ""Hello"": null}");
Assert.Null(deserializeObject["Hello"]);
Dictionary<string, object> dictionaryObject = JsonSerializer.Parse<Dictionary<string, object>>(@"{""Hello"":""World"", ""Hello"": null}");
Assert.Null(dictionaryObject["Hello"]);
}

[Fact]
Expand Down
6 changes: 3 additions & 3 deletions src/System.Text.Json/tests/Serialization/ExceptionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ public static void RootThrownFromReaderFails()
}

[Fact]
public static void ThrownFromSerializerFails()
public static void TypeMismatchIDictionaryExceptionThrown()
{
try
{
JsonSerializer.Parse<IDictionary<string, string>>(@"{""Key"":1, ""Key"":2}");
Assert.True(false, "We follow 'Last value wins' approach for duplicate keys.");
JsonSerializer.Parse<IDictionary<string, string>>(@"{""Key"":1}");
Assert.True(false, "Type Mismatch JsonException was not thrown.");
}
catch (JsonException e)
{
Expand Down

0 comments on commit 36678a0

Please sign in to comment.