diff --git a/.editorconfig b/.editorconfig index 2f625c24c4..8be8b5e112 100644 --- a/.editorconfig +++ b/.editorconfig @@ -11,6 +11,8 @@ indent_size = 4 # C# files [*.cs] +indent_style = space +indent_size = 4 #### Core EditorConfig Options #### diff --git a/src/Lucene.Net.Tests.Expressions/JS/TestCustomFunctions.cs b/src/Lucene.Net.Tests.Expressions/JS/TestCustomFunctions.cs index 8cfcd242ce..055926d3c0 100644 --- a/src/Lucene.Net.Tests.Expressions/JS/TestCustomFunctions.cs +++ b/src/Lucene.Net.Tests.Expressions/JS/TestCustomFunctions.cs @@ -1,4 +1,4 @@ -using Lucene.Net.Support; +using Lucene.Net.Support; using Lucene.Net.Util; using NUnit.Framework; using System; @@ -56,10 +56,7 @@ public virtual void TestDefaultList() Assert.AreEqual(Math.Sqrt(20), expr.Evaluate(0, null), DELTA); } - public static double ZeroArgMethod() - { - return 5; - } + public static double ZeroArgMethod() => 5; /// tests a method with no arguments [Test] @@ -71,10 +68,7 @@ public virtual void TestNoArgMethod() Assert.AreEqual(5, expr.Evaluate(0, null), DELTA); } - public static double OneArgMethod(double arg1) - { - return 3 + arg1; - } + public static double OneArgMethod(double arg1) => 3 + arg1; /// tests a method with one arguments [Test] @@ -86,18 +80,14 @@ public virtual void TestOneArgMethod() Assert.AreEqual(6, expr.Evaluate(0, null), DELTA); } - public static double ThreeArgMethod(double arg1, double arg2, double arg3) - { - return arg1 + arg2 + arg3; - } + public static double ThreeArgMethod(double arg1, double arg2, double arg3) => arg1 + arg2 + arg3; /// tests a method with three arguments [Test] public virtual void TestThreeArgMethod() { IDictionary functions = new Dictionary(); - functions["foo"] = GetType().GetMethod("ThreeArgMethod", new []{ typeof(double), typeof( - double), typeof(double)}); + functions["foo"] = GetType().GetMethod("ThreeArgMethod", new []{ typeof(double), typeof(double), typeof(double)}); var expr = JavascriptCompiler.Compile("foo(3, 4, 5)", functions); Assert.AreEqual(12, expr.Evaluate(0, null), DELTA); } @@ -113,10 +103,7 @@ public virtual void TestTwoMethods() Assert.AreEqual(11, expr.Evaluate(0, null), DELTA); } - public static string BogusReturnType() - { - return "bogus!"; - } + public static string BogusReturnType() => "bogus!"; /// wrong return type: must be double [Test] @@ -135,10 +122,7 @@ public virtual void TestWrongReturnType() } } - public static double BogusParameterType(string s) - { - return 0; - } + public static double BogusParameterType(string s) => 0; /// wrong param type: must be doubles [Test] @@ -153,15 +137,11 @@ public virtual void TestWrongParameterType() } catch (ArgumentException e) { - Assert.IsTrue(e.Message.Contains("must take only double parameters" - )); + Assert.IsTrue(e.Message.Contains("must take only double parameters")); } } - public virtual double NonStaticMethod() - { - return 0; - } + public virtual double NonStaticMethod() => 0; /// wrong modifiers: must be static [Test] @@ -180,10 +160,7 @@ public virtual void TestWrongNotStatic() } } - internal static double NonPublicMethod() - { - return 0; - } + internal static double NonPublicMethod() => 0; /// wrong modifiers: must be public [Test] @@ -205,10 +182,7 @@ public virtual void TestWrongNotPublic() internal class NestedNotPublic { - public static double Method() - { - return 0; - } + public static double Method() => 0; } /// wrong class modifiers: class containing method is not public @@ -228,6 +202,9 @@ public virtual void TestWrongNestedNotPublic() } } + + //LUCENENET: testClassLoader() was not ported. (May not apply to Lucene.Net) + internal static string MESSAGE = "This should not happen but it happens"; @@ -240,7 +217,7 @@ public static double Method() } /// the method throws an exception. - /// the method throws an exception. We should check the stack trace that it contains the source code of the expression as file name. + /// We should check the stack trace that it contains the source code of the expression as file name. /// [Test] public virtual void TestThrowingException() @@ -265,7 +242,6 @@ public virtual void TestThrowingException() } /// test that namespaces work with custom expressions. - /// test that namespaces work with custom expressions. [Test] public virtual void TestNamespaces() { diff --git a/src/Lucene.Net.Tests.Expressions/JS/TestJavascriptCompiler.cs b/src/Lucene.Net.Tests.Expressions/JS/TestJavascriptCompiler.cs index a133d5805d..a72441d08c 100644 --- a/src/Lucene.Net.Tests.Expressions/JS/TestJavascriptCompiler.cs +++ b/src/Lucene.Net.Tests.Expressions/JS/TestJavascriptCompiler.cs @@ -1,4 +1,4 @@ -using Lucene.Net.Util; +using Lucene.Net.Util; using NUnit.Framework; using System; using Assert = Lucene.Net.TestFramework.Assert; @@ -40,7 +40,6 @@ public virtual void TestValidNamespaces() Assert.IsNotNull(JavascriptCompiler.Compile("object0.object1.valid1")); } - //TODO: change all exceptions to ParseExceptions [Test] public virtual void TestInvalidNamespaces() { @@ -49,39 +48,42 @@ public virtual void TestInvalidNamespaces() JavascriptCompiler.Compile("object.0invalid"); Assert.Fail(); } - catch (Exception) + catch (InvalidOperationException) { + //expected } - //expected + try { JavascriptCompiler.Compile("0.invalid"); Assert.Fail(); } - catch (Exception) + catch (InvalidOperationException) { + //expected } - //expected + try { JavascriptCompiler.Compile("object..invalid"); Assert.Fail(); } - catch (Exception) + catch (InvalidOperationException) { + //expected } - //expected + try { JavascriptCompiler.Compile(".invalid"); Assert.Fail(); } - catch (Exception) + catch (InvalidOperationException) { + //expected } } - //expected [Test] public virtual void TestInvalidCompiles() { @@ -90,44 +92,48 @@ public virtual void TestInvalidCompiles() JavascriptCompiler.Compile("100 100"); Assert.Fail(); } - catch (Exception) + catch (InvalidOperationException) { + // expected } - // expected exception try { JavascriptCompiler.Compile("7*/-8"); Assert.Fail(); } - catch (Exception) + catch (InvalidOperationException) { + // expected } - // expected exception + try { JavascriptCompiler.Compile("0y1234"); Assert.Fail(); } - catch (Exception) + catch (InvalidOperationException) { + // expected } - // expected exception + try { JavascriptCompiler.Compile("500EE"); Assert.Fail(); } - catch (Exception) + catch (InvalidOperationException) { + // expected } - // expected exception + try { JavascriptCompiler.Compile("500.5EE"); Assert.Fail(); } - catch (Exception) + catch (InvalidOperationException) { + // expected } } @@ -139,30 +145,32 @@ public virtual void TestEmpty() JavascriptCompiler.Compile(string.Empty); Assert.Fail(); } - catch (Exception) + catch (InvalidOperationException) { + // expected } - // expected exception + try { JavascriptCompiler.Compile("()"); Assert.Fail(); } - catch (Exception) + catch (InvalidOperationException) { + // expected } - // expected exception + try { JavascriptCompiler.Compile(" \r\n \n \t"); Assert.Fail(); } - catch (Exception) + catch (InvalidOperationException) { + // expected } } - // expected exception [Test] public virtual void TestNull() { @@ -173,10 +181,10 @@ public virtual void TestNull() } catch (ArgumentNullException) { + // expected } } - // expected exception [Test] public virtual void TestWrongArity() { @@ -187,9 +195,9 @@ public virtual void TestWrongArity() } catch (ArgumentException expected) { - Assert.IsTrue(expected.Message.Contains("arguments for method call" - )); + Assert.IsTrue(expected.Message.Contains("arguments for method call")); } + try { JavascriptCompiler.Compile("tan(1, 1)"); @@ -197,8 +205,7 @@ public virtual void TestWrongArity() } catch (ArgumentException expected) { - Assert.IsTrue(expected.Message.Contains("arguments for method call" - )); + Assert.IsTrue(expected.Message.Contains("arguments for method call")); } } } diff --git a/src/Lucene.Net.Tests.Expressions/JS/TestJavascriptOperations.cs b/src/Lucene.Net.Tests.Expressions/JS/TestJavascriptOperations.cs index 520a6271e8..2422201cdd 100644 --- a/src/Lucene.Net.Tests.Expressions/JS/TestJavascriptOperations.cs +++ b/src/Lucene.Net.Tests.Expressions/JS/TestJavascriptOperations.cs @@ -1,4 +1,4 @@ -using Lucene.Net.Util; +using Lucene.Net.Util; using NUnit.Framework; using Assert = Lucene.Net.TestFramework.Assert; @@ -361,10 +361,10 @@ public virtual void TestHexConst() AssertEvaluatesTo("0x1", 1); AssertEvaluatesTo("0xF", 15); AssertEvaluatesTo("0x1234ABCDEF", 78193085935L); - AssertEvaluatesTo("1 << 0x1", 1 << (0x1)); - AssertEvaluatesTo("1 << 0xA", 1 << (0xA)); - AssertEvaluatesTo("0x1 << 2", unchecked((int)(0x1)) << 2); - AssertEvaluatesTo("0xA << 2", unchecked((int)(0xA)) << 2); + AssertEvaluatesTo("1 << 0x1", 1 << 0x1); + AssertEvaluatesTo("1 << 0xA", 1 << 0xA); + AssertEvaluatesTo("0x1 << 2", 0x1 << 2); + AssertEvaluatesTo("0xA << 2", 0xA << 2); } [Test] @@ -373,7 +373,7 @@ public virtual void TestHexConst2() AssertEvaluatesTo("0X0", 0); AssertEvaluatesTo("0X1", 1); AssertEvaluatesTo("0XF", 15); - AssertEvaluatesTo("0X1234ABCDEF", 78193085935L); + AssertEvaluatesTo("0X1234ABCDEF", 78193085935L); } [Test] @@ -382,7 +382,7 @@ public virtual void TestOctalConst() AssertEvaluatesTo("00", 0); AssertEvaluatesTo("01", 1); AssertEvaluatesTo("010", 8); - AssertEvaluatesTo("0123456777", 21913087); + AssertEvaluatesTo("0123456777", 21913087); // LUCENENET Comment: Javascript octal value via leading 0, compared with decimal value. AssertEvaluatesTo("1 << 01", 1 << 0x1); AssertEvaluatesTo("1 << 010", 1 << 0x8); AssertEvaluatesTo("01 << 2", 0x1 << 2); diff --git a/src/Lucene.Net.Tests.Expressions/TestDemoExpressions.cs b/src/Lucene.Net.Tests.Expressions/TestDemoExpressions.cs index 958b55789a..1b2165925f 100644 --- a/src/Lucene.Net.Tests.Expressions/TestDemoExpressions.cs +++ b/src/Lucene.Net.Tests.Expressions/TestDemoExpressions.cs @@ -1,4 +1,4 @@ -using Lucene.Net.Documents; +using Lucene.Net.Documents; using Lucene.Net.Expressions.JS; using Lucene.Net.Index; using Lucene.Net.Search; @@ -173,11 +173,11 @@ public virtual void TestLotsOfBindings() DoTestLotsOfBindings(byte.MaxValue - 1); DoTestLotsOfBindings(byte.MaxValue); DoTestLotsOfBindings(byte.MaxValue + 1); + // TODO: ideally we'd test > Short.MAX_VALUE too, but compilation is currently recursive. + // so if we want to test such huge expressions, we need to instead change parser to use an explicit Stack } - // TODO: ideally we'd test > Short.MAX_VALUE too, but compilation is currently recursive. - // so if we want to test such huge expressions, we need to instead change parser to use an explicit Stack - /// + private void DoTestLotsOfBindings(int n) { SimpleBindings bindings = new SimpleBindings(); diff --git a/src/Lucene.Net.Tests.Expressions/TestExpressionRescorer.cs b/src/Lucene.Net.Tests.Expressions/TestExpressionRescorer.cs index c1c43f4baf..07b66e218c 100644 --- a/src/Lucene.Net.Tests.Expressions/TestExpressionRescorer.cs +++ b/src/Lucene.Net.Tests.Expressions/TestExpressionRescorer.cs @@ -1,4 +1,4 @@ -using Lucene.Net.Documents; +using Lucene.Net.Documents; using Lucene.Net.Expressions.JS; using Lucene.Net.Index; using Lucene.Net.Search; @@ -52,6 +52,7 @@ public override void SetUp() new NumericDocValuesField("popularity", 5) }; iw.AddDocument(doc); + doc = new Document { NewStringField("id", "2", Field.Store.YES), @@ -60,6 +61,7 @@ public override void SetUp() new NumericDocValuesField("popularity", 20) }; iw.AddDocument(doc); + doc = new Document { NewStringField("id", "3", Field.Store.YES), @@ -67,6 +69,7 @@ public override void SetUp() new NumericDocValuesField("popularity", 2) }; iw.AddDocument(doc); + reader = iw.GetReader(); searcher = new IndexSearcher(reader); iw.Dispose(); @@ -86,27 +89,36 @@ 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(); + + 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")); diff --git a/src/Lucene.Net.Tests.Expressions/TestExpressionSortField.cs b/src/Lucene.Net.Tests.Expressions/TestExpressionSortField.cs index 8bcf0b015b..5078cfbb63 100644 --- a/src/Lucene.Net.Tests.Expressions/TestExpressionSortField.cs +++ b/src/Lucene.Net.Tests.Expressions/TestExpressionSortField.cs @@ -40,25 +40,33 @@ public virtual void TestToString() 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); } @@ -73,18 +81,22 @@ public virtual void TestNeedsScores() 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); @@ -96,6 +108,7 @@ public virtual void TestNeedsScores() 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); diff --git a/src/Lucene.Net.Tests.Expressions/TestExpressionSorts.cs b/src/Lucene.Net.Tests.Expressions/TestExpressionSorts.cs index 2ec8ec81bc..a18c0300e5 100644 --- a/src/Lucene.Net.Tests.Expressions/TestExpressionSorts.cs +++ b/src/Lucene.Net.Tests.Expressions/TestExpressionSorts.cs @@ -32,10 +32,8 @@ namespace Lucene.Net.Expressions /// Tests some basic expressions against different queries, /// and fieldcache/docvalues fields against an equivalent sort. /// - /// - /// Tests some basic expressions against different queries, - /// and fieldcache/docvalues fields against an equivalent sort. - /// + + [SuppressCodecs("Lucene3x")] public class TestExpressionSorts : LuceneTestCase { diff --git a/src/Lucene.Net.Tests.Expressions/TestExpressionValueSource.cs b/src/Lucene.Net.Tests.Expressions/TestExpressionValueSource.cs index bdf7788b70..77b59ab293 100644 --- a/src/Lucene.Net.Tests.Expressions/TestExpressionValueSource.cs +++ b/src/Lucene.Net.Tests.Expressions/TestExpressionValueSource.cs @@ -46,6 +46,7 @@ public override void SetUp() (Random)); iwc.SetMergePolicy(NewLogMergePolicy()); var iw = new RandomIndexWriter(Random, dir, iwc); + var doc = new Document { NewStringField("id", "1", Field.Store.YES), @@ -53,14 +54,15 @@ public override void SetUp() new NumericDocValuesField("popularity", 5) }; iw.AddDocument(doc); + doc = new Document { NewStringField("id", "2", Field.Store.YES), - NewTextField("body", "another document with different contents", Field.Store - .NO), + NewTextField("body", "another document with different contents", Field.Store.NO), new NumericDocValuesField("popularity", 20) }; iw.AddDocument(doc); + doc = new Document { NewStringField("id", "3", Field.Store.YES), @@ -69,6 +71,7 @@ public override void SetUp() }; iw.AddDocument(doc); iw.ForceMerge(1); + reader = iw.GetReader(); iw.Dispose(); } @@ -88,9 +91,11 @@ public virtual void TestTypes() 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(), leaf); + Assert.AreEqual(10, values.DoubleVal(0), 0); Assert.AreEqual(10, values.SingleVal(0), 0); Assert.AreEqual(10, values.Int64Val(0)); @@ -99,6 +104,7 @@ public virtual void TestTypes() Assert.AreEqual((byte)10, values.ByteVal(0)); Assert.AreEqual("10", values.StrVal(0)); Assert.AreEqual(System.Convert.ToDouble(10), values.ObjectVal(0)); + Assert.AreEqual(40, values.DoubleVal(1), 0); Assert.AreEqual(40, values.SingleVal(1), 0); Assert.AreEqual(40, values.Int64Val(1)); @@ -107,6 +113,7 @@ public virtual void TestTypes() Assert.AreEqual((byte)40, values.ByteVal(1)); Assert.AreEqual("40", values.StrVal(1)); Assert.AreEqual(System.Convert.ToDouble(40), values.ObjectVal(1)); + Assert.AreEqual(4, values.DoubleVal(2), 0); Assert.AreEqual(4, values.SingleVal(2), 0); Assert.AreEqual(4, values.Int64Val(2)); @@ -124,17 +131,19 @@ public virtual void TestRangeScorer() 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(), leaf); + // everything - ValueSourceScorer scorer = values.GetRangeScorer(leaf.Reader, "4" - , "40", true, true); + ValueSourceScorer scorer = values.GetRangeScorer(leaf.Reader, "4", "40", true, true); Assert.AreEqual(-1, scorer.DocID); Assert.AreEqual(0, scorer.NextDoc()); Assert.AreEqual(1, scorer.NextDoc()); Assert.AreEqual(2, scorer.NextDoc()); Assert.AreEqual(DocIdSetIterator.NO_MORE_DOCS, scorer.NextDoc()); + // just the first doc scorer = values.GetRangeScorer(leaf.Reader, "4", "40", false, false); Assert.AreEqual(-1, scorer.DocID); @@ -146,9 +155,11 @@ public virtual void TestRangeScorer() 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); @@ -156,16 +167,19 @@ public virtual void TestEquals() 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));