public void CreateInitParameters_NullDictionary()
        {
            CodeMemberMethod method = new CodeMemberMethod();

            DecoratorUtil.AddInitializeParameters(method, "par", null);

            Assert.AreEqual(2, method.Statements.Count); // new and assignment
            Assert.That(method.Statements[0], Is.InstanceOf <CodeVariableDeclarationStatement>());
            Assert.That(method.Statements[1], Is.InstanceOf <CodeAssignStatement>());
        }
예제 #2
0
        public void DecorateClass(IService service, CodeTypeDeclaration serviceClass)
        {
            var method = new CodeMemberMethod();

            // Generate: private void InitParameters()
            method.Name       = InitParametersName;
            method.ReturnType = new CodeTypeReference(typeof(void));
            method.Attributes = MemberAttributes.Private;

            // Add request parameters initialization
            DecoratorUtil.AddInitializeParameters(method, "_serviceParameters", service.Parameters);

            serviceClass.Members.Add(method);
        }
        public void DecorateClass(IResource resource, IMethod request, CodeTypeDeclaration requestClass,
                                  CodeTypeDeclaration resourceClass)
        {
            var method = new CodeMemberMethod();

            // Generate: private void InitParameters()
            method.Name       = "InitParameters";
            method.ReturnType = new CodeTypeReference(typeof(void));
            method.Attributes = MemberAttributes.Private;

            // Add request parameters initialization
            DecoratorUtil.AddInitializeParameters(method, ParametersName, request.Parameters);

            requestClass.Members.Add(method);
        }
        public void CreateInitParameters_WithParameters()
        {
            CodeMemberMethod method = new CodeMemberMethod();

            var parameters = new Dictionary <string, IDiscoveryParameter>();

            parameters["param1"] = new MockParameter
            {
                Name          = "param1",
                DefaultValue  = "default",
                IsRequired    = true,
                Pattern       = "pattern",
                ParameterType = "type",
                EnumValues    = new[] { "1", "2" }
            };
            DecoratorUtil.AddInitializeParameters(method, "par", parameters);

            Assert.AreEqual(3, method.Statements.Count); // new, assignment and add parameter on dictionary methods
            Assert.That(method.Statements[0], Is.InstanceOf <CodeVariableDeclarationStatement>());

            // parameters.Add(param1, ...)
            Assert.That(method.Statements[1], Is.InstanceOf <CodeExpressionStatement>());
            CodeExpressionStatement exp = method.Statements[1] as CodeExpressionStatement;

            Assert.That(exp.Expression, Is.InstanceOf <CodeMethodInvokeExpression>());
            CodeMethodInvokeExpression addMethod = exp.Expression as CodeMethodInvokeExpression;

            Assert.AreEqual("Add", addMethod.Method.MethodName);
            Assert.AreEqual(2, addMethod.Parameters.Count);

            // parameters = param1, CreateRuntimeParameter(...)
            Assert.That(addMethod.Parameters[0], Is.InstanceOf <CodePrimitiveExpression>());
            var nameExp = addMethod.Parameters[0] as CodePrimitiveExpression;

            Assert.AreEqual("param1", nameExp.Value);
            Assert.That(addMethod.Parameters[1], Is.InstanceOf <CodeMethodInvokeExpression>());
            var createParameterExp = addMethod.Parameters[1] as CodeMethodInvokeExpression;

            Assert.AreEqual("CreateRuntimeParameter", createParameterExp.Method.MethodName);
            Assert.AreEqual(6, createParameterExp.Parameters.Count);

            // name
            Assert.That(createParameterExp.Parameters[0], Is.InstanceOf <CodePrimitiveExpression>());
            Assert.AreEqual("param1", (createParameterExp.Parameters[0] as CodePrimitiveExpression).Value);
            // is required
            Assert.That(createParameterExp.Parameters[1], Is.InstanceOf <CodePrimitiveExpression>());
            Assert.AreEqual(true, (createParameterExp.Parameters[1] as CodePrimitiveExpression).Value);
            // parameter type
            Assert.That(createParameterExp.Parameters[2], Is.InstanceOf <CodePrimitiveExpression>());
            Assert.AreEqual("type", (createParameterExp.Parameters[2] as CodePrimitiveExpression).Value);
            // default value
            Assert.That(createParameterExp.Parameters[3], Is.InstanceOf <CodePrimitiveExpression>());
            Assert.AreEqual("default", (createParameterExp.Parameters[3] as CodePrimitiveExpression).Value);
            // pattern
            Assert.That(createParameterExp.Parameters[4], Is.InstanceOf <CodePrimitiveExpression>());
            Assert.AreEqual("pattern", (createParameterExp.Parameters[4] as CodePrimitiveExpression).Value);
            // enum values
            Assert.That(createParameterExp.Parameters[5], Is.InstanceOf <CodeArrayCreateExpression>());
            Assert.AreEqual(2, (createParameterExp.Parameters[5] as CodeArrayCreateExpression).Initializers.Count);

            Assert.That(method.Statements[2], Is.InstanceOf <CodeAssignStatement>());
        }