Skip to content

Commit

Permalink
fix getPatternNames including spaces in name
Browse files Browse the repository at this point in the history
  • Loading branch information
willr3 committed May 13, 2021
1 parent f4d9ce0 commit 92b1cc3
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 7 deletions.
6 changes: 4 additions & 2 deletions src/main/java/io/hyperfoil/tools/yaup/StringUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ public static Object jsEval(String js, Collection<String> evals,Object...args){
return jsEval(js,Collections.EMPTY_MAP,evals,args);
}
public static Object jsEval(String js, Map globals,Collection<String> evals,Object...args){
Object rtrn = null; //to return the exception if the js fails
try(Context context = Context.newBuilder("js")
.allowAllAccess(true)
.allowExperimentalOptions(true)
Expand Down Expand Up @@ -178,6 +179,7 @@ public static Object jsEval(String js, Map globals,Collection<String> evals,Obje
try { //evaluate the js to see if it directly returns a value
matcher = context.eval("js", js);
} catch (PolyglotException pge) {
rtrn = pge;
//pge.printStackTrace();
//throw new RuntimeException("failed to evaluate "+js,pge);
try {
Expand Down Expand Up @@ -224,7 +226,7 @@ public static Object jsEval(String js, Map globals,Collection<String> evals,Obje
context.leave();
}
}
return null;
return rtrn;
}
public static List<String> getPatternNames(String pattern, Map<Object,Object> map) throws PopulatePatternException{
return getPatternNames(pattern,map,PATTERN_PREFIX,PATTERN_DEFAULT_SEPARATOR,PATTERN_SUFFIX, PATTERN_JAVASCRIPT_PREFIX);
Expand Down Expand Up @@ -320,7 +322,7 @@ private static String populatePattern(String pattern, Map<Object,Object> map, Co
if(nameStart > seenIndex && seenIndex >= 0){
seen.clear();
}
String namePattern = rtrn.substring(nameStart + prefix.length(),nameEnd);
String namePattern = rtrn.substring(nameStart + prefix.length(),nameEnd).trim();
String name = populatePattern(namePattern,map,evals,prefix,separator,suffix,javascriptPrefix,seen,fullScan);
String defaultValue = defaultStart>-1?rtrn.substring(defaultStart+separator.length(),defaultEnd): null;
if(defaultValue!=null && fullScan){//fullScan added so getPatternNames can use the same logic and scan defaultValue too
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/io/hyperfoil/tools/yaup/json/Json.java
Original file line number Diff line number Diff line change
Expand Up @@ -1043,6 +1043,27 @@ private static void keyId(Json target,StringBuilder sb){
}
private static Configuration yaup = Configuration.builder().jsonProvider(new YaupJsonProvider()).options(Option.SUPPRESS_EXCEPTIONS,Option.DEFAULT_PATH_LEAF_TO_NULL).build();

public static boolean isJsonSearchPath(String path){
return path.contains("[?(") || path.contains("..");
}
public static String getPreSearchPath(String path){
if(path.startsWith("$.")){
path=path.substring("$.".length());
}
if(path.startsWith("$")){
path=path.substring("$".length());
}
while(isJsonSearchPath(path)){
if(path.contains("[?(")){
path=path.substring(0,path.indexOf("[?("));
}
if(path.contains("..")){
path=path.substring(0,path.indexOf(".."));
}
}
return path;
}

public static Object find(Json input,String jsonPath){
return find(input,jsonPath,null);
}
Expand Down
54 changes: 49 additions & 5 deletions src/test/java/perf/yaup/StringUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,17 @@ public void jsEval_array_length(){
assertEquals("length should be 4",4,value.intValue());
}


@Test
public void jsEval_lambda_invalidJs(){
try {
Map<String, String> map = new HashMap<>();
map.put("foo", "FOO");
Object result = StringUtil.jsEval("(a,b)=>{return [b;}", "a", "b");
fail("invalid js should throw an exception");
}catch(IllegalStateException ise){
//expected
}
}

@Test
public void jsEval_function(){
Expand Down Expand Up @@ -354,13 +364,14 @@ public void getPatternNames_pattern_in_map(){
fail(pe.getMessage());
}
}
@Test @Ignore /*Still working on how thsi should work for json in map*/
@Test @Ignore /*Still working on how this should work for json in map*/
public void populatePattern_jsonpath_search(){
try{
Json json = Json.fromJs("[{key:'a',value:'ant'},{key:'b',value:'bat'},{key:'c',value:'cat'}]");
Json json = Json.fromJs("[{key:'a',value:'ant'},{key:'b-b',value:'bat'},{key:'c',value:'cat'}]");
Map<Object, Object> map = new HashMap<>();
System.out.println("json = "+json);
map.put("data",json);
String response = StringUtil.populatePattern("${{data.[?(@.key=='b')].value}}",map);
String response = StringUtil.populatePattern("${{data[?(@.key==\"c\")]}}",map);
assertEquals("response should be value of b","bat",response);
}catch (PopulatePatternException pe) {
fail(pe.getMessage());
Expand Down Expand Up @@ -586,6 +597,29 @@ public void populatePattern_replace_regex(){

}

@Test
public void populatePattern_javascript_toLowerCase(){
Map<Object, Object> map = new HashMap<>();
map.put("vm","VaLuE");
try {
String response = StringUtil.populatePattern("lowercase=\"${{=\"${{vm}}\".toLowerCase()}}\"",map);
assertEquals("lowercase=\"value\"",response);
}catch (PopulatePatternException pe){
fail("did not expect a pattern exception\n"+pe.getMessage());
}
}

@Test
public void populatePattern_quotes_in_default(){
Map<Object, Object> map = new HashMap<>();
try{
String response = StringUtil.populatePattern("${{missing:'not_found'}}",map);
assertEquals("'not_found'",response);
}catch (PopulatePatternException pe){
fail("did not expect a pattern exception\n"+pe.getMessage());
}
}

@Test
public void populatePattern_add_to_list(){
Map<Object, Object> map = new HashMap<>();
Expand Down Expand Up @@ -866,7 +900,17 @@ public void populatePattern_two_values() {
} catch (PopulatePatternException pe) {
fail(pe.getMessage());
}

}
@Test
public void populatePattern_padding() {
Map<Object, Object> map = new HashMap<>();
map.put("FOO", "foo");
try {
String response = StringUtil.populatePattern("${{ FOO }}", map);
assertEquals("foo", response);
} catch (PopulatePatternException pe) {
fail(pe.getMessage());
}
}

@Test
Expand Down

0 comments on commit 92b1cc3

Please sign in to comment.