Skip to content
Browse files

improve tag filter matching

  • Loading branch information...
1 parent 19db480 commit ab06e4233a09ac9346b0c739218cf9e91b7cdb1e @gasparnagy gasparnagy committed
View
1 Generator/DefaultDependencyProvider.cs
@@ -21,6 +21,7 @@ public virtual void RegisterDefaults(ObjectContainer container)
container.RegisterTypeAs<UnitTestFeatureGenerator, UnitTestFeatureGenerator>();
container.RegisterTypeAs<FeatureGeneratorRegistry, IFeatureGeneratorRegistry>();
container.RegisterTypeAs<UnitTestFeatureGeneratorProvider, IFeatureGeneratorProvider>("default");
+ container.RegisterTypeAs<TagFilterMatcher, ITagFilterMatcher>();
container.RegisterInstanceAs(GenerationTargetLanguage.CreateCodeDomHelper(GenerationTargetLanguage.CSharp), GenerationTargetLanguage.CSharp);
container.RegisterInstanceAs(GenerationTargetLanguage.CreateCodeDomHelper(GenerationTargetLanguage.VB), GenerationTargetLanguage.VB);
View
1 Generator/TechTalk.SpecFlow.Generator.csproj
@@ -101,6 +101,7 @@
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="UnitTestConverter\IFeatureGeneratorRegistry.cs" />
<Compile Include="UnitTestConverter\TagFilteredFeatureGeneratorProvider.cs" />
+ <Compile Include="UnitTestConverter\ITagFilterMatcher.cs" />
<Compile Include="UnitTestConverter\TagFilterMatcher.cs" />
<Compile Include="UnitTestConverter\UnitTestFeatureGeneratorProvider.cs" />
<Compile Include="UnitTestFeatureGenerator.cs" />
View
36 Generator/UnitTestConverter/ITagFilterMatcher.cs
@@ -0,0 +1,36 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using TechTalk.SpecFlow.Parser.SyntaxElements;
+
+namespace TechTalk.SpecFlow.Generator.UnitTestConverter
+{
+ public interface ITagFilterMatcher
+ {
+ bool Match(string tagFilter, IEnumerable<string> tagNames);
+ bool GetTagValue(string tagFilter, IEnumerable<string> tagNames, out string value);
+ string[] GetTagValues(string tagFilter, IEnumerable<string> tagNames);
+ }
+
+ public static class TagFilterMatcherExtensions
+ {
+ public static bool Match(this ITagFilterMatcher tagFilterMatcher, string tagFilter, Feature feature)
+ {
+ if (feature.Tags == null)
+ return false;
+
+ return tagFilterMatcher.Match(tagFilter, feature.Tags.Select(t => t.Name));
+ }
+
+ public static bool GetTagValue(this ITagFilterMatcher tagFilterMatcher, string tagFilter, Feature feature, out string value)
+ {
+ if (feature.Tags == null)
+ {
+ value = null;
+ return false;
+ }
+
+ return tagFilterMatcher.GetTagValue(tagFilter, feature.Tags.Select(t => t.Name), out value);
+ }
+ }
+}
View
62 Generator/UnitTestConverter/TagFilterMatcher.cs
@@ -1,30 +1,26 @@
using System;
+using System.Collections.Generic;
using System.Linq;
-using TechTalk.SpecFlow.Parser.SyntaxElements;
namespace TechTalk.SpecFlow.Generator.UnitTestConverter
{
- public interface ITagFilterMatcher
- {
- bool Match(string tagFilter, Feature feature);
- }
-
internal class TagFilterMatcher : ITagFilterMatcher
{
private const string TAG_PREFIX_SEPARATOR = ":";
+ private const StringComparison tagComparison = StringComparison.InvariantCultureIgnoreCase;
private string GetExpectedTagName(string tagFilter)
{
return tagFilter.StartsWith("@") ? tagFilter.Substring(1) : tagFilter;
}
- public bool Match(string tagFilter, Feature feature)
+ public bool Match(string tagFilter, IEnumerable<string> tagNames)
{
string expectedTagName = GetExpectedTagName(tagFilter);
string expectedTagPrefix = GetExpectedTagPrefix(expectedTagName); // we precalculate it to speed up comparison
- return feature.Tags != null &&
- feature.Tags.Any(t => MatchTag(t, expectedTagName, expectedTagPrefix));
+ return tagNames != null &&
+ tagNames.Any(t => MatchTag(t, expectedTagName, expectedTagPrefix));
}
private static string GetExpectedTagPrefix(string expectedTagName)
@@ -32,36 +28,52 @@ private static string GetExpectedTagPrefix(string expectedTagName)
return expectedTagName + TAG_PREFIX_SEPARATOR;
}
- private bool MatchTag(Tag tag, string expectedTagName, string expectedTagPrefix)
+ private bool MatchTag(string tagName, string expectedTagName, string expectedTagPrefix)
{
return
- (tag.Name != null && tag.Name.Equals(expectedTagName, StringComparison.InvariantCultureIgnoreCase)) ||
- MatchTagPrefix(tag, expectedTagPrefix);
+ MatchExactTag(tagName, expectedTagName) ||
+ MatchTagPrefix(tagName, expectedTagPrefix);
}
- private bool MatchTagPrefix(Tag tag, string expectedTagPrefix)
+ private bool MatchExactTag(string tagName, string expectedTagName)
{
- return
- tag.Name != null &&
- tag.Name.StartsWith(expectedTagPrefix, StringComparison.InvariantCultureIgnoreCase);
+ return tagName != null &&
+ tagName.Equals(expectedTagName, tagComparison);
}
- public bool GetTagValue(string tagFilter, Feature feature, out string value)
+ private bool MatchTagPrefix(string tagName, string expectedTagPrefix)
{
- value = null;
+ return tagName != null &&
+ tagName.StartsWith(expectedTagPrefix, tagComparison);
+ }
+ private IEnumerable<string> GetTagValuesInternal(string tagFilter, IEnumerable<string> tagNames)
+ {
string expectedTagName = GetExpectedTagName(tagFilter);
string expectedTagPrefix = GetExpectedTagPrefix(expectedTagName); // we precalculate it to speed up comparison
- if (feature.Tags == null)
- return false;
+ if (tagNames == null)
+ return Enumerable.Empty<string>();
- var tagWithValue = feature.Tags.FirstOrDefault(t => MatchTag(t, expectedTagName, expectedTagPrefix));
- if (tagWithValue == null)
- return false;
+ var tagsWithValue = tagNames.Where(t => MatchTag(t, expectedTagName, expectedTagPrefix));
+
+ return tagsWithValue.Select(tagWithValue => MatchTagPrefix(tagWithValue, expectedTagPrefix) ? GetValue(expectedTagPrefix, tagWithValue) : "");
+ }
- value = MatchTagPrefix(tagWithValue, expectedTagPrefix) ? tagWithValue.Name.Substring(expectedTagPrefix.Length) : "";
- return true;
+ private string GetValue(string expectedTagPrefix, string tagWithValue)
+ {
+ return tagWithValue.Substring(expectedTagPrefix.Length);
+ }
+
+ public bool GetTagValue(string tagFilter, IEnumerable<string> tagNames, out string value)
+ {
+ value = GetTagValuesInternal(tagFilter, tagNames).FirstOrDefault();
+ return value != null;
+ }
+
+ public string[] GetTagValues(string tagFilter, IEnumerable<string> tagNames)
+ {
+ return GetTagValuesInternal(tagFilter, tagNames).ToArray();
}
}
}
View
101 Tests/GeneratorTests/TagFilterMatcherTests.cs
@@ -15,10 +15,19 @@ public void Should_match_simple_tag()
{
var matcher = new TagFilterMatcher();
- Feature theFeature = new Feature();
- theFeature.Tags = new Tags(new Tag("mytag"));
+ var tags = new string[] {"mytag"};
- matcher.Match("mytag", theFeature).Should().BeTrue();
+ matcher.Match("mytag", tags).Should().BeTrue();
+ }
+
+ [Test]
+ public void Should_match_tag_case_insensitive()
+ {
+ var matcher = new TagFilterMatcher();
+
+ var tags = new string[] {"MyTag"};
+
+ matcher.Match("mytag", tags).Should().BeTrue();
}
[Test]
@@ -26,10 +35,9 @@ public void Should_not_match_not_included_tag()
{
var matcher = new TagFilterMatcher();
- Feature theFeature = new Feature();
- theFeature.Tags = new Tags(new Tag("othertag"));
+ var tags = new string[] {"othertag"};
- matcher.Match("mytag", theFeature).Should().BeFalse();
+ matcher.Match("mytag", tags).Should().BeFalse();
}
[Test]
@@ -37,10 +45,9 @@ public void Should_not_match_empty_tag_list()
{
var matcher = new TagFilterMatcher();
- Feature theFeature = new Feature();
- theFeature.Tags = new Tags();
+ var tags = new string[0];
- matcher.Match("mytag", theFeature).Should().BeFalse();
+ matcher.Match("mytag", tags).Should().BeFalse();
}
[Test]
@@ -48,10 +55,9 @@ public void Should_not_match_null_tag_list()
{
var matcher = new TagFilterMatcher();
- Feature theFeature = new Feature();
- theFeature.Tags = null;
+ string[] tags = null;
- matcher.Match("mytag", theFeature).Should().BeFalse();
+ matcher.Match("mytag", tags).Should().BeFalse();
}
[Test]
@@ -59,10 +65,9 @@ public void Should_match_simple_tag_with_at()
{
var matcher = new TagFilterMatcher();
- Feature theFeature = new Feature();
- theFeature.Tags = new Tags(new Tag("mytag"));
+ var tags = new string[] {"mytag"};
- matcher.Match("@mytag", theFeature).Should().BeTrue();
+ matcher.Match("@mytag", tags).Should().BeTrue();
}
[Test]
@@ -70,10 +75,9 @@ public void Should_match_tag_prefix()
{
var matcher = new TagFilterMatcher();
- Feature theFeature = new Feature();
- theFeature.Tags = new Tags(new Tag("mytag:foo"));
+ var tags = new string[] {"mytag:foo"};
- matcher.Match("mytag", theFeature).Should().BeTrue();
+ matcher.Match("mytag", tags).Should().BeTrue();
}
[Test]
@@ -81,10 +85,9 @@ public void Should_match_tag_prefix_with_at()
{
var matcher = new TagFilterMatcher();
- Feature theFeature = new Feature();
- theFeature.Tags = new Tags(new Tag("mytag:foo"));
+ var tags = new string[] {"mytag:foo"};
- matcher.Match("@mytag", theFeature).Should().BeTrue();
+ matcher.Match("@mytag", tags).Should().BeTrue();
}
[Test]
@@ -92,11 +95,10 @@ public void Should_GetTagValue_return_prefixed_value()
{
var matcher = new TagFilterMatcher();
- Feature theFeature = new Feature();
- theFeature.Tags = new Tags(new Tag("mytag:foo"));
+ var tags = new string[] {"mytag:foo"};
string value;
- matcher.GetTagValue("@mytag", theFeature, out value).Should().Be(true);
+ matcher.GetTagValue("@mytag", tags, out value).Should().Be(true);
value.Should().Be("foo");
}
@@ -105,11 +107,10 @@ public void Should_GetTagValue_returns_false_when_null_tag_list()
{
var matcher = new TagFilterMatcher();
- Feature theFeature = new Feature();
- theFeature.Tags = null;
+ string[] tags = null;
string value;
- matcher.GetTagValue("@mytag", theFeature, out value).Should().Be(false);
+ matcher.GetTagValue("@mytag", tags, out value).Should().Be(false);
}
[Test]
@@ -117,11 +118,10 @@ public void Should_GetTagValue_returns_false_when_no_match()
{
var matcher = new TagFilterMatcher();
- Feature theFeature = new Feature();
- theFeature.Tags = new Tags(new Tag("bar"));;
+ var tags = new string[] {"othertag"};
string value;
- matcher.GetTagValue("@mytag", theFeature, out value).Should().Be(false);
+ matcher.GetTagValue("@mytag", tags, out value).Should().Be(false);
}
[Test]
@@ -129,12 +129,47 @@ public void Should_GetTagValue_returns_empty_when_exact_match()
{
var matcher = new TagFilterMatcher();
- Feature theFeature = new Feature();
- theFeature.Tags = new Tags(new Tag("mytag"));;
+ var tags = new string[] {"mytag"};
string value;
- matcher.GetTagValue("@mytag", theFeature, out value).Should().Be(true);
+ matcher.GetTagValue("@mytag", tags, out value).Should().Be(true);
value.Should().Be("");
}
+
+ [Test]
+ public void Should_GetTagValues_returns_prefixed_values()
+ {
+ var matcher = new TagFilterMatcher();
+
+ var tags = new string[] {"mytag:foo", "mytag:bar"};
+
+ var values = matcher.GetTagValues("mytag", tags);
+ values.Should().Contain("foo");
+ values.Should().Contain("bar");
+ }
+
+ [Test]
+ public void Should_GetTagValues_returns_empty_list_when_no_match()
+ {
+ var matcher = new TagFilterMatcher();
+
+ var tags = new string[] {"othertag"};
+
+ var values = matcher.GetTagValues("mytag", tags);
+ values.Should().BeEmpty();
+ }
+
+ [Test]
+ public void Should_GetTagValues_returns_list_with_empty_string_for_exact_mathces()
+ {
+ var matcher = new TagFilterMatcher();
+
+ var tags = new string[] {"mytag:foo", "mytag"};
+
+ var values = matcher.GetTagValues("mytag", tags);
+ values.Should().Contain("foo");
+ values.Should().Contain("");
+ }
+
}
}

0 comments on commit ab06e42

Please sign in to comment.
Something went wrong with that request. Please try again.