Example #1
0
        public void Continuation_rules_do_not_start_setup_tasks_when_precondition_fails()
        {
            // TODO: (Continuation_rules_do_not_start_setup_tasks_when_precondition_fails)
            var setupStarted  = false;
            var hasAwesomeTag = Validate.Async <string>(
                setup: url =>
            {
                setupStarted = true;
                return(Task <string> .Factory.StartNew(() => "<div>not awesome</div>"));
            },
                validate: html => html.As("page").Contains("<awesome>"))
                                .WithErrorMessage("{page} is missing the <awesome> tag!");

            var plan = new ValidationPlan <string>
            {
                isValidHttpUrl,
                hasAwesomeTag.When(isValidHttpUrl)
            };

            var task = plan.ExecuteAsync("hhttp://bing.com");

            task.Wait();
            Console.WriteLine(task.Result);
            Assert.That(setupStarted, Is.False);
        }
        public void Iterative_nested_tasks_can_be_executed_with_short_circuiting()
        {
            var innerRule = Validate.That <Individual>(i => false);
            var plan      = new ValidationPlan <Species>();

            plan.AddRule(s => s.Individuals.All(innerRule.Check));
            var species = new Species();

            species.Individuals.Add(new Individual());
            species.Individuals.Add(new Individual());
            species.Individuals.Add(new Individual());
            species.Individuals.Add(new Individual());

            var task   = plan.ExecuteAsync(species);
            var report = task.Result;

            Assert.That(report.Failures.Count(f => f.Target is Individual), Is.EqualTo(1));
        }
        public void Iterative_nested_tasks_are_all_executed()
        {
            var innerRule = Validate.That <Individual>(i => false);
            var plan      = new ValidationPlan <Species>();

            plan.AddRule(s => s.Individuals.Every(innerRule.Check));
            var species = new Species();

            species.Individuals.Add(new Individual());
            species.Individuals.Add(new Individual());
            species.Individuals.Add(new Individual());
            species.Individuals.Add(new Individual());

            var task   = plan.ExecuteAsync(species);
            var report = task.Result;

            Assert.AreEqual(4, report.Failures.Where(f => f.Target is Individual).Count());
        }
        public void ValidationPlan_Task_executes_multiple_rules_and_ValidationFailure_count_is_correct()
        {
            var plan  = new ValidationPlan <Species>();
            var rule1 = Validate.That <Species>(s => s.Name != "cat");
            var rule2 = Validate.That <Species>(s => s.Genus.Name != "felis");

            plan.AddRule(rule1);
            plan.AddRule(rule2);
            var cat = new Species {
                Name = "cat", Genus = new Genus {
                    Name = "felis"
                }
            };

            var task   = plan.ExecuteAsync(cat);
            var report = task.Result;

            Assert.AreEqual(2, report.Failures.Count());
        }
        public async Task ValidationPlan_Task_executes_all_rules()
        {
            var rule1WasCalled = false;
            var rule1          = Validate.That <Species>(_ => rule1WasCalled = true);
            var rule2WasCalled = false;
            var rule2          = Validate.That <Species>(_ => rule2WasCalled = true);
            var rule3WasCalled = false;
            var rule3          = Validate.That <Species>(_ => rule3WasCalled = true);

            var plan = new ValidationPlan <Species>
            {
                rule1,
                rule2,
                rule3
            };

            await plan.ExecuteAsync(new Species());

            Assert.That(rule1WasCalled);
            Assert.That(rule2WasCalled);
            Assert.That(rule3WasCalled);
        }
Example #6
0
        public void Rules_within_a_ValidationPlan_can_use_APM_signatures_and_run_asynchronously()
        {
            var hasAwesomeTag = Validate.Async <string>(
                setup: url =>
            {
                var request = new ApmOperation <string, string>(s =>
                                                                Task <string> .Factory.StartNew(() => "<div>" + s + "</div>"));
                var tcs = new TaskCompletionSource <string>();
                Task <string> .Factory
                .FromAsync(request.BeginInvoke,
                           request.EndInvoke,
                           url,
                           state: null,
                           creationOptions: TaskCreationOptions.AttachedToParent)
                .ContinueWith(
                    t => tcs.Complete(t, () => t.Result));
                return(tcs.Task);
            },
                validate: html => html.As("page").Contains("<awesome>"))
                                .WithErrorMessage("{page} is missing the <awesome> tag!");

            var plan = new ValidationPlan <string>
            {
                isValidHttpUrl,
                hasAwesomeTag.When(isValidHttpUrl)
            };

            var task = plan.ExecuteAsync("http://google.com");

            task.Wait();
            var result = task.Result;

            Console.WriteLine(result);
            // TODO: (Rules_within_a_ValidationPlan_can_use_APM_signatures_and_run_asynchronously)

            Assert.That(result.Failures.Any(f => f.Message == "http://google.com is missing the <awesome> tag!"));
        }