private void CheckMatch2(Type[] inputTypes, Type[] expectedTypes, StandardTypeConverter typeConverter, ArgumentsMatchKind?expectedMatchKind)
        {
            var matchInfo = ReflectionHelper.CompareArgumentsVarargs(GetTypeDescriptors(expectedTypes), GetTypeDescriptors(inputTypes), typeConverter);

            if (expectedMatchKind == null)
            {
                Assert.Null(matchInfo);
                return;
            }
            else
            {
                Assert.NotNull(matchInfo);
            }

            if (expectedMatchKind.Value == ArgumentsMatchKind.EXACT)
            {
                Assert.True(matchInfo.IsExactMatch);
            }
            else if (expectedMatchKind.Value == ArgumentsMatchKind.CLOSE)
            {
                Assert.True(matchInfo.IsCloseMatch);
            }
            else if (expectedMatchKind.Value == ArgumentsMatchKind.REQUIRES_CONVERSION)
            {
                Assert.True(matchInfo.IsMatchRequiringConversion);
            }
        }
        public void TestConvertArguments2()
        {
            var tc     = new StandardTypeConverter();
            var oneArg = typeof(ITestInterface).GetMethod("OneArg", new Type[] { typeof(string) });
            var twoArg = typeof(ITestInterface).GetMethod("TwoArg", new Type[] { typeof(string), typeof(string[]) });

            // Simple conversion: int to string
            var args = new object[] { 3 };

            ReflectionHelper.ConvertAllArguments(tc, args, oneArg);
            CheckArguments(args, "3");

            // varargs conversion
            args = new object[] { 3, false, 3.0f };
            ReflectionHelper.ConvertAllArguments(tc, args, twoArg);
            CheckArguments(args, "3", "False", "3");

            // varargs conversion but no varargs
            args = new object[] { 3 };
            ReflectionHelper.ConvertAllArguments(tc, args, twoArg);
            CheckArguments(args, "3");

            // null value
            args = new object[] { 3, null, 3.0f };
            ReflectionHelper.ConvertAllArguments(tc, args, twoArg);
            CheckArguments(args, "3", null, "3");
        }
        public void TestConvertArguments()
        {
            var tc     = new StandardTypeConverter();
            var oneArg = typeof(ITestInterface).GetMethod("OneArg", new Type[] { typeof(string) });
            var twoArg = typeof(ITestInterface).GetMethod("TwoArg", new Type[] { typeof(string), typeof(string[]) });

            // basic conversion int>String
            var args = new object[] { 3 };

            ReflectionHelper.ConvertArguments(tc, args, oneArg, null);
            CheckArguments(args, "3");

            // varargs but nothing to convert
            args = new object[] { 3 };
            ReflectionHelper.ConvertArguments(tc, args, twoArg, 1);
            CheckArguments(args, "3");

            // varargs with nothing needing conversion
            args = new object[] { 3, "abc", "abc" };
            ReflectionHelper.ConvertArguments(tc, args, twoArg, 1);
            CheckArguments(args, "3", "abc", "abc");

            // varargs with conversion required
            args = new object[] { 3, false, 3.0d };
            ReflectionHelper.ConvertArguments(tc, args, twoArg, 1);
            CheckArguments(args, "3", "False", "3");
        }
        public void TestReflectionHelperCompareArguments_NotAMatch()
        {
            var typeConverter = new StandardTypeConverter();

            // Passing (Super,String) on call to foo(Sub,String) is not a match
            CheckMatch(new Type[] { typeof(Super), typeof(string) }, new Type[] { typeof(Sub), typeof(string) }, typeConverter, null);
        }
        public void TestReflectionHelperCompareArguments_ExactMatching()
        {
            var tc = new StandardTypeConverter();

            // Calling foo(String) with (String) is exact match
            CheckMatch(new Type[] { typeof(string) }, new Type[] { typeof(string) }, tc, ArgumentsMatchKind.EXACT);

            // Calling foo(String,Integer) with (String,Integer) is exact match
            CheckMatch(new Type[] { typeof(string), typeof(int) }, new Type[] { typeof(string), typeof(int) }, tc, ArgumentsMatchKind.EXACT);
        }
        public void TestReflectionHelperCompareArguments_CloseMatching()
        {
            var tc = new StandardTypeConverter();

            // Calling foo(List) with (ArrayList) is close match (no conversion required)
            CheckMatch(new Type[] { typeof(ArrayList) }, new Type[] { typeof(IList) }, tc, ArgumentsMatchKind.CLOSE);

            // Passing (Sub,String) on call to foo(Super,String) is close match
            CheckMatch(new Type[] { typeof(Sub), typeof(string) }, new Type[] { typeof(Super), typeof(string) }, tc, ArgumentsMatchKind.CLOSE);

            // Passing (String,Sub) on call to foo(String,Super) is close match
            CheckMatch(new Type[] { typeof(string), typeof(Sub) }, new Type[] { typeof(string), typeof(Super) }, tc, ArgumentsMatchKind.CLOSE);
        }
        public void TestReflectionHelperCompareArguments_Varargs_ExactMatching()
        {
            var tc = new StandardTypeConverter();

            // Passing (String[]) on call to (String[]) is exact match
            CheckMatch2(new Type[] { typeof(string[]) }, new Type[] { typeof(string[]) }, tc, ArgumentsMatchKind.EXACT);

            // Passing (Integer, String[]) on call to (Integer, String[]) is exact match
            CheckMatch2(new Type[] { typeof(int), typeof(string[]) }, new Type[] { typeof(int), typeof(string[]) }, tc, ArgumentsMatchKind.EXACT);

            // Passing (String, Integer, String[]) on call to (String, String, String[]) is exact match
            CheckMatch2(new Type[] { typeof(string), typeof(int), typeof(string[]) }, new Type[] { typeof(string), typeof(int), typeof(string[]) }, tc, ArgumentsMatchKind.EXACT);

            // Passing (Sub, String[]) on call to (Super, String[]) is exact match
            CheckMatch2(new Type[] { typeof(Sub), typeof(string[]) }, new Type[] { typeof(Super), typeof(string[]) }, tc, ArgumentsMatchKind.CLOSE);

            // Passing (Integer, String[]) on call to (String, String[]) is exact match
            CheckMatch2(new Type[] { typeof(int), typeof(string[]) }, new Type[] { typeof(string), typeof(string[]) }, tc, ArgumentsMatchKind.REQUIRES_CONVERSION);

            // Passing (Integer, Sub, String[]) on call to (String, Super, String[]) is exact match
            CheckMatch2(new Type[] { typeof(int), typeof(Sub), typeof(string[]) }, new Type[] { typeof(string), typeof(Super), typeof(string[]) }, tc, ArgumentsMatchKind.REQUIRES_CONVERSION);

            // Passing (String) on call to (String[]) is exact match
            CheckMatch2(new Type[] { typeof(string) }, new Type[] { typeof(string[]) }, tc, ArgumentsMatchKind.EXACT);

            // Passing (Integer,String) on call to (Integer,String[]) is exact match
            CheckMatch2(new Type[] { typeof(int), typeof(string) }, new Type[] { typeof(int), typeof(string[]) }, tc, ArgumentsMatchKind.EXACT);

            // Passing (String) on call to (Integer[]) is conversion match (String to Integer)
            CheckMatch2(new Type[] { typeof(string) }, new Type[] { typeof(int[]) }, tc, ArgumentsMatchKind.REQUIRES_CONVERSION);

            // Passing (Sub) on call to (Super[]) is close match
            CheckMatch2(new Type[] { typeof(Sub) }, new Type[] { typeof(Super[]) }, tc, ArgumentsMatchKind.CLOSE);

            // Passing (Super) on call to (Sub[]) is not a match
            CheckMatch2(new Type[] { typeof(Super) }, new Type[] { typeof(Sub[]) }, tc, null);

            CheckMatch2(new Type[] { typeof(Unconvertable), typeof(string) }, new Type[] { typeof(Sub), typeof(Super[]) }, tc, null);

            CheckMatch2(new Type[] { typeof(int), typeof(int), typeof(string) }, new Type[] { typeof(string), typeof(string), typeof(Super[]) }, tc, null);

            CheckMatch2(new Type[] { typeof(Unconvertable), typeof(string) }, new Type[] { typeof(Sub), typeof(Super[]) }, tc, null);

            CheckMatch2(new Type[] { typeof(int), typeof(int), typeof(string) }, new Type[] { typeof(string), typeof(string), typeof(Super[]) }, tc, null);

            CheckMatch2(new Type[] { typeof(int), typeof(int), typeof(Sub) }, new Type[] { typeof(string), typeof(string), typeof(Super[]) }, tc, ArgumentsMatchKind.REQUIRES_CONVERSION);

            CheckMatch2(new Type[] { typeof(int), typeof(int), typeof(int) }, new Type[] { typeof(int), typeof(string[]) }, tc, ArgumentsMatchKind.REQUIRES_CONVERSION);

            // what happens on (Integer,String) passed to (Integer[]) ?
        }
        public void TestReflectionHelperCompareArguments_RequiresConversionMatching()
        {
            var tc = new StandardTypeConverter();

            // Calling foo(String,int) with (String,Integer) requires boxing conversion of argument one
            CheckMatch(new Type[] { typeof(string), typeof(int) }, new Type[] { typeof(string), typeof(object) }, tc, ArgumentsMatchKind.CLOSE);

            // Passing (int,String) on call to foo(Integer,String) requires boxing conversion of argument zero
            CheckMatch(new Type[] { typeof(int), typeof(string) }, new Type[] { typeof(object), typeof(string) }, tc, ArgumentsMatchKind.CLOSE);

            // Passing (int,Sub) on call to foo(Integer,Super) requires boxing conversion of argument zero
            CheckMatch(new Type[] { typeof(int), typeof(Sub) }, new Type[] { typeof(object), typeof(Super) }, tc, ArgumentsMatchKind.CLOSE);

            // Passing (int,Sub,boolean) on call to foo(Integer,Super,Boolean) requires boxing conversion of arguments zero and two
            // TODO CheckMatch(new Type[] {Integer.TYPE, typeof(Sub), Boolean.TYPE}, new Type[] {typeof(int), typeof(Super), Boolean.class}, tc, ArgsMatchKind.REQUIRES_CONVERSION);
        }
示例#9
0
        public void TestStandardTypeConverter()
        {
            var tc = new StandardTypeConverter();

            tc.ConvertValue(3, typeof(int), typeof(double));
        }