public void SyncResolverMethodGenerator_GenerateWithSourceArgumentAndTwoArguments() { // arrange FieldResolverArgumentDescriptor argumentDescriptor1 = new FieldResolverArgumentDescriptor("a", "b", FieldResolverArgumentKind.Source, typeof(GeneratorTestDummy)); FieldResolverArgumentDescriptor argumentDescriptor2 = new FieldResolverArgumentDescriptor("b", "c", FieldResolverArgumentKind.Argument, typeof(string)); FieldResolverArgumentDescriptor argumentDescriptor3 = new FieldResolverArgumentDescriptor("c", "d", FieldResolverArgumentKind.Argument, typeof(int)); Type sourceType = typeof(GeneratorTestDummy); MethodInfo method = typeof(GeneratorTestDummyResolver).GetMethods() .Single(t => t.Name == "GetFoo" && t.GetParameters().Length == 3); FieldResolverDescriptor descriptor = FieldResolverDescriptor .CreateCollectionMethod(new FieldReference("Foo", "bar"), method.ReflectedType, sourceType, method, false, new[] { argumentDescriptor1, argumentDescriptor2, argumentDescriptor3 }); // act StringBuilder source = new StringBuilder(); SyncResolverMethodGenerator generator = new SyncResolverMethodGenerator(); string result = generator.Generate("abc", descriptor); // assert Assert.Equal(Snapshot.Current(), Snapshot.New(result)); }
public void SyncResolverMethodGenerator_GenerateWithoutArguments() { // arrange Type sourceType = typeof(GeneratorTestDummy); MethodInfo method = typeof(GeneratorTestDummyResolver).GetMethods() .Single(t => t.Name == "GetFoo" && t.GetParameters().Length == 0); FieldResolverDescriptor descriptor = FieldResolverDescriptor .CreateCollectionMethod(new FieldReference("Foo", "bar"), method.ReflectedType, sourceType, method, false, Enumerable.Empty <FieldResolverArgumentDescriptor>()); // act StringBuilder source = new StringBuilder(); SyncResolverMethodGenerator generator = new SyncResolverMethodGenerator(); string result = generator.Generate("abc", descriptor); // assert Assert.Equal(Snapshot.Current(), Snapshot.New(result)); }
private FieldResolverDescriptor CreateResolverDescriptor( FieldResolverMember fieldResolverMember, Type resolverType, Type sourceType) { if (fieldResolverMember.Member is PropertyInfo p) { return(FieldResolverDescriptor.CreateCollectionProperty( fieldResolverMember, resolverType, 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.CreateCollectionMethod( fieldResolverMember, resolverType, sourceType, m, isAsync, argumentDescriptors)); } throw new NotSupportedException(); }
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 FieldResolverArgumentDescriptor argumentDescriptor = FieldResolverArgumentDescriptor.Create("a", FieldResolverArgumentKind.Service, typeof(GeneratorTestDummy)); Type sourceType = typeof(GeneratorTestDummy); MethodInfo method = typeof(GeneratorTestDummyResolver).GetMethods() .Single(t => t.Name == "GetFooAsync" && t.GetParameters().Length == 1); FieldResolverDescriptor descriptor = FieldResolverDescriptor .CreateCollectionMethod(new FieldReference("Foo", "bar"), method.ReflectedType, sourceType, method, true, new[] { argumentDescriptor }); // act StringBuilder source = new StringBuilder(); AsyncResolverMethodGenerator generator = new AsyncResolverMethodGenerator(); string result = generator.Generate("abc", descriptor); // assert Assert.Equal(Snapshot.Current(), Snapshot.New(result)); }