Пример #1
0
        public void SyncSourceMethodGenerator_GenerateWithTwoArgument()
        {
            // arrange
            FieldResolverArgumentDescriptor argumentDescriptor1 =
                FieldResolverArgumentDescriptor.Create("a",
                                                       FieldResolverArgumentKind.Argument,
                                                       typeof(string));

            FieldResolverArgumentDescriptor argumentDescriptor2 =
                FieldResolverArgumentDescriptor.Create("b",
                                                       FieldResolverArgumentKind.Argument,
                                                       typeof(int));

            MethodInfo method = typeof(GeneratorTestDummy).GetMethods()
                                .Single(t => t.Name == "GetFoo" && t.GetParameters().Length == 2);
            FieldResolverDescriptor descriptor = FieldResolverDescriptor
                                                 .CreateSourceMethod(new FieldReference("Foo", "bar"),
                                                                     method.ReflectedType, method, false,
                                                                     new[] { argumentDescriptor1, argumentDescriptor2 });

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

            // assert
            Assert.Equal(Snapshot.Current(), Snapshot.New(result));
        }
Пример #2
0
        public void AsyncResolverMethodGenerator_GenerateWithSourceArgumentAndTwoArguments()
        {
            // arrange
            FieldResolverArgumentDescriptor argumentDescriptor1 =
                FieldResolverArgumentDescriptor.Create("a",
                                                       FieldResolverArgumentKind.Source,
                                                       typeof(GeneratorTestDummy));

            FieldResolverArgumentDescriptor argumentDescriptor2 =
                FieldResolverArgumentDescriptor.Create("b",
                                                       FieldResolverArgumentKind.Argument,
                                                       typeof(string));

            FieldResolverArgumentDescriptor argumentDescriptor3 =
                FieldResolverArgumentDescriptor.Create("c",
                                                       FieldResolverArgumentKind.Argument,
                                                       typeof(int));

            Type       sourceType = typeof(GeneratorTestDummy);
            MethodInfo method     = typeof(GeneratorTestDummyResolver).GetMethods()
                                    .Single(t => t.Name == "GetFooAsync" && t.GetParameters().Length == 3);
            FieldResolverDescriptor descriptor = FieldResolverDescriptor
                                                 .CreateCollectionMethod(new FieldReference("Foo", "bar"),
                                                                         method.ReflectedType, sourceType, method, true,
                                                                         new[] { argumentDescriptor1, argumentDescriptor2, argumentDescriptor3 });

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

            // assert
            Assert.Equal(Snapshot.Current(), Snapshot.New(result));
        }
        internal static IReadOnlyCollection <FieldResolverArgumentDescriptor> CreateResolverArgumentDescriptors(
            MethodInfo method, Type resolverType, Type sourceType)
        {
            List <FieldResolverArgumentDescriptor> arguments =
                new List <FieldResolverArgumentDescriptor>();

            foreach (ParameterInfo parameter in method.GetParameters())
            {
                FieldResolverArgumentKind kind = FieldResolverArgumentDescriptor
                                                 .LookupKind(parameter.ParameterType, sourceType);
                arguments.Add(FieldResolverArgumentDescriptor.Create(
                                  parameter.Name, kind, parameter.ParameterType));
            }

            return(arguments);
        }
        internal static IReadOnlyCollection <FieldResolverArgumentDescriptor> CreateResolverArgumentDescriptors(
            MethodInfo method, Type sourceType)
        {
            var arguments = new List <FieldResolverArgumentDescriptor>();
            int i         = 0;

            foreach (ParameterInfo parameter in method.GetParameters())
            {
                FieldResolverArgumentKind kind = FieldResolverArgumentDescriptor
                                                 .LookupKind(parameter.ParameterType, sourceType);
                string variableName = $"v{i++}_{parameter.Name}";
                arguments.Add(new FieldResolverArgumentDescriptor(
                                  parameter.Name, variableName, kind, parameter.ParameterType));
            }

            return(arguments);
        }
        public void CreateAsyncCollectionMethodResolver()
        {
            // arrange
            Mock <IResolverContext> context = new Mock <IResolverContext>(MockBehavior.Strict);

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

            FieldResolverArgumentDescriptor argumentDescriptor =
                new FieldResolverArgumentDescriptor(
                    "foo", "b", FieldResolverArgumentKind.Source,
                    typeof(FooType));

            FieldReference          fieldReference = new FieldReference("type", "field");
            FieldResolverDescriptor descriptor     = FieldResolverDescriptor
                                                     .CreateCollectionMethod(fieldReference,
                                                                             typeof(FooTypeResolver), typeof(FooType),
                                                                             typeof(FooTypeResolver).GetMethod("BarResolverAsync"), true,
                                                                             new[] { argumentDescriptor });

            // 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 = ((Task <object>)r.Resolver(context.Object, CancellationToken.None)).Result;
                Assert.Equal("Hello World_123", result);
            });
        }
        public void AsyncResolverMethodGenerator_GenerateWithQueryService()
        {
            // arrange
            var argumentDescriptor =
                new FieldResolverArgumentDescriptor("a", "b",
                                                    FieldResolverArgumentKind.Service,
                                                    typeof(GeneratorTestDummy));

            var        sourceType = typeof(GeneratorTestDummy);
            MethodInfo method     = typeof(GeneratorTestDummyResolver).GetMethods()
                                    .Single(t => t.Name == "GetFooAsync" && t.GetParameters().Length == 1);
            var descriptor = FieldResolverDescriptor
                             .CreateCollectionMethod(new FieldReference("Foo", "bar"),
                                                     method.ReflectedType, sourceType, method, true,
                                                     new[] { argumentDescriptor });

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

            // assert
            Assert.Equal(Snapshot.Current(), Snapshot.New(result));
        }