|
| 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 | +} |
0 commit comments