public virtual void TestToString() { Expression expr = JavascriptCompiler.Compile("sqrt(_score) + ln(popularity)"); SimpleBindings bindings = new SimpleBindings(); bindings.Add(new SortField("_score", SortField.Type_e.SCORE)); bindings.Add(new SortField("popularity", SortField.Type_e.INT)); SortField sf = expr.GetSortField(bindings, true); AreEqual("<expr \"sqrt(_score) + ln(popularity)\">!", sf.ToString()); }
public virtual void TestNull() { try { JavascriptCompiler.Compile(null); Fail(); } catch (ArgumentNullException) { } }
private ValueSource GetDistanceValueSource() { Expression distance = JavascriptCompiler.Compile( string.Format(CultureInfo.InvariantCulture, "haversin({0:R},{1:R},latitude,longitude)", ORIGIN_LATITUDE, ORIGIN_LONGITUDE)); SimpleBindings bindings = new SimpleBindings(); bindings.Add(new SortField("latitude", SortFieldType.DOUBLE)); bindings.Add(new SortField("longitude", SortFieldType.DOUBLE)); return(distance.GetValueSource(bindings)); }
public virtual void TestEmpty() { IDictionary <string, MethodInfo> functions = new HashMap <string, MethodInfo>(); try { JavascriptCompiler.Compile("sqrt(20)", functions); Fail(); } catch (ArgumentException e) { IsTrue(e.Message.Contains("Unrecognized method")); } }
public virtual void TestWrongReturnType() { IDictionary <string, MethodInfo> functions = new Dictionary <string, MethodInfo>(); functions["foo"] = GetType().GetMethod("BogusReturnType"); try { JavascriptCompiler.Compile("foo()", functions); Fail(); } catch (ArgumentException e) { IsTrue(e.Message.Contains("does not return a double")); } }
public virtual void Test() { // compile an expression: var expr = JavascriptCompiler.Compile("sqrt(_score) + ln(popularity)"); // we use SimpleBindings: which just maps variables to SortField instances SimpleBindings bindings = new SimpleBindings(); bindings.Add(new SortField("_score", SortFieldType.SCORE)); bindings.Add(new SortField("popularity", SortFieldType.INT32)); // create a sort field and sort by it (reverse order) Sort sort = new Sort(expr.GetSortField(bindings, true)); Query query = new TermQuery(new Term("body", "contents")); searcher.Search(query, null, 3, sort); }
public virtual void TestWrongNestedNotPublic() { IDictionary <string, MethodInfo> functions = new Dictionary <string, MethodInfo>(); functions["foo"] = typeof(NestedNotPublic).GetMethod("Method"); try { JavascriptCompiler.Compile("foo()", functions); Fail(); } catch (ArgumentException e) { IsTrue(e.Message.Contains("is not public")); } }
public virtual void TestSelfRecursion() { SimpleBindings bindings = new SimpleBindings(); bindings.Add("cycle0", JavascriptCompiler.Compile("cycle0")); try { bindings.Validate(); Assert.Fail("didn't get expected exception"); } catch (ArgumentException expected) { Assert.IsTrue(expected.Message.Contains("Cycle detected")); } }
public virtual void TestValidExternals() { SimpleBindings bindings = new SimpleBindings(); bindings.Add(new SortField("valid0", SortFieldType.INT32)); bindings.Add(new SortField("valid1", SortFieldType.INT32)); bindings.Add(new SortField("valid2", SortFieldType.INT32)); bindings.Add(new SortField("_score", SortFieldType.SCORE)); bindings.Add("valide0", JavascriptCompiler.Compile("valid0 - valid1 + valid2 + _score" )); bindings.Validate(); bindings.Add("valide1", JavascriptCompiler.Compile("valide0 + valid0")); bindings.Validate(); bindings.Add("valide2", JavascriptCompiler.Compile("valide0 * valide1")); bindings.Validate(); }
public virtual void TestInvalidExternal2() { SimpleBindings bindings = new SimpleBindings(); bindings.Add(new SortField("valid", SortFieldType.INT32)); bindings.Add("invalid", JavascriptCompiler.Compile("valid + badreference")); try { bindings.Validate(); Assert.Fail("didn't get expected exception"); } catch (ArgumentException expected) { Assert.IsTrue(expected.Message.Contains("Invalid reference")); } }
public virtual void TestWrongNotPublic() { IDictionary <string, MethodInfo> functions = new Dictionary <string, MethodInfo>(); functions["foo"] = GetType().GetMethod("NonPublicMethod", BindingFlags.NonPublic | BindingFlags.Static); try { JavascriptCompiler.Compile("foo()", functions); Fail(); } catch (ArgumentException e) { IsTrue(e.Message.Contains("is not public")); } }
public virtual void TestWrongParameterType() { IDictionary <string, MethodInfo> functions = new Dictionary <string, MethodInfo>(); functions["foo"] = GetType().GetMethod("BogusParameterType", new [] { typeof(string) }); try { JavascriptCompiler.Compile("foo(2)", functions); Fail(); } catch (ArgumentException e) { IsTrue(e.Message.Contains("must take only double parameters" )); } }
public virtual void TestNeedsScores() { SimpleBindings bindings = new SimpleBindings(); // refers to score directly Expression exprA = JavascriptCompiler.Compile("_score"); // constant Expression exprB = JavascriptCompiler.Compile("0"); // field Expression exprC = JavascriptCompiler.Compile("intfield"); // score + constant Expression exprD = JavascriptCompiler.Compile("_score + 0"); // field + constant Expression exprE = JavascriptCompiler.Compile("intfield + 0"); // expression + constant (score ref'd) Expression exprF = JavascriptCompiler.Compile("a + 0"); // expression + constant Expression exprG = JavascriptCompiler.Compile("e + 0"); // several variables (score ref'd) Expression exprH = JavascriptCompiler.Compile("b / c + e * g - sqrt(f)"); // several variables Expression exprI = JavascriptCompiler.Compile("b / c + e * g"); bindings.Add(new SortField("_score", SortFieldType.SCORE)); bindings.Add(new SortField("intfield", SortFieldType.INT32)); bindings.Add("a", exprA); bindings.Add("b", exprB); bindings.Add("c", exprC); bindings.Add("d", exprD); bindings.Add("e", exprE); bindings.Add("f", exprF); bindings.Add("g", exprG); bindings.Add("h", exprH); bindings.Add("i", exprI); Assert.IsTrue(exprA.GetSortField(bindings, true).NeedsScores); Assert.IsFalse(exprB.GetSortField(bindings, true).NeedsScores); Assert.IsFalse(exprC.GetSortField(bindings, true).NeedsScores); Assert.IsTrue(exprD.GetSortField(bindings, true).NeedsScores); Assert.IsFalse(exprE.GetSortField(bindings, true).NeedsScores); Assert.IsTrue(exprF.GetSortField(bindings, true).NeedsScores); Assert.IsFalse(exprG.GetSortField(bindings, true).NeedsScores); Assert.IsTrue(exprH.GetSortField(bindings, true).NeedsScores); Assert.IsFalse(exprI.GetSortField(bindings, false).NeedsScores); }
public virtual void TestInvalidCompiles() { try { JavascriptCompiler.Compile("100 100"); Fail(); } catch (Exception) { } // expected exception try { JavascriptCompiler.Compile("7*/-8"); Fail(); } catch (Exception) { } // expected exception try { JavascriptCompiler.Compile("0y1234"); Fail(); } catch (Exception) { } // expected exception try { JavascriptCompiler.Compile("500EE"); Fail(); } catch (Exception) { } // expected exception try { JavascriptCompiler.Compile("500.5EE"); Fail(); } catch (Exception) { } }
public virtual void TestDistanceSort() { var distance = JavascriptCompiler.Compile("haversin(40.7143528,-74.0059731,latitude,longitude)"); SimpleBindings bindings = new SimpleBindings(); bindings.Add(new SortField("latitude", SortFieldType.DOUBLE)); bindings.Add(new SortField("longitude", SortFieldType.DOUBLE)); Sort sort = new Sort(distance.GetSortField(bindings, false)); TopFieldDocs td = searcher.Search(new MatchAllDocsQuery(), null, 3, sort); FieldDoc d = (FieldDoc)td.ScoreDocs[0]; AreEqual(0.4619D, (double)d.Fields[0], 1E-4); d = (FieldDoc)td.ScoreDocs[1]; AreEqual(1.0546D, (double)d.Fields[0], 1E-4); d = (FieldDoc)td.ScoreDocs[2]; AreEqual(5.2842D, (double)d.Fields[0], 1E-4); }
static int StreamInput(TextReader input, Encoding encoding, bool errorsWithStackTrace) { var builtinTypes = LoadBuiltinTypes(); var charReader = new TextReaderCharReader(input); var tokenizer = new Tokenizer(charReader); var interpreter = new DocumentProcessor(tokenizer, builtinTypes, new InstructionInterpreterFactory()); try { var namespaces = interpreter.Read(); var compiler = new JavascriptCompiler("example", namespaces, builtinTypes); foreach (var f in LoadBuiltinFunctions()) { compiler.AddBuiltinFunction(f); } using (MemoryStream mem = new MemoryStream()) using (StreamWriter memW = new StreamWriter(mem, encoding)) { compiler.Compile(memW); memW.Flush(); Console.Out.Write(encoding.GetString(mem.ToArray())); } return(0); } catch (SyntaxException ex) { Console.Error.WriteLine("Syntax error at " + ex.Position); Console.Error.WriteLine(ex.Message); if (errorsWithStackTrace) { Console.Error.WriteLine(ex.StackTrace); } return(0x101); } catch (CompileException ex) { Console.Error.WriteLine("Compile error at " + ex.Position); Console.Error.WriteLine(ex.Message); if (errorsWithStackTrace) { Console.Error.WriteLine(ex.StackTrace); } return(0x102); } }
public virtual void TestCoRecursion4() { SimpleBindings bindings = new SimpleBindings(); bindings.Add("cycle0", JavascriptCompiler.Compile("100")); bindings.Add("cycle1", JavascriptCompiler.Compile("100")); bindings.Add("cycle2", JavascriptCompiler.Compile("cycle1 + cycle0 + cycle3")); bindings.Add("cycle3", JavascriptCompiler.Compile("cycle0 + cycle1 + cycle2")); try { bindings.Validate(); Fail("didn't get expected exception"); } catch (ArgumentException expected) { IsTrue(expected.Message.Contains("Cycle detected")); } }
public virtual void TestSortValues() { var expr = JavascriptCompiler.Compile("sqrt(_score)"); SimpleBindings bindings = new SimpleBindings(); bindings.Add(new SortField("_score", SortFieldType.SCORE)); Sort sort = new Sort(expr.GetSortField(bindings, true)); Query query = new TermQuery(new Term("body", "contents")); TopFieldDocs td = searcher.Search(query, null, 3, sort, true, true); for (int i = 0; i < 3; i++) { FieldDoc d = (FieldDoc)td.ScoreDocs[i]; float expected = (float)Math.Sqrt(d.Score); float actual = (float)((double)d.Fields[0]); AreEqual(expected, actual, CheckHits.ExplainToleranceDelta(expected, actual)); } }
public virtual void TestTwoOfSameBinding() { var expr = JavascriptCompiler.Compile("_score + _score"); SimpleBindings bindings = new SimpleBindings(); bindings.Add(new SortField("_score", SortFieldType.SCORE)); Sort sort = new Sort(expr.GetSortField(bindings, true)); Query query = new TermQuery(new Term("body", "contents")); TopFieldDocs td = searcher.Search(query, null, 3, sort, true, true); for (int i = 0; i < 3; i++) { FieldDoc d = (FieldDoc)td.ScoreDocs[i]; float expected = 2 * d.Score; float actual = ((J2N.Numerics.Double)d.Fields[0]).ToSingle(); Assert.AreEqual(expected, actual, CheckHits.ExplainToleranceDelta (expected, actual)); } }
public virtual void TestEquals() { Expression expr = JavascriptCompiler.Compile("sqrt(a) + ln(b)"); SimpleBindings bindings = new SimpleBindings(); bindings.Add(new SortField("a", SortFieldType.INT32)); bindings.Add(new SortField("b", SortFieldType.INT32)); ValueSource vs1 = expr.GetValueSource(bindings); // same instance Assert.AreEqual(vs1, vs1); // null Assert.IsFalse(vs1.Equals(null)); // other object Assert.IsFalse(vs1.Equals("foobar")); // same bindings and expression instances ValueSource vs2 = expr.GetValueSource(bindings); Assert.AreEqual(vs1.GetHashCode(), vs2.GetHashCode()); Assert.AreEqual(vs1, vs2); // equiv bindings (different instance) SimpleBindings bindings2 = new SimpleBindings(); bindings2.Add(new SortField("a", SortFieldType.INT32)); bindings2.Add(new SortField("b", SortFieldType.INT32)); ValueSource vs3 = expr.GetValueSource(bindings2); Assert.AreEqual(vs1, vs3); // different bindings (same names, different types) SimpleBindings bindings3 = new SimpleBindings(); bindings3.Add(new SortField("a", SortFieldType.INT64)); bindings3.Add(new SortField("b", SortFieldType.INT32)); ValueSource vs4 = expr.GetValueSource(bindings3); Assert.IsFalse(vs1.Equals(vs4)); }
public virtual void TestBasic() { // create a sort field and sort by it (reverse order) Query query = new TermQuery(new Term("body", "contents")); IndexReader r = searcher.IndexReader; // Just first pass query TopDocs hits = searcher.Search(query, 10); Assert.AreEqual(3, hits.TotalHits); Assert.AreEqual("3", r.Document(hits.ScoreDocs[0].Doc).Get("id")); Assert.AreEqual("1", r.Document(hits.ScoreDocs[1].Doc).Get("id")); Assert.AreEqual("2", r.Document(hits.ScoreDocs[2].Doc).Get("id")); // Now, rescore: Expression e = JavascriptCompiler.Compile("sqrt(_score) + ln(popularity)"); SimpleBindings bindings = new SimpleBindings(); bindings.Add(new SortField("popularity", SortFieldType.INT32)); bindings.Add(new SortField("_score", SortFieldType.SCORE)); Rescorer rescorer = e.GetRescorer(bindings); hits = rescorer.Rescore(searcher, hits, 10); Assert.AreEqual(3, hits.TotalHits); Assert.AreEqual("2", r.Document(hits.ScoreDocs[0].Doc).Get("id")); Assert.AreEqual("1", r.Document(hits.ScoreDocs[1].Doc).Get("id")); Assert.AreEqual("3", r.Document(hits.ScoreDocs[2].Doc).Get("id")); string expl = rescorer.Explain(searcher, searcher.Explain(query, hits.ScoreDocs[0].Doc), hits.ScoreDocs[0].Doc).ToString(); // Confirm the explanation breaks out the individual // variables: Assert.IsTrue(expl.Contains("= variable \"popularity\"")); // Confirm the explanation includes first pass details: Assert.IsTrue(expl.Contains("= first pass score")); Assert.IsTrue(expl.Contains("body:contents in")); }
public virtual void TestExpressionRefersToExpression() { var expr1 = JavascriptCompiler.Compile("_score"); var expr2 = JavascriptCompiler.Compile("2*expr1"); var bindings = new SimpleBindings(); bindings.Add(new SortField("_score", SortFieldType.SCORE)); bindings.Add("expr1", expr1); Sort sort = new Sort(expr2.GetSortField(bindings, true)); Query query = new TermQuery(new Term("body", "contents")); TopFieldDocs td = searcher.Search(query, null, 3, sort, true, true); for (int i = 0; i < 3; i++) { FieldDoc d = (FieldDoc)td.ScoreDocs[i]; float expected = 2 * d.Score; float actual = (float)((double)d.Fields[0]); Assert.AreEqual(expected, actual, CheckHits.ExplainToleranceDelta (expected, actual)); } }
internal virtual void AssertQuery(Query query, Filter filter, Sort sort) { int size = TestUtil.NextInt(Random(), 1, searcher.IndexReader.MaxDoc / 5); TopDocs expected = searcher.Search(query, filter, size, sort, Random().NextBoolean (), Random().NextBoolean()); // make our actual sort, mutating original by replacing some of the // sortfields with equivalent expressions SortField[] original = sort.GetSort(); SortField[] mutated = new SortField[original.Length]; for (int i = 0; i < mutated.Length; i++) { if (Random().Next(3) > 0) { SortField s = original[i]; Expression expr = JavascriptCompiler.Compile(s.Field); SimpleBindings simpleBindings = new SimpleBindings(); simpleBindings.Add(s); bool reverse = s.Type == SortField.Type_e.SCORE || s.Reverse; mutated[i] = expr.GetSortField(simpleBindings, reverse); } else { mutated[i] = original[i]; } } Sort mutatedSort = new Sort(mutated); TopDocs actual = searcher.Search(query, filter, size, mutatedSort, Random().NextBoolean (), Random().NextBoolean()); CheckHits.CheckEqual(query, expected.ScoreDocs, actual.ScoreDocs); if (size < actual.TotalHits) { expected = searcher.SearchAfter(expected.ScoreDocs[size - 1], query, filter, size , sort); actual = searcher.SearchAfter(actual.ScoreDocs[size - 1], query, filter, size, mutatedSort ); CheckHits.CheckEqual(query, expected.ScoreDocs, actual.ScoreDocs); } }
public virtual void TestThrowingException() { IDictionary <string, MethodInfo> functions = new Dictionary <string, MethodInfo>(); functions["foo"] = typeof(StaticThrowingException).GetMethod("Method"); string source = "3 * foo() / 5"; var expr = JavascriptCompiler.Compile(source, functions); try { expr.Evaluate(0, null); Fail(); } catch (ArithmeticException e) { AreEqual(MESSAGE, e.Message); StringWriter sw = new StringWriter(); e.printStackTrace(); //.NET Port IsTrue(e.StackTrace.Contains("Lucene.Net.Expressions.CompiledExpression.Evaluate(Int32 , FunctionValues[] )")); } }
public virtual void TestTypes() { Expression expr = JavascriptCompiler.Compile("2*popularity"); SimpleBindings bindings = new SimpleBindings(); bindings.Add(new SortField("popularity", SortFieldType.INT64)); ValueSource vs = expr.GetValueSource(bindings); Assert.AreEqual(1, reader.Leaves.Count); AtomicReaderContext leaf = reader.Leaves[0]; FunctionValues values = vs.GetValues(new Dictionary <string, object>(), leaf); Assert.AreEqual(10, values.DoubleVal(0), 0); Assert.AreEqual(10, values.SingleVal(0), 0); Assert.AreEqual(10, values.Int64Val(0)); Assert.AreEqual(10, values.Int32Val(0)); Assert.AreEqual(10, values.Int16Val(0)); Assert.AreEqual((byte)10, values.ByteVal(0)); Assert.AreEqual("10.0", values.StrVal(0)); Assert.AreEqual(J2N.Numerics.Double.GetInstance(10), values.ObjectVal(0)); Assert.AreEqual(40, values.DoubleVal(1), 0); Assert.AreEqual(40, values.SingleVal(1), 0); Assert.AreEqual(40, values.Int64Val(1)); Assert.AreEqual(40, values.Int32Val(1)); Assert.AreEqual(40, values.Int16Val(1)); Assert.AreEqual((byte)40, values.ByteVal(1)); Assert.AreEqual("40.0", values.StrVal(1)); Assert.AreEqual(J2N.Numerics.Double.GetInstance(40), values.ObjectVal(1)); Assert.AreEqual(4, values.DoubleVal(2), 0); Assert.AreEqual(4, values.SingleVal(2), 0); Assert.AreEqual(4, values.Int64Val(2)); Assert.AreEqual(4, values.Int32Val(2)); Assert.AreEqual(4, values.Int16Val(2)); Assert.AreEqual((byte)4, values.ByteVal(2)); Assert.AreEqual("4.0", values.StrVal(2)); Assert.AreEqual(J2N.Numerics.Double.GetInstance(4), values.ObjectVal(2)); }
public virtual void TestInvalidNamespaces() { try { JavascriptCompiler.Compile("object.0invalid"); Fail(); } catch (Exception) { } //expected try { JavascriptCompiler.Compile("0.invalid"); Fail(); } catch (Exception) { } //expected try { JavascriptCompiler.Compile("object..invalid"); Fail(); } catch (Exception) { } //expected try { JavascriptCompiler.Compile(".invalid"); Fail(); } catch (Exception) { } }
public virtual void TestWrongArity() { try { JavascriptCompiler.Compile("tan()"); Fail(); } catch (ArgumentException expected) { IsTrue(expected.Message.Contains("arguments for method call" )); } try { JavascriptCompiler.Compile("tan(1, 1)"); Fail(); } catch (ArgumentException expected) { IsTrue(expected.Message.Contains("arguments for method call" )); } }
public virtual void TestEquals() { Expression expr = JavascriptCompiler.Compile("sqrt(_score) + ln(popularity)"); SimpleBindings bindings = new SimpleBindings(); bindings.Add(new SortField("_score", SortFieldType.SCORE)); bindings.Add(new SortField("popularity", SortFieldType.INT32)); SimpleBindings otherBindings = new SimpleBindings(); otherBindings.Add(new SortField("_score", SortFieldType.INT64)); otherBindings.Add(new SortField("popularity", SortFieldType.INT32)); SortField sf1 = expr.GetSortField(bindings, true); // different order SortField sf2 = expr.GetSortField(bindings, false); Assert.IsFalse(sf1.Equals(sf2)); // different bindings sf2 = expr.GetSortField(otherBindings, true); Assert.IsFalse(sf1.Equals(sf2)); // different expression Expression other = JavascriptCompiler.Compile("popularity/2"); sf2 = other.GetSortField(bindings, true); Assert.IsFalse(sf1.Equals(sf2)); // null Assert.IsFalse(sf1.Equals(null)); // same instance: Assert.AreEqual(sf1, sf1); }
public virtual void TestTypes() { Expression expr = JavascriptCompiler.Compile("2*popularity"); SimpleBindings bindings = new SimpleBindings(); bindings.Add(new SortField("popularity", SortField.Type_e.LONG)); ValueSource vs = expr.GetValueSource(bindings); AreEqual(1, reader.Leaves.Count); AtomicReaderContext leaf = reader.Leaves[0]; FunctionValues values = vs.GetValues(new Dictionary <string, object>(), leaf); AreEqual(10, values.DoubleVal(0), 0); AreEqual(10, values.FloatVal(0), 0); AreEqual(10, values.LongVal(0)); AreEqual(10, values.IntVal(0)); AreEqual(10, values.ShortVal(0)); AreEqual(10, values.ByteVal(0)); AreEqual("10", values.StrVal(0)); AreEqual(System.Convert.ToDouble(10), values.ObjectVal(0)); AreEqual(40, values.DoubleVal(1), 0); AreEqual(40, values.FloatVal(1), 0); AreEqual(40, values.LongVal(1)); AreEqual(40, values.IntVal(1)); AreEqual(40, values.ShortVal(1)); AreEqual(40, values.ByteVal(1)); AreEqual("40", values.StrVal(1)); AreEqual(System.Convert.ToDouble(40), values.ObjectVal(1)); AreEqual(4, values.DoubleVal(2), 0); AreEqual(4, values.FloatVal(2), 0); AreEqual(4, values.LongVal(2)); AreEqual(4, values.IntVal(2)); AreEqual(4, values.ShortVal(2)); AreEqual(4, values.ByteVal(2)); AreEqual("4", values.StrVal(2)); AreEqual(System.Convert.ToDouble(4), values.ObjectVal(2)); }
public virtual void TestValidNamespaces() { IsNotNull(JavascriptCompiler.Compile("object.valid0")); IsNotNull(JavascriptCompiler.Compile("object0.object1.valid1")); }