// 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. using System; using System.Collections.Generic; using Xunit; namespace Microsoft.AspNetCore.Mvc.ViewFeatures.Internal { public class TempDataSerializerTest { public static TheoryData InvalidTypes { get { return new TheoryData { { new object(), typeof(object) }, { new object[3], typeof(object) }, { new TestItem(), typeof(TestItem) }, { new List(), typeof(TestItem) }, { new Dictionary(), typeof(TestItem) }, }; } } [Theory] [MemberData(nameof(InvalidTypes))] public void EnsureObjectCanBeSerialized_ThrowsException_OnInvalidType(object value, Type type) { // Arrange var testProvider = new TempDataSerializer(); // Act & Assert var exception = Assert.Throws(() => { testProvider.EnsureObjectCanBeSerialized(value); }); Assert.Equal($"The '{typeof(TempDataSerializer).FullName}' cannot serialize " + $"an object of type '{type}'.", exception.Message); } public static TheoryData InvalidDictionaryTypes { get { return new TheoryData { { new Dictionary(), typeof(int) }, { new Dictionary(), typeof(Uri) }, { new Dictionary(), typeof(object) }, { new Dictionary(), typeof(TestItem) } }; } } [Theory] [MemberData(nameof(InvalidDictionaryTypes))] public void EnsureObjectCanBeSerialized_ThrowsException_OnInvalidDictionaryType(object value, Type type) { // Arrange var testProvider = new TempDataSerializer(); // Act & Assert var exception = Assert.Throws(() => { testProvider.EnsureObjectCanBeSerialized(value); }); Assert.Equal($"The '{typeof(TempDataSerializer).FullName}' cannot serialize a dictionary " + $"with a key of type '{type}'.", exception.Message); } public static TheoryData ValidTypes { get { return new TheoryData { { 10 }, { new int[]{ 10, 20 } }, { "FooValue" }, { new Uri("http://Foo") }, { Guid.NewGuid() }, { new List { "foo", "bar" } }, { new DateTimeOffset() }, { 100.1m }, { new Dictionary() }, { new Uri[] { new Uri("http://Foo"), new Uri("http://Bar") } }, { DayOfWeek.Sunday }, }; } } [Theory] [MemberData(nameof(ValidTypes))] public void EnsureObjectCanBeSerialized_DoesNotThrow_OnValidType(object value) { // Arrange var testProvider = new TempDataSerializer(); // Act & Assert (Does not throw) testProvider.EnsureObjectCanBeSerialized(value); } [Fact] public void DeserializeTempData_ReturnsEmptyDictionary_DataIsEmpty() { // Arrange var serializer = new TempDataSerializer(); // Act var tempDataDictionary = serializer.Deserialize(new byte[0]); // Assert Assert.NotNull(tempDataDictionary); Assert.Empty(tempDataDictionary); } [Fact] public void SerializeAndDeserialize_NullValue_RoundTripsSuccessfully() { // Arrange var key = "NullKey"; var testProvider = new TempDataSerializer(); var input = new Dictionary { { key, null } }; // Act var bytes = testProvider.Serialize(input); var values = testProvider.Deserialize(bytes); // Assert Assert.True(values.ContainsKey(key)); Assert.Null(values[key]); } private class TestItem { public int DummyInt { get; set; } } } }