TMDbLib/TMDbLibTests/JsonHelpers/TestBase.cs
Michael Bisbjerg 83a9dbf500 Merge in prototype change detector
Detect changes in JSON objects when running tests
2016-05-22 14:10:30 +02:00

110 lines
4.2 KiB
C#

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
namespace TMDbLibTests.JsonHelpers
{
public class TestBase
{
private static readonly Regex NormalizeRegex = new Regex(@"\[[\d]+\]", RegexOptions.Compiled);
private readonly List<ErrorEventArgs> _errors = new List<ErrorEventArgs>();
/// <summary>
/// Run once, on every test
/// </summary>
[TestInitialize]
public virtual void Initiator()
{
_errors.Clear();
JsonConvert.DefaultSettings = () =>
{
JsonSerializerSettings sett = new JsonSerializerSettings();
sett.MissingMemberHandling = MissingMemberHandling.Error;
sett.ContractResolver = new FailingContractResolver();
sett.Error = Error;
return sett;
};
}
private void Error(object sender, ErrorEventArgs errorEventArgs)
{
_errors.Add(errorEventArgs);
errorEventArgs.ErrorContext.Handled = true;
}
/// <summary>
/// Run once, on every test
/// </summary>
[TestCleanup]
public virtual void Closer()
{
if (_errors.Any())
{
// Sort the errors
// Also deduplicate them, as there is no point in blasting us with multiple instances of the "same" error
Dictionary<string, ErrorEventArgs> missingFieldInCSharp = new Dictionary<string, ErrorEventArgs>();
Dictionary<string, ErrorEventArgs> missingPropertyInJson = new Dictionary<string, ErrorEventArgs>();
Dictionary<string, ErrorEventArgs> other = new Dictionary<string, ErrorEventArgs>();
foreach (ErrorEventArgs error in _errors)
{
string key = error.ErrorContext.Path + " / " + error.ErrorContext.Member;
string errorMessage = error.ErrorContext.Error.Message;
key = NormalizeRegex.Replace(key, "[array]");
if (errorMessage.StartsWith("Could not find member"))
{
// Field in JSON is missing in C#
if (!missingFieldInCSharp.ContainsKey(key))
missingFieldInCSharp.Add(key, error);
}
else if (errorMessage.StartsWith("Required property"))
{
// Field in C# is missing in JSON
if (!missingPropertyInJson.ContainsKey(key))
missingPropertyInJson.Add(key, error);
}
else
{
if (!other.ContainsKey(key))
other.Add(key, error);
}
}
// Combine all errors into a nice text
StringBuilder sb = new StringBuilder();
if (missingFieldInCSharp.Any())
{
sb.AppendLine("Fields missing in C# (Present in JSON)");
foreach (KeyValuePair<string, ErrorEventArgs> pair in missingFieldInCSharp)
sb.AppendLine($"{pair.Key}: {pair.Value.ErrorContext.Error.Message}");
}
if (missingPropertyInJson.Any())
{
sb.AppendLine("Fields missing in JSON (Present in C#)");
foreach (KeyValuePair<string, ErrorEventArgs> pair in missingPropertyInJson)
sb.AppendLine($"{pair.Key}: {pair.Value.ErrorContext.Error.Message}");
}
if (other.Any())
{
sb.AppendLine("Other errors");
foreach (KeyValuePair<string, ErrorEventArgs> pair in other)
sb.AppendLine($"{pair.Key}: {pair.Value.ErrorContext.Error.Message}");
}
Assert.Inconclusive(sb.ToString());
}
}
}
}