2024-02-14 12:10:45 +01:00
|
|
|
using System.Text.Json;
|
|
|
|
|
using System.Text.Json.Serialization;
|
2022-06-21 08:09:38 +02:00
|
|
|
using NUnit.Framework;
|
2021-10-19 23:11:54 +11:00
|
|
|
using Umbraco.Cms.Infrastructure.Serialization;
|
2021-06-24 09:43:57 -06:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Infrastructure.Serialization;
|
|
|
|
|
|
|
|
|
|
[TestFixture]
|
|
|
|
|
public class AutoInterningStringConverterTests
|
2021-06-24 09:43:57 -06:00
|
|
|
{
|
2022-06-21 08:09:38 +02:00
|
|
|
[Test]
|
|
|
|
|
public void Intern_Property_String()
|
2021-06-24 09:43:57 -06:00
|
|
|
{
|
2022-06-21 08:09:38 +02:00
|
|
|
var str1 = "Hello";
|
|
|
|
|
var obj = new Test { Name = str1 + Guid.NewGuid() };
|
2021-06-24 09:43:57 -06:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
// ensure the raw value is not interned
|
|
|
|
|
Assert.IsNull(string.IsInterned(obj.Name));
|
2021-06-24 09:43:57 -06:00
|
|
|
|
2024-02-14 12:10:45 +01:00
|
|
|
var serialized = JsonSerializer.Serialize(obj);
|
|
|
|
|
obj = JsonSerializer.Deserialize<Test>(serialized);
|
2021-06-24 09:43:57 -06:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
Assert.IsNotNull(string.IsInterned(obj.Name));
|
|
|
|
|
}
|
2021-06-24 09:43:57 -06:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
[Test]
|
|
|
|
|
public void Intern_Property_Dictionary()
|
|
|
|
|
{
|
|
|
|
|
var str1 = "key";
|
|
|
|
|
var obj = new Test
|
2021-06-24 09:43:57 -06:00
|
|
|
{
|
2022-06-21 08:09:38 +02:00
|
|
|
Values = new Dictionary<string, int> { [str1 + Guid.NewGuid()] = 0, [str1 + Guid.NewGuid()] = 1 },
|
|
|
|
|
};
|
2021-06-24 09:43:57 -06:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
// ensure the raw value is not interned
|
|
|
|
|
Assert.IsNull(string.IsInterned(obj.Values.Keys.First()));
|
|
|
|
|
Assert.IsNull(string.IsInterned(obj.Values.Keys.Last()));
|
2021-06-24 09:43:57 -06:00
|
|
|
|
2024-02-14 12:10:45 +01:00
|
|
|
var serialized = JsonSerializer.Serialize(obj);
|
|
|
|
|
obj = JsonSerializer.Deserialize<Test>(serialized);
|
2021-06-24 09:43:57 -06:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
Assert.IsNotNull(string.IsInterned(obj.Values.Keys.First()));
|
|
|
|
|
Assert.IsNotNull(string.IsInterned(obj.Values.Keys.Last()));
|
|
|
|
|
}
|
2021-06-24 09:43:57 -06:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
public class Test
|
|
|
|
|
{
|
|
|
|
|
[JsonConverter(typeof(AutoInterningStringConverter))]
|
|
|
|
|
public string Name { get; set; }
|
2021-06-24 09:43:57 -06:00
|
|
|
|
2022-06-21 08:09:38 +02:00
|
|
|
[JsonConverter(typeof(AutoInterningStringKeyCaseInsensitiveDictionaryConverter<int>))]
|
|
|
|
|
public Dictionary<string, int> Values { get; set; } = new();
|
2021-06-24 09:43:57 -06:00
|
|
|
}
|
|
|
|
|
}
|