public void InvalidChoiceValueForParameterHasNullCanonicalValueTest()
        {
            ITemplateParameter param = new MockParameter()
            {
                Name    = "TestName",
                Choices = new Dictionary <string, string>()
                {
                    { "foo", "Foo value" },
                    { "bar", "Bar value" }
                }
            };
            IReadOnlyList <ITemplateParameter> parametersForTemplate = new List <ITemplateParameter>()
            {
                param
            };

            ITemplateInfo template = new MockTemplateInfo()
            {
                Parameters = parametersForTemplate
            };

            string canonical = TelemetryHelper.GetCanonicalValueForChoiceParamOrDefault(template, "TestName", "whatever");

            Assert.Null(canonical);
        }
        public void UniqueStartsWithValueResolvesCanonicalValueTest()
        {
            ITemplateParameter param = new MockParameter()
            {
                Name    = "TestName",
                Choices = new Dictionary <string, string>()
                {
                    { "foo", "Foo value" },
                    { "bar", "Bar value" }
                }
            };
            IReadOnlyList <ITemplateParameter> parametersForTemplate = new List <ITemplateParameter>()
            {
                param
            };

            ITemplateInfo template = new MockTemplateInfo()
            {
                Parameters = parametersForTemplate
            };

            string canonical = TelemetryHelper.GetCanonicalValueForChoiceParamOrDefault(template, "TestName", "f");

            Assert.Equal("foo", canonical);
        }
        public void ChoiceValueCaseDifferenceIsAMatchTest()
        {
            ITemplateParameter param = new MockParameter()
            {
                Name    = "TestName",
                Choices = new Dictionary <string, string>()
                {
                    { "foo", "Foo value" },
                    { "bar", "Bar value" }
                }
            };
            IReadOnlyList <ITemplateParameter> parametersForTemplate = new List <ITemplateParameter>()
            {
                param
            };

            ITemplateInfo template = new MockTemplateInfo()
            {
                Parameters = parametersForTemplate
            };

            string canonical = TelemetryHelper.GetCanonicalValueForChoiceParamOrDefault(template, "TestName", "FOO");

            Assert.Equal("foo", canonical);
        }
        public void AmbiguousStartsWithValueHasNullCanonicalValueTest()
        {
            ITemplateParameter param = new MockParameter()
            {
                Name    = "TestName",
                Choices = new Dictionary <string, ParameterChoice>()
                {
                    { "foo", new ParameterChoice("Foo", "Foo value") },
                    { "bar", new ParameterChoice("Bar", "Bar value") },
                    { "foot", new ParameterChoice("Foot", "Foot value") }
                }
            };
            IReadOnlyList <ITemplateParameter> parametersForTemplate = new List <ITemplateParameter>()
            {
                param
            };

            ITemplateInfo template = new MockTemplateInfo()
            {
                Parameters = parametersForTemplate
            };

            string canonical = TelemetryHelper.GetCanonicalValueForChoiceParamOrDefault(template, "TestName", "f");

            Assert.Null(canonical);
        }
        public void ValidChoiceForParameterIsItsOwnCanonicalValueTest()
        {
            ITemplateParameter param = new MockParameter()
            {
                Name    = "TestName",
                Choices = new Dictionary <string, ParameterChoice>()
                {
                    { "foo", new ParameterChoice("Foo", "Foo value") },
                    { "bar", new ParameterChoice("Bar", "Bar value") }
                }
            };
            IReadOnlyList <ITemplateParameter> parametersForTemplate = new List <ITemplateParameter>()
            {
                param
            };

            ITemplateInfo template = new MockTemplateInfo()
            {
                Parameters = parametersForTemplate
            };

            string canonical = TelemetryHelper.GetCanonicalValueForChoiceParamOrDefault(template, "TestName", "foo");

            Assert.Equal("foo", canonical);
        }
        public void ChoiceValueCaseDifferencesContributeToAmbiguousMatchTest()
        {
            ITemplateParameter param = new MockParameter()
            {
                Name    = "TestName",
                Choices = new Dictionary <string, ParameterChoice>()
                {
                    { "foot", new ParameterChoice("Foo", "Foo value") },
                    { "bar", new ParameterChoice("Bar", "Bar value") },
                    { "Football", new ParameterChoice("Football", "Foo value") },
                    { "FOOTPOUND", new ParameterChoice("Footpound", "Foo value") }
                }
            };
            IReadOnlyList <ITemplateParameter> parametersForTemplate = new List <ITemplateParameter>()
            {
                param
            };

            ITemplateInfo template = new MockTemplateInfo()
            {
                Parameters = parametersForTemplate
            };

            string canonical = TelemetryHelper.GetCanonicalValueForChoiceParamOrDefault(template, "TestName", "foo");

            Assert.Null(canonical);
        }
        public void TestGetParameterType()
        {
            Assert.Throws(typeof(ArgumentNullException), () => ResourceBaseGenerator.GetParameterType(null));

            MockParameter param = new MockParameter();

            // Null => string.
            param.ValueType = null;
            Assert.AreEqual(typeof(string), ResourceBaseGenerator.GetParameterType(param));

            // "" => string.
            param.ValueType = "";
            Assert.AreEqual(typeof(string), ResourceBaseGenerator.GetParameterType(param));

            // "string" => string.
            param.ValueType = "string";
            Assert.AreEqual(typeof(string), ResourceBaseGenerator.GetParameterType(param));

            // "integer" => long.
            param.ValueType = "integer";
            Assert.AreEqual(typeof(long), ResourceBaseGenerator.GetParameterType(param));

            // "boolean" => bool.
            param.ValueType = "boolean";
            Assert.AreEqual(typeof(bool), ResourceBaseGenerator.GetParameterType(param));

            // "AnyOldRubbish" => string.
            param.ValueType = "AGreatBigFish";
            Assert.AreEqual(typeof(string), ResourceBaseGenerator.GetParameterType(param));

            // repeatable "string" => Repeatable<string>.
            param.IsRepeatable = true;
            param.ValueType    = "string";
            Assert.AreEqual(typeof(Repeatable <string>), ResourceBaseGenerator.GetParameterType(param));
        }
Esempio n. 8
0
        public void CreateConstructorTest()
        {
            var parameter = new MockParameter()
            {
                Name = "Param", IsRequired = true
            };
            var method = new MockMethod()
            {
                Name = "Method", Parameters = new Dictionary <string, IParameter>()
            };

            method.Parameters.Add("Param", parameter);
            var resource = new MockResource();

            resource.Methods.Add("Method", method);
            var resourceDecl = new CodeTypeDeclaration();
            var typeProvider = new DefaultObjectTypeProvider("Schema");

            // Confirm that the "service" parameter is added.
            var             decorator   = new RequestConstructorDecorator(typeProvider);
            CodeConstructor constructor = decorator.CreateRequiredConstructor(resourceDecl, method, false);

            Assert.AreEqual(2, constructor.Parameters.Count);
            Assert.AreEqual("service", constructor.Parameters[0].Name);
            Assert.AreEqual(1, constructor.BaseConstructorArgs.Count);
        }
Esempio n. 9
0
        public void TestLuceneNet472()
        {
            var thing      = new MockParameter("THING");
            var otherThing = new MockParameter("OTHERTHING");

            // LUCENENET-472 - NRE on ==/!= parameter
            Assert.IsTrue(thing != null);
            Assert.IsFalse(thing == null);
            Assert.IsTrue(otherThing != null);
        }
Esempio n. 10
0
        public void IsTestMethodReturnsFalseWhenMethodHasHasParameters()
        {
            MockAttribute testAttribute = new MockAttribute("Test");
            MockMethod    mockMethod    = MockMethod.CreateMockMethodWithAttribute(testAttribute);
            MockParameter mockParameter = new MockParameter();

            mockMethod.Parameters.Add(mockParameter);

            Assert.IsFalse(testFramework.IsTestMethod(mockMethod));
        }
Esempio n. 11
0
        public void MethodHasParameters()
        {
            List <MockAttribute> attributes = new List <MockAttribute>();

            attributes.Add(new MockAttribute("Test"));
            MockMethod    mockMethod    = CreateMockMethod(attributes);
            MockParameter mockParameter = new MockParameter();

            mockMethod.Parameters.Add(mockParameter);

            Assert.IsFalse(TestMethod.IsTestMethod(mockMethod));
        }
Esempio n. 12
0
        public void IsTestMember_MethodHasParameters_ReturnsFalse()
        {
            CreateTestFramework();
            var        testAttribute = new MockAttribute("Test");
            MockMethod mockMethod    = MockMethod.CreateMockMethodWithAttribute(testAttribute);
            var        mockParameter = new MockParameter();

            mockMethod.Parameters.Add(mockParameter);

            bool result = testFramework.IsTestMember(mockMethod);

            Assert.IsFalse(result);
        }
Esempio n. 13
0
        public void CreateParameterComment()
        {
            var instance       = new DefaultEnglishCommentCreator();
            var parameter      = new MockParameter();
            var progamaticName = "testParameter";

            CodeCommentStatementCollection result = instance.CreateParameterComment(parameter, progamaticName);

            Assert.IsNotNull(result);
            Assert.AreEqual(1, result.Count);
            Assert.IsNotNull(result[0].Comment);
            Assert.AreEqual("<param name=\"testParameter\">Optional</param>", result[0].Comment.Text);
        }
        public void TestGetParameterTypeReference()
        {
            MockParameter param = new MockParameter()
            {
                Name = "Parameter"
            };
            CodeTypeDeclaration decl = new CodeTypeDeclaration();
            CodeTypeReference   refType;

            param.IsRequired = true;

            // Normal string.
            param.ValueType = "string";
            refType         = ResourceBaseGenerator.GetParameterTypeReference(decl, param);
            Assert.AreEqual(typeof(string).FullName, refType.BaseType);

            // Normal int
            param.ValueType = "integer";
            refType         = ResourceBaseGenerator.GetParameterTypeReference(decl, param);
            Assert.AreEqual(typeof(long).FullName, refType.BaseType);

            // optional int
            param.IsRequired = false;
            param.ValueType  = "integer";
            refType          = ResourceBaseGenerator.GetParameterTypeReference(decl, param);
            Assert.AreEqual(typeof(Nullable <>).FullName, refType.BaseType);
            Assert.AreEqual(1, refType.TypeArguments.Count);
            Assert.AreEqual(typeof(Int64).FullName, refType.TypeArguments[0].BaseType);

            // Enumeration
            param.IsRequired            = true;
            param.ValueType             = "string";
            param.EnumValues            = new[] { "TestA", "TestB" };
            param.EnumValueDescriptions = new[] { "DescA", "DescB" };
            decl.Members.Add(EnumResourceDecorator.GenerateEnum(
                                 decl, "TestEnum", null, param.EnumValues, param.EnumValueDescriptions));
            refType = ResourceBaseGenerator.GetParameterTypeReference(decl, param);
            Assert.AreEqual("TestEnum", refType.BaseType);

            // Optional enumeration
            param.IsRequired            = false;
            param.ValueType             = "string";
            param.EnumValues            = new[] { "TestA", "TestB" };
            param.EnumValueDescriptions = new[] { "DescA", "DescB" };
            refType = ResourceBaseGenerator.GetParameterTypeReference(decl, param);
            Assert.AreEqual(typeof(Nullable <>).FullName, refType.BaseType);
            Assert.AreEqual(1, refType.TypeArguments.Count);
            Assert.AreEqual("TestEnum", refType.TypeArguments[0].BaseType);
        }
Esempio n. 15
0
        public void GetParameterMetaData()
        {
            var instance       = new DefaultEnglishCommentCreator();
            var parameter      = new MockParameter();
            var progamaticName = "testParameter";

            parameter.Name       = progamaticName;
            parameter.IsRequired = false;

            string result = instance.GetParameterMetaData(parameter, progamaticName);

            Assert.AreEqual("Optional", result);

            parameter.IsRequired = true;
            result = instance.GetParameterMetaData(parameter, progamaticName);
            Assert.AreEqual("Required", result);

            parameter.Name = "test-parameters";
            result         = instance.GetParameterMetaData(parameter, progamaticName);
            Assert.AreEqual("test-parameters - Required", result);

            parameter.Name    = progamaticName;
            parameter.Minimum = "53";
            result            = instance.GetParameterMetaData(parameter, progamaticName);
            Assert.AreEqual("Required - Minimum value of 53", result);

            parameter.Minimum = null;
            parameter.Maximum = "105";
            result            = instance.GetParameterMetaData(parameter, progamaticName);
            Assert.AreEqual("Required - Maximum value of 105", result);

            parameter.Maximum = null;
            parameter.Pattern = ".*\\.java";
            result            = instance.GetParameterMetaData(parameter, progamaticName);
            Assert.AreEqual("Required - Must match pattern .*\\.java", result);

            parameter.Pattern    = null;
            parameter.EnumValues = new List <string> {
                "a", "b", "c", "d"
            };
            result = instance.GetParameterMetaData(parameter, progamaticName);
            Assert.AreEqual("Required - Must be one of the following values [a, b, c, d]", result);

            parameter.EnumValues  = null;
            parameter.Description = "A Test Description";
            result = instance.GetParameterMetaData(parameter, progamaticName);
            Assert.AreEqual("Required - A Test Description", result);
        }
Esempio n. 16
0
        public void CreateParameterCommentValidateParams()
        {
            var    instance       = new DefaultEnglishCommentCreator();
            var    parameter      = new MockParameter();
            string progamaticName = null;

            Assert.Throws(
                typeof(ArgumentNullException), () => instance.CreateParameterComment(parameter, progamaticName));

            progamaticName = "";
            Assert.Throws(typeof(ArgumentException), () => instance.CreateParameterComment(parameter, progamaticName));

            progamaticName = "testParameter";
            parameter      = null;
            Assert.Throws(
                typeof(ArgumentNullException), () => instance.CreateParameterComment(parameter, progamaticName));
        }
Esempio n. 17
0
        // fills in enough of the parameter info for alias assignment
        private static IReadOnlyList <ITemplateParameter> ParameterNamesToParametersTransform(IReadOnlyList <string> paramNameList)
        {
            List <ITemplateParameter> parameterList = new List <ITemplateParameter>();

            foreach (string paramName in paramNameList)
            {
                ITemplateParameter parameter = new MockParameter()
                {
                    Name     = paramName,
                    Priority = TemplateParameterPriority.Required,
                };

                parameterList.Add(parameter);
            }

            return(parameterList);
        }
Esempio n. 18
0
        public void TestEquals()
        {
            var first = new MockParameter("FIRST");
            var other = new MockParameter("OTHER");

            // Make sure it's equal against itself
            Assert.AreEqual(first, first);
            // Not equal if it has a different name
            Assert.AreNotEqual(first, other);

            // Test == operator
            Assert.IsTrue(first == first);
            Assert.IsFalse(first == other);

            // Test != operator
            Assert.IsFalse(first != first);
            Assert.IsTrue(first != other);
        }
Esempio n. 19
0
        public void MethodWith_LimitParameters_HasNotAnalyzerError()
        {
            var methodParameterRule = QualityRules.MethodParametersCount;
            var maximumParameters   = new Metric().MaximumNumberOfMethodParameters;

            var methodName       = "newMethod";
            var methodParameters = MockParameter.ParameterDeclarations(maximumParameters - 1);
            var method           = MockMethod.Method(methodName, methodParameters);

            var parameterAnalyzer = new ParametersCount(TestBootStrapped.GetInstanceOfIMetric());

            parameterAnalyzer.Analyze(method);
            var analyzeResult = parameterAnalyzer.GetResult();

            var hasAcceptableNumberOfParameters = !analyzeResult.Any(a => a.ViolatedRule.Equals(methodParameterRule));

            Assert.IsTrue(hasAcceptableNumberOfParameters);
        }
Esempio n. 20
0
        public void AddRequiredParametersTest()
        {
            var parameterA = new MockParameter()
            {
                Name = "ParamA", IsRequired = false
            };
            var parameterB = new MockParameter()
            {
                Name = "ParamB", IsRequired = true
            };
            var method = new MockMethod()
            {
                Name       = "Method",
                Parameters = new Dictionary <string, IDiscoveryParameter>()
            };

            method.Parameters.Add("ParamA", parameterA);
            method.Parameters.Add("ParamB", parameterB);
            var resource = new MockResource();

            resource.Methods.Add("Method", method);
            var resourceDecl = new CodeTypeDeclaration();
            var typeProvider = new DefaultObjectTypeProvider("Schema");

            // Confirm that the "service" parameter is added.
            var             decorator   = new RequestConstructorDecorator(typeProvider);
            CodeConstructor constructor = new CodeConstructor();

            decorator.AddRequestParameters(resourceDecl, method, constructor, false);

            Assert.AreEqual(1, constructor.Parameters.Count);
            Assert.AreEqual("paramB", constructor.Parameters[0].Name);
            Assert.AreEqual(1, constructor.Statements.Count);

            // Check that optional parameters are added when the appropriate flag is set.
            constructor = new CodeConstructor();
            decorator.AddRequestParameters(resourceDecl, method, constructor, true);

            Assert.AreEqual(2, constructor.Parameters.Count);
            Assert.AreEqual("paramB", constructor.Parameters[0].Name);
            Assert.AreEqual("paramA", constructor.Parameters[1].Name);
            Assert.AreEqual(2, constructor.Statements.Count);
        }
Esempio n. 21
0
        public void UnknownParameterNameHasNullCanonicalValueTest()
        {
            ITemplateParameter param = new MockParameter()
            {
                Name    = "TestName",
                Choices = null
            };
            IReadOnlyList <ITemplateParameter> parametersForTemplate = new List <ITemplateParameter>()
            {
                param
            };

            ITemplateInfo template = new MockTemplateInfo()
            {
                Parameters = parametersForTemplate
            };

            string canonical = TelemetryHelper.GetCanonicalValueForChoiceParamOrDefault(template, "OtherName", "whatever");

            Assert.Null(canonical);
        }
        public void GenerateParameterPropertyTest()
        {
            var parameter = new MockParameter()
            {
                Name = "Param"
            };
            var method = new MockMethod()
            {
                Name       = "Method",
                Parameters = new Dictionary <string, IDiscoveryParameter>()
            };

            method.Parameters.Add("Param", parameter);
            var resource = new MockResource();

            resource.Methods.Add("Method", method);
            var resourceDecl = new CodeTypeDeclaration();

            // Confirm that two properties and two fields are generated.
            var decorator = new ParameterPropertyDecorator();
            CodeTypeMemberCollection newMembers = decorator.GenerateParameterProperty(
                parameter, method, resourceDecl, Enumerable.Empty <string>());

            Assert.AreEqual(2, newMembers.Count); // Property  + field.
            Assert.AreEqual(0, resourceDecl.Members.Count);

            // Check the generated property.
            Assert.IsInstanceOf <CodeMemberField>(newMembers[0]);

            CodeMemberProperty property = (CodeMemberProperty)newMembers[1];

            Assert.AreEqual("Param", property.Name);

            // Check that the property has a Key attribute.
            Assert.AreEqual(1, property.CustomAttributes.Count);
            Assert.AreEqual(
                typeof(RequestParameterAttribute).FullName, property.CustomAttributes[0].AttributeType.BaseType);
            Assert.AreEqual(
                "Param", ((CodePrimitiveExpression)property.CustomAttributes[0].Arguments[0].Value).Value);
        }
        private MockParameter GetMockMultiValueParameter()
        {
            var mockValue = new MockParameter(new[] { "testValueOne", "testValueTwo", "testValueThree" });

            return(mockValue);
        }
        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>());
        }