public void CreateSourcePropertyResolver()
        {
            // arrange
            Mock <IResolverContext> context = new Mock <IResolverContext>(MockBehavior.Strict);

            context.Setup(t => t.Parent <FooType>()).Returns(new FooType());

            FieldReference          fieldReference = new FieldReference("type", "field");
            FieldResolverDescriptor descriptor     = FieldResolverDescriptor
                                                     .CreateSourceProperty(fieldReference, typeof(FooType),
                                                                           typeof(FooType).GetProperty("BarProperty"));

            // act
            FieldResolverBuilder fieldResolverBuilder = new FieldResolverBuilder();

            FieldResolver[] resolvers = fieldResolverBuilder.Build(
                new[] { descriptor }).ToArray();

            // assert
            Assert.Collection(resolvers,
                              r =>
            {
                Assert.Equal("type", r.TypeName);
                Assert.Equal("field", r.FieldName);
                Assert.NotNull(r.Resolver);

                object result = r.Resolver(context.Object, CancellationToken.None);
                Assert.Equal("Hello World Property", result);
            });
        }
예제 #2
0
        public void SourcePropertyGenerator_Generate()
        {
            // arrange
            PropertyInfo            property   = typeof(GeneratorTestDummy).GetProperties().Single();
            FieldResolverDescriptor descriptor = FieldResolverDescriptor
                                                 .CreateSourceProperty(new FieldReference("Foo", "bar"),
                                                                       property.ReflectedType, property);

            // act
            StringBuilder           source    = new StringBuilder();
            SourcePropertyGenerator generator = new SourcePropertyGenerator();
            string result = generator.Generate("abc", descriptor);

            // assert
            Assert.Equal(Snapshot.Current(), Snapshot.New(result));
        }
        private FieldResolverDescriptor CreateSourceResolverDescriptor(
            FieldResolverMember fieldResolverMember,
            Type resolverType, Type sourceType)
        {
            if (fieldResolverMember.Member is PropertyInfo p)
            {
                return(FieldResolverDescriptor.CreateSourceProperty(
                           fieldResolverMember, sourceType, p));
            }
            else if (fieldResolverMember.Member is MethodInfo m)
            {
                bool isAsync = typeof(Task).IsAssignableFrom(m.ReturnType);
                IReadOnlyCollection <FieldResolverArgumentDescriptor> argumentDescriptors =
                    CreateResolverArgumentDescriptors(m, resolverType, sourceType);
                return(FieldResolverDescriptor.CreateSourceMethod(
                           fieldResolverMember, sourceType, m, isAsync,
                           argumentDescriptors));
            }

            throw new NotSupportedException();
        }