Ejemplo n.º 1
0
        public void MatchedRoute_HasCorrectParameters(string route, string path, params string[] parameters)
        {
            if (parameters.Length % 2 != 0)
            {
                throw new InvalidOperationException("Parameters should be in name, value pairs.");
            }

            RouteMatcher.ClearCache();

            var parameterCount = parameters.Length / 2;

            Assert.IsTrue(RouteMatcher.TryParse(route, false, out var matcher));
            Assert.AreEqual(parameterCount, matcher.ParameterNames.Count);
            for (var i = 0; i < parameterCount; i++)
            {
                Assert.AreEqual(parameters[2 * i], matcher.ParameterNames[i]);
            }

            var match = matcher.Match(path);

            Assert.IsNotNull(match);
            var keys   = match.Keys.ToArray();
            var values = match.Values.ToArray();

            Assert.AreEqual(parameterCount, keys.Length);
            Assert.AreEqual(parameterCount, values.Length);
            for (var i = 0; i < parameterCount; i++)
            {
                Assert.AreEqual(parameters[2 * i], keys[i]);
                Assert.AreEqual(parameters[(2 * i) + 1], values[i]);
            }
        }
Ejemplo n.º 2
0
        [TestCase("/abc/{width}/{height}/")] // 2 parameters, different segments.
        public void ValidBaseRoute_IsValid(string route)
        {
            RouteMatcher.ClearCache();

            Assert.IsTrue(Route.IsValid(route, true));
            Assert.DoesNotThrow(() => RouteMatcher.Parse(route, true));
            Assert.IsTrue(RouteMatcher.TryParse(route, true, out _));
        }
Ejemplo n.º 3
0
        [TestCase("/abc/{id}/{name?}/")]  // No segment of a base route can be optional.
        public void InvalidBaseRoute_IsNotValid(string route)
        {
            RouteMatcher.ClearCache();

            Assert.IsFalse(Route.IsValid(route, true));
            Assert.Throws <FormatException>(() => RouteMatcher.Parse(route, true));
            Assert.IsFalse(RouteMatcher.TryParse(route, true, out _));
        }
Ejemplo n.º 4
0
        [TestCase("/abc/{id}/{date?}", "id", "date")]          // 2 parameters, different segments, 1 optional.
        public void RouteParameters_HaveCorrectNames(string route, params string[] parameterNames)
        {
            RouteMatcher.ClearCache();

            Assert.IsTrue(RouteMatcher.TryParse(route, false, out var matcher));
            Assert.AreEqual(parameterNames.Length, matcher !.ParameterNames.Count);
            for (var i = 0; i < parameterNames.Length; i++)
            {
                Assert.AreEqual(parameterNames[i], matcher.ParameterNames[i]);
            }
        }
Ejemplo n.º 5
0
        public void MatchedBaseRoute_HasCorrectSubPath(string route, string path, string subPath)
        {
            RouteMatcher.ClearCache();

            Assert.IsTrue(RouteMatcher.TryParse(route, true, out var matcher));

            var match = matcher !.Match(path);

            Assert.IsNotNull(match);
            Assert.AreEqual(subPath, match.SubPath);
        }