public void ContainsValueTestCaseSensitiveToAndFromJson(string valueToSearch, bool expectedResult) { var rule = new ContainsValueRule <string> { EqualityComparerPropertyName = "Ordinal", EqualityComparerClassName = "System.StringComparer", CollectionToSearch = { "one", "two", "three", "four", "five", "six" } }; // convert to Json var ruleJson = JsonConvert.SerializeObject(rule, Formatting.Indented, new JsonConverterForRule()); _testOutputHelper.WriteLine($"{nameof(ruleJson)}:{Environment.NewLine}{ruleJson}"); // re-hydrate from json var ruleFromJson = JsonConvert.DeserializeObject <ContainsValueRule <string> >(ruleJson, new JsonConverterForRule()); var compileResult = ruleFromJson.Compile(); compileResult.Should().BeTrue(); _testOutputHelper.WriteLine($"{nameof(ruleFromJson)}:{Environment.NewLine}" + $"{ruleFromJson.ExpressionDebugView()}"); var containsValue = ruleFromJson.ContainsValue(valueToSearch); _testOutputHelper.WriteLine($"expected: {expectedResult} - actual: {containsValue}"); containsValue.Should().Be(expectedResult); }
public void CreateComparerOnTheFlyUsingReflectionToAndFromJson() { var containsRule = new ContainsValueRule <string> { EqualityComparerClassName = "System.StringComparer", EqualityComparerPropertyName = "OrdinalIgnoreCase", CollectionToSearch = { "one", "two", "three", "four", "five", "six" } }; var converter = new JsonConverterForRule(); // convert to json var json = JsonConvert.SerializeObject(containsRule, Formatting.Indented, converter); _testOutputHelper.WriteLine($"rule in json:{Environment.NewLine}{json}"); // bring back from json var ruleFromJson = JsonConvert.DeserializeObject <ContainsValueRule <string> >(json, converter); var compileResult = ruleFromJson.Compile(); compileResult.Should().BeTrue(); var a1 = ruleFromJson.ContainsValue("One"); a1.Should().BeTrue(); var a2 = ruleFromJson.ContainsValue("tWo"); a2.Should().BeTrue(); var a7 = ruleFromJson.ContainsValue("seven"); a7.Should().BeFalse(); }
public void CreateComparerOnTheFlyUsingReflection() { var containsRule = new ContainsValueRule <string> { EqualityComparerClassName = "System.StringComparer", EqualityComparerPropertyName = "OrdinalIgnoreCase", CollectionToSearch = { "one", "two", "three", "four", "five", "six" } }; var compileResult = containsRule.Compile(); compileResult.Should().BeTrue(); _testOutputHelper.WriteLine($"{nameof(containsRule)}:{Environment.NewLine}" + $"{containsRule.ExpressionDebugView()}"); var a1 = containsRule.ContainsValue("One"); a1.Should().BeTrue(); var a2 = containsRule.ContainsValue("tWo"); a2.Should().BeTrue(); var a7 = containsRule.ContainsValue("seven"); a7.Should().BeFalse(); }
public static ContainsValueRule <T> CreateContainsValueRule <T>(IList <T> collectionToSearch, string equalityComparerClassName, string equalityComparerPropertyName) { var rule = new ContainsValueRule <T> { EqualityComparerClassName = equalityComparerClassName, EqualityComparerPropertyName = equalityComparerPropertyName }; if (collectionToSearch != null) { rule.CollectionToSearch.AddRange(collectionToSearch); } return(rule); }
public void ContainsValueTestForIntCollection(int valueToSearch, bool expectedResult) { var containsRule = new ContainsValueRule <int> { EqualityComparer = null, CollectionToSearch = { 1, 2, 3, 4, 5, 6 } }; var compileResult = containsRule.Compile(); compileResult.Should().BeTrue(); _testOutputHelper.WriteLine($"{nameof(containsRule)}:{Environment.NewLine}" + $"{containsRule.ExpressionDebugView()}"); var containsValue = containsRule.ContainsValue(valueToSearch); _testOutputHelper.WriteLine($"expected: {expectedResult} - actual: {containsValue}"); containsValue.Should().Be(expectedResult); }
public void ContainsValueTestCaseSensitive(string valueToSearch, bool expectedResult) { var containsRule = new ContainsValueRule <string> { EqualityComparer = StringComparer.Ordinal, CollectionToSearch = { "one", "two", "three", "four", "five", "six" } }; var compileResult = containsRule.Compile(); compileResult.Should().BeTrue(); _testOutputHelper.WriteLine($"{nameof(containsRule)}:{Environment.NewLine}" + $"{containsRule.ExpressionDebugView()}"); var containsValue = containsRule.ContainsValue(valueToSearch); _testOutputHelper.WriteLine($"expected: {expectedResult} - actual: {containsValue}"); containsValue.Should().Be(expectedResult); }
public void ContainsValueTestForIntCollectionToAndFromJson(int valueToSearch, bool expectedResult) { var rule = new ContainsValueRule <int> { EqualityComparer = null, CollectionToSearch = { 1, 2, 3, 4, 5, 6 } }; // convert to Json var ruleJson = JsonConvert.SerializeObject(rule, Formatting.Indented, new JsonConverterForRule()); _testOutputHelper.WriteLine($"{nameof(ruleJson)}:{Environment.NewLine}{ruleJson}"); // re-hydrate from json var ruleFromJson = JsonConvert.DeserializeObject <ContainsValueRule <int> >(ruleJson, new JsonConverterForRule()); var compileResult = ruleFromJson.Compile(); compileResult.Should().BeTrue(); var containsValue = ruleFromJson.ContainsValue(valueToSearch); _testOutputHelper.WriteLine($"expected: {expectedResult} - actual: {containsValue}"); containsValue.Should().Be(expectedResult); }