Files
Umbraco-CMS/tests/Umbraco.Tests/Serialization/AutoInterningStringConverterTests.cs
Paul Johnson 00133e880d Move test projects from src/ to tests/ (#11357)
* Update gitignore

* Move csproj

* Update project references

* Update solutions

* Update build scripts

* Tests used to share editorconfig with projects in src

* Fix broken tests.

* Stop copying around .editorconfig

merged root one with linting

* csharp_style_expression_bodied -> suggestion

* Move StyleCop rulesets to matching directories and update shared build properties

* Remove legacy build files, update NuGet.cofig and solution files

* Restore myget source

* Clean up .gitignore

* Update .gitignore

* Move new test classes to tests after merge

* Gitignore + nuget config

* Move new test

Co-authored-by: Ronald Barendse <ronald@barend.se>
2021-10-18 08:14:04 +01:00

68 lines
2.0 KiB
C#

using Newtonsoft.Json;
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
using Umbraco.Core.Serialization;
namespace Umbraco.Tests.Serialization
{
[TestFixture]
public class AutoInterningStringConverterTests
{
[Test]
public void Intern_Property_String()
{
var str1 = "Hello";
var obj = new Test
{
Name = str1 + Guid.NewGuid()
};
// ensure the raw value is not interned
Assert.IsNull(string.IsInterned(obj.Name));
var serialized = JsonConvert.SerializeObject(obj);
obj = JsonConvert.DeserializeObject<Test>(serialized);
Assert.IsNotNull(string.IsInterned(obj.Name));
}
[Test]
public void Intern_Property_Dictionary()
{
var str1 = "key";
var obj = new Test
{
Values = new Dictionary<string, int>
{
[str1 + Guid.NewGuid()] = 0,
[str1 + Guid.NewGuid()] = 1
}
};
// ensure the raw value is not interned
Assert.IsNull(string.IsInterned(obj.Values.Keys.First()));
Assert.IsNull(string.IsInterned(obj.Values.Keys.Last()));
var serialized = JsonConvert.SerializeObject(obj);
obj = JsonConvert.DeserializeObject<Test>(serialized);
Assert.IsNotNull(string.IsInterned(obj.Values.Keys.First()));
Assert.IsNotNull(string.IsInterned(obj.Values.Keys.Last()));
}
public class Test
{
[JsonConverter(typeof(AutoInterningStringConverter))]
public string Name { get; set; }
[JsonConverter(typeof(AutoInterningStringKeyCaseInsensitiveDictionaryConverter<int>))]
public Dictionary<string, int> Values = new Dictionary<string, int>();
}
}
}