Skip to content

Commit f099a75

Browse files
committed
First batch of tests for the new changes
1 parent e9a907d commit f099a75

17 files changed

+334
-209
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using NUnit.Framework.Constraints;
2+
using Snapper.Core;
3+
4+
namespace Snapper.Nunit;
5+
6+
// TODO add more extensions with settings
7+
public class EqualToSnapshotConstraint : Constraint
8+
{
9+
private readonly SnapshotId _snapshotId;
10+
private readonly string _childSnapshotName;
11+
12+
public EqualToSnapshotConstraint(string childSnapshotName = null)
13+
{
14+
_childSnapshotName = childSnapshotName;
15+
}
16+
17+
public override ConstraintResult ApplyTo<TActual>(TActual actual)
18+
{
19+
SnapResult snapResult;
20+
if (_snapshotId != null)
21+
{
22+
var snapper = SnapperFactory.CreateJsonSnapper(null);
23+
snapResult = snapper.MatchSnapshot(actual, _snapshotId);
24+
}
25+
else
26+
{
27+
snapResult = MatchSnapshot(actual);
28+
}
29+
30+
return new NUnitConstraintResult(this, actual, snapResult);
31+
}
32+
33+
private SnapResult MatchSnapshot(object actual)
34+
{
35+
var snapper = SnapperFactory.CreateJsonSnapper(null);
36+
return _childSnapshotName == null
37+
? snapper.MatchSnapshot(actual)
38+
: snapper.MatchSnapshot(actual, _childSnapshotName);
39+
}
40+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using NUnit.Framework.Constraints;
2+
3+
namespace Snapper.Nunit;
4+
5+
public static class EqualToSnapshotConstraintExtensions
6+
{
7+
public static EqualToSnapshotConstraint EqualToSnapshot(this ConstraintExpression expression)
8+
{
9+
var constraint = new EqualToSnapshotConstraint();
10+
expression.Append(constraint);
11+
return constraint;
12+
}
13+
}

project/Snapper.Nunit/Matches.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
namespace Snapper.Nunit;
2+
3+
// TODO add more extensions with settings
4+
public class Matches
5+
{
6+
public static EqualToSnapshotConstraint Snapshot()
7+
{
8+
return new EqualToSnapshotConstraint();
9+
}
10+
11+
public static EqualToSnapshotConstraint ChildSnapshot(string snapshotName)
12+
{
13+
return new EqualToSnapshotConstraint(snapshotName);
14+
}
15+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using System;
2+
using NUnit.Framework.Constraints;
3+
using Snapper.Core;
4+
5+
namespace Snapper.Nunit;
6+
7+
internal class NUnitConstraintResult : ConstraintResult
8+
{
9+
private readonly SnapResult _snapResult;
10+
11+
public NUnitConstraintResult(IConstraint constraint, object actualValue, SnapResult snapResult)
12+
: base(constraint, actualValue)
13+
{
14+
_snapResult = snapResult;
15+
if (snapResult.Status == SnapResultStatus.SnapshotsMatch ||
16+
snapResult.Status == SnapResultStatus.SnapshotUpdated)
17+
{
18+
Status = ConstraintStatus.Success;
19+
}
20+
else
21+
{
22+
Status = ConstraintStatus.Failure;
23+
}
24+
}
25+
26+
public NUnitConstraintResult(IConstraint constraint, object actualValue, ConstraintStatus status)
27+
: base(constraint, actualValue, status)
28+
{
29+
throw new NotSupportedException();
30+
}
31+
32+
public NUnitConstraintResult(IConstraint constraint, object actualValue, bool isSuccess)
33+
: base(constraint, actualValue, isSuccess)
34+
{
35+
throw new NotSupportedException();
36+
}
37+
38+
public override void WriteMessageTo(MessageWriter writer)
39+
{
40+
writer.WriteValue(Messages.GetSnapResultMessage(_snapResult));
41+
}
42+
}

project/Tests/Snapper.Internals.Tests/Core/SnapshotIdResolverTests.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace Snapper.Internals.Tests.Core
99
{
10+
// TODO add more tests here
1011
public class SnapshotIdResolverTests
1112
{
1213
private readonly SnapshotIdResolver _snapshotIdResolver;
@@ -20,7 +21,7 @@ public SnapshotIdResolverTests()
2021
[Fact]
2122
public void ResolveSnapshotId()
2223
{
23-
var snapshotId = _snapshotIdResolver.ResolveSnapshotId(null);
24+
var snapshotId = _snapshotIdResolver.ResolveSnapshotId(null, SnapshotSettings.New());
2425

2526
var filePath = Path.Combine("Snapper.Internals.Tests", "Core",
2627
"_snapshots", $"{nameof(SnapshotIdResolverTests)}_{nameof(ResolveSnapshotId)}.json");
@@ -33,7 +34,7 @@ public void ResolveSnapshotId()
3334
[Fact]
3435
public void ResolveSnapshotId_WithPartialName()
3536
{
36-
var snapshotId = _snapshotIdResolver.ResolveSnapshotId("partialName");
37+
var snapshotId = _snapshotIdResolver.ResolveSnapshotId("partialName", SnapshotSettings.New());
3738

3839
var filePath = Path.Combine("Snapper.Internals.Tests", "Core",
3940
"_snapshots", $"{nameof(SnapshotIdResolverTests)}_{nameof(ResolveSnapshotId_WithPartialName)}.json");
@@ -58,7 +59,7 @@ public SnapshotIdResolverPerClassTests()
5859
[Fact]
5960
public void ResolveSnapshotId()
6061
{
61-
var snapshotId = _snapshotIdResolver.ResolveSnapshotId(null);
62+
var snapshotId = _snapshotIdResolver.ResolveSnapshotId(null, SnapshotSettings.New());
6263

6364
var filePath = Path.Combine("Snapper.Internals.Tests", "Core",
6465
"_snapshots", $"{nameof(SnapshotIdResolverPerClassTests)}.json");
@@ -71,7 +72,7 @@ public void ResolveSnapshotId()
7172
[Fact]
7273
public void ResolveSnapshotId_WithPartialName()
7374
{
74-
var snapshotId = _snapshotIdResolver.ResolveSnapshotId("partialName");
75+
var snapshotId = _snapshotIdResolver.ResolveSnapshotId("partialName", SnapshotSettings.New());
7576

7677
var filePath = Path.Combine("Snapper.Internals.Tests", "Core",
7778
"_snapshots", $"{nameof(SnapshotIdResolverPerClassTests)}.json");

project/Tests/Snapper.Internals.Tests/Json/JsonDiffGeneratorTests.cs

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using System;
22
using System.Text;
33
using FluentAssertions;
4+
using Newtonsoft.Json.Linq;
5+
using Snapper.Core;
46
using Snapper.Json;
57
using Xunit;
68

@@ -11,15 +13,15 @@ public class JsonDiffGeneratorTests
1113
[Fact]
1214
public void ValueChangedTest()
1315
{
14-
var currentSnapshot = new
16+
var currentSnapshot = MakeJsonSnapshot(new
1517
{
1618
Key = "Value"
17-
};
19+
});
1820

19-
var newSnapshot = new
21+
var newSnapshot = MakeJsonSnapshot(new
2022
{
2123
Key = "Value2"
22-
};
24+
});
2325

2426
var result = JsonDiffGenerator.GetDiffMessage(currentSnapshot, newSnapshot);
2527

@@ -35,16 +37,16 @@ public void ValueChangedTest()
3537
[Fact]
3638
public void ValueRemovedTest()
3739
{
38-
var currentSnapshot = new
40+
var currentSnapshot = MakeJsonSnapshot(new
3941
{
4042
Key = "Value",
4143
Key2 = "Value"
42-
};
44+
});
4345

44-
var newSnapshot = new
46+
var newSnapshot = MakeJsonSnapshot(new
4547
{
4648
Key = "Value"
47-
};
49+
});
4850

4951
var result = JsonDiffGenerator.GetDiffMessage(currentSnapshot, newSnapshot);
5052

@@ -61,16 +63,16 @@ public void ValueRemovedTest()
6163
[Fact]
6264
public void ValueAddedTest()
6365
{
64-
var currentSnapshot = new
66+
var currentSnapshot = MakeJsonSnapshot(new
6567
{
6668
Key = "Value"
67-
};
69+
});
6870

69-
var newSnapshot = new
71+
var newSnapshot = MakeJsonSnapshot(new
7072
{
7173
Key = "Value",
7274
Key2 = "Value"
73-
};
75+
});
7476

7577
var result = JsonDiffGenerator.GetDiffMessage(currentSnapshot, newSnapshot);
7678

@@ -87,17 +89,17 @@ public void ValueAddedTest()
8789
[Fact]
8890
public void ValueAddedTest_LargeObject()
8991
{
90-
var currentSnapshot = new
92+
var currentSnapshot = MakeJsonSnapshot(new
9193
{
9294
Key = "Value",
9395
Key1 = "Value",
9496
Key2 = "Value",
9597
Key3 = "Value",
9698
Key4 = "Value",
9799
Key5 = "Value",
98-
};
100+
});
99101

100-
var newSnapshot = new
102+
var newSnapshot = MakeJsonSnapshot(new
101103
{
102104
Key = "Value",
103105
Key1 = "Value",
@@ -106,7 +108,7 @@ public void ValueAddedTest_LargeObject()
106108
NewKey = "Value",
107109
Key4 = "Value",
108110
Key5 = "Value",
109-
};
111+
});
110112

111113
var result = JsonDiffGenerator.GetDiffMessage(currentSnapshot, newSnapshot);
112114

@@ -123,24 +125,24 @@ public void ValueAddedTest_LargeObject()
123125
[Fact]
124126
public void ValueRemovedTest_LargeObject()
125127
{
126-
var currentSnapshot = new
128+
var currentSnapshot = MakeJsonSnapshot(new
127129
{
128130
Key = "Value",
129131
Key1 = "Value",
130132
Key2 = "Value",
131133
Key3 = "Value",
132134
Key4 = "Value",
133135
Key5 = "Value",
134-
};
136+
});
135137

136-
var newSnapshot = new
138+
var newSnapshot = MakeJsonSnapshot(new
137139
{
138140
Key = "Value",
139141
Key1 = "Value",
140142
Key2 = "Value",
141143
Key4 = "Value",
142144
Key5 = "Value",
143-
};
145+
});
144146

145147
var result = JsonDiffGenerator.GetDiffMessage(currentSnapshot, newSnapshot);
146148

@@ -157,25 +159,25 @@ public void ValueRemovedTest_LargeObject()
157159
[Fact]
158160
public void ValueChangedTest_LargeObject()
159161
{
160-
var currentSnapshot = new
162+
var currentSnapshot = MakeJsonSnapshot(new
161163
{
162164
Key = "Value",
163165
Key1 = "Value",
164166
Key2 = "Value",
165167
Key3 = "Value",
166168
Key4 = "Value",
167169
Key5 = "Value",
168-
};
170+
});
169171

170-
var newSnapshot = new
172+
var newSnapshot = MakeJsonSnapshot(new
171173
{
172174
Key = "Value",
173175
Key1 = "Value",
174176
Key2 = "Value",
175177
Key3 = "NewValue",
176178
Key4 = "Value",
177179
Key5 = "Value",
178-
};
180+
});
179181

180182
var result = JsonDiffGenerator.GetDiffMessage(currentSnapshot, newSnapshot);
181183

@@ -200,5 +202,11 @@ private static string DiffMaker(Action<StringBuilder> addLinesFunc)
200202
addLinesFunc.Invoke(diff);
201203
return diff.ToString();
202204
}
205+
206+
private JsonSnapshot MakeJsonSnapshot(object val)
207+
=> new JsonSnapshot(DummySnapshotId(), JObject.FromObject(val));
208+
209+
private static SnapshotId DummySnapshotId()
210+
=> new SnapshotId("dir", "filename", "testname", false);
203211
}
204212
}

0 commit comments

Comments
 (0)