Skip to content

Commit

Permalink
Empty root namespace (SpecFlowOSS#1606)
Browse files Browse the repository at this point in the history
* Enable empty root namespace

 * Remove [Required] from build task RootNamespace
 * Take folder paths into account when root namespace absent
 * Add tests for previously supported and new scenarois

* Add changelog entry for root namespace fix
  • Loading branch information
idg10 committed Jan 8, 2020
1 parent f408485 commit e2b461c
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ public GenerateFeatureFileCodeBehindTask()
[Required]
public string ProjectPath { get; set; }

[Required]
public string RootNamespace { get; set; }

public string ProjectFolder => Path.GetDirectoryName(ProjectPath);
Expand Down
11 changes: 6 additions & 5 deletions TechTalk.SpecFlow.Generator/TestGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,16 +137,17 @@ protected string GetTargetNamespace(FeatureFileInput featureFileInput)
if (!string.IsNullOrEmpty(featureFileInput.CustomNamespace))
return featureFileInput.CustomNamespace;

if (projectSettings == null || string.IsNullOrEmpty(projectSettings.DefaultNamespace))
return null;

string targetNamespace = projectSettings.DefaultNamespace;
string targetNamespace = projectSettings == null || string.IsNullOrEmpty(projectSettings.DefaultNamespace)
? null
: projectSettings.DefaultNamespace;

var directoryName = Path.GetDirectoryName(featureFileInput.ProjectRelativePath);
string namespaceExtension = string.IsNullOrEmpty(directoryName) ? null :
string.Join(".", directoryName.TrimStart('\\', '/', '.').Split('\\', '/').Select(f => f.ToIdentifier()).ToArray());
if (!string.IsNullOrEmpty(namespaceExtension))
targetNamespace += "." + namespaceExtension;
targetNamespace = targetNamespace == null
? namespaceExtension
: targetNamespace + "." + namespaceExtension;
return targetNamespace;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ public void Execute_OnlyRequiredPropertiesAreSet_ShouldWork()

var generateFeatureFileCodeBehindTask = new GenerateFeatureFileCodeBehindTask
{
RootNamespace = "RootNamespace",
ProjectPath = "ProjectPath",
BuildEngine = new MockBuildEngine(_output),
CodeBehindGenerator = generatorMock.Object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ public void Should_parse_CSProj_New_csproj_file_correctly()
Should_parse_csproj_file_correctly(PathHelper.SanitizeDirectorySeparatorChar(@"Data\CSProj_New\sampleCsProjectfile.csproj"), GenerationTargetLanguage.CSharp, "sampleCsProjectfile", "sampleCsProjectfile", "sampleCsProjectfile");
}

[Fact]
public void Should_parse_CSProj_New_csproj_file_correctly_when_RootNamespace_empty()
{
Should_parse_csproj_file_correctly(PathHelper.SanitizeDirectorySeparatorChar(@"Data\CSProj_New\sampleCsProjectfile.csproj"), GenerationTargetLanguage.CSharp, "sampleCsProjectfile", null, "sampleCsProjectfile");
}

[Fact]
public void Should_parse_CSProj_NewComplex_csproj_file_correctly()
{
Expand Down
36 changes: 34 additions & 2 deletions Tests/TechTalk.SpecFlow.GeneratorTests/TestGeneratorBasicsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ namespace TechTalk.SpecFlow.GeneratorTests

public class TestGeneratorBasicsTests : TestGeneratorTestsBase
{
private string GenerateTestFromSimpleFeature(ProjectSettings projectSettings)
private string GenerateTestFromSimpleFeature(ProjectSettings projectSettings, string projectRelativeFolderPath = null)
{
var testGenerator = CreateTestGenerator(projectSettings);

var result = testGenerator.GenerateTestFile(CreateSimpleValidFeatureFileInput(), defaultSettings);
var result = testGenerator.GenerateTestFile(CreateSimpleValidFeatureFileInput(projectRelativeFolderPath), defaultSettings);
result.Success.Should().Be(true);
return result.GeneratedTestCode;
}
Expand Down Expand Up @@ -58,6 +58,38 @@ public void Should_include_generator_version_in_the_header()
outputFile.Should().Contain(string.Format("SpecFlow Generator Version:{0}", TestGeneratorFactory.GeneratorVersion));
}

[Fact]
public void Should_include_namespace_declaration_using_default_namespace_when_file_in_project_root()
{
net35CSProjectSettings.DefaultNamespace = "Default.TestNamespace";
string outputFile = GenerateTestFromSimpleFeature(net35CSProjectSettings);
outputFile.Should().Contain(string.Format("namespace {0}", net35CSProjectSettings.DefaultNamespace));
}

[Fact]
public void Should_include_namespace_declaration_using_default_namespace_and_folder_path_when_file_in_subfolder()
{
net35CSProjectSettings.DefaultNamespace = "Default.TestNamespace";
string outputFile = GenerateTestFromSimpleFeature(net35CSProjectSettings, @"Folder1\Folder2");
outputFile.Should().Contain(string.Format("namespace {0}.Folder1.Folder2", net35CSProjectSettings.DefaultNamespace));
}

[Fact]
public void Should_include_namespace_declaration_using_fallback_namespace_when_default_namespace_not_set_and_file_in_project_root()
{
net35CSProjectSettings.DefaultNamespace = null;
string outputFile = GenerateTestFromSimpleFeature(net35CSProjectSettings);
outputFile.Should().Contain("namespace SpecFlow.GeneratedTests");
}

[Fact]
public void Should_include_namespace_declaration_using_folder_path_when_default_namespace_not_set_and_file_in_subfolder()
{
net35CSProjectSettings.DefaultNamespace = null;
string outputFile = GenerateTestFromSimpleFeature(net35CSProjectSettings, @"Folder1\Folder2");
outputFile.Should().Contain(string.Format("namespace Folder1.Folder2", net35CSProjectSettings.DefaultNamespace));
}

[Fact]
public void Should_generate_test_from_feature_file_specified_by_path()
{
Expand Down
13 changes: 9 additions & 4 deletions Tests/TechTalk.SpecFlow.GeneratorTests/TestGeneratorTestsBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public TestGeneratorTestsBase()
TestUpToDateCheckerStub = new Mock<ITestUpToDateChecker>();
}

protected FeatureFileInput CreateSimpleValidFeatureFileInput()
protected FeatureFileInput CreateSimpleValidFeatureFileInput(string projectRelativeFolderPath = null)
{
return CreateSimpleFeatureFileInput(@"
Feature: Addition
Expand All @@ -60,12 +60,17 @@ protected FeatureFileInput CreateSimpleValidFeatureFileInput()
And I have entered 70 into the calculator
When I press add
Then the result should be 120 on the screen
");
",
projectRelativeFolderPath);
}

protected FeatureFileInput CreateSimpleFeatureFileInput(string featureFileContent)
protected FeatureFileInput CreateSimpleFeatureFileInput(string featureFileContent, string projectRelativeFolderPath = null)
{
return new FeatureFileInput(@"Dummy.feature") {FeatureFileContent = featureFileContent};
const string FeatureFileName = @"Dummy.feature";
string projectRelativeFilePath = projectRelativeFolderPath == null
? FeatureFileName
: Path.Combine(projectRelativeFolderPath, FeatureFileName);
return new FeatureFileInput(projectRelativeFilePath) {FeatureFileContent = featureFileContent};
}

protected FeatureFileInput CreateSimpleInvalidFeatureFileInput()
Expand Down
5 changes: 5 additions & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
3.0

Fixes:
+ Empty root namespaces now work when generating feature codebehind files with the MSBuild task

3.0 - 2019-04-24

Changes:
Expand Down

0 comments on commit e2b461c

Please sign in to comment.