コード例 #1
0
        public void GenericMethod_WithConstraints()
        {
            const string code = @"
            public class Class1
            {
                public void Method1<T>(T param) where T : struct
                {

                }
            }
            ";

            CSharpParser parser = new CSharpParser();
            parser.ParseCode(code);

            if (parser.ErrorOccurred)
                Assert.Fail(parser.GetFormattedErrors());

            ICodeRoot codeRoot = parser.CreatedCodeRoot;
            Class clazz = (Class)codeRoot.WalkChildren()[0];

            Function function = (Function)clazz.WalkChildren()[0];
            Assert.That(function.Name, Is.EqualTo("Method1"));
            Assert.That(function.GenericParameters.Count, Is.EqualTo(1));
            Assert.That(function.GenericParameters[0], Is.EqualTo("T"));
            Assert.That(function.GenericConstraintClause, Is.EqualTo("where T : struct"));
            Assert.That(function.ReturnType.ToString(), Is.EqualTo("void"));
            Assert.That(function.Parameters, Has.Count(1));
            Assert.That(function.Parameters[0].Name, Is.EqualTo("param"));
            Assert.That(function.Parameters[0].DataType, Is.EqualTo("T"));
        }
コード例 #2
0
        public void GenericInterfaceMethod_WithConstraints()
        {
            const string code = @"
            public interface Interface1
            {
                string Method1<T>(int param, T i) where T : class;
            }
            ";

            CSharpParser parser = new CSharpParser();
            parser.ParseCode(code);

            if(parser.ErrorOccurred)
                Assert.Fail(parser.GetFormattedErrors());

            ICodeRoot codeRoot = parser.CreatedCodeRoot;
            Interface clazz = (Interface)codeRoot.WalkChildren()[0];

            InterfaceMethod inter = (InterfaceMethod)clazz.WalkChildren()[0];
            Assert.That(inter.Name, Is.EqualTo("Method1"));
            Assert.That(inter.GenericParameters.Count, Is.EqualTo(1));
            Assert.That(inter.GenericParameters[0], Is.EqualTo("T"));
            Assert.That(inter.GenericConstraintClause, Is.EqualTo("where T : class"));
            Assert.That(inter.ReturnType.ToString(), Is.EqualTo("string"));
            Assert.That(inter.Parameters, Has.Count(2));
            Assert.That(inter.Parameters[0].Name, Is.EqualTo("param"));
            Assert.That(inter.Parameters[0].DataType, Is.EqualTo("int"));
            Assert.That(inter.Parameters[1].Name, Is.EqualTo("i"));
            Assert.That(inter.Parameters[1].DataType, Is.EqualTo("T"));
        }