TagManager extends AbstractExecutionNodeVisitor<Boolean> { public boolean acceptTaggedScenario(final Set<String> tags) { if (acceptAll || (acceptedTags.isEmpty() && excludedTags.isEmpty())) { return true; } else if (!acceptedTags.isEmpty() && (tags == null || tags.isEmpty())) { return false; } else if (containsAny(tags, excludedTags)) { return false; } else { return tags == null || tags.containsAll(acceptedTags); } } TagManager(final String tagList); static TagManager fromTags(final String tags); void insertTagOverlay(final String textValue); @Override Boolean visit(IExecutionNode node); @Override Boolean visit(TaggedNode taggedNode); boolean isApplicable(final IExecutionNode node); boolean acceptTaggedScenario(final Set<String> tags); Set<String> getAcceptedTags();  }
@Test public void testEmptyTagManager() { final TagManager tMgr = new TagManager(""); Assert.assertTrue(tMgr.acceptTaggedScenario(empty)); Assert.assertTrue(tMgr.acceptTaggedScenario(Sets.newHashSet("@fred"))); Assert.assertTrue(tMgr.acceptTaggedScenario(Sets.newHashSet("@fred", "@bob"))); }
@Test public void testNonEmptyTagManager() { final String runTags = "@bob"; final TagManager tMgr = new TagManager(runTags); Assert.assertFalse(tMgr.acceptTaggedScenario(empty)); Assert.assertFalse(tMgr.acceptTaggedScenario(Sets.newHashSet("@fred"))); Assert.assertTrue(tMgr.acceptTaggedScenario(Sets.newHashSet("@bob"))); Assert.assertTrue(tMgr.acceptTaggedScenario(Sets.newHashSet("@fred", "@bob"))); }
@Test public void testAndedTagManager() { final String runTags = "@bob @fred"; final TagManager tMgr = new TagManager(runTags); Assert.assertFalse(tMgr.acceptTaggedScenario(empty)); Assert.assertFalse(tMgr.acceptTaggedScenario(Sets.newHashSet("@fred"))); Assert.assertFalse(tMgr.acceptTaggedScenario(Sets.newHashSet("@bob"))); Assert.assertTrue(tMgr.acceptTaggedScenario(Sets.newHashSet("@fred", "@bob"))); }
@Test public void testNegativeTags() { final String runTags = "--@fred"; final TagManager tMgr = new TagManager(runTags); Assert.assertFalse(tMgr.acceptTaggedScenario(Sets.newHashSet("@fred"))); Assert.assertFalse(tMgr.acceptTaggedScenario(Sets.newHashSet("@fred", "@bob"))); Assert.assertTrue(tMgr.acceptTaggedScenario(empty)); Assert.assertTrue(tMgr.acceptTaggedScenario(Sets.newHashSet("@bob"))); }
@Test public void testTagAnnotations() { final List<Class<?>> stepImplsList = new ArrayList<Class<?>>(); stepImplsList.add(TestStepImplementations.class); final TagManager tagManager = new TagManager(null); final TestParameters testParams = new TestParameters(tagManager, null, "./target/test-classes/features/tagged.feature"); testParams.init(); List<FeatureFile> featureFileList = testParams.getFeatureFileList(); List<Scenario> scenarios = featureFileList.get(0).getScenarios(); Assert.assertThat(scenarios.size(), is(4)); for (final Scenario sc : scenarios) { if (!tagManager.acceptTaggedScenario(sc.getTags())) { Assert.fail("all scenarios should be runnable"); } } final TagManager tagManager2 = new TagManager("@runme"); final TestParameters testParams2 = new TestParameters(tagManager2, null, "./target/test-classes/features/tagged.feature"); testParams2.init(); featureFileList = testParams2.getFeatureFileList(); scenarios = featureFileList.get(0).getScenarios(); Assert.assertThat(scenarios.size(), is(4)); final Set<String> excludedTaggedScenarios = new HashSet<String>(); excludedTaggedScenarios.add("An excluded tagged scenario"); excludedTaggedScenarios.add("An untagged scenario"); excludedTaggedScenarios.add("multilined tagged scenario"); for (final Scenario sc : scenarios) { if (tagManager2.acceptTaggedScenario(sc.getTags())) { Assert.assertThat(sc.getDescription(), is("A tagged scenario")); } else { if (!excludedTaggedScenarios.contains(sc.getDescription())) { Assert.fail("expecting some excluded tags: " + sc.getDescription()); } } } final Scenario scenario = featureFileList.get(0).getScenarios().get(3); Assert.assertThat("expecting a tag to be present", scenario.getTags(), hasItem("@all")); Assert.assertThat("expecting a tag to be present", scenario.getTags(), hasItem("@searchcontracts")); Assert.assertThat("expecting a tag to be present", scenario.getTags(), hasItem("@searchcontracts_30")); }