Skip to content

Commit

Permalink
Add test and name to query
Browse files Browse the repository at this point in the history
  • Loading branch information
lukfor committed Dec 28, 2024
1 parent 848cb69 commit 559983a
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 8 deletions.
26 changes: 18 additions & 8 deletions src/main/java/com/askimed/nf/test/core/TagQueryExpression.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.askimed.nf.test.core;

import groovy.lang.Binding;
import groovy.lang.GroovyShell;
import groovy.util.Eval;

import java.util.Arrays;
Expand All @@ -20,18 +22,18 @@ public boolean matches(ITaggable taggable) {
return true;
}

Map<String, Object> bindingContext = createBindingContext(taggable);
Binding binding = createBindingContext(taggable);

try {
return (Boolean) Eval.me("tags", bindingContext.get("tags"), query);
} catch (Exception e) {
GroovyShell shell = new GroovyShell(binding);
return (Boolean) shell.evaluate(query); } catch (Exception e) {
throw new IllegalArgumentException("Invalid query: " + query, e);
}
}

private Map<String, Object> createBindingContext(ITaggable taggable) {
private Binding createBindingContext(ITaggable taggable) {
Map<String, Boolean> tagMap = new HashMap<>();
Map<String, Object> context = new HashMap<>();
Binding binding = new groovy.lang.Binding();

// Add tags from the current taggable
taggable.getTags().forEach(tag -> {
Expand All @@ -49,10 +51,18 @@ private Map<String, Object> createBindingContext(ITaggable taggable) {
parent = parent.getParent();
}

context.put("tags", new DefaultTagMap(tagMap)); // Map for key-based access
context.put("query", query); // The actual query as a string
binding.setVariable("tags", new DefaultTagMap(tagMap)); // Map for key-based access

return context;
if (taggable instanceof AbstractTest) {
binding.setVariable("test", taggable.getName());
binding.setVariable("name", taggable.getParent().getName());
} else {
binding.setVariable("test", "");
binding.setVariable("name", "");
}


return binding;
}

@Override
Expand Down
24 changes: 24 additions & 0 deletions src/test/java/com/askimed/nf/test/core/TestSuiteResolverTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,30 @@ public void executeTestByName() throws Throwable {

}

@Test
public void executeTestByTestNameQuery() throws Throwable {

TagQuery query = new TagQueryExpression("test == 'test 1'");
List<String> tests = collectTests(query);
Assertions.assertEquals(1, tests.size());
Assertions.assertTrue(tests.contains("test 1"));
Assertions.assertFalse(tests.contains("test 2"));
Assertions.assertFalse(tests.contains("test 3"));

}

@Test
public void executeTestByNameQuery() throws Throwable {

TagQuery query = new TagQueryExpression("name == 'suite 1'");
List<String> tests = collectTests(query);
Assertions.assertEquals(2, tests.size());
Assertions.assertTrue(tests.contains("test 1"));
Assertions.assertTrue(tests.contains("test 2"));
Assertions.assertFalse(tests.contains("test 3"));

}

@Test
public void executeTestByWrongHash() throws Throwable {
List<File> scripts = new Vector<File>();
Expand Down

0 comments on commit 559983a

Please sign in to comment.