public void WhenTooFewParametersAreSupplied_ThrowsIncorrectParameterCountException() { var generator = new ExpressionGenerator(); generator.AddFunctions(typeof(MockFormulae)); Assert.Throws <IncorrectParameterCountException>(() => generator.Generate("Not()")); }
public void GeneratorDoesNotExposeStaticFunctionsWithUnsupportedReturnTypes() { var generator = new ExpressionGenerator(); generator.AddFunctions(typeof(InvalidFunctions)); Assert.Throws <UnknownFunctionException>(() => generator.Generate("ReturnRegex()")); }
public void FunctionsNamesAreCaseInsensitive() { var generator = new ExpressionGenerator(); generator.AddFunctions(typeof(MockFormulae)); generator.Generate("ONE()"); }
public void GeneratorDoesNotExposeStaticFunctionsWithUnsupportedTypeParameters() { var generator = new ExpressionGenerator(); generator.AddFunctions(typeof(InvalidFunctions)); Assert.Throws <UnknownFunctionException>(() => generator.Generate("RegexToString(\".*\")")); }
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); }
public void WhenLastParametersIsAnIFormatProvider_ThenItIsOptional() { var generator = new ExpressionGenerator(); generator.AddFunctions(typeof(MockFormulae)); var result = (string)generator.Generate("NumberToString(5)")(null); Assert.Equal("5", result); }
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); }
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); }
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); }
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); }
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); }
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); }
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); }
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); } }
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)); }
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); }