bacarate.get()正在返回表达式中的第一个值,而不是返回布尔值

发布于 2025-01-30 04:54:23 字数 1072 浏览 2 评论 0原文

我正在尝试使用JavaScript在空手道中使用算术运算符评估表达式。作为JavaScript eval()的替代方案,我正在使用karate.get()方法执行评估。在所有情况下,我都可以评估表达式并获得布尔值。但是,在对JSON对象进行评估时,要检查其是否为空,空手道。get返回表达式的第一个值,而不是布尔值。任何帮助将不胜感激。提前致谢。注意:仅当我在pom.xml中添加了空手道核依赖性时,我才会面对这个问题。

* json input1 = '{ "id": 4068, "name": "Rep. Draupadi Asan"}'
* def input2 = null
* def operator = "!="

* def Dataiterate =
      """
          function(input1, input2,operator){ 
            
          let stringifiedInput1 = JSON.stringify(input1);
          let stringifiedInput2 = JSON.stringify(input2);
          let valid = karate.get(stringifiedInput1 + operator + stringifiedInput2)
          karate.log(valid)
           if(valid)
           {
            karate.log("true")
           }
           else
           {
            karate.log("false")
           }
           
       }
     """
 * def final = Dataiterate(input1,input2,operator)

输出的快照:

I'm trying to evaluate expressions with arithematic operators in karate using javascript. As an alternative to javascript eval(), I'm using karate.get() method to perform the evaluations. In All cases, I'm able to evaluate the expression and get a boolean value. But when performing evaluation on JSON object, to check if it null, karate.get is returning first value of the expression instead of a boolean value. Any help would be appreciated. Thanks in advance. Note: I'm facing this issue only when I have added karate-core dependency in my pom.xml.

* json input1 = '{ "id": 4068, "name": "Rep. Draupadi Asan"}'
* def input2 = null
* def operator = "!="

* def Dataiterate =
      """
          function(input1, input2,operator){ 
            
          let stringifiedInput1 = JSON.stringify(input1);
          let stringifiedInput2 = JSON.stringify(input2);
          let valid = karate.get(stringifiedInput1 + operator + stringifiedInput2)
          karate.log(valid)
           if(valid)
           {
            karate.log("true")
           }
           else
           {
            karate.log("false")
           }
           
       }
     """
 * def final = Dataiterate(input1,input2,operator)

Snapshot of output:
snapshot

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

复古式 2025-02-06 04:54:23

首先,我要说的是,您的代码中有很多问题和错误的理解。我觉得您正在尝试以不适合和不可能的方式使用空手道。

这是我解释您要做什么的最好猜测。如果这没有意义,我建议您寻找其他一些框架。

* def concat =
  """
  function(arg1, arg2, op) {
    return karate.toString(arg1) + " " + arg2 + " " + op;
  }
 """
* def input1 = { "id": 4068, "name": "Rep. Draupadi Asan" }
* def input2 = null
* def operator = "!="
* def final = concat(input1, input2, operator)
* match final == '{"id":4068,"name":"Rep. Draupadi Asan"} null !='

First let me say that there are so many problems and mis-understandings in your code. I feel you are trying to use Karate in ways that it is not designed for and not possible.

Here is my best guess of interpreting what you are trying to do. If this does not make sense, I suggest you look for some other framework.

* def concat =
  """
  function(arg1, arg2, op) {
    return karate.toString(arg1) + " " + arg2 + " " + op;
  }
 """
* def input1 = { "id": 4068, "name": "Rep. Draupadi Asan" }
* def input2 = null
* def operator = "!="
* def final = concat(input1, input2, operator)
* match final == '{"id":4068,"name":"Rep. Draupadi Asan"} null !='
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文