From 559983a58984bf0c1e367c9727b79b185a574b78 Mon Sep 17 00:00:00 2001 From: Lukas Forer Date: Sat, 28 Dec 2024 17:06:20 +0100 Subject: [PATCH] Add test and name to query --- .../nf/test/core/TagQueryExpression.java | 26 +++++++++++++------ .../nf/test/core/TestSuiteResolverTest.java | 24 +++++++++++++++++ 2 files changed, 42 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/askimed/nf/test/core/TagQueryExpression.java b/src/main/java/com/askimed/nf/test/core/TagQueryExpression.java index 1060618c..428cc9b4 100644 --- a/src/main/java/com/askimed/nf/test/core/TagQueryExpression.java +++ b/src/main/java/com/askimed/nf/test/core/TagQueryExpression.java @@ -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; @@ -20,18 +22,18 @@ public boolean matches(ITaggable taggable) { return true; } - Map 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 createBindingContext(ITaggable taggable) { + private Binding createBindingContext(ITaggable taggable) { Map tagMap = new HashMap<>(); - Map context = new HashMap<>(); + Binding binding = new groovy.lang.Binding(); // Add tags from the current taggable taggable.getTags().forEach(tag -> { @@ -49,10 +51,18 @@ private Map 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 diff --git a/src/test/java/com/askimed/nf/test/core/TestSuiteResolverTest.java b/src/test/java/com/askimed/nf/test/core/TestSuiteResolverTest.java index a5e5b231..9c6525de 100644 --- a/src/test/java/com/askimed/nf/test/core/TestSuiteResolverTest.java +++ b/src/test/java/com/askimed/nf/test/core/TestSuiteResolverTest.java @@ -35,6 +35,30 @@ public void executeTestByName() throws Throwable { } + @Test + public void executeTestByTestNameQuery() throws Throwable { + + TagQuery query = new TagQueryExpression("test == 'test 1'"); + List 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 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 scripts = new Vector();