Esempio n. 1
0
        public void IsTypeMatch_WithAny_ReturnsTrue(Type type, Type conventionType)
        {
            // Act
            var result = ApiConventionMatcher.IsTypeMatch(type, conventionType, ApiConventionTypeMatchBehavior.Any);

            // Assert
            Assert.True(result);
        }
Esempio n. 2
0
        public void IsNameMatch_WithAny_AlwaysReturnsTrue(string name, string conventionName)
        {
            // Act
            var result = ApiConventionMatcher.IsNameMatch(name, conventionName, ApiConventionNameMatchBehavior.Any);

            // Assert
            Assert.True(result);
        }
Esempio n. 3
0
        public void IsNameMatch_WithSuffix_ReturnFalse_IfNameIsNotProperSuffix(string name, string conventionName)
        {
            // Act
            var result = ApiConventionMatcher.IsNameMatch(name, conventionName, ApiConventionNameMatchBehavior.Suffix);

            // Assert
            Assert.False(result);
        }
Esempio n. 4
0
        public void IsNameMatch_WithExact_ReturnsFalse_IfNamesAreDifferent()
        {
            // Arrange
            var name           = "Name";
            var conventionName = "Different";

            // Act
            var result = ApiConventionMatcher.IsNameMatch(name, conventionName, ApiConventionNameMatchBehavior.Exact);

            // Assert
            Assert.False(result);
        }
Esempio n. 5
0
        public void IsMatch_ReturnsTrue_IfParamsArrayMatchesRemainingArguments()
        {
            // Arrange
            var method           = typeof(TestController).GetMethod(nameof(TestController.Search));
            var conventionMethod = typeof(TestConvention).GetMethod(nameof(TestConvention.Search));

            // Act
            var result = ApiConventionMatcher.IsMatch(method, conventionMethod);

            // Assert
            Assert.True(result);
        }
Esempio n. 6
0
        public void IsMatch_ReturnsTrue_IfMethodNameAndParametersMatchs()
        {
            // Arrange
            var method           = typeof(TestController).GetMethod(nameof(TestController.Get));
            var conventionMethod = typeof(TestConvention).GetMethod(nameof(TestConvention.Get));

            // Act
            var result = ApiConventionMatcher.IsMatch(method, conventionMethod);

            // Assert
            Assert.True(result);
        }
Esempio n. 7
0
        public void IsNameMatch_WithPrefix_ReturnsFalse_IfNamesDifferInCase()
        {
            // Arrange
            var name           = "GetPerson";
            var conventionName = "post";

            // Act
            var result = ApiConventionMatcher.IsNameMatch(name, conventionName, ApiConventionNameMatchBehavior.Prefix);

            // Assert
            Assert.False(result);
        }
Esempio n. 8
0
        public void IsMatch_ReturnsFalse_IfMethodHasFewerParametersThanConvention()
        {
            // Arrange
            var method           = typeof(TestController).GetMethod(nameof(TestController.Get));
            var conventionMethod = typeof(TestConvention).GetMethod(nameof(TestConvention.GetTwoArgs));

            // Act
            var result = ApiConventionMatcher.IsMatch(method, conventionMethod);

            // Assert
            Assert.False(result);
        }
Esempio n. 9
0
        public void IsTypeMatch_WithAssignableFrom_ReturnsFalseForUnrelated()
        {
            // Arrange
            var type           = typeof(string);
            var conventionType = typeof(Derived);

            // Act
            var result = ApiConventionMatcher.IsTypeMatch(type, conventionType, ApiConventionTypeMatchBehavior.AssignableFrom);

            // Assert
            Assert.False(result);
        }
Esempio n. 10
0
        public void IsTypeMatch_WithAssignableFrom_ReturnsTrueForDerived()
        {
            // Arrange
            var type           = typeof(Derived);
            var conventionType = typeof(Base);

            // Act
            var result = ApiConventionMatcher.IsTypeMatch(type, conventionType, ApiConventionTypeMatchBehavior.AssignableFrom);

            // Assert
            Assert.True(result);
        }
Esempio n. 11
0
        public void IsNameMatch_WithSuffix_ReturnTrue_IfNameIsProperSuffix()
        {
            // Arrange
            var name           = "personId";
            var conventionName = "id";

            // Act
            var result = ApiConventionMatcher.IsNameMatch(name, conventionName, ApiConventionNameMatchBehavior.Suffix);

            // Assert
            Assert.True(result);
        }
Esempio n. 12
0
        public void IsNameMatch_WithSuffix_ReturnFalse_IfNameDiffersInCase()
        {
            // Arrange
            var name           = "test";
            var conventionName = "Test";

            // Act
            var result = ApiConventionMatcher.IsNameMatch(name, conventionName, ApiConventionNameMatchBehavior.Suffix);

            // Assert
            Assert.False(result);
        }
Esempio n. 13
0
        public void IsNameMatch_WithPrefix_ReturnsFalse_IfNameIsSuffix()
        {
            // Arrange
            var name           = "GoPost";
            var conventionName = "Post";

            // Act
            var result = ApiConventionMatcher.IsNameMatch(name, conventionName, ApiConventionNameMatchBehavior.Prefix);

            // Assert
            Assert.False(result);
        }
Esempio n. 14
0
        public void IsMatch_WithEmpty_MatchesMethodWithNoParameters()
        {
            // Arrange
            var method           = typeof(TestController).GetMethod(nameof(TestController.SearchEmpty));
            var conventionMethod = typeof(TestConvention).GetMethod(nameof(TestConvention.SearchWithParams));

            // Act
            var result = ApiConventionMatcher.IsMatch(method, conventionMethod);

            // Assert
            Assert.True(result);
        }
Esempio n. 15
0
        public void IsMatch_ReturnsFalse_IfParametersDoNotMatch()
        {
            // Arrange
            var method           = typeof(TestController).GetMethod(nameof(TestController.Get));
            var conventionMethod = typeof(TestConvention).GetMethod(nameof(TestConvention.GetParameterNotMatching));

            // Act
            var result = ApiConventionMatcher.IsMatch(method, conventionMethod);

            // Assert
            Assert.False(result);
        }
Esempio n. 16
0
        private static MethodInfo GetConventionMethod(MethodInfo method, Type conventionType)
        {
            foreach (var conventionMethod in conventionType.GetMethods(BindingFlags.Public | BindingFlags.Static))
            {
                if (ApiConventionMatcher.IsMatch(method, conventionMethod))
                {
                    return(conventionMethod);
                }
            }

            return(null);
        }
Esempio n. 17
0
        public void IsNameMatch_WithPrefix_ReturnsTrue_IfNamesAreExact()
        {
            // Arrange
            var name           = "PostPerson";
            var conventionName = "PostPerson";

            // Act
            var result = ApiConventionMatcher.IsNameMatch(name, conventionName, ApiConventionNameMatchBehavior.Prefix);

            // Assert
            Assert.True(result);
        }
Esempio n. 18
0
        public void IsNameMatch_WithExact_ReturnsTrue_IfExactMatch()
        {
            // Arrange
            var name           = "parameterName";
            var conventionName = "parameterName";

            // Act
            var result = ApiConventionMatcher.IsNameMatch(name, conventionName, ApiConventionNameMatchBehavior.Exact);

            // Assert
            Assert.True(result);
        }
Esempio n. 19
0
        public void IsNameMatch_WithExact_ReturnsFalse_IfConventionNameIsSuperString()
        {
            // Arrange
            var name           = "Regular";
            var conventionName = "RegularName";

            // Act
            var result = ApiConventionMatcher.IsNameMatch(name, conventionName, ApiConventionNameMatchBehavior.Exact);

            // Assert
            Assert.False(result);
        }
Esempio n. 20
0
        public void GetTypeMatchBehavior_ReturnsIsAssignableFrom_WhenNoMatchingAttributesArePresent()
        {
            // Arrange
            var expected   = ApiConventionTypeMatchBehavior.AssignableFrom;
            var attributes = new object[] { new CLSCompliantAttribute(false), new ProducesResponseTypeAttribute(200) };
            var provider   = Mock.Of <ICustomAttributeProvider>(p => p.GetCustomAttributes(false) == attributes);

            // Act
            var result = ApiConventionMatcher.GetTypeMatchBehavior(provider);

            // Assert
            Assert.Equal(expected, result);
        }
Esempio n. 21
0
        public void GetTypeMatchBehavior_ReturnsIsAssignableFrom_WhenNoAttributesArePresent()
        {
            // Arrange
            var expected   = ApiConventionTypeMatchBehavior.AssignableFrom;
            var attributes = new object[0];
            var provider   = Mock.Of <ICustomAttributeProvider>(p => p.GetCustomAttributes(false) == attributes);

            // Act
            var result = ApiConventionMatcher.GetTypeMatchBehavior(provider);

            // Assert
            Assert.Equal(expected, result);
        }
Esempio n. 22
0
        public void GetNameMatchBehavior_ReturnsExact_WhenNoNameMatchBehaviorAttributeIsSpecified()
        {
            // Arrange
            var expected   = ApiConventionNameMatchBehavior.Exact;
            var attributes = new object[] { new CLSCompliantAttribute(false), new ProducesResponseTypeAttribute(200) };
            var provider   = Mock.Of <ICustomAttributeProvider>(p => p.GetCustomAttributes(false) == attributes);

            // Act
            var result = ApiConventionMatcher.GetNameMatchBehavior(provider);

            // Assert
            Assert.Equal(expected, result);
        }
Esempio n. 23
0
        private static MethodInfo GetConventionMethod(MethodInfo method, ApiConventionTypeAttribute[] apiConventionAttributes)
        {
            foreach (var attribute in apiConventionAttributes)
            {
                var conventionMethods = attribute.ConventionType.GetMethods(BindingFlags.Public | BindingFlags.Static);
                foreach (var conventionMethod in conventionMethods)
                {
                    if (ApiConventionMatcher.IsMatch(method, conventionMethod))
                    {
                        return(conventionMethod);
                    }
                }
            }

            return(null);
        }