This repository has been archived by the owner on Dec 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 632
/
TestBase.cs
133 lines (112 loc) · 4.12 KB
/
TestBase.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using NUnit.Framework;
namespace ServiceStack.Text.Tests
{
public abstract class TestBase
{
protected TestBase()
{
//Uncomment to run tests under a different Culture
//System.Threading.Thread.CurrentThread.CurrentCulture =
// System.Globalization.CultureInfo.GetCultureInfo("sv-SE");
}
public virtual void Log(string message, params object[] args)
{
#if DEBUG
#endif
if (args.Length > 0)
Console.WriteLine(message, args);
else
Console.WriteLine(message);
}
public T Serialize<T>(T model, bool includeXml = true)
{
return Serialize(model, false, includeXml);
}
public T JsonSerialize<T>(T model)
{
return JsonSerialize(model, false);
}
public T JsonSerializeAndCompare<T>(T model)
{
return JsonSerialize(model, true);
}
public T SerializeAndCompare<T>(T model, bool includeXml = true)
{
return Serialize(model, true, includeXml);
}
private T Serialize<T>(T model, bool assertEqual, bool includeXml)
{
var stopwatch = Stopwatch.StartNew();
var jsv = TypeSerializer.SerializeToString(model);
stopwatch.Stop();
var partialJsv = jsv.Length > 100 ? jsv.Substring(0, 100) + "..." : jsv;
Console.WriteLine("JSV Time: {0} ticks, Len: {1}: {2}", stopwatch.ElapsedTicks, jsv.Length, partialJsv);
stopwatch = Stopwatch.StartNew();
var json = JsonSerializer.SerializeToString(model);
stopwatch.Stop();
var partialJson = json.Length > 100 ? json.Substring(0, 100) + "..." : json;
Console.WriteLine("JSON Time: {0} ticks, Len: {1}: {2}", stopwatch.ElapsedTicks, json.Length, partialJson);
if (includeXml)
{
using (var xmlWriter = new System.IO.StringWriter())
{
stopwatch = Stopwatch.StartNew();
XmlSerializer.SerializeToWriter((object)model, xmlWriter);
var xml = xmlWriter.ToString();
stopwatch.Stop();
var partialXml = xml.Length > 100 ? xml.Substring(0, 100) + "..." : xml;
Console.WriteLine("XML Time: {0} ticks, Len: {1}: {2}", stopwatch.ElapsedTicks, xml.Length, partialXml);
}
}
var fromJsvModel = TypeSerializer.DeserializeFromString<T>(jsv);
var fromJsonModel = JsonSerializer.DeserializeFromString<T>(json);
if (assertEqual)
{
var enumerableModel = model as IEnumerable;
if (enumerableModel != null)
{
Assert.That(fromJsvModel, Is.EquivalentTo(enumerableModel),
string.Format("Deserialized JSV {0} was not equal to the original\n{1}", typeof(T), partialJsv));
Assert.That(fromJsonModel, Is.EquivalentTo(enumerableModel),
string.Format("Deserialized JSON {0} was not equal to the original\n{1}", typeof(T), partialJson));
}
else
{
Assert.That(fromJsvModel, Is.EqualTo(model),
string.Format("Deserialized JSV {0} was not equal to the original\n{1}", typeof(T), partialJsv));
Assert.That(fromJsonModel, Is.EqualTo(model),
string.Format("Deserialized JSON {0} was not equal to the original\n{1}", typeof(T), partialJson));
}
}
return fromJsonModel;
}
private T JsonSerialize<T>(T model, bool assertEqual)
{
var stopwatch = Stopwatch.StartNew();
var json = JsonSerializer.SerializeToString(model);
stopwatch.Stop();
var partialJson = json.Length > 100 ? json.Substring(0, 100) + "..." : json;
Console.WriteLine("JSON Time: {0} ticks, Len: {1}: {2}", stopwatch.ElapsedTicks, json.Length, partialJson);
var fromJsonModel = JsonSerializer.DeserializeFromString<T>(json);
if (assertEqual)
{
var enumerableModel = model as IEnumerable;
if (enumerableModel != null)
{
Assert.That(fromJsonModel, Is.EquivalentTo(enumerableModel),
string.Format("Deserialized JSON {0} was not equal to the original\n{1}", typeof(T), partialJson));
}
else
{
Assert.That(fromJsonModel, Is.EqualTo(model),
string.Format("Deserialized JSON {0} was not equal to the original\n{1}", typeof(T), partialJson));
}
}
return fromJsonModel;
}
}
}