-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathconstraints.cake
157 lines (128 loc) · 4.33 KB
/
constraints.cake
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// ***********************************************************************
// Copyright (c) Charlie Poole and contributors.
// Licensed under the MIT License. See LICENSE.txt in root directory.
// ***********************************************************************
public abstract class Constraint<TActual>
{
public abstract bool Matches(TActual actual);
public string Message { get; protected set; }
internal static string VAL<T>(T arg) => arg is string ? $"\"{arg}\"" : arg.ToString();
}
public class EqualConstraint<TActual> : Constraint<TActual>
{
// Expected and actual types must match, possibly through conversion
private TActual _expected;
public EqualConstraint(TActual expected)
{
_expected = expected;
}
public override bool Matches(TActual actual)
{
if (_expected.Equals(actual))
return true;
Message = $"Expected: {VAL(_expected)} But was: {VAL(actual)}";
return false;
}
}
public class GreaterThanConstraint<TActual> : Constraint<TActual> where TActual : IComparable
{
// Expected and actual types must match, possibly through conversion
private TActual _expected;
public GreaterThanConstraint(TActual expected)
{
_expected = expected;
}
public override bool Matches(TActual actual)
{
if (actual.CompareTo(_expected) > 0)
return true;
Message = $"Expected: value > {VAL(_expected)} But was: {VAL(actual)}";
return false;
}
}
public class XmlElementConstraint : Constraint<XmlNode>
{
private string _name;
private int _expectedCount;
public XmlElementConstraint(string name, int expectedCount = -1)
{
_name = name;
_expectedCount = expectedCount;
}
public override bool Matches(XmlNode actual)
{
XmlNodeList elements = actual.SelectNodes(_name);
if (_expectedCount < 0) // No count specified
{
if (elements.Count == 0)
{
Message = $"Expected element <{_name}> was not found.";
return false;
}
Message = $"Element <{actual.Name}> contains an element <{_name}>";
}
else // Count was specified
{
if (elements.Count != _expectedCount)
{
Message = $"Expected {_expectedCount} <{_name}> elements but found {elements.Count}.";
return false;
}
Message = $"Element {actual.Name} has exactly {elements.Count} {_name} elements.";
}
return true;
}
}
public class XmlAttributeConstraint : Constraint<XmlNode>
{
private string _name;
private string _value;
public XmlAttributeConstraint(string name)
{
_name = name;
}
public override bool Matches(XmlNode actual)
{
var attr = actual.Attributes[_name];
if (attr == null)
{
var xml = actual.OuterXml;
int end = xml.IndexOf('>');
if (end > 0) xml = xml.Substring(0, end + 1);
Message = $"Expected: XmlNode with attribute {VAL(_name)}\r\nBut was: {xml}";
return false;
}
if (_value != null && attr.Value != _value)
{
Message = $"Expected: {_name}={VAL(_value)} But was: {VAL(attr.Value)}";
return false;
}
return true;
}
public XmlAttributeConstraint EqualTo(string value)
{
_value = value;
return this;
}
}
public static class Is
{
public static EqualConstraint<T> EqualTo<T>(T expected) => new EqualConstraint<T>(expected);
public static GreaterThanConstraint<T> GreaterThan<T>(T expected) where T : IComparable
{
return new GreaterThanConstraint<T>(expected);
}
}
public static class Has
{
public static XmlElementConstraint Element(string name, int expectedCount = -1) => new XmlElementConstraint(name, expectedCount);
public static XmlAttributeConstraint Attribute(string name) => new XmlAttributeConstraint(name);
public static HasExactly Exactly(int count) => new HasExactly(count);
public static HasExactly One => new HasExactly(1);
public class HasExactly
{
private int _count;
public HasExactly(int count) { _count = count; }
public XmlElementConstraint Element(string name) => new XmlElementConstraint(name, _count);
}
}