Ejemplo n.º 1
0
        public void WhenTooFewParametersAreSupplied_ThrowsIncorrectParameterCountException()
        {
            var generator = new ExpressionGenerator();

            generator.AddFunctions(typeof(MockFormulae));
            Assert.Throws <IncorrectParameterCountException>(() => generator.Generate("Not()"));
        }
Ejemplo n.º 2
0
        public void GeneratorDoesNotExposeStaticFunctionsWithUnsupportedReturnTypes()
        {
            var generator = new ExpressionGenerator();

            generator.AddFunctions(typeof(InvalidFunctions));
            Assert.Throws <UnknownFunctionException>(() => generator.Generate("ReturnRegex()"));
        }
Ejemplo n.º 3
0
        public void FunctionsNamesAreCaseInsensitive()
        {
            var generator = new ExpressionGenerator();

            generator.AddFunctions(typeof(MockFormulae));
            generator.Generate("ONE()");
        }
Ejemplo n.º 4
0
        public void GeneratorDoesNotExposeStaticFunctionsWithUnsupportedTypeParameters()
        {
            var generator = new ExpressionGenerator();

            generator.AddFunctions(typeof(InvalidFunctions));
            Assert.Throws <UnknownFunctionException>(() => generator.Generate("RegexToString(\".*\")"));
        }
Ejemplo n.º 5
0
        public void GivenAFunctionWithAnIFormatProviderAsTheLastParameter_WhenPassingAStringAsTheLocale_ThenItConvertsTheString()
        {
            var generator = new ExpressionGenerator();

            generator.AddFunctions(typeof(MockFormulae));
            var result = (string)generator.Generate("NumberToString(5.3, \"fr-FR\")")(null);

            Assert.Equal("5,3", result);
        }
Ejemplo n.º 6
0
        public void WhenLastParametersIsAnIFormatProvider_ThenItIsOptional()
        {
            var generator = new ExpressionGenerator();

            generator.AddFunctions(typeof(MockFormulae));
            var result = (string)generator.Generate("NumberToString(5)")(null);

            Assert.Equal("5", result);
        }
Ejemplo n.º 7
0
        public void CallingAFunctionWithAnInvalidParameterThrowsAnException()
        {
            var generator = new ExpressionGenerator();

            generator.AddFunctions(typeof(MockFormulae));
            var ex = Assert.Throws <TypeMismatchException>(() => generator.Generate("AddDay(\"Hello\")"));

            Assert.Equal(typeof(DateTime), ex.Expected);
            Assert.Equal(typeof(string), ex.Actual);
        }
Ejemplo n.º 8
0
        public void GeneratorExposesStaticFunctionsWithNumberParameters()
        {
            var generator = new ExpressionGenerator();

            generator.AddFunctions(typeof(MockFormulae));
            var func   = generator.Generate("Add(5, 7)");
            var result = func(null);

            Assert.Equal(12m, (decimal)result);
        }
Ejemplo n.º 9
0
        public void GeneratorExposesStaticFunctionsWithBooleangParameters()
        {
            var generator = new ExpressionGenerator();

            generator.AddFunctions(typeof(MockFormulae));
            var func   = generator.Generate("Not(\"Foo\" = \"Bar\")");
            var result = func(null);

            Assert.True((bool)result);
        }
Ejemplo n.º 10
0
        public void GeneratorExposesStaticFunctionsWithDateParameters()
        {
            var generator = new ExpressionGenerator();

            generator.AddFunctions(typeof(MockFormulae));
            var func   = generator.Generate("AddDay('2012-12-30')");
            var result = func(null);

            Assert.Equal(new DateTime(2012, 12, 31), (DateTime)result);
        }
Ejemplo n.º 11
0
        public void GeneratorExposesStaticFunctionsWithStringParameters()
        {
            var generator = new ExpressionGenerator();

            generator.AddFunctions(typeof(MockFormulae));
            var func   = generator.Generate("AddMoo(\"Foo\")");
            var result = func(null);

            Assert.Equal("FooMoo", (string)result);
        }
Ejemplo n.º 12
0
        public void GeneratorExposesStaticFunctionsFromSpecifiedTypes()
        {
            var generator = new ExpressionGenerator();

            generator.AddFunctions(typeof(MockFormulae));
            var func   = generator.Generate("One()");
            var result = func(null);

            Assert.Equal(1m, (decimal)result);
        }
Ejemplo n.º 13
0
        public void IfOnlyEvaluatesTheUsedResultParameter()
        {
            var generator = new ExpressionGenerator();

            generator.AddFunctions(typeof(MockFormulae));
            var func   = generator.Generate("If(1=2, GoBoom(), \"Success\")");
            var result = func(null);

            Assert.Equal("Success", result);
        }
Ejemplo n.º 14
0
        public OutputSpecVisitor(Type[] functionTypes, IEnumerable <string> specialFieldNames)
        {
            _generator = new ExpressionGenerator();

            foreach (var type in functionTypes ?? new Type[0])
            {
                _generator.AddFunctions(type);
            }
            _generator.ValidIdentifiers = new HashSet <string>(StringComparer.InvariantCultureIgnoreCase);
            if (specialFieldNames != null)
            {
                _generator.ValidIdentifiers.UnionWith(specialFieldNames);
            }
        }
Ejemplo n.º 15
0
        public void GivenAFunctionWithAParameterOfDictionaryStringString_WhenAnIncorrectStringIsPassedToTheFunction_ThrowsAKeyNotFoundException()
        {
            var generator = new ExpressionGenerator();

            generator.AddFunctions(typeof(MockFormulae));

            generator.Lookups = new Dictionary <string, Dictionary <string, string> >
            {
                ["FooLookup"] = new Dictionary <string, string> {
                    ["Bar"] = "Foo"
                }
            };
            var func = generator.Generate("Lookup(\"NotALookup\", \"Bar\")");

            Assert.Throws <KeyNotFoundException>(() => func(null));
        }
Ejemplo n.º 16
0
        public void GivenAFunctionWithAParameterOfDictionaryStringString_WhenAStringIsPassedToTheFunction_ReturnsALookupTableFromTheList()
        {
            var generator = new ExpressionGenerator();

            generator.AddFunctions(typeof(MockFormulae));

            generator.Lookups = new Dictionary <string, Dictionary <string, string> >
            {
                ["FooLookup"] = new Dictionary <string, string> {
                    ["Bar"] = "Foo"
                }
            };
            var result = (string)generator.Generate("Lookup(\"FooLookup\", \"Bar\")")(null);

            Assert.Equal("Foo", result);
        }