Skip to content

Commit d2300fc

Browse files
authored
15 assert method for strings (#28)
close #15 (new: StringConstraintBuilder)
1 parent 351dcf9 commit d2300fc

27 files changed

+584
-51
lines changed

access-add-in/AccUnitLoader.accda

8 KB
Binary file not shown.
5 KB
Binary file not shown.
1.8 KB
Binary file not shown.
5.5 KB
Binary file not shown.
1.8 KB
Binary file not shown.
0 Bytes
Binary file not shown.

source/AccUnit/AccessCodeLib.AccUnit.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@
9393
<Compile Include="Assertions\Constraints\EmptyConstraint.cs" />
9494
<Compile Include="Assertions\Constraints\NullConstraint.cs" />
9595
<Compile Include="Assertions\Constraints\NotConstraint.cs" />
96+
<Compile Include="Assertions\Constraints\StringComparerConstraint.cs" />
9697
<Compile Include="Assertions\Interfaces\AssertionException.cs" />
9798
<Compile Include="Assertions\Interfaces\IAssertion.cs" />
9899
<Compile Include="Assertions\Interfaces\IAssertionsBuilder.cs" />
@@ -102,11 +103,13 @@
102103
<Compile Include="Assertions\Interfaces\IMatchResultCollector.cs" />
103104
<Compile Include="Assertions\Interfaces\IMatchResultCollectorBridge.cs" />
104105
<Compile Include="Assertions\AssertThrowsContainer.cs" />
106+
<Compile Include="Assertions\StringConstraintBuilder.cs" />
105107
<Compile Include="CodeCoverage\CodeModuleTracker.cs" />
106108
<Compile Include="CodeCoverage\ProcedureTracker.cs" />
107109
<Compile Include="Interfaces\ITestItemTag.cs" />
108110
<Compile Include="Interfaces\ITagList.cs" />
109111
<Compile Include="Interfaces\ITestClassMemberList.cs" />
112+
<Compile Include="Interfaces\StringCompareMode.cs" />
110113
<Compile Include="Interop\InteropConverter.cs" />
111114
<Compile Include="~trash\CodeModuleMemberWithMarker.cs" />
112115
<Compile Include="~trash\CompareTypeHelper.cs" />

source/AccUnit/Assertions/ConstraintBuilder.cs

Lines changed: 95 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,51 +4,129 @@
44

55
namespace AccessCodeLib.AccUnit.Assertions
66
{
7-
public class ConstraintBuilder : IConstraintBuilder, IConstraint
7+
public class ConstraintBuilder : ConstraintBuilderBase<object>, IConstraintBuilder, IConstraint
8+
{
9+
public ConstraintBuilder()
10+
{
11+
}
12+
13+
public ConstraintBuilder(bool strict) : base(strict)
14+
{
15+
}
16+
17+
public new IConstraintBuilder EqualTo(object expected)
18+
{
19+
AddComparerConstraint("actual = expected", expected, 0);
20+
return this;
21+
}
22+
23+
public new IConstraintBuilder LessThan(object expected)
24+
{
25+
AddComparerConstraint("actual < expected", expected, -1);
26+
return this;
27+
}
28+
29+
public new IConstraintBuilder LessThanOrEqualTo(object expected)
30+
{
31+
AddComparerConstraint("actual <= expected", expected, -1, 0);
32+
return this;
33+
}
34+
35+
public new IConstraintBuilder GreaterThan(object expected)
36+
{
37+
AddComparerConstraint("actual > expected", expected, +1);
38+
return this;
39+
}
40+
41+
public new IConstraintBuilder GreaterThanOrEqualTo(object expected)
42+
{
43+
AddComparerConstraint("actual >= expected", expected, +1, 0);
44+
return this;
45+
}
46+
47+
public new IConstraintBuilder Null
48+
{
49+
get
50+
{
51+
AddChild(new NullConstraint());
52+
return this;
53+
}
54+
}
55+
56+
public new IConstraintBuilder DBNull
57+
{
58+
get
59+
{
60+
AddChild(new DBNullConstraint());
61+
return this;
62+
}
63+
}
64+
65+
public new IConstraintBuilder Empty
66+
{
67+
get
68+
{
69+
AddChild(new EmptyConstraint());
70+
return this;
71+
}
72+
}
73+
74+
public new IConstraintBuilder Not
75+
{
76+
get
77+
{
78+
AddChild(new NotConstraint());
79+
return this;
80+
}
81+
}
82+
83+
}
84+
85+
public abstract class ConstraintBuilderBase<T> : IConstraintBuilderBase<T>, IConstraint
886
{
987
private IConstraint _firstchild;
1088

1189
private readonly bool _strict = false;
1290

13-
public ConstraintBuilder()
91+
public ConstraintBuilderBase()
1492
{
1593
}
1694

17-
public ConstraintBuilder(bool strict)
95+
public ConstraintBuilderBase(bool strict)
1896
{
1997
_strict = strict;
2098
}
2199

22-
public IConstraintBuilder EqualTo(object expected)
100+
public IConstraintBuilderBase<T> EqualTo(T expected)
23101
{
24102
AddComparerConstraint("actual = expected", expected, 0);
25103
return this;
26104
}
27105

28-
public IConstraintBuilder LessThan(object expected)
106+
public IConstraintBuilderBase<T> LessThan(T expected)
29107
{
30108
AddComparerConstraint("actual < expected", expected, -1);
31109
return this;
32110
}
33111

34-
public IConstraintBuilder LessThanOrEqualTo(object expected)
112+
public IConstraintBuilderBase<T> LessThanOrEqualTo(T expected)
35113
{
36114
AddComparerConstraint("actual <= expected", expected, -1, 0);
37115
return this;
38116
}
39117

40-
public IConstraintBuilder GreaterThan(object expected)
118+
public IConstraintBuilderBase<T> GreaterThan(T expected)
41119
{
42120
AddComparerConstraint("actual > expected", expected, +1);
43121
return this;
44122
}
45-
public IConstraintBuilder GreaterThanOrEqualTo(object expected)
123+
public IConstraintBuilderBase<T> GreaterThanOrEqualTo(T expected)
46124
{
47125
AddComparerConstraint("actual >= expected", expected, +1, 0);
48126
return this;
49127
}
50128

51-
private void AddComparerConstraint(string compareText, object expected, int expectedComparerResult)
129+
protected virtual void AddComparerConstraint(string compareText, object expected, int expectedComparerResult)
52130
{
53131
if (expected is Array expectedArray)
54132
{
@@ -75,7 +153,7 @@ private void AddComparerConstraint(string compareText, object expected, int expe
75153
AddChild((IConstraint)newConstraint);
76154
}
77155

78-
private void AddArrayComparerConstraint(string compareText, Array expected, int expectedComparerResult)
156+
protected virtual void AddArrayComparerConstraint(string compareText, Array expected, int expectedComparerResult)
79157
{
80158
Type T = expected.GetType().GetElementType();
81159
Type myType = typeof(ArrayConstraint<>).MakeGenericType(T);
@@ -112,7 +190,7 @@ private static bool IsNumeric(Type T)
112190
typeof(byte), typeof(uint), typeof(ulong), typeof(ushort), typeof(sbyte)
113191
};
114192

115-
private void AddComparerConstraint(string compareText, object expected, int expectedComparerResult, int expectedComparerResult2)
193+
protected virtual void AddComparerConstraint(string compareText, object expected, int expectedComparerResult, int expectedComparerResult2)
116194
{
117195
if (expected is Array expectedArray)
118196
{
@@ -126,7 +204,7 @@ private void AddComparerConstraint(string compareText, object expected, int expe
126204
AddChild((IConstraint)newConstraint);
127205
}
128206

129-
private void AddArrayComparerConstraint(string compareText, Array expected, int expectedComparerResult, int expectedComparerResult2)
207+
protected virtual void AddArrayComparerConstraint(string compareText, Array expected, int expectedComparerResult, int expectedComparerResult2)
130208
{
131209
//var newConstraint = new ArrayConstraint(compareText, expected, expectedComparerResult, expectedComparerResult2);
132210

@@ -136,7 +214,7 @@ private void AddArrayComparerConstraint(string compareText, Array expected, int
136214
AddChild((IConstraint)newConstraint);
137215
}
138216

139-
public IConstraintBuilder Null
217+
public IConstraintBuilderBase<T> Null
140218
{
141219
get
142220
{
@@ -145,7 +223,7 @@ public IConstraintBuilder Null
145223
}
146224
}
147225

148-
public IConstraintBuilder DBNull
226+
public IConstraintBuilderBase<T> DBNull
149227
{
150228
get
151229
{
@@ -154,7 +232,7 @@ public IConstraintBuilder DBNull
154232
}
155233
}
156234

157-
public IConstraintBuilder Empty
235+
public IConstraintBuilderBase<T> Empty
158236
{
159237
get
160238
{
@@ -163,7 +241,7 @@ public IConstraintBuilder Empty
163241
}
164242
}
165243

166-
public IConstraintBuilder Not
244+
public IConstraintBuilderBase<T> Not
167245
{
168246
get
169247
{
@@ -174,8 +252,7 @@ public IConstraintBuilder Not
174252

175253
IConstraint IConstraint.Child { get; set; }
176254

177-
178-
private void AddChild(IConstraint constraint)
255+
protected void AddChild(IConstraint constraint)
179256
{
180257
if (_firstchild is null)
181258
{

source/AccUnit/Assertions/Constraints/ComparerConstraint.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,4 +110,4 @@ private static string FormattedTypeDescription(Type type, bool strict = false)
110110
return (!strict && type == typeof(double)) ? "numeric type" : type.Name;
111111
}
112112
}
113-
}
113+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
using System;
2+
3+
namespace AccessCodeLib.AccUnit.Assertions.Constraints
4+
{
5+
class StringComparerConstraint : ComparerConstraint<string>
6+
{
7+
readonly StringComparison _compareMethod;
8+
readonly bool _nullIsEqualEmptyString = false;
9+
10+
public StringComparerConstraint(string compareText, string expected, int expectedComparerResult, StringComparison compareMethod = StringComparison.InvariantCulture, bool nullIsEqualEmptyString = false)
11+
: base(compareText, expected, expectedComparerResult, false)
12+
{
13+
_compareMethod = compareMethod;
14+
_nullIsEqualEmptyString = nullIsEqualEmptyString;
15+
}
16+
17+
public StringComparerConstraint(string compareText, string expected, int expectedComparerResult, int expectedComparerResult2, StringComparison compareMethod = StringComparison.InvariantCulture, bool nullIsEqualEmptyString = false)
18+
: base(compareText, expected, expectedComparerResult, expectedComparerResult2, false)
19+
{
20+
_compareMethod = compareMethod;
21+
_nullIsEqualEmptyString = nullIsEqualEmptyString;
22+
}
23+
24+
protected override IMatchResult Compare(object actual)
25+
{
26+
if (actual is null || (_nullIsEqualEmptyString && actual is string s && s == string.Empty))
27+
{
28+
if (Expected == null || _nullIsEqualEmptyString && Expected == string.Empty)
29+
{
30+
if (ExpectedComparerResult == 0 || (UseOr == true && ExpectedComparerResult2 == 0))
31+
{
32+
return new MatchResult(CompareText, true, null, actual, Expected);
33+
}
34+
return new MatchResult(CompareText, false, "actual is vbNullstring, expected is " + (Expected == null ? "vbNullstring" : "Empty"), actual, Expected);
35+
}
36+
37+
return new MatchResult(CompareText, false, "actual is vbNullstring and expected is not vbNullString" + (Expected == string.Empty ? " or Empty" : ""), actual, Expected);
38+
}
39+
40+
if (actual == DBNull.Value)
41+
{
42+
return new MatchResult(CompareText, false, "actual is Null and expected is not Null", actual, Expected);
43+
}
44+
45+
// Check type
46+
var actualType = ConstraintBuilder.Type2Compare(actual, Strict);
47+
var expectedType = typeof(string);
48+
if (actualType != expectedType)
49+
{
50+
var returnText = "actual (" + actual.GetType().Name + ") is not of type String";
51+
return new MatchResult(CompareText, false, returnText, actual, Expected);
52+
}
53+
54+
// Check value
55+
var a = (string)Convert.ChangeType(actual, typeof(string));
56+
var result = string.Compare(a, Expected, _compareMethod);
57+
58+
if (result == ExpectedComparerResult)
59+
{
60+
return new MatchResult(CompareText, true, null, actual, Expected);
61+
}
62+
63+
if (UseOr && (result == ExpectedComparerResult2))
64+
{
65+
return new MatchResult(CompareText, true, null, actual, Expected);
66+
}
67+
68+
string compareInfo;
69+
if (result < 0)
70+
{
71+
compareInfo = "actual is less then expected";
72+
}
73+
else if (result > 0)
74+
{
75+
compareInfo = "actual is greather then expected";
76+
}
77+
else
78+
{
79+
compareInfo = "actual is equal expected";
80+
}
81+
82+
return new MatchResult(CompareText, false, compareInfo, actual, Expected);
83+
}
84+
}
85+
}

0 commit comments

Comments
 (0)