Add unit test to show JsonUtil handling IList<T> types

This commit is contained in:
Steve Sanderson 2018-03-12 10:52:59 +01:00
parent 53ab54d7b1
commit 48ae58196d
1 changed files with 7 additions and 3 deletions

View File

@ -1,6 +1,7 @@
// Copyright (c) .NET Foundation. All rights reserved. // Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Generic;
using Xunit; using Xunit;
namespace Microsoft.AspNetCore.Blazor.Test namespace Microsoft.AspNetCore.Blazor.Test
@ -47,12 +48,13 @@ namespace Microsoft.AspNetCore.Blazor.Test
Id = 1844, Id = 1844,
Name = "Athos", Name = "Athos",
Pets = new[] { "Aramis", "Porthos", "D'Artagnan" }, Pets = new[] { "Aramis", "Porthos", "D'Artagnan" },
Hobby = Hobbies.Swordfighting Hobby = Hobbies.Swordfighting,
Nicknames = new List<string> { "Comte de la Fère", "Armand" }
}; };
// Act/Assert // Act/Assert
Assert.Equal( Assert.Equal(
"{\"Id\":1844,\"Name\":\"Athos\",\"Pets\":[\"Aramis\",\"Porthos\",\"D'Artagnan\"],\"Hobby\":2}", "{\"Id\":1844,\"Name\":\"Athos\",\"Pets\":[\"Aramis\",\"Porthos\",\"D'Artagnan\"],\"Hobby\":2,\"Nicknames\":[\"Comte de la Fère\",\"Armand\"]}",
JsonUtil.Serialize(person)); JsonUtil.Serialize(person));
} }
@ -60,7 +62,7 @@ namespace Microsoft.AspNetCore.Blazor.Test
public void CanDeserializeClassFromJson() public void CanDeserializeClassFromJson()
{ {
// Arrange // Arrange
var json = "{\"Id\":1844,\"Name\":\"Athos\",\"Pets\":[\"Aramis\",\"Porthos\",\"D'Artagnan\"],\"Hobby\":2}"; var json = "{\"Id\":1844,\"Name\":\"Athos\",\"Pets\":[\"Aramis\",\"Porthos\",\"D'Artagnan\"],\"Hobby\":2,\"Nicknames\":[\"Comte de la Fère\",\"Armand\"]}";
// Act // Act
var person = JsonUtil.Deserialize<Person>(json); var person = JsonUtil.Deserialize<Person>(json);
@ -70,6 +72,7 @@ namespace Microsoft.AspNetCore.Blazor.Test
Assert.Equal("Athos", person.Name); Assert.Equal("Athos", person.Name);
Assert.Equal(new[] { "Aramis", "Porthos", "D'Artagnan" }, person.Pets); Assert.Equal(new[] { "Aramis", "Porthos", "D'Artagnan" }, person.Pets);
Assert.Equal(Hobbies.Swordfighting, person.Hobby); Assert.Equal(Hobbies.Swordfighting, person.Hobby);
Assert.Equal(new[] { "Comte de la Fère", "Armand" }, person.Nicknames);
} }
class Person class Person
@ -78,6 +81,7 @@ namespace Microsoft.AspNetCore.Blazor.Test
public string Name { get; set; } public string Name { get; set; }
public string[] Pets { get; set; } public string[] Pets { get; set; }
public Hobbies Hobby { get; set; } public Hobbies Hobby { get; set; }
public IList<string> Nicknames { get; set; }
} }
enum Hobbies { Reading = 1, Swordfighting = 2 } enum Hobbies { Reading = 1, Swordfighting = 2 }