/// <summary>
        /// Checks whether the supplied <paramref name="method"/> matches the <paramref name="rule"/>.
        /// </summary>
        /// <param name="rule">The rule to match against the <paramref name="method"/>.</param>
        /// <param name="type">The type to check if it matches the <paramref name="rule"/> if
        /// <see cref="RegexRule.Type"/> is not null.</param>
        /// <param name="method">The method to check if it matches the <paramref name="rule"/>.</param>
        /// <returns><see langword="true"/> if the <paramref name="rule"/> matches against the
        /// <paramref name="method"/>; otherwise <see langword="false"/>.</returns>
        /// <exception cref="ArgumentNullException">The <paramref name="rule"/>, <paramref name="type"/> or
        /// <paramref name="method"/> parameters are <see langword="null"/>.</exception>
        /// <exception cref="ArgumentException">The <see cref="RegexRule.Method"/> property of the
        /// <paramref name="rule"/> is <see langword="null"/>.</exception>
        public static bool MatchMethod(this RegexRule rule, Type type, MethodBase method)
        {
            if (rule == null)
            {
                throw new ArgumentNullException(nameof(rule));
            }
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }
            if (method == null)
            {
                throw new ArgumentNullException(nameof(method));
            }
            if (rule.Method == null)
            {
                throw new ArgumentException("The rule has a null Method regular expression.", nameof(rule));
            }

            // If there is a type regular expression it must match the type.
            if (rule.Type != null && !rule.MatchType(type))
            {
                return(false);
            }

            return(rule.Method.IsMatch(method.Name));
        }
// ReSharper disable UnusedParameter.Local
        private void AssertMethodRule(Func <string, Type, IRegexFilter> addMethod, MethodBase method, Type type, bool expectedInclude)
        {
            IRegexFilter actualFilter = addMethod(method.Name, type);

            int expectedRules = 1;

            if (expectedInclude && type != null)
            {
                expectedRules++;
            }

            Assert.Same(addMethod.Target, actualFilter);
            Assert.Equal(expectedRules, actualFilter.Rules.Count);
            RegexRule addedRule = actualFilter.MethodRules.Single();

            Assert.Equal(expectedInclude, addedRule.Include);
            Assert.NotNull(addedRule.Method);
            Assert.True(addedRule.MatchMethod(type ?? GetType(), method));
            Assert.Null(addedRule.Parameter);

            if (type == null)
            {
                Assert.Null(addedRule.Type);
            }
            else
            {
                Assert.NotNull(addedRule.Type);
                Assert.True(addedRule.MatchType(type));
            }
        }
        public void ThrowIfNoTypeRuleForTypeMatch(Type type)
        {
            // Arrange
            var rule = new RegexRule("Throw rule");

            // Act/Assert
            string paramName = Assert.Throws<ArgumentException>(() => rule.MatchType(type)).ParamName;
            Assert.Equal("rule", paramName);
        }
        public void ThrowIfNoMethodRuleForMatchMethod(Type type, MethodBase method)
        {
            // Arrange
            var rule = new RegexRule("Throw rule");

            // Act/Assert
            string paramName = Assert.Throws <ArgumentException>(() => rule.MatchMethod(type, method)).ParamName;

            Assert.Equal("rule", paramName);
        }
        public void ThrowIfNoTypeRuleForTypeMatch(Type type)
        {
            // Arrange
            var rule = new RegexRule("Throw rule");

            // Act/Assert
            string paramName = Assert.Throws <ArgumentException>(() => rule.MatchType(type)).ParamName;

            Assert.Equal("rule", paramName);
        }
        public void NotMatchAType(Type type)
        {
            // Arrange
            var rule = new RegexRule("Miss rule", type: new Regex("Miss"));

            // Act
            bool actual = rule.MatchType(type);

            // Assert
            Assert.False(actual);
        }
        public void NotMatchAType(Type type)
        {
            // Arrange
            var rule = new RegexRule("Miss rule", type: new Regex("Miss"));

            // Act
            bool actual = rule.MatchType(type);

            // Assert
            Assert.False(actual);
        }
        public void InitialiseProperties(string name, bool include, Regex type, Regex method, Regex parameter)
        {
            // Act
            var sut = new RegexRule(name, include, type, method, parameter);

            // Assert
            Assert.Same(name, sut.Name);
            Assert.Equal(include, sut.Include);
            Assert.Same(type, sut.Type);
            Assert.Same(method, sut.Method);
            Assert.Same(parameter, sut.Parameter);
        }
Esempio n. 9
0
        public void InitialiseProperties(string name, bool include, Regex type, Regex method, Regex parameter)
        {
            // Act
            var sut = new RegexRule(name, include, type, method, parameter);

            // Assert
            Assert.Same(name, sut.Name);
            Assert.Equal(include, sut.Include);
            Assert.Same(type, sut.Type);
            Assert.Same(method, sut.Method);
            Assert.Same(parameter, sut.Parameter);
        }
        public void NotMatchAMethod(Type type, Mock <MethodBase> methodMock)
        {
            // Arrange
            methodMock.SetupGet(m => m.Name).Returns("Name" + Guid.NewGuid());
            var rule = new RegexRule("Miss rule", method: new Regex("Miss"));

            // Act
            bool actual = rule.MatchMethod(type, methodMock.Object);

            // Assert
            Assert.False(actual);
        }
        public void IgnoreTypeRegexIfNullWhenMatchMethod(Type type, Mock<MethodBase> methodMock)
        {
            // Arrange
            methodMock.SetupGet(m => m.Name).Returns("Name" + Guid.NewGuid());
            var rule = new RegexRule(methodMock.Object.Name + " hit rule", method: new Regex(methodMock.Object.Name));

            // Act
            bool actual = rule.MatchMethod(type, methodMock.Object);

            // Assert
            Assert.True(actual);
        }
        public void IgnoreTypeRegexIfNullWhenMatchMethod(Type type, Mock <MethodBase> methodMock)
        {
            // Arrange
            methodMock.SetupGet(m => m.Name).Returns("Name" + Guid.NewGuid());
            var rule = new RegexRule(methodMock.Object.Name + " hit rule", method: new Regex(methodMock.Object.Name));

            // Act
            bool actual = rule.MatchMethod(type, methodMock.Object);

            // Assert
            Assert.True(actual);
        }
        public void ThrowIfNoParameterRuleForMatchParameter(
            Type type,
            MethodBase method,
            Mock <ParameterInfo> parameterMock)
        {
            // Arrange
            var rule = new RegexRule("Throw rule");

            // Act/Assert
            string paramName = Assert.Throws <ArgumentException>(() => rule.MatchParameter(type, method, parameterMock.Object)).ParamName;

            Assert.Equal("rule", paramName);
        }
        public void MatchAType(Type type)
        {
            // Arrange
            var rule = new RegexRule(
                type.Name + " hit rule",
                include: true,
                type: new Regex(@".+\." + type.Name));

            // Act
            bool actual = rule.MatchType(type);

            // Assert
            Assert.True(actual);
        }
// ReSharper disable UnusedParameter.Local
        private void AssertTypeRule(Func <Type, IRegexFilter> addMethod, bool expectedInclude)
        {
            IRegexFilter actualFilter = addMethod(GetType());

            Assert.Same(addMethod.Target, actualFilter);
            Assert.Single(actualFilter.Rules);
            RegexRule addedRule = actualFilter.Rules.Single();

            Assert.Equal(expectedInclude, addedRule.Include);
            Assert.NotNull(addedRule.Type);
            Assert.Null(addedRule.Method);
            Assert.Null(addedRule.Parameter);
            Assert.True(addedRule.MatchType(GetType()));
        }
        public void MatchAType(Type type)
        {
            // Arrange
            var rule = new RegexRule(
                type.Name + " hit rule",
                include: true,
                type: new Regex(@".+\." + type.Name));

            // Act
            bool actual = rule.MatchType(type);

            // Assert
            Assert.True(actual);
        }
        public void ExcludeAllParameters()
        {
            // Arrange
            var sut = new RegexFilter();

            // Act
            IRegexFilter result = sut.ExcludeAllParameters();

            // Assert
            Assert.Same(sut, result);
            Assert.Single(sut.Rules);
            RegexRule parameterRule = sut.ParameterRules.Single();

            Assert.Matches(parameterRule.Parameter, Guid.NewGuid().ToString());
        }
        public void ExcludeAllMethods()
        {
            // Arrange
            var sut = new RegexFilter();

            // Act
            IRegexFilter result = sut.ExcludeAllMethods();

            // Assert
            Assert.Same(sut, result);
            Assert.Single(sut.Rules);
            RegexRule methodRule = sut.MethodRules.Single();

            Assert.Matches(methodRule.Method, Guid.NewGuid().ToString());
        }
        public void ApplyTypeRegexIfProvidedWhenMatchMethod(Type type, Mock<MethodBase> methodMock)
        {
            // Arrange
            methodMock.SetupGet(m => m.Name).Returns("Name" + Guid.NewGuid());
            var rule = new RegexRule(
                methodMock.Object.Name + " hit rule",
                type: new Regex(Guid.NewGuid().ToString()),
                method: new Regex(methodMock.Object.Name));

            // Act
            bool actual = rule.MatchMethod(type, methodMock.Object);

            // Assert
            Assert.False(actual);
        }
        public void ApplyTypeRegexIfProvidedWhenMatchMethod(Type type, Mock <MethodBase> methodMock)
        {
            // Arrange
            methodMock.SetupGet(m => m.Name).Returns("Name" + Guid.NewGuid());
            var rule = new RegexRule(
                methodMock.Object.Name + " hit rule",
                type: new Regex(Guid.NewGuid().ToString()),
                method: new Regex(methodMock.Object.Name));

            // Act
            bool actual = rule.MatchMethod(type, methodMock.Object);

            // Assert
            Assert.False(actual);
        }
        public void IgnoreTypeAndMethodRegexIfNullWhenMatchParameter(
            Type type,
            Mock <MethodBase> methodMock,
            Mock <ParameterInfo> parameterMock)
        {
            // Arrange
            parameterMock.SetupGet(p => p.Name).Returns("Name" + Guid.NewGuid());
            var rule = new RegexRule(parameterMock.Object.Name + " hit rule", parameter: new Regex(parameterMock.Object.Name));

            // Act
            bool actual = rule.MatchParameter(type, methodMock.Object, parameterMock.Object);

            // Assert
            Assert.True(actual);
        }
        public void ProvideDebuggerDisplay(RegexRule sut)
        {
            // Assert as we go
            var debuggerDisplay = sut.GetType().GetCustomAttribute<DebuggerDisplayAttribute>(inherit: false);
            Assert.NotNull(debuggerDisplay);
            Assert.Contains("DebuggerDisplay", debuggerDisplay.Value);

            PropertyInfo propertyInfo = sut.GetType().GetProperty("DebuggerDisplay", BindingFlags.NonPublic | BindingFlags.Instance);
            Assert.NotNull(propertyInfo);

            MethodInfo getMethod = propertyInfo.GetGetMethod(true);
            Assert.NotNull(getMethod);

            var display = Assert.IsType<string>(getMethod.Invoke(sut, new object[] {}));
            Assert.Contains(sut.Name, display);
        }
        /// <summary>
        /// Checks whether the supplied <paramref name="type"/> matches the <paramref name="rule"/>.
        /// </summary>
        /// <param name="rule">The rule to match against the <paramref name="type"/>.</param>
        /// <param name="type">The type to check if it matches the <paramref name="rule"/>.</param>
        /// <returns><see langword="true"/> if the <paramref name="rule"/> matches against the <paramref name="type"/>;
        /// otherwise <see langword="false"/>.</returns>
        /// <exception cref="ArgumentNullException">The <paramref name="rule"/> or <paramref name="type"/> parameters
        /// are <see langword="null"/>.</exception>
        /// <exception cref="ArgumentException">The <see cref="RegexRule.Type"/> property of the <paramref name="rule"/>
        /// is <see langword="null"/>.</exception>
        public static bool MatchType(this RegexRule rule, Type type)
        {
            if (rule == null)
            {
                throw new ArgumentNullException(nameof(rule));
            }
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }
            if (rule.Type == null)
            {
                throw new ArgumentException("The rule has a null Type regular expression.", nameof(rule));
            }

            return(rule.Type.IsMatch(type.FullName));
        }
        public void ApplyTypeRegexIfProvidedWhenMatchParameter(
            Type type,
            Mock <MethodBase> methodMock,
            Mock <ParameterInfo> parameterMock)
        {
            // Arrange
            parameterMock.SetupGet(p => p.Name).Returns("Name" + Guid.NewGuid());
            var rule = new RegexRule(
                methodMock.Object.Name + " hit rule",
                type: new Regex(Guid.NewGuid().ToString()),
                parameter: new Regex(parameterMock.Object.Name));

            // Act
            bool actual = rule.MatchParameter(type, methodMock.Object, parameterMock.Object);

            // Assert
            Assert.False(actual);
        }
// ReSharper disable UnusedParameter.Local
        private void AssertParameterRule(Func <string, Type, string, IRegexFilter> addMethod, ParameterInfo parameter, Type type, MethodBase method, bool expectedInclude)
        {
            IRegexFilter actualFilter = addMethod(parameter.Name, type, method == null ? null : method.Name);

            int expectedRules = 1;

            if (expectedInclude && method != null)
            {
                expectedRules++;
            }
            if (expectedInclude && type != null)
            {
                expectedRules++;
            }

            Assert.Same(addMethod.Target, actualFilter);
            Assert.Equal(expectedRules, actualFilter.Rules.Count);
            RegexRule addedRule = actualFilter.ParameterRules.Single();

            Assert.Equal(expectedInclude, addedRule.Include);
            Assert.NotNull(addedRule.Parameter);
            Assert.True(addedRule.MatchParameter(type ?? GetType(), method ?? new Mock <MethodBase>().Object, parameter));

            if (type == null)
            {
                Assert.Null(addedRule.Type);
            }
            else
            {
                Assert.NotNull(addedRule.Type);
                Assert.True(addedRule.MatchType(type));
            }

            if (method == null)
            {
                Assert.Null(addedRule.Method);
            }
            else
            {
                Assert.NotNull(addedRule.Method);
                Assert.True(addedRule.MatchMethod(type ?? GetType(), method));
            }
        }
Esempio n. 26
0
        public void ProvideDebuggerDisplay(RegexRule sut)
        {
            // Assert as we go
            DebuggerDisplayAttribute debuggerDisplay = sut.GetType().GetTypeInfo().GetCustomAttribute <DebuggerDisplayAttribute>(inherit: false);

            Assert.NotNull(debuggerDisplay);
            Assert.Contains("DebuggerDisplay", debuggerDisplay.Value);

            PropertyInfo propertyInfo = sut.GetType().GetProperty("DebuggerDisplay", BindingFlags.NonPublic | BindingFlags.Instance);

            Assert.NotNull(propertyInfo);

            MethodInfo getMethod = propertyInfo.GetGetMethod(true);

            Assert.NotNull(getMethod);

            string display = Assert.IsType <string>(getMethod.Invoke(sut, new object[] {}));

            Assert.Contains(sut.Name, display);
        }
        public void ExcludeAll()
        {
            // Arrange
            var sut = new RegexFilter();

            // Act
            IRegexFilter result = sut.ExcludeAll();

            // Assert
            Assert.Same(sut, result);
            Assert.Equal(3, sut.Rules.Count);
            RegexRule typeRule = sut.TypeRules.Single();

            Assert.Matches(typeRule.Type, Guid.NewGuid().ToString());
            RegexRule methodRule = sut.MethodRules.Single();

            Assert.Matches(methodRule.Method, Guid.NewGuid().ToString());
            RegexRule parameterRule = sut.ParameterRules.Single();

            Assert.Matches(parameterRule.Parameter, Guid.NewGuid().ToString());
        }
        /// <summary>
        /// Checks whether the supplied <paramref name="parameter"/> matches the <paramref name="rule"/>.
        /// </summary>
        /// <param name="rule">The rule to match against the <paramref name="method"/>.</param>
        /// <param name="type">The type to check if it matches the <paramref name="rule"/> if
        /// <see cref="RegexRule.Type"/> is not null.</param>
        /// <param name="method">The method to check if it matches the <paramref name="rule"/> if
        /// <see cref="RegexRule.Method"/> is not null.</param>
        /// <param name="parameter">The parameter to check if it matches the <paramref name="rule"/>.</param>
        /// <returns><see langword="true"/> if the <paramref name="rule"/> matches against the
        /// <paramref name="parameter"/>; otherwise <see langword="false"/>.</returns>
        /// <exception cref="ArgumentNullException">The <paramref name="rule"/>, <paramref name="type"/>,
        /// <paramref name="method"/> or <paramref name="parameter"/> parameters are <see langword="null"/>.</exception>
        /// <exception cref="ArgumentException">The <see cref="RegexRule.Parameter"/> property of the
        /// <paramref name="rule"/> is <see langword="null"/>.</exception>
        public static bool MatchParameter(this RegexRule rule, Type type, MethodBase method, ParameterInfo parameter)
        {
            if (rule == null)
            {
                throw new ArgumentNullException(nameof(rule));
            }
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }
            if (method == null)
            {
                throw new ArgumentNullException(nameof(method));
            }
            if (parameter == null)
            {
                throw new ArgumentNullException(nameof(parameter));
            }
            if (rule.Parameter == null)
            {
                throw new ArgumentException("The rule has a null Parameter regular expression.", nameof(rule));
            }

            // If there is a method regular expression it must match the method.
            if (rule.Method != null)
            {
                if (!rule.MatchMethod(type, method))
                {
                    return(false);
                }
            }
            else if (rule.Type != null && !rule.MatchType(type))
            {
                // If there is a type regular expression but no method it must match the type.
                return(false);
            }

            return(rule.Parameter.IsMatch(parameter.Name));
        }
        public void ThrowIfNoMethodRuleForMatchMethod(Type type, MethodBase method)
        {
            // Arrange
            var rule = new RegexRule("Throw rule");

            // Act/Assert
            string paramName = Assert.Throws<ArgumentException>(() => rule.MatchMethod(type, method)).ParamName;
            Assert.Equal("rule", paramName);
        }
        public void ApplyMethodRegexIfProvidedWhenMatchParameter(
            Type type,
            Mock<MethodBase> methodMock,
            Mock<ParameterInfo> parameterMock)
        {
            // Arrange
            parameterMock.SetupGet(p => p.Name).Returns("Name" + Guid.NewGuid());
            methodMock.SetupGet(p => p.Name).Returns("Name" + Guid.NewGuid());
            var rule = new RegexRule(
                methodMock.Object.Name + " hit rule",
                method: new Regex(Guid.NewGuid().ToString()),
                parameter: new Regex(parameterMock.Object.Name));

            // Act
            bool actual = rule.MatchParameter(type, methodMock.Object, parameterMock.Object);

            // Assert
            Assert.False(actual);
        }
        public void ThrowIfNoParameterRuleForMatchParameter(
            Type type,
            MethodBase method,
            Mock<ParameterInfo> parameterMock)
        {
            // Arrange
            var rule = new RegexRule("Throw rule");

            // Act/Assert
            string paramName = Assert.Throws<ArgumentException>(() => rule.MatchParameter(type, method, parameterMock.Object)).ParamName;
            Assert.Equal("rule", paramName);
        }
        public void NotMatchAMethod(Type type, Mock<MethodBase> methodMock)
        {
            // Arrange
            methodMock.SetupGet(m => m.Name).Returns("Name" + Guid.NewGuid());
            var rule = new RegexRule("Miss rule", method: new Regex("Miss"));

            // Act
            bool actual = rule.MatchMethod(type, methodMock.Object);

            // Assert
            Assert.False(actual);
        }
        public void IgnoreTypeAndMethodRegexIfNullWhenMatchParameter(
            Type type,
            Mock<MethodBase> methodMock,
            Mock<ParameterInfo> parameterMock)
        {
            // Arrange
            parameterMock.SetupGet(p => p.Name).Returns("Name" + Guid.NewGuid());
            var rule = new RegexRule(parameterMock.Object.Name + " hit rule", parameter: new Regex(parameterMock.Object.Name));

            // Act
            bool actual = rule.MatchParameter(type, methodMock.Object, parameterMock.Object);

            // Assert
            Assert.True(actual);
        }