This is an automated email from the ASF dual-hosted git repository. sergeykamov pushed a commit to branch NLPCRAFT-283 in repository https://gitbox.apache.org/repos/asf/incubator-nlpcraft.git
commit 16191ce07d28127a798bceb4a8fe5cbeccc99a8e Author: Sergey Kamov <[email protected]> AuthorDate: Thu Mar 25 11:46:12 2021 +0300 WIP. --- .../model/dialog/NCIdlFunctionsCustom.scala | 63 ++++++++++++++++++++++ .../idl/compiler/functions/NCIdlFunctions.scala | 46 ++++++++++++---- 2 files changed, 98 insertions(+), 11 deletions(-) diff --git a/nlpcraft/src/test/scala/org/apache/nlpcraft/model/dialog/NCIdlFunctionsCustom.scala b/nlpcraft/src/test/scala/org/apache/nlpcraft/model/dialog/NCIdlFunctionsCustom.scala new file mode 100644 index 0000000..8f00078 --- /dev/null +++ b/nlpcraft/src/test/scala/org/apache/nlpcraft/model/dialog/NCIdlFunctionsCustom.scala @@ -0,0 +1,63 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.nlpcraft.model.dialog + +import org.apache.nlpcraft.model.{NCTokenPredicateContext, NCTokenPredicateResult} +import org.apache.nlpcraft.model.intent.idl.compiler.functions.NCIdlFunctions +import org.junit.jupiter.api.Test + +class NCIdlFunctionsCustomImpl { + def trueOn123(ctx: NCTokenPredicateContext): NCTokenPredicateResult = + new NCTokenPredicateResult(ctx.getToken.getOriginalText == "123", 1) +} + +/** + * Tests for 'custom' functions. + */ +class NCIdlFunctionsCustom extends NCIdlFunctions { + @Test + def testErrors(): Unit = { + expectError( + TestDesc( + truth = "org.apache.nlpcraft.model.dialog.NCIdlFunctionsCustomImpl#missed", + isCustom = true, + token = Some(tkn(txt = "123")), + expectedTokensUsed = Some(1) + ), + ) + } + + + @Test + def test(): Unit = + test( + TestDesc( + truth = "org.apache.nlpcraft.model.dialog.NCIdlFunctionsCustomImpl#trueOn123", + isCustom = true, + token = Some(tkn(txt = "123")), + expectedTokensUsed = Some(1) + ), + TestDesc( + truth = "org.apache.nlpcraft.model.dialog.NCIdlFunctionsCustomImpl#trueOn123", + isCustom = true, + token = Some(tkn(txt = "456")), + expectedRes = false, + expectedTokensUsed = Some(1) + ) + ) +} 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 4c8840d..aa88297 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 @@ -31,7 +31,7 @@ import scala.language.implicitConversions /** * Tests for IDL functions. */ -private[functions] trait NCIdlFunctions { +trait NCIdlFunctions { private final val MODEL_ID = "test.mdl.id" final val MODEL: NCModel = new NCModel { @@ -45,9 +45,19 @@ private[functions] trait NCIdlFunctions { @BeforeEach def before(): Unit = NCIdlCompilerGlobal.clearCache(MODEL_ID) - case class TestDesc(truth: String, token: Option[NCToken] = None, idlCtx: NCIdlContext = ctx()) { - val term: NCIdlTerm = { - val intents = NCIdlCompiler.compileIntents(s"intent=i term(t)={$truth}", MODEL, MODEL_ID) + case class TestDesc( + truth: String, + token: Option[NCToken] = None, + idlCtx: NCIdlContext = ctx(), + isCustom: Boolean = false, + expectedRes: Boolean = true, + expectedTokensUsed: Option[Int] = None + ) { + // It should be lazy for errors verification methods. + lazy val term: NCIdlTerm = { + val (s1, s2) = if (isCustom) ('/', '/') else ('{', '}') + + val intents = NCIdlCompiler.compileIntents(s"intent=i term(t)=$s1$truth$s2", MODEL, MODEL_ID) require(intents.size == 1) require(intents.head.terms.size == 1) @@ -148,34 +158,48 @@ private[functions] trait NCIdlFunctions { protected def test(funcs: TestDesc*): Unit = for (f ← funcs) { - val res = + val item = try { // Process declarations. f.idlCtx.vars ++= f.term.decls // Execute term's predicate. - f.term.pred.apply(f.token.getOrElse(tkn()), f.idlCtx).value + f.term.pred.apply(f.token.getOrElse(tkn()), f.idlCtx) } catch { case e: NCE ⇒ throw new NCE(s"Execution error processing: $f", e) case e: Exception ⇒ throw new Exception(s"Execution error processing: $f", e) } - res match { - case b: java.lang.Boolean ⇒ require(b, s"Unexpected FALSE result for: $f") + item.value match { + case b: java.lang.Boolean ⇒ require(if (f.expectedRes) b else !b, s"Unexpected '$b' result for: $f") case _ ⇒ require( requirement = false, s"Unexpected result type [" + - s"resType=${if (res == null) "null" else res.getClass.getName}, " + - s"resValue=$res, " + + s"resType=${if (item.value == null) "null" else item.value.getClass.getName}, " + + s"resValue=${item.value}, " + s"function=$f" + s"]" ) } + + f.expectedTokensUsed match { + case Some(exp) ⇒ + require( + exp == item.tokUse, + s"Unexpected tokens used [" + + s"expectedTokensUsed=$exp, " + + s"resultTokensUsed=${item.tokUse}, " + + s"function=$f" + + s"]" + ) + + case None ⇒ // No-op. + } } - protected def expectError(f: String): Unit = + protected def expectError(f: TestDesc): Unit = try { test(f)
