예제 #1
0
        public void GetRouteData_WithNonMatchingSourceRoute_ReturnsNull()
        {
            // Arrange
            var sourceRouteMock = new Mock <RouteBase>();
            var targetRoute     = new Mock <RouteBase>().Object;

            sourceRouteMock.Setup(r => r.GetRouteData(It.IsAny <HttpContextBase>())).Returns((RouteData)null);
            var redirectRoute = new RedirectRoute(sourceRouteMock.Object, targetRoute, permanent: true);

            // Act
            var routeData = redirectRoute.GetRouteData(new Mock <HttpContextBase>().Object);

            // Assert
            PAssert.IsTrue(() => routeData == null);
            // These next few asserts are "laziness" asserts.
            PAssert.IsTrue(() => redirectRoute.Permanent == true);
            PAssert.IsTrue(() => redirectRoute.SourceRoute == sourceRouteMock.Object);
            PAssert.IsTrue(() => redirectRoute.TargetRoute == targetRoute);
        }
예제 #2
0
        public void MapDelegateSetsRouteNameAndHttpHandlerRouteHandler() {
            // Arrange
            var httpHandler = new Mock<IHttpHandler>().Object;
            var routes = new RouteCollection();
            bool isSet = false;
            var httpRequest = new HttpRequest("foo", "http://foo.com/", "");
            var httpResponse = new HttpResponse(new Mock<TextWriter>().Object);
            var httpContext = new HttpContext(httpRequest, httpResponse);
            var requestContext = new RequestContext(new HttpContextWrapper(httpContext), new RouteData());

            // Act
            var route = routes.MapDelegate("route-name", "url", c => isSet = true);
            route.RouteHandler.GetHttpHandler(requestContext).ProcessRequest(httpContext);

            // Assert
            PAssert.IsTrue(() => route.GetRouteName() == "route-name");
            PAssert.IsTrue(() => route.RouteHandler.GetHttpHandler(requestContext).GetType() == typeof(DelegateHttpHandler));
            PAssert.IsTrue(() => isSet == true);
        }
        public void ReadmeListsAllBuiltInRules()
        {
            var readmeLines = File.ReadAllLines("README.md");
            var ruleTypes   = typeof(AppModule).Assembly
                              .GetTypes()
                              .Where(t => !t.IsAbstract && !t.IsInterface && typeof(IVersionAnalysisRule).IsAssignableFrom(t));

            var sb = new StringBuilder();

            foreach (Type type in ruleTypes)
            {
                if (!readmeLines.Contains($"  - `{type.Name}`"))
                {
                    sb.AppendLine(type.Name);
                }
            }

            PAssert.IsTrue(() => sb.ToString() == string.Empty);
        }
        public void MapHttpHandlerSetsRouteNameAndHttpHandlerRouteHandler()
        {
            // Arrange
            var obj = new RouteValueDictionary {
                { "foo", "bar" }
            };
            var routeValues = new RouteValueDictionary(obj);

            PAssert.IsTrue(() => (string)routeValues["foo"] == "bar");

            var httpHandler = new Mock <IHttpHandler>().Object;
            var routes      = new RouteCollection();

            // Act
            var route = routes.MapHttpHandler("route-name", "url", httpHandler);

            // Assert
            PAssert.IsTrue(() => route.GetRouteName() == "route-name");
            PAssert.IsTrue(() => route.RouteHandler.GetHttpHandler(null) == httpHandler);
        }
예제 #5
0
        public void GetHttpHandler_WithTargetRoute_ButRequestDoesNotMatchTargetRouteReturnsDelegateHttpHandler()
        {
            // Arrange
            var httpContext = new Mock <HttpContextBase>();

            httpContext.Setup(c => c.Request.Path).Returns("/qux");
            httpContext.Setup(c => c.Request.AppRelativeCurrentExecutionFilePath).Returns("~/qux");
            httpContext.Setup(c => c.Request.ApplicationPath).Returns("/");
            httpContext.Setup(c => c.Response.ApplyAppPathModifier(It.IsAny <string>())).Returns <string>(s => s);
            var routeData            = new RouteData();
            var requestContext       = new RequestContext(httpContext.Object, new RouteData());
            var targetRoute          = new Route("foo/{bar}", null);
            var redirectRouteHandler = new RedirectRoute(null, targetRoute, permanent: false, additionalRouteValues: null);

            // Act
            var httpHandler = redirectRouteHandler.GetHttpHandler(requestContext) as DelegateHttpHandler;

            // Assert
            PAssert.IsTrue(() => httpHandler != null);
        }
예제 #6
0
        public void GetRouteData_WithMatchingSourceRoute_SwapsRouteHandler()
        {
            // Arrange
            var routeData    = new RouteData();
            var routeHandler = new Mock <IRouteHandler>().Object;

            routeData.RouteHandler = routeHandler;
            var sourceRouteMock = new Mock <RouteBase>();

            sourceRouteMock.Setup(r => r.GetRouteData(It.IsAny <HttpContextBase>())).Returns(routeData);
            var redirectRoute = new RedirectRoute(sourceRouteMock.Object, new Mock <RouteBase>().Object, permanent: false);

            // Act
            var redirectRouteData = redirectRoute.GetRouteData(new Mock <HttpContextBase>().Object);

            // Assert
            PAssert.IsTrue(() => redirectRouteData.RouteHandler != routeHandler);
            PAssert.IsTrue(() => redirectRouteData.RouteHandler is RedirectRoute);
            PAssert.IsTrue(() => redirectRouteData == routeData);
            PAssert.IsTrue(() => redirectRoute.Permanent == false);
        }
예제 #7
0
        public void GetHttpHandler_OnRedirectionActionIsCalled()
        {
            // Arrange
            var onRedirectCalled = false;
            Action <RequestContext, RedirectRoute> onRedirection = (context, route) => onRedirectCalled = true;

            var httpContext = new Mock <HttpContextBase>();

            httpContext.Setup(c => c.Request.Path).Returns("/foo");
            httpContext.Setup(c => c.Request.AppRelativeCurrentExecutionFilePath).Returns("~/foo");
            httpContext.Setup(c => c.Request.ApplicationPath).Returns("/");
            var requestContext = new RequestContext(httpContext.Object, new RouteData());

            var redirectRoute = new RedirectRoute(new Mock <RouteBase>().Object, new Mock <RouteBase>().Object, false, null, onRedirection);

            // Act
            redirectRoute.GetHttpHandler(requestContext);

            // Assert
            PAssert.IsTrue(() => onRedirectCalled);
        }
예제 #8
0
        public void DefaultFilterPreventedWhenColumnNotFilterable()
        {
            var firstItem = _repo.GetAll().First();

            _grid.Columns.Add(x => x.Title, "Title").Filterable(false).SetInitialFilter(GridFilterType.Equals, firstItem.Title);

            var settingsMock     = new Mock <IGridSettingsProvider>();
            var filterSetting    = new Mock <IGridFilterSettings>();
            var filterCollection = new DefaultFilterColumnCollection();

            filterSetting.Setup(t => t.FilteredColumns).Returns(filterCollection);
            filterSetting.Setup(t => t.IsInitState).Returns(true);

            settingsMock.Setup(s => s.FilterSettings).Returns(filterSetting.Object);
            settingsMock.Setup(s => s.SortSettings).Returns(new QueryStringSortSettings());
            _grid.Settings = settingsMock.Object;

            var resultCollection = _grid.GetItemsToDisplay();

            PAssert.IsTrue(() => resultCollection.SequenceEqual(_repo.GetAll()));
        }
        public void Merge_WithRouteValueDictionaries_GivesTargetPrecedence()
        {
            // Arrange
            var source = new RouteValueDictionary()
            {
                { "foo", "foovalue" }, { "bar", "barvalue" }
            };
            var target = new RouteValueDictionary()
            {
                { "foo", "foovalue2" }, { "baz", "bazvalue" }
            };

            // Act
            var merged = source.Merge(target);

            // Assert
            PAssert.IsTrue(() => merged.Count == 3);
            PAssert.IsTrue(() => (string)merged["foo"] == "foovalue2");
            PAssert.IsTrue(() => (string)merged["bar"] == "barvalue");
            PAssert.IsTrue(() => (string)merged["baz"] == "bazvalue");
        }
예제 #10
0
        private void IsTrue(Expression <Func <bool> > func)
        {
            var timespan  = new TimeSpan(0, 0, 0, 30);
            var stopwatch = Stopwatch.StartNew();

            while (true)
            {
                try
                {
                    PAssert.IsTrue(func);
                    return;
                }
                catch
                {
                    if (stopwatch.Elapsed > timespan)
                    {
                        throw;
                    }
                }
            }
        }
예제 #11
0
        public void TestColumnsDataAnnotation()
        {
            _grid.AutoGenerateColumns();
            int i = 0;

            foreach (var pi in typeof(TestGridAnnotationModel).GetProperties(BindingFlags.Public | BindingFlags.Instance))
            {
                if (pi.GetAttribute <NotMappedColumnAttribute>() != null)
                {
                    continue;
                }

                var gridOpt = pi.GetAttribute <GridColumnAttribute>();

                if (gridOpt != null)
                {
                    var column = _grid.Columns.ElementAt(i) as IGridColumn <TestGridAnnotationModel>;
                    if (column == null)
                    {
                        Assert.Fail();
                    }

                    PAssert.IsTrue(() => column.EncodeEnabled == gridOpt.EncodeEnabled);
                    PAssert.IsTrue(() => column.FilterEnabled == gridOpt.FilterEnabled);
                    PAssert.IsTrue(() => column.SanitizeEnabled == gridOpt.SanitizeEnabled);

                    if (!string.IsNullOrEmpty(gridOpt.Title))
                    {
                        PAssert.IsTrue(() => column.Title == gridOpt.Title);
                    }

                    if (!string.IsNullOrEmpty(gridOpt.Width))
                    {
                        PAssert.IsTrue(() => column.Width == gridOpt.Width);
                    }
                }
                i++;
            }
            PAssert.IsTrue(() => _grid.Columns.Count() == 3);
        }
예제 #12
0
        public void ContentContainsSingleHeader()
        {
            // Arrange
            var expectedToc = new Toc
            {
                ToCValueForPasting = @"# Table of Content
* Right Here"
            };
            const string content = @"There is a single header
# Right Here
But nothing more";

            // Act
            var response = controller.CreateToc(content);

            // Assert
            var printer   = GetTestPrinter();
            Toc actualToc = null;

            PAssert.IsTrue(() => response.TryGetContentValue(out actualToc));
            Assert.AreEqual(printer.PrintObject(expectedToc), printer.PrintObject(actualToc));
        }
예제 #13
0
        protected static void IsFalse([InstantHandle] Expression <Func <bool> > expression, string message = null)
        {
            var negated = Expression.Lambda <Func <bool> >(
                Expression.Not(expression.Body),
                expression.Parameters);

            try
            {
                PAssert.IsTrue(negated);
            }
            catch (Exception e)
            {
                var expressionTrace = RemoveHeadline(e.Message);

                if (message != null)
                {
                    Assert.Fail(message + Environment.NewLine + expressionTrace);
                }

                Assert.Fail(expressionTrace);
            }
        }
예제 #14
0
        public void GetVirtualPath_WithMatchingRequest_AlwaysReturnsNull()
        {
            // Arrange
            var httpContext = new Mock <HttpContextBase>();

            httpContext.Setup(c => c.Request.Path).Returns("/foo");
            httpContext.Setup(c => c.Request.AppRelativeCurrentExecutionFilePath).Returns("~/foo");
            httpContext.Setup(c => c.Request.ApplicationPath).Returns("/");
            var requestContext = new RequestContext(httpContext.Object, new RouteData());

            var sourceRouteMock = new Mock <RouteBase>();

            sourceRouteMock.Setup(r => r.GetVirtualPath(It.IsAny <RequestContext>(), It.IsAny <RouteValueDictionary>())).Returns(new VirtualPathData(sourceRouteMock.Object, "/foo"));
            var targetRoute   = new Mock <RouteBase>().Object;
            var redirectRoute = new RedirectRoute(sourceRouteMock.Object, targetRoute, permanent: true);

            // Act
            var virtualPath = redirectRoute.GetVirtualPath(requestContext, new RouteValueDictionary());

            // Assert
            PAssert.IsTrue(() => virtualPath == null);
        }
예제 #15
0
        public void ContentContainsMultipleHeaders()
        {
            // Arrange
            var expectedToc = new Toc
            {
                ToCValueForPasting = "# Table of Content" + Environment.NewLine + "* One" + Environment.NewLine + "* Two" + Environment.NewLine + "* Three"
            };
            var content = "There contains multiple headers" + Environment.NewLine + "# One" + Environment.NewLine + "But nothing more" + Environment.NewLine + "# Two" + Environment.NewLine + "# Three";
            var reader  = new Mock <UrlReader>(MockBehavior.Strict);

            reader.Setup(x => x.ReadUrl(It.IsAny <Uri>())).Returns(content);
            controller.reader = reader.Object;

            // Act
            var response = controller.Get(anyUrl);

            // Assert
            var printer   = Create.Printer();
            Toc actualToc = null;

            PAssert.IsTrue(() => response.TryGetContentValue(out actualToc));
            //Assert.AreEqual(printer.PrintObject(expectedToc), printer.PrintObject(actualToc));
        }
예제 #16
0
        public void ContentDoesNotContainAnyHeaders()
        {
            // Arrange
            var expectedToc = new Toc {
                ToCValueForPasting = "# Table of Content"
            };

            const string content = "There is something here, but no headers";
            var          reader  = new Mock <UrlReader>(MockBehavior.Strict);

            reader.Setup(x => x.ReadUrl(It.IsAny <Uri>())).Returns(content);
            controller.reader = reader.Object;

            // Act
            var response = controller.Get(anyUrl);

            // Assert
            var printer   = Create.Printer();
            Toc actualToc = null;

            PAssert.IsTrue(() => response.TryGetContentValue(out actualToc));
            // Assert.AreEqual(printer.PrintObject(expectedToc), printer.PrintObject(actualToc));
        }
예제 #17
0
        public void GetHttpHandler_WithTargetRoute_ReturnsHandlerFromTargetRoute()
        {
            // Arrange
            var httpContext = new Mock <HttpContextBase>();

            httpContext.Setup(c => c.Request.Path).Returns("/qux");
            httpContext.Setup(c => c.Request.AppRelativeCurrentExecutionFilePath).Returns("~/qux");
            httpContext.Setup(c => c.Request.ApplicationPath).Returns("/");
            httpContext.Setup(c => c.Response.ApplyAppPathModifier(It.IsAny <string>())).Returns <string>(s => s);
            var routeData = new RouteData();

            routeData.Values.Add("bar", "the-value-of-bar");
            var requestContext       = new RequestContext(httpContext.Object, routeData);
            var targetRoute          = new Route("foo/{bar}", null);
            var redirectRouteHandler = new RedirectRoute(null, targetRoute, permanent: false, additionalRouteValues: null);

            // Act
            var httpHandler = redirectRouteHandler.GetHttpHandler(requestContext) as RedirectHttpHandler;

            // Assert
            PAssert.IsTrue(() => httpHandler.IsReusable == false);
            PAssert.IsTrue(() => httpHandler.TargetUrl == "~/foo/the-value-of-bar");
            PAssert.IsTrue(() => httpHandler.Permanent == false);
        }
 public void RemovesObsoleteAttributeDoesNotApply()
 {
     PAssert.IsTrue(() => !_rule.Applies(_localDef, _obsoleteDef));
 }
 public void StaysNonObsoleteAttributeDoesNotApply()
 {
     PAssert.IsTrue(() => !_rule.Applies(_obsoleteDef, _obsoleteDef));
 }
 public void AddsObsoleteAttributeApplies()
 {
     PAssert.IsTrue(() => _rule.Applies(_obsoleteDef, _localDef));
 }
        void LocalSecondaryIndex_HasNonKeyAttribute_OfType_(string indexName, string attributeName, ScalarAttributeType type)
        {
            var index = _table.LocalSecondaryIndexes.Single(x => x.IndexName == indexName);

            PAssert.IsTrue(() => index.Projection.NonKeyAttributes.Contains(attributeName));
        }
 public void SuggestPatch()
 {
     PAssert.IsTrue(() => "2.3.4".GetSuggestedVersion(VersionBumpType.Patch) == "2.3.5");
 }
 public void SuggestNone()
 {
     PAssert.IsTrue(() => "2.3.4".GetSuggestedVersion(VersionBumpType.None) == "2.3.4");
 }
예제 #24
0
 public void IsTrue <TTarget>(TTarget target, Expression <Func <TTarget, bool> > expression)
 {
     Try(() => PAssert.IsTrue(target, expression));
 }
예제 #25
0
 public void IsTrue(Expression <Func <bool> > expression)
 {
     Try(() => PAssert.IsTrue(expression));
 }
 void TableNameIs_(string name)
 {
     PAssert.IsTrue(() => _table.TableName == name);
 }
예제 #27
0
 public void WithInitialSorting()
 {
     _opt.WithInitialSorting(x => x.Title, GridSortDirection.Descending);
     PAssert.IsTrue(() => _grid.Settings.SortSettings.ColumnName == "Title");
     PAssert.IsTrue(() => _grid.Settings.SortSettings.Direction == GridSortDirection.Descending);
 }
예제 #28
0
 public void TestPaging()
 {
     PAssert.IsTrue(() => _grid.EnablePaging);
     PAssert.IsTrue(() => _grid.Pager.PageSize == 20);
 }
예제 #29
0
        private RunResult Run <T>(TypedSpecification <T> spec)
        {
            var result = new RunResult {
                SpecificationName = spec.GetName()
            };

            try
            {
                var before = spec.GetBefore();
                before.InvokeIfNotNull();
            }
            catch (Exception ex)
            {
                result.MarkFailure("Before Failed", ex.InnerException);
                return(result);
            }
            object sut = null;

            try
            {
                var given = spec.GetOn();
                sut       = given.DynamicInvoke();
                result.On = given;
            }
            catch (Exception ex)
            {
                result.MarkFailure("On Failed", ex.InnerException);
            }
            object   whenResult = null;
            Delegate when;

            try
            {
                when = spec.GetWhen();
                if (when == null)
                {
                    return new RunResult {
                               SpecificationName = spec.GetName(), Passed = false, Message = "No when on specification"
                    }
                }
                ;
                if (when.Method.GetParameters().Length == 1)
                {
                    whenResult = when.DynamicInvoke(new[] { sut });
                }
                else
                {
                    whenResult = when.DynamicInvoke();
                }
                if (when.Method.ReturnType != typeof(void))
                {
                    result.Result = whenResult;
                }
                else
                {
                    result.Result = sut;
                }
            }
            catch (Exception ex)
            {
                result.MarkFailure("When Failed", ex.InnerException);
                return(result);
            }
            var  fromWhen = when.Method.ReturnType == typeof(void) ? sut : whenResult;
            bool allOk    = true;

            foreach (var exp in spec.GetAssertions())
            {
                var partiallyApplied = PartialApplicationVisitor.Apply(exp, fromWhen);
                try
                {
                    PAssert.IsTrue(partiallyApplied);
                    result.Expectations.Add(new ExpectationResult {
                        Passed = true, Text = PAssert.CreateSimpleFormatFor(partiallyApplied), OriginalExpression = exp
                    });
                }
                catch (Exception ex)
                {
                    allOk = false;
                    result.Expectations.Add(new ExpectationResult {
                        Passed = false, Text = PAssert.CreateSimpleFormatFor(partiallyApplied), OriginalExpression = exp, Exception = ex
                    });
                }
            }
            try
            {
                var Finally = spec.GetFinally();
                Finally.InvokeIfNotNull();
            }
            catch (Exception ex)
            {
                allOk          = false;
                result.Message = "Finally failed";
                result.Thrown  = ex.InnerException;
            }
            result.Passed = allOk;
            return(result);
        }
        void LocalSecondaryIndexNamed_ForProjectType_(string name, Amazon.DynamoDBv2.ProjectionType type)
        {
            var index = _table.LocalSecondaryIndexes.Single(x => x.IndexName == name);

            PAssert.IsTrue(() => index.Projection.ProjectionType == type);
        }