Skip to content

Commit 25fab3f

Browse files
author
Jana S
committed
clean up and added comments for understaing override of runner tags
1 parent e2a87fd commit 25fab3f

File tree

7 files changed

+82
-133
lines changed

7 files changed

+82
-133
lines changed

src/main/java/com/pdsl/gherkin/filter/GherkinTagFiltererAdapter.java

+22
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,36 @@
55
import java.util.Set;
66
import java.util.stream.Collectors;
77

8+
/**
9+
* This class adapts the `GherkinTagFilterer` interface to the `TagFilterer` interface. It provides
10+
* a way to use the functionality of `GherkinTagFilterer` with code that expects a `TagFilterer`.
11+
*/
812
public class GherkinTagFiltererAdapter implements TagFilterer {
913

14+
/**
15+
* The instance of the `GherkinTagFilterer` to be adapted.
16+
*/
1017
private final GherkinTagFilterer gherkinTagFilterer;
1118

19+
/**
20+
* Constructor for the `GherkinTagFiltererAdapter`.
21+
*
22+
* @param gherkinTagFilterer The instance of the `GherkinTagFilterer` to be adapted.
23+
*/
1224
public GherkinTagFiltererAdapter(GherkinTagFilterer gherkinTagFilterer) {
1325
this.gherkinTagFilterer = gherkinTagFilterer;
1426
}
1527

28+
/**
29+
* This method checks whether a collection of tags matches a given tag expression. It converts the
30+
* collection of tags to a `Set` using a `HashSet` and then delegates the logic to the
31+
* `gherkinTagFilterer.tagExpressionMatchesPickle` method.
32+
*
33+
* @param tags The collection of tags to be checked.
34+
* @param tagExpression The tag expression to match against.
35+
* @return True if the tags match the tag expression, False otherwise.
36+
*/
37+
1638
@Override
1739
public boolean tagExpressionMatches(Collection<String> tags, String tagExpression) {
1840
return gherkinTagFilterer.tagExpressionMatchesPickle(new HashSet<>(tags), tagExpression);

src/main/java/com/pdsl/runners/PdslTestParams.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,13 @@ public static PdslTestParams from(
3737
List<String> tags,
3838
String[] includesResources,
3939
String[] excludesResources){
40-
return new PdslTestParams(lexerRecognizerClass,parserRecognizerClass,interpreters,tags,includesResources,excludesResources,(tags1, tagExpression) -> true,"");
40+
41+
/* Creates a default tag filter that always returns true, effectively disabling tag filtering.,
42+
Empty tag expression as no filtering is applied */
43+
return new PdslTestParams(
44+
lexerRecognizerClass, parserRecognizerClass, interpreters,
45+
tags, includesResources, excludesResources,
46+
(tags1, tagExpression) -> true, "");
4147
}
4248
/**
4349
* A visitor used to extend the behavior of the PdslTestParams obje3ct without modifying

src/main/java/com/pdsl/runners/SharedTestSuiteVisitor.java

+14
Original file line numberDiff line numberDiff line change
@@ -63,18 +63,32 @@ public SharedTestSuite recognizerParamsOperation(RecognizerParams recognizerPara
6363
// test cases to create from each file, which parts to ignore, etc.
6464
Collection<TestSpecification> specifications = getSpecifications(parser, recognizerParams, params, testResources);
6565
// Convert the specificaitons into test cases
66+
67+
/*
68+
* Filters test cases based on tags and organizes them per interpreter.
69+
*
70+
* This code iterates through a collection of test cases for a single interpreter,
71+
* filters them based on tags using a provided `TagFilterer`, and then adds the
72+
* matched test cases to a list of test cases per interpreter. It also collects
73+
* the interpreter objects.
74+
*/
6675
List<TestCase> testCasesForSingleInterpreter = new ArrayList<>(getTestCases(recognizerParams.providers().testCaseFactoryProvider().get(), specifications));
6776
for (TestCase testcase : testCasesForSingleInterpreter) {
6877
if (testcase instanceof TaggedTestCase) {
78+
// Cast the TestCase to TaggedTestCase to access tag information.
6979
TaggedTestCase test = (TaggedTestCase) testcase;
7080
//Filter test cases by tags using the injected TagFilterer
81+
// Check if any of the tags on the test case match the given tag expression.
7182
boolean anyMatchedTag = params.tagFilterer().tagExpressionMatches(test.getTags(), params.tagExpression());
83+
// If the test case's tags match the tag expression, add it to the matched test cases.
7284
if (anyMatchedTag) {
7385
matchedTestCases.add(testcase);
7486
}
7587
}
7688
}
89+
// Add the list of matched test cases for the current interpreter to the overall list.
7790
testCasesPerInterpreters.add(matchedTestCases);
91+
// Get the interpreter object from the parser provider and add it to the list of interpreter objects.
7892
interpreterObjs.add(parser.interpreterProvider().get());
7993

8094
}

src/main/java/com/pdsl/testcases/PreorderTestCaseFactory.java

+1-6
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,16 @@ public static final class DefaultProvider implements Provider<TestCaseFactory> {
2727
public PreorderTestCaseFactory get() {
2828
return INSTANCE;
2929
}
30-
3130
}
31+
3232
public static final class DefaultSupplier implements Supplier<TestCaseFactory> {
3333

3434
@Override
3535
public TestCaseFactory get() {
3636
return INSTANCE;
3737
}
38-
3938
}
4039

41-
4240
@Override
4341
public Collection<TestCase> processTestSpecification(Collection<TestSpecification> testSpecifications) {
4442
Collection<TestCase> testCases = new ArrayList<>(testSpecifications.size()); // Will likely need to be resized regardless
@@ -80,9 +78,6 @@ private List<TestCase> recursiveWalkAndCreateOnLeaf(TestSpecification testSpecif
8078
if (!tags.isEmpty()) {
8179
testCase = new DefaultTaggedTestCase(testCase, tags);
8280
}
83-
84-
85-
8681
List<TestCase> singleTestCase = new ArrayList<>(1);
8782
singleTestCase.add(testCase);
8883
return singleTestCase;

src/main/java/org/junit/jupiter/engine/descriptor/PdslTestParameter.java

+17-59
Original file line numberDiff line numberDiff line change
@@ -205,20 +205,25 @@ public Builder withVisitor(Supplier<? extends ParseTreeVisitor<?>> visitor) {
205205
return this;
206206
}
207207

208-
public Builder withTagExpression(String str) {
209-
Preconditions.checkNotNull(str, "Tag expression cannot be null!");
210-
String commandLineTags = System.getProperty("tags");
211-
if(commandLineTags != null && !commandLineTags.isEmpty()) {
212-
this.tagExpression = commandLineTags;
213-
logger.info("Overriding runner tags with tags provided in the command line {%s}".formatted(commandLineTags) );
214-
logger.info("Ignoring provided tag expression {%s}".formatted(str) );
215-
} else {
216-
this.tagExpression =str;
208+
public Builder withTagExpression(String str) {
209+
Preconditions.checkNotNull(str, "Tag expression cannot be null!");
210+
// Check if there are tags specified using the system property "tags".
211+
String commandLineTags = System.getProperty("tags");
212+
if (commandLineTags != null && !commandLineTags.isEmpty()) {
213+
// If command-line tags are present, use them and log a message informing the user
214+
// that the provided tag expression is being ignored.
215+
this.tagExpression = commandLineTags;
216+
logger.info("Overriding runner tags with tags provided in the command line {%s}".formatted(
217+
commandLineTags));
218+
logger.info("Ignoring provided tag expression {%s}".formatted(str));
219+
} else {
220+
// If no command-line tags are present, use the provided tag expression.
221+
this.tagExpression = str;
222+
}
223+
224+
return this;
217225
}
218226

219-
return this;
220-
}
221-
222227
public Builder withIncludedResources(String[] resources) {
223228
this.includesResources = resources;
224229
return this;
@@ -246,52 +251,5 @@ public PdslTestParameter build() {
246251
return new PdslTestParameter(this);
247252
}
248253
}
249-
public static PdslTestParameter createTestParameter(Supplier<ParseTreeListener> parseTreeListenerSupplier,
250-
Class<? extends Lexer> lexerClass,
251-
Class<? extends Parser> parserClass,
252-
String[] includedResources) {
253-
String defaultTagExpression = "@comment_tag#2";
254-
String commandLineTags = System.getProperty("tags"); // Get the value
255-
256-
String effectiveTagExpression = (commandLineTags != null && !commandLineTags.isEmpty())
257-
? commandLineTags
258-
: defaultTagExpression;
259-
260-
return new PdslTestParameter.Builder(parseTreeListenerSupplier, lexerClass, parserClass)
261-
.withTagExpression(effectiveTagExpression)
262-
.withIncludedResources(includedResources)
263-
.build();
264-
}
265-
266-
/**
267-
* I've introduced a static factory method createTestParameter. This method handles the logic for
268-
* overriding the tag expression. This keeps the builder focused on building objects and separates
269-
* the configuration logic.
270-
*/
271-
public static PdslTestParameter createTestParameter(
272-
Supplier<ParseTreeListener> parseTreeListenerSupplier,
273-
Class<? extends Lexer> lexerClass,
274-
Class<? extends Parser> parserClass,
275-
String[] includedResources, String defaultTagExpression) {
276-
// retrieves the value passed from the command line (e.g., -Dtags="@smoke").
277-
String commandLineTags = System.getProperty("tags");
278-
// handles the logic for overriding the tag expression
279-
String effectiveTagExpression = (commandLineTags != null && !commandLineTags.isEmpty())
280-
? commandLineTags
281-
: defaultTagExpression;
282-
283-
// // String effectiveTagExpression = defaultTagExpression; // Use default if invalid format
284-
// if (commandLineTags != null && !commandLineTags.isEmpty()) {
285-
// String pattern = "^(?:@\\w+(?: and | or )?)*@\\w+$";
286-
// if (Pattern.matches(pattern, commandLineTags)) {
287-
// // ... logic to build effectiveTagExpression from commandLineTags (as shown above) ...
288-
// }
289-
// }
290-
291-
return new PdslTestParameter.Builder(parseTreeListenerSupplier, lexerClass, parserClass)
292-
.withTagExpression(effectiveTagExpression)
293-
.withIncludedResources(includedResources)
294-
.build();
295-
}
296254
}
297255

src/test/java/com/pdsl/uat/java/jupiter/OverrideRunnerTagViaCLITest.java

+21-18
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,9 @@
44
import com.pdsl.grammars.AllGrammarsLexer;
55
import com.pdsl.grammars.AllGrammarsParser;
66
import com.pdsl.grammars.AllGrammarsParserBaseVisitor;
7-
import com.pdsl.grammars.InterpreterAllLexer;
8-
import com.pdsl.grammars.InterpreterAllParser;
9-
import com.pdsl.grammars.MultiInterpreter2Parser;
10-
import com.pdsl.grammars.MultiInterpreter2Parser.ExecuteSentenceContext;
11-
import com.pdsl.grammars.MultiInterpreter2Parser.ParserTwoAllRulesContext;
12-
import com.pdsl.grammars.MultiInterpreter2ParserVisitor;
13-
import com.pdsl.grammars.MultiInterpreterParser.ExecutedByAllParsersContext;
14-
import com.pdsl.grammars.MultiInterpreterParser.FirstInterpreterContext;
15-
import com.pdsl.grammars.MultiInterpreterParser.IgnoreInterpreterContext;
16-
import com.pdsl.grammars.MultiInterpreterParser.ParseSentenceContext;
17-
import com.pdsl.grammars.MultiInterpreterParser.ParsedByAllInterpretersContext;
18-
import com.pdsl.grammars.MultiInterpreterParser.ParserOneAllRulesContext;
19-
import com.pdsl.grammars.MultiInterpreterParser.RecognizeInterpreterContext;
20-
import com.pdsl.grammars.MultiInterpreterParserVisitor;
21-
import com.pdsl.grammars.MultiInterpreterRecognizerLexer;
22-
import com.pdsl.grammars.MultiInterpreterRecognizerParser;
237
import java.nio.file.Paths;
248
import java.util.List;
25-
import java.util.logging.Logger;
269
import java.util.stream.Stream;
27-
import org.antlr.v4.runtime.tree.AbstractParseTreeVisitor;
2810
import org.junit.jupiter.api.AfterAll;
2911
import org.junit.jupiter.api.Assertions;
3012
import org.junit.jupiter.api.TestTemplate;
@@ -37,23 +19,43 @@
3719
import org.junit.jupiter.engine.descriptor.PdslSharedInvocationContextProvider;
3820
import org.junit.jupiter.engine.descriptor.PdslTestParameter;
3921

22+
/**
23+
* This test class demonstrates how to override the tag expression set in the test
24+
* with tags provided via the command line using the system property "tags".
25+
*/
4026
public class OverrideRunnerTagViaCLITest {
4127

4228
private static int totalRunTests = 0;
4329

30+
/**
31+
* This test template uses the `PdslExtension` to configure and run the test.
32+
* The purpose of this test is to verify that the total number of tests run is 1.
33+
*/
4434
@TestTemplate
4535
@ExtendWith(PdslExtension.class)
4636
public void pdslGherkinTestFrameworkResources(PdslExecutable executable) {
4737
totalRunTests++;
4838
}
4939

40+
/**
41+
* This method asserts that only one test was run. This verifies that the
42+
* overridden tag expression from the command line took effect.
43+
*/
5044
@AfterAll
5145
public static void executeAfterAll(){
5246
Assertions.assertEquals(1,totalRunTests);
5347
}
5448

5549
private static class PdslExtension extends PdslSharedInvocationContextProvider {
5650

51+
/**
52+
* This method provides the test template invocation contexts.
53+
* It sets the system property "tags" to "@ex_tag1" before creating the
54+
* PdslConfigParameter and then clears the property afterwards.
55+
*
56+
* @param context The ExtensionContext for the test.
57+
* @return A stream of TestTemplateInvocationContext objects.
58+
*/
5759
public Stream<TestTemplateInvocationContext> provideTestTemplateInvocationContexts(
5860
ExtensionContext context) {
5961
System.setProperty("tags", "@ex_tag1");
@@ -78,6 +80,7 @@ public Stream<TestTemplateInvocationContext> provideTestTemplateInvocationContex
7880
.toUri())
7981
.build())
8082
.stream();
83+
// Clear command-line tags
8184
System.clearProperty("tags");
8285
return contexts;
8386
}

src/test/java/com/pdsl/uat/java/jupiter/PdslTestParameterTest.java

-49
This file was deleted.

0 commit comments

Comments
 (0)