public void TryCreateReturnValueParameterDescriptor_ReturnBindingPresent_ReturnsExpectedValue() { JObject json = new JObject { { "type", "blob" }, { "name", ScriptConstants.SystemReturnParameterBindingName }, { "direction", "out" }, { "path", "foo/bar" } }; FunctionBinding functionBinding = TestHelpers.CreateTestBinding(json); FunctionBinding[] bindings = new FunctionBinding[] { functionBinding }; ParameterDescriptor descriptor = null; var result = DotNetFunctionDescriptorProvider.TryCreateReturnValueParameterDescriptor(typeof(string), bindings, out descriptor); Assert.True(result); Assert.Equal(ScriptConstants.SystemReturnParameterName, descriptor.Name); Assert.True((descriptor.Attributes & ParameterAttributes.Out) != 0); Assert.Equal(typeof(string).MakeByRefType(), descriptor.Type); Assert.Equal(1, descriptor.CustomAttributes.Count); result = DotNetFunctionDescriptorProvider.TryCreateReturnValueParameterDescriptor(typeof(Task <string>), bindings, out descriptor); Assert.True(result); Assert.Equal(ScriptConstants.SystemReturnParameterName, descriptor.Name); Assert.True((descriptor.Attributes & ParameterAttributes.Out) != 0); Assert.Equal(typeof(string).MakeByRefType(), descriptor.Type); Assert.Equal(1, descriptor.CustomAttributes.Count); // return type task means no return value result = DotNetFunctionDescriptorProvider.TryCreateReturnValueParameterDescriptor(typeof(Task), bindings, out descriptor); Assert.False(result); }
public async Task Generate_WithReturnValue(string userFuncName, Type generatedMethodReturnType, string expectedResult) { var userFunc = this.GetType().GetMethod(userFuncName, BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic); string functionName = "FunctionWithStrReturn"; Collection <ParameterDescriptor> parameters = new Collection <ParameterDescriptor>(); var userRetType = userFunc.ReturnType; ParameterDescriptor returnParameter; if (DotNetFunctionDescriptorProvider.TryCreateReturnValueParameterDescriptor(userRetType, new FunctionBinding[0], out returnParameter)) { parameters.Add(returnParameter); } FunctionMetadata metadata = new FunctionMetadata(); var invoker = new RealInvoker(userFunc); FunctionDescriptor function = new FunctionDescriptor(functionName, invoker, metadata, parameters, null, null, null); Collection <FunctionDescriptor> functions = new Collection <FunctionDescriptor>(); functions.Add(function); // generate the Type Type functionType = FunctionGenerator.Generate("TestScriptHost", "TestFunctions", null, functions); // verify the generated function MethodInfo method = functionType.GetMethod(functionName); IEnumerable <Attribute> attributes = functionType.GetCustomAttributes(); Assert.Empty(attributes); ParameterInfo[] functionParams = method.GetParameters(); // One input parameter Assert.Equal(0, functionParams.Length); // The final generated function is always Task Assert.Equal(generatedMethodReturnType, method.ReturnType); // Verify that the method is invocable var result = method.Invoke(null, new object[] { }); Task <object> taskResult = Unwrap(result); var realResult = await taskResult; Assert.Equal(expectedResult, realResult); }
public void TryCreateReturnValueParameterDescriptor_NoReturnBinding_ReturnsExpectedValue() { JObject json = new JObject { { "type", "httpTrigger" }, { "name", "myInput" }, { "direction", "in" } }; FunctionBinding functionBinding = TestHelpers.CreateTestBinding(json); FunctionBinding[] bindings = new FunctionBinding[] { functionBinding }; ParameterDescriptor descriptor = null; var result = DotNetFunctionDescriptorProvider.TryCreateReturnValueParameterDescriptor(typeof(void), bindings, out descriptor); Assert.False(result); }
public void TryCreateReturnValueParameterDescriptor_ReturnBindingPresent_ReturnsExpectedValue() { JObject json = new JObject { { "type", "blob" }, { "name", ScriptConstants.SystemReturnParameterBindingName }, { "direction", "out" }, { "path", "foo/bar" } }; FunctionBinding functionBinding = TestHelpers.CreateTestBinding(json); FunctionBinding[] bindings = new FunctionBinding[] { functionBinding }; ParameterDescriptor descriptor = null; var result = DotNetFunctionDescriptorProvider.TryCreateReturnValueParameterDescriptor(typeof(string), bindings, out descriptor); Assert.True(result); Assert.Equal(ScriptConstants.SystemReturnParameterName, descriptor.Name); Assert.True((descriptor.Attributes & ParameterAttributes.Out) != 0); Assert.Equal(typeof(string).MakeByRefType(), descriptor.Type); Assert.Equal(1, descriptor.CustomAttributes.Count); result = DotNetFunctionDescriptorProvider.TryCreateReturnValueParameterDescriptor(typeof(Task <string>), bindings, out descriptor); Assert.True(result); Assert.Equal(ScriptConstants.SystemReturnParameterName, descriptor.Name); Assert.True((descriptor.Attributes & ParameterAttributes.Out) != 0); Assert.Equal(typeof(string).MakeByRefType(), descriptor.Type); Assert.Equal(1, descriptor.CustomAttributes.Count); var ex = Assert.Throws <InvalidOperationException>(() => { DotNetFunctionDescriptorProvider.TryCreateReturnValueParameterDescriptor(typeof(Task), bindings, out descriptor); }); Assert.Equal($"{ScriptConstants.SystemReturnParameterBindingName} cannot be bound to return type {typeof(Task).Name}.", ex.Message); }