-
Notifications
You must be signed in to change notification settings - Fork 288
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix instanceof
as predicate for value expression
#7313
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
...ent/agent-debugger/debugger-el/src/test/java/com/datadog/debugger/el/ValueScriptTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package com.datadog.debugger.el; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import com.squareup.moshi.Moshi; | ||
import datadog.trace.bootstrap.debugger.el.Values; | ||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.InputStreamReader; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import okio.Okio; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class ValueScriptTest { | ||
|
||
static class Obj { | ||
String str = "hello"; | ||
int i = 10; | ||
List<String> list = Arrays.asList("a", "b", "c"); | ||
long l = 100_000_000_000L; | ||
float f = 2.5F; | ||
double d = 3.14D; | ||
char c = 'a'; | ||
} | ||
|
||
@Test | ||
public void predicates() { | ||
ValueScript valueScript = loadFromResource("/test_value_expr_01.json"); | ||
assertEquals( | ||
Boolean.TRUE, valueScript.execute(RefResolverHelper.createResolver(new Obj())).getValue()); | ||
} | ||
|
||
@Test | ||
public void topLevelPredicates() { | ||
List<String> lines = loadLinesFromResource("/test_one_liner_value_expr_01.txt"); | ||
for (String line : lines) { | ||
ValueScript valueScript = load(line); | ||
assertEquals( | ||
Boolean.TRUE, | ||
valueScript.execute(RefResolverHelper.createResolver(new Obj())).getValue(), | ||
line); | ||
} | ||
} | ||
|
||
@Test | ||
public void topLevelPrimitives() { | ||
Object[] expectedValues = | ||
new Object[] { | ||
"hello", | ||
"hello", | ||
10L, | ||
100_000_000_000L, | ||
2.5D, | ||
3.14D, | ||
"a", | ||
"b", | ||
5L, | ||
3L, | ||
"el", | ||
Values.NULL_OBJECT, | ||
Boolean.TRUE, | ||
Boolean.FALSE | ||
}; | ||
List<String> lines = loadLinesFromResource("/test_one_liner_value_expr_02.txt"); | ||
int i = 0; | ||
for (String line : lines) { | ||
ValueScript valueScript = load(line); | ||
assertEquals( | ||
expectedValues[i], | ||
valueScript.execute(RefResolverHelper.createResolver(new Obj())).getValue(), | ||
line); | ||
i++; | ||
} | ||
} | ||
|
||
private static List<String> loadLinesFromResource(String resourcePath) { | ||
try (InputStream input = ProbeConditionTest.class.getResourceAsStream(resourcePath)) { | ||
BufferedReader reader = new BufferedReader(new InputStreamReader(input)); | ||
return reader.lines().collect(Collectors.toList()); | ||
} catch (IOException e) { | ||
throw new RuntimeException("Failed to load resource: " + resourcePath, e); | ||
} | ||
} | ||
|
||
private static ValueScript loadFromResource(String resourcePath) { | ||
try (InputStream input = ProbeConditionTest.class.getResourceAsStream(resourcePath)) { | ||
Moshi moshi = | ||
new Moshi.Builder().add(ValueScript.class, new ValueScript.ValueScriptAdapter()).build(); | ||
return moshi.adapter(ValueScript.class).fromJson(Okio.buffer(Okio.source(input))); | ||
} catch (IOException e) { | ||
throw new RuntimeException("Failed to load resource: " + resourcePath, e); | ||
} | ||
} | ||
|
||
private static ValueScript load(String json) { | ||
try { | ||
Moshi moshi = | ||
new Moshi.Builder().add(ValueScript.class, new ValueScript.ValueScriptAdapter()).build(); | ||
return moshi.adapter(ValueScript.class).fromJson(json); | ||
} catch (IOException e) { | ||
throw new RuntimeException("Failed to load json: " + json, e); | ||
} | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
dd-java-agent/agent-debugger/debugger-el/src/test/resources/test_one_liner_value_expr_01.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{"dsl": "", "json": {"eq": [{"ref": "i"}, 10]}} | ||
{"dsl": "", "json": {"==": [{"ref": "i"}, 10]}} | ||
{"dsl": "", "json": {"lt": [{"ref": "i"}, 11]}} | ||
{"dsl": "", "json": {"<": [{"ref": "i"}, 11]}} | ||
{"dsl": "", "json": {"le": [{"ref": "i"}, 10]}} | ||
{"dsl": "", "json": {"<=": [{"ref": "i"}, 10]}} | ||
{"dsl": "", "json": {"gt": [{"ref": "i"}, 0]}} | ||
{"dsl": "", "json": {">": [{"ref": "i"}, 0]}} | ||
{"dsl": "", "json": {"ge": [{"ref": "i"}, 10]}} | ||
{"dsl": "", "json": {">=": [{"ref": "i"}, 10]}} | ||
{"dsl": "", "json": {"not": {"eq": [{"ref": "i"}, 0]}}} | ||
{"dsl": "", "json": {"neq": [{"ref": "i"}, 0]}} | ||
{"dsl": "", "json": {"!=": [{"ref": "i"}, 0]}} | ||
{"dsl": "", "json": {"not": {"isEmpty": {"ref": "list"}}}} | ||
{"dsl": "", "json": {"hasAny": [{"ref": "list"}, {"eq": [{"ref": "@it"}, "a"]}]}} | ||
{"dsl": "", "json": {"hasAll": [{"ref": "list"}, {"neq": [{"ref": "@it"}, "d"]}]}} | ||
{"dsl": "", "json": {"startsWith": [{"ref": "str"}, "hel"]}} | ||
{"dsl": "", "json": {"endsWith": [{"ref": "str"}, "llo"]}} | ||
{"dsl": "", "json": {"contains": [{"ref": "str"}, "ll"]}} | ||
{"dsl": "", "json": {"matches": [{"ref": "str"}, "[helo]+"]}} | ||
{"dsl": "", "json": {"instanceof": [{"ref": "str"}, "java.lang.String"]}} |
14 changes: 14 additions & 0 deletions
14
dd-java-agent/agent-debugger/debugger-el/src/test/resources/test_one_liner_value_expr_02.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{"dsl": "", "json": {"ref": "str"}} | ||
{"dsl": "", "json": {"getmember": [{"ref": "this"}, "str"]}} | ||
{"dsl": "", "json": {"ref": "i"}} | ||
{"dsl": "", "json": {"ref": "l"}} | ||
{"dsl": "", "json": {"ref": "f"}} | ||
{"dsl": "", "json": {"ref": "d"}} | ||
{"dsl": "", "json": {"ref": "c"}} | ||
{"dsl": "", "json": {"index": [{"ref": "list"}, 1]}} | ||
{"dsl": "", "json": {"len": {"ref": "str"}}} | ||
{"dsl": "", "json": {"count": {"ref": "list"}}} | ||
{"dsl": "", "json": {"substring": [{"ref": "str"}, 1, 3]}} | ||
{"dsl": "", "json": null} | ||
{"dsl": "", "json": true} | ||
{"dsl": "", "json": false} |
32 changes: 32 additions & 0 deletions
32
dd-java-agent/agent-debugger/debugger-el/src/test/resources/test_value_expr_01.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
{ | ||
"dsl": "", | ||
"json": { | ||
"and": [ | ||
{ | ||
"or": [ | ||
{"eq": [{"ref": "i"}, 10]}, | ||
{"==": [{"ref": "i"}, 10]}, | ||
{"lt": [{"ref": "i"}, 11]}, | ||
{"<": [{"ref": "i"}, 11]}, | ||
{"le": [{"ref": "i"}, 10]}, | ||
{"<=": [{"ref": "i"}, 10]}, | ||
{"gt": [{"ref": "i"}, 0]}, | ||
{">": [{"ref": "i"}, 0]}, | ||
{"ge": [{"ref": "i"}, 10]}, | ||
{">=": [{"ref": "i"}, 10]}, | ||
{"not": {"eq": [{"ref": "i"}, 0]}}, | ||
{"neq": [{"ref": "i"}, 0]}, | ||
{"!=": [{"ref": "i"}, 0]}, | ||
{"not": {"isEmpty": {"ref": "list"}}}, | ||
{"hasAny": [{"ref": "list"}, {"eq": [{"ref": "@it"}, "a"]}]}, | ||
{"hasAll": [{"ref": "list"}, {"neq": [{"ref": "@it"}, "d"]}]}, | ||
{"startsWith": [{"ref": "str"}, "hel"]}, | ||
{"endsWith": [{"ref": "str"}, "llo"]}, | ||
{"contains": [{"ref": "str"}, "ll"]}, | ||
{"matches": [{"ref": "str"}, "[helo]+"]}, | ||
{"instanceof": [{"ref": "str"}, "java.lang.String"]} | ||
] | ||
} | ||
] | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
there are no assertions here. not sure what this test expect to catch
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done