Exemplo n.º 1
0
        public void Because_uncalled_does_not_create_any_rule_messages()
        {
            var alwaysTrue = MakeRule.That(() => true).OrCreateArgumentException();

            var actualException = alwaysTrue.Item3(typeof(string), new List <string>(), "", "value");

            var builtInException = new ArgumentException(message: "", paramName: "value");

            alwaysTrue.Item2.Should().BeEmpty();
            actualException.Message.Should().Be(builtInException.Message);
        }
        public void Must2_adds_supplied_rule_to_rule_set()
        {
            var arg1 = null as object;
            var arg2 = null as object;

            var intialRuleSet = arg1.Named(nameof(arg1)).And(arg2.Named(nameof(arg2))).Must();
            var alwaysBeTrue  = MakeRule.That(() => true).OrCreateArgumentException();
            var newRuleSet    = arg1.Named(nameof(arg1)).And(arg2.Named(nameof(arg2))).Must(alwaysBeTrue);

            intialRuleSet.Item2.Should().BeEmpty();
            newRuleSet.Item2.Should().HaveCount(1);
            newRuleSet.Item2.Should().Contain(alwaysBeTrue);
        }
Exemplo n.º 3
0
        public void OrCreate6_makes_rule_that_calls_original_exception()
        {
            var alwaysTrue        = MakeRule.That(() => true);
            var expectedException = new Exception();
            var expectedMessages  = new List <string>();

            var rule = alwaysTrue.OrCreate(expectedException);

            var actualException = rule.Item3(typeof(object), expectedMessages, null, "value");

            actualException.Should().Be(expectedException);
            actualException.InnerException.Should().BeNull();
        }
Exemplo n.º 4
0
        public void OrCreateArgumentException_creates_expected_exception()
        {
            var alwaysTrue = MakeRule.That(() => true).OrCreateArgumentException();

            var actualException = alwaysTrue.Item3(typeof(object), new List <string> {
                "Test 1", "Test 2"
            }, null, "value");

            actualException.Should().BeOfType <ArgumentException>();
            (actualException as ArgumentException).ParamName.Should().Be("value");
            actualException.InnerException.Should().BeNull();
            actualException.Message.Should().Contain("Test 1");
            actualException.Message.Should().Contain("Test 2");
        }
        public void And_adds_supplied_rule_to_rule_set()
        {
            var arg1 = null as object;
            var arg2 = null as object;
            var arg3 = null as object;

            var intialRuleSet = new { arg1, arg2, arg3 }.Must();
            var alwaysBeTrue = MakeRule.That(() => true).OrCreateArgumentException();
            var newRuleSet   = intialRuleSet.And(alwaysBeTrue);

            intialRuleSet.Item2.Should().BeEmpty();
            newRuleSet.Item2.Should().HaveCount(1);
            newRuleSet.Item2.Should().Contain(alwaysBeTrue);
        }
Exemplo n.º 6
0
        public void OrCreateArgumentNullException_creates_expected_exception()
        {
            var alwaysTrue = MakeRule.That(() => true).OrCreateArgumentNullException();

            var actualException = alwaysTrue.Item3(typeof(object), new List <string> {
                "First Test", "Second Test", "Third Test"
            }, new object(), "argument");

            actualException.Should().BeOfType <ArgumentNullException>();
            (actualException as ArgumentNullException).ParamName.Should().Be("argument");
            actualException.InnerException.Should().BeNull();
            actualException.Message.Should().Contain("First Test");
            actualException.Message.Should().Contain("Second Test");
            actualException.Message.Should().Contain("Third Test");
        }
Exemplo n.º 7
0
        public void Because_adds_message_to_rule_and_exception()
        {
            var alwaysTrue = MakeRule.That(() => true).OrCreateArgumentException()
                             .Because("Because_adds_message_to_rule_and_exception-1")
                             .Because("Because_adds_message_to_rule_and_exception-2");

            var actualException = alwaysTrue.Item3(typeof(string), alwaysTrue.Item2, "", "value");

            alwaysTrue.Item2.Should()
            .ContainInOrder(
                "Because_adds_message_to_rule_and_exception-1",
                "Because_adds_message_to_rule_and_exception-2");

            actualException.Message.Should().Contain("Because_adds_message_to_rule_and_exception-1");
            actualException.Message.Should().Contain("Because_adds_message_to_rule_and_exception-2");
        }
Exemplo n.º 8
0
        public void OrCreateArgumentOutOfRangeException_creates_expected_exception()
        {
            var alwaysTrue = MakeRule.That(() => true).OrCreateArgumentOutOfRangeException();

            var actualException = alwaysTrue.Item3(typeof(int), new List <string> {
                "The reason"
            }, 50, "test");

            actualException.Should().BeOfType <ArgumentOutOfRangeException>();
            var argumentOutOfRangeException = actualException as ArgumentOutOfRangeException;

            argumentOutOfRangeException.ActualValue.Should().Be(50);
            argumentOutOfRangeException.ParamName.Should().Be("test");
            actualException.InnerException.Should().BeNull();
            actualException.Message.Should().Contain("The reason");
        }
        public void And_can_be_chained()
        {
            var arg1 = null as object;
            var arg2 = null as object;
            var arg3 = null as object;

            var intialRuleSet = new { arg1, arg2, arg3 }.Must();
            var alwaysBeFalse = MakeRule.That(() => false).OrCreateArgumentException();
            var alwaysBeTrue  = MakeRule.That(() => true).OrCreateArgumentException();
            var newRuleSet    = intialRuleSet.And(alwaysBeFalse).And(alwaysBeTrue);

            intialRuleSet.Item2.Should().BeEmpty();
            newRuleSet.Item2.Should().HaveCount(2);
            newRuleSet.Item2.Should().Contain(alwaysBeFalse);
            newRuleSet.Item2.Should().Contain(alwaysBeTrue);
        }
Exemplo n.º 10
0
        public void OrCreate_throws_on_null_exception_argument()
        {
            var alwaysPass = MakeRule.That(() => true);

            Assert.Throws <ArgumentNullException>(() =>
                                                  alwaysPass.OrCreate(null as Func <Type, IEnumerable <string>, object, string, Exception>));
            Assert.Throws <ArgumentNullException>(() =>
                                                  alwaysPass.OrCreate(null as Func <IEnumerable <string>, object, string, Exception>));
            Assert.Throws <ArgumentNullException>(() =>
                                                  alwaysPass.OrCreate(null as Func <object, string, Exception>));
            Assert.Throws <ArgumentNullException>(() =>
                                                  alwaysPass.OrCreate(null as Func <string, Exception>));
            Assert.Throws <ArgumentNullException>(() =>
                                                  alwaysPass.OrCreate(null as Func <Exception>));
            Assert.Throws <ArgumentNullException>(() =>
                                                  alwaysPass.OrCreate(null as Exception));
        }
Exemplo n.º 11
0
        public void OrCreate_makes_rule_that_does_not_call_original_rule_when_type_does_not_match()
        {
            var actualArgument   = 0.0;
            var expectedArgument = 5;
            var times            = 0;
            var rule             = MakeRule.That <double>(
                v =>
            {
                actualArgument = v;
                times++;
                return(true);
            }).OrCreate(new Exception("Test should not create this exception."));

            rule.Item1(typeof(int), expectedArgument);
            times.Should().Be(0);
            actualArgument.Should().Be(0.0);
        }
Exemplo n.º 12
0
        public void OrCreate_makes_rule_that_calls_original_rule_when_type_matches()
        {
            int?actualArgument   = null;
            int?expectedArgument = 5;
            var times            = 0;
            var rule             = MakeRule.That <int?>(
                v =>
            {
                actualArgument = v;
                times++;
                return(true);
            }).OrCreate(new Exception("Test should not create this exception."));

            rule.Item1(typeof(int?), expectedArgument);
            times.Should().Be(1);
            actualArgument.Should().Be(expectedArgument);
        }
Exemplo n.º 13
0
        public void OrCreate5_makes_rule_that_calls_original_exception()
        {
            var alwaysTrue        = MakeRule.That(() => true);
            var expectedException = new Exception();
            var expectedMessages  = new List <string> {
                "First Test", "Second Test", "Third Test"
            };

            var rule = alwaysTrue.OrCreate(() => expectedException);

            var actualException = rule.Item3(typeof(int), expectedMessages, 0, "arg");

            actualException.Should().Be(expectedException);
            actualException.InnerException.Should().NotBeNull();
            actualException.InnerException.Should().BeOfType <ArgumentException>();
            actualException.InnerException.Message.Should().Contain(expectedMessages.First());
            actualException.InnerException.Message.Should().Contain(expectedMessages.Skip(1).First());
            actualException.InnerException.Message.Should().Contain(expectedMessages.Skip(2).First());
            (actualException.InnerException as ArgumentException).ParamName.Should().Be("arg");
        }
        public void Because_appends_message_to_all_rules()
        {
            var arg1 = null as object;

            var alwaysBeFalse1 = MakeRule.That(() => false).OrCreateArgumentException().Because("false1");
            var alwaysBeFalse2 = MakeRule.That(() => false).OrCreateArgumentException().Because("false2");

            var initialValidationPlan = new { arg1 }.Must(alwaysBeFalse1, alwaysBeFalse2);
            var newValidationPlan = initialValidationPlan.Because("Shared Message");
            var initialRules      = initialValidationPlan.Item2;
            var newRules = newValidationPlan.Item2;

            initialRules.Should().HaveCount(2);
            initialRules.First().Item2.Should().HaveCount(1).And.Contain("false1");
            initialRules.Skip(1).First().Item2.Should().HaveCount(1).And.Contain("false2");

            newRules.Should().HaveCount(2);
            newRules.First().Item2.Should().HaveCount(2).And.Contain("false1").And.Contain("Shared Message");
            newRules.Skip(1).First().Item2.Should().HaveCount(2).And.Contain("false2").And.Contain("Shared Message");
        }
Exemplo n.º 15
0
 public void That_throws_on_null_arguments()
 {
     Assert.Throws <ArgumentNullException>(() => MakeRule.That(null as Func <object, bool>));
     Assert.Throws <ArgumentNullException>(() => MakeRule.That(null as Func <bool>));
 }