-
Notifications
You must be signed in to change notification settings - Fork 0
/
TestHelperExtensions.cs
58 lines (52 loc) · 1.95 KB
/
TestHelperExtensions.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
using System;
using LanguageExt;
using Newtonsoft.Json;
using Shouldly;
namespace Liu233w.Compiler.CompilerFramework.Test
{
/// <summary>
/// 存放便于测试使用的 Extensions
/// </summary>
public static class TestHelperExtensions
{
public static string ToJsonString(this object obj, TypeNameHandling handling = TypeNameHandling.All)
{
return JsonConvert.SerializeObject(obj,
new JsonSerializerSettings { TypeNameHandling = handling });
}
public static void ShouldMatchObject(this object obj, object that, TypeNameHandling handling = TypeNameHandling.All)
{
obj.ToJsonString(handling).ShouldBe(that.ToJsonString(handling));
}
// ReSharper disable InconsistentNaming
public static void ShouldBeLeft<L, R>(this Either<L, R> self, Action<L> action)
// ReSharper restore InconsistentNaming
{
self.Match(
Right: _ => throw new ShouldAssertException("Either: Expect Left"),
Left: action
);
}
// ReSharper disable InconsistentNaming
public static void ShouldBeRight<L, R>(this Either<L, R> self, Action<R> action)
// ReSharper restore InconsistentNaming
{
self.Match(
Right: action,
Left: _ => throw new ShouldAssertException("Either: Expect Right")
);
}
// ReSharper disable InconsistentNaming
public static void ShouldMatchRight<L, R>(this Either<L, R> self, R right)
// ReSharper restore InconsistentNaming
{
self.ShouldBeRight(res => res.ShouldMatchObject(right));
}
// ReSharper disable InconsistentNaming
public static void ShouldMatchLeft<L, R>(this Either<L, R> self, L left)
// ReSharper restore InconsistentNaming
{
self.ShouldBeLeft(res => res.ShouldMatchObject(left));
}
}
}