Skip to content

Commit

Permalink
Reviewed and cleaned up Expressions Tests, part of Issue apache#259
Browse files Browse the repository at this point in the history
  • Loading branch information
rclabo committed Feb 24, 2021
1 parent d5a5e98 commit a8c6548
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 84 deletions.
2 changes: 1 addition & 1 deletion src/Lucene.Net.Expressions/JS/JavascriptLexer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/*
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
Expand Down
54 changes: 15 additions & 39 deletions src/Lucene.Net.Tests.Expressions/JS/TestCustomFunctions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Lucene.Net.Support;
using Lucene.Net.Support;
using Lucene.Net.Util;
using NUnit.Framework;
using System;
Expand Down Expand Up @@ -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() { return 5; }

/// <summary>tests a method with no arguments</summary>
[Test]
Expand All @@ -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) { return 3 + arg1; }

/// <summary>tests a method with one arguments</summary>
[Test]
Expand All @@ -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) { return arg1 + arg2 + arg3;}

/// <summary>tests a method with three arguments</summary>
[Test]
public virtual void TestThreeArgMethod()
{
IDictionary<string, MethodInfo> functions = new Dictionary<string, MethodInfo>();
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);
}
Expand All @@ -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!";

/// <summary>wrong return type: must be double</summary>
[Test]
Expand All @@ -135,10 +122,7 @@ public virtual void TestWrongReturnType()
}
}

public static double BogusParameterType(string s)
{
return 0;
}
public static double BogusParameterType(string s) => 0;

/// <summary>wrong param type: must be doubles</summary>
[Test]
Expand All @@ -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;

/// <summary>wrong modifiers: must be static</summary>
[Test]
Expand All @@ -180,10 +160,7 @@ public virtual void TestWrongNotStatic()
}
}

internal static double NonPublicMethod()
{
return 0;
}
internal static double NonPublicMethod() => 0;

/// <summary>wrong modifiers: must be public</summary>
[Test]
Expand All @@ -205,10 +182,7 @@ public virtual void TestWrongNotPublic()

internal class NestedNotPublic
{
public static double Method()
{
return 0;
}
public static double Method() => 0;
}

/// <summary>wrong class modifiers: class containing method is not public</summary>
Expand All @@ -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";

Expand All @@ -240,7 +217,7 @@ public static double Method()
}

/// <summary>the method throws an exception.</summary>
/// <remarks>the method throws an exception. We should check the stack trace that it contains the source code of the expression as file name.
/// <remarks>We should check the stack trace that it contains the source code of the expression as file name.
/// </remarks>
[Test]
public virtual void TestThrowingException()
Expand All @@ -265,7 +242,6 @@ public virtual void TestThrowingException()
}

/// <summary>test that namespaces work with custom expressions.</summary>
/// <remarks>test that namespaces work with custom expressions.</remarks>
[Test]
public virtual void TestNamespaces()
{
Expand Down
67 changes: 37 additions & 30 deletions src/Lucene.Net.Tests.Expressions/JS/TestJavascriptCompiler.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Lucene.Net.Util;
using Lucene.Net.Util;
using NUnit.Framework;
using System;
using Assert = Lucene.Net.TestFramework.Assert;
Expand Down Expand Up @@ -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()
{
Expand All @@ -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()
{
Expand All @@ -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
}
}

Expand All @@ -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()
{
Expand All @@ -173,10 +181,10 @@ public virtual void TestNull()
}
catch (ArgumentNullException)
{
// expected
}
}

// expected exception
[Test]
public virtual void TestWrongArity()
{
Expand All @@ -187,18 +195,17 @@ 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)");
Assert.Fail();
}
catch (ArgumentException expected)
{
Assert.IsTrue(expected.Message.Contains("arguments for method call"
));
Assert.IsTrue(expected.Message.Contains("arguments for method call"));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -361,8 +361,8 @@ public virtual void TestHexConst()
AssertEvaluatesTo("0x1", 1);
AssertEvaluatesTo("0xF", 15);
AssertEvaluatesTo("0x1234ABCDEF", 78193085935L);
AssertEvaluatesTo("1 << 0x1", 1 << (0x1));
AssertEvaluatesTo("1 << 0xA", 1 << (0xA));
AssertEvaluatesTo("1 << 0x1", 1 << 0x1);
AssertEvaluatesTo("1 << 0xA", 1 << 0xA);
AssertEvaluatesTo("0x1 << 2", unchecked((int)(0x1)) << 2);
AssertEvaluatesTo("0xA << 2", unchecked((int)(0xA)) << 2);
}
Expand Down
6 changes: 3 additions & 3 deletions src/Lucene.Net.Tests.Expressions/TestDemoExpressions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
/// <exception cref="Exception"></exception>

private void DoTestLotsOfBindings(int n)
{
SimpleBindings bindings = new SimpleBindings();
Expand Down
Loading

0 comments on commit a8c6548

Please sign in to comment.