Skip to content

Commit 0ff7acd

Browse files
committed
testing Assertion class
1 parent 179531c commit 0ff7acd

File tree

3 files changed

+227
-18
lines changed

3 files changed

+227
-18
lines changed

OptimizelySDK.Tests/AssertionTest.cs

Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
/*
2+
* Copyright 2022, Optimizely
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
using Moq;
18+
using NUnit.Framework;
19+
using OptimizelySDK.Entity;
20+
using OptimizelySDK.Logger;
21+
using OptimizelySDK.OptimizelyDecisions;
22+
using OptimizelySDK.OptlyConfig;
23+
using System.Collections.Generic;
24+
using System.Diagnostics.CodeAnalysis;
25+
using System.Linq;
26+
27+
namespace OptimizelySDK.Tests
28+
{
29+
/// <summary>
30+
/// Simplifies assertions and provides more insight into which particular piece is failing for a value.
31+
/// especially helpful for Test Driven Development
32+
/// </summary>
33+
[ExcludeFromCodeCoverage]
34+
public class AssertionTest
35+
{
36+
private Mock<ILogger> LoggerMock;
37+
38+
[SetUp]
39+
public void Initialize()
40+
{
41+
LoggerMock = new Mock<ILogger>();
42+
}
43+
44+
#region Basic asserts
45+
46+
[Test]
47+
public void HasItemsTestNotNullButEqualNumberOfItems()
48+
{
49+
var expected = new string[] { "abc", "def" };
50+
var actual = new string[] { "abc", "ghi" };
51+
52+
Assert.IsTrue(Assertions.HasItems(expected, actual, false));
53+
}
54+
55+
[Test]
56+
public void HasItemsTestBothNullAndAllowNullIsTrue()
57+
{
58+
IEnumerable<object> expected = null;
59+
IEnumerable<object> actual = null;
60+
61+
Assert.IsTrue(Assertions.HasItems(expected, actual, true));
62+
}
63+
64+
[Test]
65+
public void HasItemsTestBothNullAndAllowNullIsFalse()
66+
{
67+
IEnumerable<object> expected = null;
68+
IEnumerable<object> actual = null;
69+
70+
Assert.IsFalse(Assertions.HasItems(expected, actual, false));
71+
}
72+
73+
[Test]
74+
public void AreEquivalentTestBothAreEqual()
75+
{
76+
var expected = new string[] { "abc", "def" };
77+
var actual = new string[] { "abc", "def" };
78+
79+
Assertions.AreEquivalent(expected, actual);
80+
}
81+
82+
[Test]
83+
public void AreEquivalentDictTestBothAreEqual()
84+
{
85+
Dictionary<string, string> expected = new Dictionary<string, string> { { "a", "valA" }, { "b", "valB" } };
86+
Dictionary<string, string> actual = new Dictionary<string, string> { { "a", "valA" }, { "b", "valB" } };
87+
88+
Assertions.AreEquivalent(expected, actual);
89+
}
90+
91+
[Test]
92+
public void AreEqualOptimizelyJSONTest()
93+
{
94+
OptimizelyJSON expected = new OptimizelyJSON("{\"abc\":\"def\"}", null, LoggerMock.Object);
95+
OptimizelyJSON actual = new OptimizelyJSON("{\"abc\":\"def\"}", null, LoggerMock.Object);
96+
Assertions.AreEqual(expected, actual);
97+
}
98+
99+
#endregion Basic asserts
100+
101+
#region
102+
103+
[Test]
104+
public static void AreEqualOptimizelyForcedDecisionTest()
105+
{
106+
var expected = new OptimizelyForcedDecision("varKey");
107+
var actual = new OptimizelyForcedDecision("varKey");
108+
Assertions.AreEqual(expected, actual);
109+
}
110+
111+
#endregion
112+
113+
#region OptimizelyAttribute
114+
115+
[Test]
116+
public void AreEquivalentOptimizelyAttributeTest()
117+
{
118+
var expected = new OptimizelyAttribute[] { new OptimizelyAttribute() { Id = "1", Key = "thisKey" } };
119+
var actual = new OptimizelyAttribute[] { new OptimizelyAttribute() { Id = "1", Key = "thisKey" } };
120+
Assertions.AreEquivalent(expected, actual);
121+
}
122+
123+
[Test]
124+
public void AreEqualOptimizelyAttributeTest()
125+
{
126+
var expected = new OptimizelyAttribute() { Id = "1", Key = "thisKey" };
127+
var actual = new OptimizelyAttribute() { Id = "1", Key = "thisKey" };
128+
Assertions.AreEqual(expected, actual);
129+
}
130+
131+
#endregion OptimizelyAttribute
132+
133+
#region OptimizelyConfig
134+
135+
[Test]
136+
public void AreEqualOptimizelyConfigTest()
137+
{
138+
OptimizelyConfig expected = new OptimizelyConfig("1", "sdkKey", "envKey", new OptimizelyAttribute[0], new OptimizelyAudience[0], new OptimizelyEvent[0],
139+
new Dictionary<string, OptimizelyExperiment>
140+
{
141+
{ "1",
142+
new OptimizelyExperiment("1", "ex1", "", new Dictionary<string, OptimizelyVariation>
143+
{
144+
{ "var1", new OptimizelyVariation("var1", "varKey", false, null) }
145+
})
146+
}
147+
},
148+
new Dictionary<string, OptimizelyFeature>
149+
{
150+
{
151+
"2",
152+
new OptimizelyFeature("2", "feat2", new List<OptimizelyExperiment> {
153+
{ new OptimizelyExperiment("1", "ex1", "", new Dictionary<string, OptimizelyVariation>
154+
{
155+
{ "var1", new OptimizelyVariation("var1", "varKey", false, null) }
156+
})
157+
}
158+
},
159+
new List<OptimizelyExperiment> {
160+
{ new OptimizelyExperiment("1", "ex1", "", new Dictionary<string, OptimizelyVariation>
161+
{
162+
{ "var1", new OptimizelyVariation("var1", "varKey", false, null) }
163+
})
164+
}
165+
}, null, null
166+
)
167+
}
168+
}
169+
);
170+
OptimizelyConfig actual = new OptimizelyConfig("1", "sdkKey", "envKey", new OptimizelyAttribute[0], new OptimizelyAudience[0], new OptimizelyEvent[0],
171+
new Dictionary<string, OptimizelyExperiment>
172+
{
173+
{ "1",
174+
new OptimizelyExperiment("1", "ex1", "", new Dictionary<string, OptimizelyVariation>
175+
{
176+
{ "var1", new OptimizelyVariation("var1", "varKey", false, null) }
177+
})
178+
}
179+
},
180+
new Dictionary<string, OptimizelyFeature>
181+
{
182+
{
183+
"2",
184+
new OptimizelyFeature("2", "feat2", new List<OptimizelyExperiment> {
185+
{ new OptimizelyExperiment("1", "ex1", "", new Dictionary<string, OptimizelyVariation>
186+
{
187+
{ "var1", new OptimizelyVariation("var1", "varKey", false, null) }
188+
})
189+
}
190+
},
191+
new List<OptimizelyExperiment> {
192+
{ new OptimizelyExperiment("1", "ex1", "", new Dictionary<string, OptimizelyVariation>
193+
{
194+
{ "var1", new OptimizelyVariation("var1", "varKey", false, null) }
195+
})
196+
}
197+
}, null, null
198+
)
199+
}
200+
}
201+
);
202+
Assertions.AreEqual(expected, actual);
203+
}
204+
205+
#endregion OptimizelyConfig
206+
}
207+
}

OptimizelySDK.Tests/Assertions.cs

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017, 2019-2021, Optimizely
2+
* Copyright 2021-2022, Optimizely
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,18 +18,15 @@
1818
using OptimizelySDK.Entity;
1919
using OptimizelySDK.OptimizelyDecisions;
2020
using OptimizelySDK.OptlyConfig;
21-
using System;
2221
using System.Collections.Generic;
2322
using System.Diagnostics.CodeAnalysis;
2423
using System.Linq;
25-
using System.Text;
26-
using System.Threading.Tasks;
2724

2825
namespace OptimizelySDK.Tests
2926
{
3027
/// <summary>
3128
/// Simplifies assertions and provides more insight into which particular piece is failing for a value.
32-
/// Especially helpful for Test Driven Development
29+
/// especially helpful for Test Driven Development
3330
/// </summary>
3431
[ExcludeFromCodeCoverage]
3532
public class Assertions
@@ -40,7 +37,7 @@ public static bool HasItems(IEnumerable<object> expected, IEnumerable<object> ac
4037
{
4138
if (allowNull && expected == null && actual == null)
4239
return false;
43-
40+
4441
Assert.AreEqual(expected.Count(), actual.Count());
4542

4643
return true;
@@ -116,20 +113,22 @@ public static void AreEqual(OptimizelyForcedDecision expected, OptimizelyForcedD
116113

117114
public static void AreEquivalent(OptimizelyAttribute[] expected, OptimizelyAttribute[] actual)
118115
{
119-
Assert.AreEqual(expected.Count(), actual.Count());
120-
var zipped = expected.Zip(actual, (e, a) =>
116+
if (HasItems(expected, actual, false))
121117
{
122-
return new
118+
var zipped = expected.Zip(actual, (e, a) =>
123119
{
124-
Expected = e,
125-
Actual = a
126-
};
127-
}).ToList();
120+
return new
121+
{
122+
Expected = e,
123+
Actual = a
124+
};
125+
}).ToList();
128126

129-
foreach (var z in zipped)
130-
{
131-
AreEqual(z.Expected, z.Actual);
132-
};
127+
foreach (var z in zipped)
128+
{
129+
AreEqual(z.Expected, z.Actual);
130+
};
131+
}
133132
}
134133

135134
public static void AreEqual(OptimizelyAttribute expected, OptimizelyAttribute actual)
@@ -722,7 +721,9 @@ public static void AreEqual(Result<Variation> expected, Result<Variation> actual
722721
AreEqual(expected.ResultObject, actual.ResultObject);
723722
}
724723

725-
#endregion Result T
726724
}
725+
726+
#endregion Result T
727+
727728
}
728729
}

OptimizelySDK.Tests/OptimizelySDK.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
<Reference Include="System.Net.Http.WebRequest" />
7171
</ItemGroup>
7272
<ItemGroup>
73+
<Compile Include="AssertionTest.cs" />
7374
<Compile Include="Assertions.cs" />
7475
<Compile Include="AudienceConditionsTests\ConditionEvaluationTest.cs" />
7576
<Compile Include="AudienceConditionsTests\ConditionsTest.cs" />

0 commit comments

Comments
 (0)