This is an automated email from the ASF dual-hosted git repository. sergeykamov pushed a commit to branch NLPCRAFT-278 in repository https://gitbox.apache.org/repos/asf/incubator-nlpcraft.git
commit e9ec949ed698745754d53ad2ebb7af0a6b09b389 Author: Sergey Kamov <[email protected]> AuthorDate: Tue Mar 23 14:13:10 2021 +0300 WIP. --- .../idl/compiler/functions/NCIdlFunctions.scala | 52 ++++++++++------------ .../compiler/functions/NCIdlFunctionsColl.scala | 8 ++-- .../compiler/functions/NCIdlFunctionsCompany.scala | 2 +- .../compiler/functions/NCIdlFunctionsDate.scala | 32 ++++++++++--- .../compiler/functions/NCIdlFunctionsMath.scala | 2 +- .../compiler/functions/NCIdlFunctionsRequest.scala | 2 +- .../compiler/functions/NCIdlFunctionsStat.scala | 6 +-- .../compiler/functions/NCIdlFunctionsStrings.scala | 2 +- .../compiler/functions/NCIdlFunctionsToken.scala | 2 +- .../compiler/functions/NCIdlFunctionsUser.scala | 2 +- 10 files changed, 63 insertions(+), 47 deletions(-) diff --git a/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/idl/compiler/functions/NCIdlFunctions.scala b/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/idl/compiler/functions/NCIdlFunctions.scala index b059f23..a4ef1a5 100644 --- a/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/idl/compiler/functions/NCIdlFunctions.scala +++ b/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/idl/compiler/functions/NCIdlFunctions.scala @@ -43,27 +43,13 @@ private[functions] trait NCIdlFunctions { @BeforeEach def before(): Unit = NCIdlCompilerGlobal.clearCache(MODEL_ID) - import TrueFunc._ - case class TrueFunc( truth: String, token: NCToken = tkn(), idlCtx: NCIdlContext = ctx() ) { - val function: NCIdlFunction = mkFunc(truth) - - override def toString: String = - s"Boolean function [" + - s"token=${t2s(token)}, " + - s"function=$truth" + - s"]" - } - - object TrueFunc { - private def t2s(t: NCToken) = s"${t.getOriginalText} (${t.getId})" - - private def mkFunc(function: String): NCIdlFunction = { - val intents = NCIdlCompiler.compileIntents(s"intent=i term(t)={$function}", MODEL, MODEL_ID) + val function: NCIdlFunction = { + val intents = NCIdlCompiler.compileIntents(s"intent=i term(t)={$truth}", MODEL, MODEL_ID) require(intents.size == 1) @@ -76,6 +62,15 @@ private[functions] trait NCIdlFunctions { override def toString(): String = s"Function, based on term: $function" } } + + private def nvl(s: String, name: String): String = if (s != null) s else s"$name (not set)" + private def t2s(t: NCToken) = s"text=${nvl(t.getOriginalText, "text")} [${nvl(t.getId, "id")}]" + + override def toString: String = + s"Function [" + + s"token=${t2s(token)}, " + + s"function=$truth" + + s"]" } protected def ctx( @@ -143,32 +138,31 @@ private[functions] trait NCIdlFunctions { } protected def test(funcs: TrueFunc*): Unit = - for ((func, idx) ← funcs.zipWithIndex) { + for (f ← funcs) { val res = try - func.function.apply(func.token, func.idlCtx).value + f.function.apply(f.token, f.idlCtx).value catch { - case e: Exception ⇒ throw new Exception(s"Execution error [index=$idx, testFunc=$func]", e) + case e: Exception ⇒ throw new Exception(s"Execution error [func=$f]", e) } res match { case b: java.lang.Boolean ⇒ - require(b, - s"Unexpected result [" + - s"index=$idx, " + - s"testFunc=$func, " + - s"result=$res" + + require( + b, + s"Unexpected FALSE result [" + + s"testFunc=$f " + s"]" ) case _ ⇒ - require(requirement = false, + require( + requirement = false, s"Unexpected result type [" + - s"index=$idx, " + - s"testFunc=$func, " + - s"result=$res" + + s"resType=${if (res == null) null else res.getClass.getName}, " + + s"testFunc='$f', " + + s"resValue=$res" + s"]" ) } - } } diff --git a/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/idl/compiler/functions/NCIdlFunctionsColl.scala b/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/idl/compiler/functions/NCIdlFunctionsColl.scala index ba253cd..f1e9562 100644 --- a/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/idl/compiler/functions/NCIdlFunctionsColl.scala +++ b/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/idl/compiler/functions/NCIdlFunctionsColl.scala @@ -20,17 +20,17 @@ package org.apache.nlpcraft.model.intent.idl.compiler.functions import org.junit.jupiter.api.Test /** - * Tests for IDL functions. + * Tests for 'collections' functions. */ class NCIdlFunctionsColl extends NCIdlFunctions { @Test def test(): Unit = test( - // BoolFunc(boolCondition = "first(list(1, 2, 3)) == 1"), - // BoolFunc(boolCondition = "last(list(1, 2, 3)) == 3") TrueFunc(truth = "is_empty(list()) == true"), TrueFunc(truth = "is_empty(list(1)) == false"), TrueFunc(truth = "non_empty(list()) == false"), - TrueFunc(truth = "non_empty(list(1)) == true") + TrueFunc(truth = "non_empty(list(1)) == true"), + TrueFunc(truth = "first(list(1, 2, 3)) == 1"), + TrueFunc(truth = "last(list(1, 2, 3)) == 3") ) } diff --git a/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/idl/compiler/functions/NCIdlFunctionsCompany.scala b/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/idl/compiler/functions/NCIdlFunctionsCompany.scala index 9554ad2..3ad354c 100644 --- a/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/idl/compiler/functions/NCIdlFunctionsCompany.scala +++ b/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/idl/compiler/functions/NCIdlFunctionsCompany.scala @@ -25,7 +25,7 @@ import java.util import java.util.Optional /** - * Tests for IDL functions. + * Tests for 'company' functions. */ class NCIdlFunctionsCompany extends NCRestSpec with NCIdlFunctions { private var company: NCCompany = _ diff --git a/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/idl/compiler/functions/NCIdlFunctionsDate.scala b/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/idl/compiler/functions/NCIdlFunctionsDate.scala index cb8901e..31125a7 100644 --- a/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/idl/compiler/functions/NCIdlFunctionsDate.scala +++ b/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/idl/compiler/functions/NCIdlFunctionsDate.scala @@ -19,16 +19,38 @@ package org.apache.nlpcraft.model.intent.idl.compiler.functions import org.junit.jupiter.api.Test +import java.time.temporal.IsoFields +import java.time.{LocalDate, LocalTime} +import java.util.Calendar + /** - * Tests for IDL functions. + * Tests for 'dates' functions. */ class NCIdlFunctionsDate extends NCIdlFunctions { @Test def test(): Unit = { - val now = System.currentTimeMillis() + def test0(): Unit = + test( + TrueFunc(truth = s"year() - ${LocalDate.now.getYear} == 0"), + TrueFunc(truth = s"month() - ${LocalDate.now.getMonthValue} == 0"), + TrueFunc(truth = s"day_of_month() - ${LocalDate.now.getDayOfMonth} == 0"), + TrueFunc(truth = s"day_of_week() - ${LocalDate.now.getDayOfWeek.getValue} == 0"), + TrueFunc(truth = s"day_of_year() - ${LocalDate.now.getDayOfYear} == 0"), + TrueFunc(truth = s"hour() - ${LocalTime.now.getHour} == 0"), + TrueFunc(truth = s"minute() - ${LocalTime.now.getMinute} == 0"), + TrueFunc(truth = s"second() - ${LocalTime.now.getSecond} < 5"), + TrueFunc(truth = s"week_of_month() - ${Calendar.getInstance().get(Calendar.WEEK_OF_MONTH)} == 0"), + TrueFunc(truth = s"week_of_year() - ${Calendar.getInstance().get(Calendar.WEEK_OF_YEAR)} == 0"), + TrueFunc(truth = s"quarter() - ${LocalDate.now().get(IsoFields.QUARTER_OF_YEAR)} == 0"), + TrueFunc(truth = s"now() - ${System.currentTimeMillis()} < 5000") + ) - test( - TrueFunc(truth = s"now() - $now < 1000") - ) + try + test0() + catch { + case _: AssertionError ⇒ + // Some field more than `second` can be changed. One more attempt. + test0() + } } } diff --git a/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/idl/compiler/functions/NCIdlFunctionsMath.scala b/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/idl/compiler/functions/NCIdlFunctionsMath.scala index 1e9ffe0..aa16f41 100644 --- a/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/idl/compiler/functions/NCIdlFunctionsMath.scala +++ b/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/idl/compiler/functions/NCIdlFunctionsMath.scala @@ -20,7 +20,7 @@ package org.apache.nlpcraft.model.intent.idl.compiler.functions import org.junit.jupiter.api.Test /** - * Tests for IDL functions. + * Tests for 'math' functions. */ class NCIdlFunctionsMath extends NCIdlFunctions { @Test diff --git a/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/idl/compiler/functions/NCIdlFunctionsRequest.scala b/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/idl/compiler/functions/NCIdlFunctionsRequest.scala index aad7973..998b39b 100644 --- a/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/idl/compiler/functions/NCIdlFunctionsRequest.scala +++ b/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/idl/compiler/functions/NCIdlFunctionsRequest.scala @@ -20,7 +20,7 @@ package org.apache.nlpcraft.model.intent.idl.compiler.functions import org.junit.jupiter.api.Test /** - * Tests for IDL functions. + * Tests for 'requests' functions. */ class NCIdlFunctionsRequest extends NCIdlFunctions { @Test diff --git a/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/idl/compiler/functions/NCIdlFunctionsStat.scala b/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/idl/compiler/functions/NCIdlFunctionsStat.scala index bed9c30..674085b 100644 --- a/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/idl/compiler/functions/NCIdlFunctionsStat.scala +++ b/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/idl/compiler/functions/NCIdlFunctionsStat.scala @@ -20,13 +20,13 @@ package org.apache.nlpcraft.model.intent.idl.compiler.functions import org.junit.jupiter.api.Test /** - * Tests for IDL functions. + * Tests for 'stat' functions. */ class NCIdlFunctionsStat extends NCIdlFunctions { @Test def test(): Unit = test( - TrueFunc(truth = "max(list(1, 2, 3)) == 3"), - TrueFunc(truth = "min(list(1, 2, 3)) == 1") + TrueFunc(truth = "max(list(1, 2, 3)) == 3"), + TrueFunc(truth = "min(list(1, 2, 3)) == 1") ) } diff --git a/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/idl/compiler/functions/NCIdlFunctionsStrings.scala b/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/idl/compiler/functions/NCIdlFunctionsStrings.scala index c09dc3d..b370f9c 100644 --- a/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/idl/compiler/functions/NCIdlFunctionsStrings.scala +++ b/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/idl/compiler/functions/NCIdlFunctionsStrings.scala @@ -20,7 +20,7 @@ package org.apache.nlpcraft.model.intent.idl.compiler.functions import org.junit.jupiter.api.Test /** - * Tests for IDL functions. + * Tests for 'strings' functions. */ class NCIdlFunctionsStrings extends NCIdlFunctions { @Test diff --git a/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/idl/compiler/functions/NCIdlFunctionsToken.scala b/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/idl/compiler/functions/NCIdlFunctionsToken.scala index 272fac9..d2e8b63 100644 --- a/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/idl/compiler/functions/NCIdlFunctionsToken.scala +++ b/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/idl/compiler/functions/NCIdlFunctionsToken.scala @@ -20,7 +20,7 @@ package org.apache.nlpcraft.model.intent.idl.compiler.functions import org.junit.jupiter.api.Test /** - * Tests for IDL functions. + * Tests for 'tokens' functions. */ class NCIdlFunctionsToken extends NCIdlFunctions { @Test diff --git a/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/idl/compiler/functions/NCIdlFunctionsUser.scala b/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/idl/compiler/functions/NCIdlFunctionsUser.scala index 6d874c7..2df88d9 100644 --- a/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/idl/compiler/functions/NCIdlFunctionsUser.scala +++ b/nlpcraft/src/test/scala/org/apache/nlpcraft/model/intent/idl/compiler/functions/NCIdlFunctionsUser.scala @@ -25,7 +25,7 @@ import java.util import java.util.Optional /** - * Tests for IDL functions. + * Tests for 'user' functions. */ class NCIdlFunctionsUser extends NCRestSpec with NCIdlFunctions { private var usr: NCUser = _
