Пример #1
0
 public virtual ConstructorSpec GetTypeConstructor(Type type)
 {
     return
         (type.GetConstructors().OrderByDescending(x => x.GetParameters().Length).Select(
              x => ConstructorSpec.FromConstructorInfo(x, defaultFactory: () => null)).FirstOrDefault(
              x => x != null));
 }
Пример #2
0
 public HttpMethod GetPropertyAccessMode(PropertyInfo propertyInfo, ConstructorSpec constructorSpec)
 {
     return(FromMappingOrDefault(
                propertyInfo.ReflectedType,
                propertyInfo,
                x =>
                GetCombinedPropertyAccessMode(propertyInfo, constructorSpec, x),
                () => this.wrappedFilter.GetPropertyAccessMode(propertyInfo, constructorSpec)));
 }
Пример #3
0
        private static ConstructorSpec GetConstructorSpecWithFiveArguments()
        {
            Expression <Func <IConstructorControl <Inherited>, Inherited> > expr =
                c =>
                new Inherited(c.Requires().TheUntouchable,
                              c.Requires().TheOverridable,
                              c.Requires().TheAbstract,
                              c.Requires().TheRequired,
                              c.Optional().TheOptional);
            var constructorSpec = new ConstructorSpec(expr);

            return(constructorSpec);
        }
Пример #4
0
        InjectingConstructorExpression_FromEntityWithNonNullablePropertyPairedWithNullableConstructorParam_BehavesCorrectly
            ()
        {
            Expression <Func <IConstructorControl <NullableTestClass>, NullableTestClass> > cspecExpr =
                c => new NullableTestClass(c.Optional().Foo);
            var cspec = new ConstructorSpec(cspecExpr);
            var func  = ((
                             Expression <Func <IConstructorPropertySource, NullableTestClass> >)
                         cspec.InjectingConstructorExpression).Compile();
            var result = func(new MockedPropertySourceHavingZeroProperties());

            Assert.That(result.Foo, Is.EqualTo(1337));
        }
Пример #5
0
        public virtual HttpMethod GetPropertyAccessMode(PropertyInfo propertyInfo, ConstructorSpec constructorSpec)
        {
            var mode = (propertyInfo.CanRead ? HttpMethod.Get : 0);

            if ((propertyInfo.CanWrite && propertyInfo.GetSetMethod() != null))
            {
                mode |= HttpMethod.Put | HttpMethod.Post;
            }
            if (constructorSpec != null)
            {
                var paramSpec = constructorSpec.GetParameterSpec(propertyInfo);
                if (paramSpec != null)
                {
                    mode |= HttpMethod.Post;
                }
            }
            return(mode);
        }
Пример #6
0
        private HttpMethod GetCombinedPropertyAccessMode(PropertyInfo propertyInfo,
                                                         ConstructorSpec constructorSpec,
                                                         PropertyMappingOptions opts)
        {
            var accessModeFromWrappedFilter = this.wrappedFilter.GetPropertyAccessMode(propertyInfo, constructorSpec);

            accessModeFromWrappedFilter |= PatchOfTypeIsAllowed(propertyInfo.PropertyType) ? HttpMethod.Patch : 0;

            Type elementType;

            if (propertyInfo.PropertyType.TryGetEnumerableElementType(out elementType))
            {
                accessModeFromWrappedFilter |=
                    (TypeIsMappedAsTransformedType(elementType) && PostOfTypeIsAllowed(elementType))
                        ? HttpMethod.Post
                        : 0;
            }
            return((opts.Method & opts.MethodMask) | (accessModeFromWrappedFilter & ~(opts.MethodMask)));
        }
Пример #7
0
        private static void AssertConstructorSpecWithFiveArguments(ConstructorSpec constructorSpec)
        {
            var pspecs = constructorSpec.ParameterSpecs.ToList();

            Assert.That(pspecs.Count, Is.EqualTo(5));
            Assert.That(pspecs[0].PropertyInfo.Name, Is.EqualTo("TheUntouchable"));
            Assert.That(pspecs[0].IsRequired, Is.True);

            Assert.That(pspecs[1].PropertyInfo.Name, Is.EqualTo("TheOverridable"));
            Assert.That(pspecs[1].IsRequired, Is.True);

            Assert.That(pspecs[2].PropertyInfo.Name, Is.EqualTo("TheAbstract"));
            Assert.That(pspecs[2].IsRequired, Is.True);

            Assert.That(pspecs[3].PropertyInfo.Name, Is.EqualTo("TheRequired"));
            Assert.That(pspecs[3].IsRequired, Is.True);

            Assert.That(pspecs[4].PropertyInfo.Name, Is.EqualTo("TheOptional"));
            Assert.That(pspecs[4].IsRequired, Is.False);

            Assert.That(pspecs.Select((x, i) => new { x.Position, i }).All(x => x.Position == x.i), Is.True);
        }
Пример #8
0
        public void DocumentationIsParsed()
        {
            ClassSpec classSpec = NapackFileAnalyzer.Analyze("Test", "unused", SampleStrings.LargeSampleClass).Classes.First();

            Assert.IsNotNull(classSpec.Name);
            Assert.IsNotNull(classSpec.Name.Name);
            Assert.IsNotNull(classSpec.Name.Documentation);

            Assert.IsTrue(classSpec.PublicMethods.Count != 0);
            MethodSpec method = classSpec.PublicMethods.First();

            Assert.IsNotNull(method.Name);
            Assert.IsNotNull(method.Name.Name);
            Assert.IsNotNull(method.Name.Documentation);

            Assert.IsTrue(classSpec.PublicConstructors.Count != 0);
            ConstructorSpec constructor = classSpec.PublicConstructors.First();

            Assert.IsNotNull(constructor.Name);
            Assert.IsNotNull(constructor.Name.Name);
            Assert.IsNotNull(constructor.Name.Documentation);
        }
Пример #9
0
        public override ConstructorSpec LoadConstructor(TypeSpec typeSpec)
        {
            var ria = typeSpec.DeclaredAttributes.OfType <ResourceInfoAttribute>().FirstOrDefault();

            if (ria == null)
            {
                return(typeSpec.OnLoadConstructor() ??
                       ConstructorSpec.FromConstructorInfo(
                           typeSpec.Type.GetConstructors(BindingFlags.Instance | BindingFlags.Public
                                                         | BindingFlags.NonPublic).First(),
                           defaultFactory: () => null));
            }

            if (ria.PocoType == null)
            {
                throw new NotSupportedException();
            }

            return(ConstructorSpec.FromConstructorInfo(
                       ria.PocoType.GetConstructors(BindingFlags.Instance | BindingFlags.NonPublic
                                                    | BindingFlags.Public).First(),
                       ria.InterfaceType));
        }
Пример #10
0
        public void ConstructorAndClassTest()
        {
            ClassSpec classSpec = NapackFileAnalyzer.Analyze("Test", "unused", SampleStrings.LargeSampleClass).Classes.First();

            Assert.IsTrue(classSpec.PublicConstructors.Count != 0);

            ConstructorSpec constructor = classSpec.PublicConstructors.First();

            bool oneHasDefault = false;

            foreach (ParameterSpec parameter in constructor.Parameters)
            {
                Assert.IsNotNull(parameter.Name);
                Assert.IsNotNull(parameter.Modifier);
                Assert.IsNotNull(parameter.Type);
                if (!string.IsNullOrWhiteSpace(parameter.Default))
                {
                    oneHasDefault = true;
                }
            }

            Assert.IsTrue(oneHasDefault);
        }
Пример #11
0
 public virtual HttpMethod GetPropertyAccessMode(PropertyInfo propertyInfo, ConstructorSpec constructorSpec)
 {
     return this.wrappedFilter.GetPropertyAccessMode(propertyInfo, constructorSpec);
 }
Пример #12
0
        public void FromConstructorInfo_ReturnsCorrectConstructorSpec()
        {
            var cspec = ConstructorSpec.FromConstructorInfo(typeof(Inherited).GetConstructors().First());

            AssertConstructorSpecWithFiveArguments(cspec);
        }
Пример #13
0
 public virtual HttpMethod GetPropertyAccessMode(PropertyInfo propertyInfo, ConstructorSpec constructorSpec)
 {
     return(this.wrappedFilter.GetPropertyAccessMode(propertyInfo, constructorSpec));
 }
Пример #14
0
        internal static ClassSpec AnalyzeClassSyntaxTree(string napackName, string filename, ClassDeclarationSyntax classNode)
        {
            ClassSpec classSpec = new ClassSpec();

            classSpec.Name       = DocumentedElement.LoadFromSyntaxNode(classNode);
            classSpec.IsAbstract = classNode.Modifiers.Any(modifier => modifier.IsKind(SyntaxKind.AbstractKeyword));
            classSpec.IsStatic   = classNode.Modifiers.Any(modifier => modifier.IsKind(SyntaxKind.StaticKeyword));
            classSpec.IsSealed   = classSpec.IsStatic || classNode.Modifiers.Any(modifier => modifier.IsKind(SyntaxKind.SealedKeyword));

            // Parse classes recursively
            foreach (ClassDeclarationSyntax node in classNode.ChildNodes().Where(node => node.IsKind(SyntaxKind.ClassDeclaration)))
            {
                if (node.Modifiers.Any(modifier => modifier.IsKind(SyntaxKind.PublicKeyword)) ||
                    (classSpec.ProtectedItemsConsideredPublic && node.Modifiers.Any(modifier => modifier.IsKind(SyntaxKind.ProtectedKeyword))))
                {
                    // This recursion will exit because we aren't *compiling* the code, but merely parsing it.
                    classSpec.PublicClasses.Add(AnalyzeClassSyntaxTree(napackName, filename, node));
                }
            }

            // Parse methods
            foreach (MethodDeclarationSyntax node in classNode.ChildNodes().Where(node => node.IsKind(SyntaxKind.MethodDeclaration)))
            {
                if (node.Modifiers.Any(modifier => modifier.IsKind(SyntaxKind.PublicKeyword)) ||
                    (classSpec.ProtectedItemsConsideredPublic && node.Modifiers.Any(modifier => modifier.IsKind(SyntaxKind.ProtectedKeyword))))
                {
                    classSpec.PublicMethods.Add(MethodSpec.LoadFromSyntaxNode(node));
                }
            }

            // Parse constructors
            foreach (ConstructorDeclarationSyntax node in classNode.ChildNodes().Where(node => node.IsKind(SyntaxKind.ConstructorDeclaration)))
            {
                if (node.Modifiers.Any(modifier => modifier.IsKind(SyntaxKind.PublicKeyword)) ||
                    (classSpec.ProtectedItemsConsideredPublic && node.Modifiers.Any(modifier => modifier.IsKind(SyntaxKind.ProtectedKeyword))))
                {
                    classSpec.PublicConstructors.Add(ConstructorSpec.LoadFromSyntaxNode(node));
                }
            }

            // Parse fields
            foreach (FieldDeclarationSyntax node in classNode.ChildNodes().Where(node => node.IsKind(SyntaxKind.FieldDeclaration)))
            {
                if (node.Modifiers.Any(modifier => modifier.IsKind(SyntaxKind.PublicKeyword)) ||
                    (classSpec.ProtectedItemsConsideredPublic && node.Modifiers.Any(modifier => modifier.IsKind(SyntaxKind.ProtectedKeyword))))
                {
                    classSpec.PublicFields.Add(FieldSpec.LoadFromSyntaxNode(node));
                }
            }

            // Parse properties
            foreach (PropertyDeclarationSyntax node in classNode.ChildNodes().Where(node => node.IsKind(SyntaxKind.PropertyDeclaration)))
            {
                if (node.Modifiers.Any(modifier => modifier.IsKind(SyntaxKind.PublicKeyword)) ||
                    (classSpec.ProtectedItemsConsideredPublic && node.Modifiers.Any(modifier => modifier.IsKind(SyntaxKind.ProtectedKeyword))))
                {
                    classSpec.PublicProperties.Add(PropertySpec.LoadFromSyntaxNode(node));
                }
            }

            return(classSpec);
        }
Пример #15
0
 public virtual HttpMethod GetPropertyAccessMode(PropertyInfo propertyInfo, ConstructorSpec constructorSpec)
 {
     var mode = (propertyInfo.CanRead ? HttpMethod.Get : 0);
     if ((propertyInfo.CanWrite && propertyInfo.GetSetMethod() != null))
         mode |= HttpMethod.Put | HttpMethod.Post;
     if (constructorSpec != null)
     {
         var paramSpec = constructorSpec.GetParameterSpec(propertyInfo);
         if (paramSpec != null)
             mode |= HttpMethod.Post;
     }
     return mode;
 }