Skip to content

Commit

Permalink
Fix issue karatelabs#2406
Browse files Browse the repository at this point in the history
  • Loading branch information
Songshen1996 committed Oct 28, 2023
1 parent b936233 commit d9b96ee
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ public class Variable {

public static enum Type {
NULL,
// add undefined (to differ from null)
UNDEFINED,
BOOLEAN,
NUMBER,
STRING,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,14 @@ public JsValue(Value v) {
}
this.original = v;
try {
if (v.isNull()) {
if (v.isNull() && v.toString().equals("null")) {
value = null;
type = Type.NULL;
} else if (v.isNull() && v.toString().equals("undefined")) {
// if v is undefined, convert it to #notpresent,
// which semantically matches to undefined
type = Type.OTHER;
value = "#notpresent";
} else if (v.isHostObject()) {
if (v.isMetaObject()) { // java.lang.Class !
value = v; // special case, keep around as graal value
Expand Down
29 changes: 29 additions & 0 deletions karate-e2e-tests/src/test/java/json/NullTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package json;

import com.intuit.karate.Results;
import com.intuit.karate.Runner;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class NullTest {
void shouldFail(String name) {
Results results = Runner.path("src/test/java/json/" + name + ".feature").parallel(1);
assertEquals(1, results.getFailCount(), results.getErrorMessages());
}

@Test
void testEmptyObject() {
shouldFail("empty-object");
}

@Test
void testEmptyString() {
shouldFail("empty-string");
}

@Test
void testRecommend() {
shouldFail("recommend");
}
}
6 changes: 6 additions & 0 deletions karate-e2e-tests/src/test/java/json/empty-object.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Feature:

Scenario: empty object should fail:
* def response = {}
* match response.id == '#null'
* match response.time == '#present'
6 changes: 6 additions & 0 deletions karate-e2e-tests/src/test/java/json/empty-string.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Feature:

Scenario: empty reponse should fail:
* def response = ''
* match response.time == '#present'
* match response.id == '#null'
5 changes: 5 additions & 0 deletions karate-e2e-tests/src/test/java/json/recommend.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Feature:

Scenario: recommended to use should not fail:
* def response = ''
* match response == { id: '#null', time: '#present' }

0 comments on commit d9b96ee

Please sign in to comment.