private Func<BehaviorChain> find(ChainSearch search)
        {
            var candidates = search.FindCandidates(_behaviorGraph);

            var count = candidates.Count();
            switch (count)
            {
                case 1:
                    var chain = candidates.Single();
                    return () => chain;

                case 0:
                    return () =>
                    {
                        throw new FubuException(2104, "No behavior chains are registered matching criteria:  " + search);
                    };

                default:
                    var message = "More than one behavior chain matching criteria:  " + search;
                    message += "\nMatches:";

                    candidates.Each(x =>
                    {
                        message += "\n" + x;
                    });

                    return () =>
                    {
                        throw new FubuException(2108, message);
                    };
            }
        }
        public void find_by_null_category_with_multiple_chains_but_only_one_is_default()
        {
            var search = new ChainSearch
            {
                CategoryMode = CategorySearchMode.Relaxed,
                CategoryOrHttpMethod = null
            };

            var chain1 = new RoutedChain(""){
                UrlCategory ={
                    Category = null
                }
            };

            var chain2 = new RoutedChain("")
            {
                UrlCategory =
                {
                    Category = Categories.DEFAULT
                }
            };

            var chains = new BehaviorChain[] { chain1, chain2};

            search.FindForCategory(chains).Single().ShouldBeTheSameAs(chain2);
        }
        public void find_by_category_when_the_category_is_null_and_relaxed_search_and_only_one_chain()
        {
            var search = new ChainSearch{
                CategoryMode = CategorySearchMode.Relaxed,
                CategoryOrHttpMethod = null
            };

            var chains = new BehaviorChain[]{new BehaviorChain(),};

            search.FindForCategory(chains).Single().ShouldBeTheSameAs(chains.Single());
        }
        public void find_by_category_when_the_category_is_null_and_relaxed_search_and_only_one_chain_2()
        {
            var search = new ChainSearch
            {
                CategoryMode = CategorySearchMode.Relaxed,
                CategoryOrHttpMethod = null
            };

            var chain1 = new RoutedChain("something");
            chain1.UrlCategory.Category = Categories.DEFAULT;

            var chains = new BehaviorChain[] { chain1, };

            search.FindForCategory(chains).Single().ShouldBeTheSameAs(chains.Single());
        }
        public void SetUp()
        {
            theServices = new InMemoryServiceLocator();
            theSearch = ChainSearch.ByUniqueInputType(typeof (object));
            theInput = new object();

            theResolver = MockRepository.GenerateStub<IChainResolver>();
            theUrlResolver = MockRepository.GenerateStub<IChainUrlResolver>();

            theChain = new BehaviorChain();

            theServices.Add(theResolver);
            theServices.Add(theUrlResolver);

            theRequest = new FormRequest(theSearch, theInput);
        }
        public BehaviorChain FindUnique(object model, string category = null)
        {
            if (model == null)
            {
                throw new ArgumentNullException("model");
            }

            var modelType = model.GetType();

            var search = new ChainSearch
            {
                Type = modelType, TypeMode = TypeSearchMode.InputModelOnly, CategoryOrHttpMethod = category
            };

            return Find(search);
        }
        public BehaviorChain Find(Type handlerType, MethodInfo method, string category = null)
        {
            var search = new ChainSearch{
                Type = handlerType,
                TypeMode = TypeSearchMode.HandlerOnly,
                MethodName = method == null ? null : method.Name,
                CategoryOrHttpMethod = category
            };

            if (method == null)
            {
                search.TypeMode = TypeSearchMode.Any;
            }

            return Find(search);
        }
        public void find_by_category_relaxed_with_only_one_chain()
        {
            var search = new ChainSearch
            {
                CategoryMode = CategorySearchMode.Relaxed,
                CategoryOrHttpMethod = "something"
            };

            var chain3 = new RoutedChain("")
            {
                UrlCategory =
                {
                    Category = null
                }
            };

            var chains = new BehaviorChain[] { chain3 };

            search.FindForCategory(chains).ShouldHaveTheSameElementsAs(chain3);
        }
        public void find_by_category_strict_with_multiple_chains_1()
        {
            var search = new ChainSearch
            {
                CategoryMode = CategorySearchMode.Strict,
                CategoryOrHttpMethod = "something"
            };

            var chain1 = new RoutedChain("")
            {
                UrlCategory =
                {
                    Category = "something"
                }
            };

            var chain2 = new RoutedChain("")
            {
                UrlCategory =
                {
                    Category = Categories.DEFAULT
                }
            };

            var chain3 = new RoutedChain("")
            {
                UrlCategory =
                {
                    Category = null
                }
            };

            var chains = new BehaviorChain[] { chain1, chain2, chain3 };

            search.FindForCategory(chains).ShouldHaveTheSameElementsAs(chain1);
        }
        public void find_by_any_looks_at_resource_model_first_then_handler_type_second()
        {
            var candidates = new ChainSearch
            {
                TypeMode = TypeSearchMode.ResourceModelOnly,
                Type = typeof(SimpleOutputModel)
            }.FindCandidatesByType(theGraph);

            candidates.First().Select(x => x.FirstCall().Description)
                .ShouldHaveTheSameElementsAs(
                "OneController.Report() : SimpleOutputModel",
                "OneController.Query(SimpleInputModel model) : SimpleOutputModel",
                "TwoController.Report() : SimpleOutputModel",
                "TwoController.NotQuery(SimpleInputModel model) : SimpleOutputModel"

                );
        }
        public void find_by_null_category_with_multiple_chains_but_only_one_is_default_3()
        {
            var search = new ChainSearch
            {
                CategoryMode = CategorySearchMode.Relaxed,
                CategoryOrHttpMethod = null
            };

            var chain1 = new RoutedChain("")
            {
                UrlCategory =
                {
                    Category = "something"
                }
            };

            var chain2 = new RoutedChain("")
            {
                UrlCategory =
                {
                    Category = Categories.DEFAULT
                }
            };

            var chain3 = new RoutedChain("")
            {
                UrlCategory =
                {
                    Category = Categories.DEFAULT
                }
            };

            var chains = new BehaviorChain[] { chain1, chain2, chain3 };

            search.FindForCategory(chains).ShouldHaveTheSameElementsAs(chain2, chain3);
        }
        public void find_by_category_strict_with_only_one_chain_that_does_not_match_still_returns_nothing()
        {
            var search = new ChainSearch
            {
                CategoryMode = CategorySearchMode.Strict,
                CategoryOrHttpMethod = "something"
            };

            var chain3 = new RoutedChain("")
            {
                UrlCategory =
                {
                    Category = null
                }
            };

            var chains = new BehaviorChain[] { chain3 };

            search.FindForCategory(chains).Any().ShouldBeFalse();
        }
Exemple #13
0
        public void find_by_method_if_it_exists()
        {
            var candidates = new ChainSearch
            {
                TypeMode = TypeSearchMode.Any,
                Type = typeof(SimpleInputModel),
                MethodName = "DoSomething"
            }.FindCandidatesByType(theGraph);

            candidates.First().Any().ShouldBeFalse();
            candidates.Last().Single().FirstCall().Description.ShouldEqual("SimpleInputModel.DoSomething(InputModel2 model) : void");
        }
Exemple #14
0
 public BehaviorChain Find(ChainSearch search)
 {
     return(_results[search]());
 }
        private Func<BehaviorChain> find(ChainSearch search)
        {
            var candidates = search.FindCandidates(_behaviorGraph);

            var count = candidates.Count();
            switch (count)
            {
                case 1:
                    var chain = candidates.Single();
                    return () => chain;

                case 0:
                    return () =>
                    {
                        throw new FubuException(2104, "No behavior chains are registered matching criteria:  " + search);
                    };

                default:
                    var message = "More than one behavior chain matching criteria:  " + search;
                    message += "\nMatches:";

                    candidates.Each(x =>
                    {
                        // TODO -- BehaviorChain needs a Description or a better ToString()

                        var description = "\n";
                        if (x.Route != null)
                        {
                            description += x.Route.Pattern + "  ";
                        }

                        if (x.FirstCall() != null)
                        {
                            description += " -- " + x.FirstCall().Description;
                        }

                        message += description;
                    });

                    return () =>
                    {
                        throw new FubuException(2108, message);
                    };
            }
        }
Exemple #16
0
 public BehaviorChain Find(Type handlerType, MethodInfo method, string category = null)
 {
     return(Find(ChainSearch.ForMethod(handlerType, method, category)));
 }
        public void find_by_method_if_it_exists_2()
        {
            var candidates = new ChainSearch
            {
                TypeMode = TypeSearchMode.Any,
                Type = typeof(SimpleInputModel),
                MethodName = "Query"
            }.FindCandidatesByType(theGraph).SelectMany(x => x);

            candidates.Select(x => x.FirstCall().Description)
                .ShouldHaveTheSameElementsAs("OneController.Query(SimpleInputModel model) : SimpleOutputModel");
        }
        public void find_by_input_model_only()
        {
            var chainSearch = new ChainSearch
                              {
                                  TypeMode = TypeSearchMode.InputModelOnly,
                                  Type = typeof(SimpleInputModel)
                              };

            chainSearch.FindCandidatesByType(theGraph).Single().Select(x => x.FirstCall().Description)
            .ShouldHaveTheSameElementsAs("OneController.Query(SimpleInputModel model) : SimpleOutputModel", "TwoController.NotQuery(SimpleInputModel model) : SimpleOutputModel");
        }
Exemple #19
0
 public BehaviorChain FindUniqueByType(Type modelType, string category = null)
 {
     return(Find(ChainSearch.ByUniqueInputType(modelType, category)));
 }
 public FormRequest(ChainSearch search, object input)
 {
     _search = search;
     _input = input;
 }
        public BehaviorChain FindUnique(object model, string category = null)
        {
            if (model == null)
            {
                throw new ArgumentNullException("model");
            }

            var forwarder = FindForwarder(model, category);
            if (forwarder != null)
            {
                return forwarder.FindChain(this, model).Chain;
            }

            var modelType = _typeResolver.ResolveType(model);

            var search = new ChainSearch
            {
                Type = modelType, TypeMode = TypeSearchMode.InputModelOnly, CategoryOrHttpMethod = category
            };

            return Find(search);
        }
        public void find_by_method_if_it_exists()
        {
            var candidates = new ChainSearch
            {
                TypeMode = TypeSearchMode.Any,
                Type = typeof(SimpleInputModel),
                MethodName = "DoSomething"
            }.FindCandidatesByType(theGraph).SelectMany(x => x);

            candidates.Any(x => x.FirstCall().Description == "SimpleInputModel.DoSomething(InputModel2 model) : void")
                .ShouldBeTrue();
        }
 public FormRequest(ChainSearch search, object input) : this(search, input, false) { }
        public void find_candidates_by_type_fall_back_to_handler_type_if_possible()
        {
            var chains = new ChainSearch{
                Type = typeof (SingleActionController),
                TypeMode = TypeSearchMode.Any
            }.FindCandidatesByType(theGraph).SelectMany(x => x);

            chains.Single()
                .FirstCall().Description.ShouldEqual("SingleActionController.DoSomething(InputModel model) : void");
        }
 public FormRequest(ChainSearch search, object input, bool closeTag)
 {
     _search = search;
     _input = input;
     _closeTag = closeTag;
 }
        public void find_by_category_strict_with_multiple_chains_2()
        {
            var search = new ChainSearch
            {
                CategoryMode = CategorySearchMode.Strict,
                CategoryOrHttpMethod = "something"
            };

            var chain1 = new RoutedChain("")
            {
                UrlCategory =
                {
                    Category = "different"
                }
            };

            var chain2 = new RoutedChain("")
            {
                UrlCategory =
                {
                    Category = Categories.DEFAULT
                }
            };

            var chain3 = new RoutedChain("")
            {
                UrlCategory =
                {
                    Category = "else"
                }
            };

            var chains = new BehaviorChain[] { chain1, chain2, chain3 };

            search.FindForCategory(chains).Any().ShouldBeFalse();
        }
Exemple #27
0
        public void find_by_category_strict_with_multiple_chains_by_method()
        {
            var search = new ChainSearch
            {
                CategoryMode = CategorySearchMode.Strict,
                CategoryOrHttpMethod = "POST"
            };

            var chain1 = new BehaviorChain
            {
                Route = new RouteDefinition("whatever"),
                UrlCategory =
                {
                    Category = "something"
                }
            };

            chain1.Route.AllowedHttpMethods.Add("POST");

            var chain2 = new BehaviorChain
            {
                UrlCategory =
                {
                    Category = Categories.DEFAULT
                }
            };

            var chain3 = new BehaviorChain
            {
                UrlCategory =
                {
                    Category = null
                }
            };

            var chains = new BehaviorChain[] { chain1, chain2, chain3 };

            search.FindForCategory(chains).ShouldHaveTheSameElementsAs(chain1);
        }
Exemple #28
0
        public void find_by_null_category_with_multiple_chains_but_none_is_marked_default()
        {
            var search = new ChainSearch
            {
                CategoryMode = CategorySearchMode.Relaxed,
                CategoryOrHttpMethod = null
            };

            var chain1 = new BehaviorChain
            {
                UrlCategory =
                {
                    Category = "something"
                }
            };

            var chain2 = new BehaviorChain
            {
                UrlCategory =
                {
                    Category = "else"
                }
            };

            var chain3 = new BehaviorChain
            {
                UrlCategory =
                {
                    Category = null
                }
            };

            var chains = new BehaviorChain[] { chain1, chain2, chain3 };

            search.FindForCategory(chains).ShouldHaveTheSameElementsAs(chain3);
        }
Exemple #29
0
        public void find_by_any_looks_at_input_model_first_then_handler_type_second()
        {
            var candidates = new ChainSearch{
                TypeMode = TypeSearchMode.Any,
                Type = typeof (SimpleInputModel)
            }.FindCandidatesByType(theGraph);

            candidates.Count().ShouldEqual(2);

            candidates.First().Select(x => x.FirstCall().Description)
                .ShouldHaveTheSameElementsAs("OneController.Query(SimpleInputModel model) : SimpleOutputModel", "TwoController.NotQuery(SimpleInputModel model) : SimpleOutputModel");

            candidates.Last().Single().FirstCall().Description.ShouldEqual("SimpleInputModel.DoSomething(InputModel2 model) : void");
        }
 public BehaviorChain Find(ChainSearch search)
 {
     return _results[search]();
 }
Exemple #31
0
 public bool Equals(ChainSearch other)
 {
     if (ReferenceEquals(null, other)) return false;
     if (ReferenceEquals(this, other)) return true;
     return Equals(other.Type, Type) && Equals(other.CategoryOrHttpMethod, CategoryOrHttpMethod) && Equals(other.CategoryMode, CategoryMode) && Equals(other.TypeMode, TypeMode) && Equals(other.MethodName, MethodName);
 }