-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Processing stub implementation request. #8
- Loading branch information
BugDiver
committed
Jul 6, 2018
1 parent
f622567
commit ae02f9b
Showing
7 changed files
with
241 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Copyright 2018 ThoughtWorks, Inc. | ||
// | ||
// This file is part of Gauge-CSharp. | ||
// | ||
// Gauge-CSharp is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// Gauge-CSharp is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with Gauge-CSharp. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
using System; | ||
using System.IO; | ||
using Gauge.Dotnet.Processors; | ||
using Gauge.Messages; | ||
using NUnit.Framework; | ||
|
||
namespace Gauge.Dotnet.IntegrationTests | ||
{ | ||
public class StubImplementationCodeTests | ||
{ | ||
private readonly string _testProjectPath = TestUtils.GetIntegrationTestSampleDirectory(); | ||
|
||
[SetUp] | ||
public void Setup() | ||
{ | ||
Environment.SetEnvironmentVariable("GAUGE_PROJECT_ROOT", _testProjectPath); | ||
} | ||
|
||
[Test] | ||
public void ShouldProcessMessage() | ||
{ | ||
var message = new Message | ||
{ | ||
MessageId = 1234, | ||
MessageType = Message.Types.MessageType.StubImplementationCodeRequest, | ||
StubImplementationCodeRequest = new StubImplementationCodeRequest | ||
{ | ||
ImplementationFilePath = "New File", | ||
Codes = | ||
{ | ||
"method" | ||
} | ||
} | ||
}; | ||
|
||
var processor = new StubImplementationCodeProcessor(); | ||
var result = processor.Process(message); | ||
Assert.AreEqual("StepImplementation1.cs", Path.GetFileName(result.FileDiff.FilePath)); | ||
Assert.AreEqual(1, result.FileDiff.TextDiffs.Count); | ||
Assert.True(result.FileDiff.TextDiffs[0].Content.Contains("namespace IntegrationTestSample")); | ||
Assert.True(result.FileDiff.TextDiffs[0].Content.Contains("class StepImplementation1")); | ||
} | ||
|
||
[TearDown] | ||
public void TearDown() | ||
{ | ||
Environment.SetEnvironmentVariable("GAUGE_PROJECT_ROOT", null); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// Copyright 2018 ThoughtWorks, Inc. | ||
// | ||
// This file is part of Gauge-CSharp. | ||
// | ||
// Gauge-CSharp is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// Gauge-CSharp is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with Gauge-CSharp. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
|
||
using System.Collections.Generic; | ||
using System.IO; | ||
using Gauge.CSharp.Core; | ||
using Gauge.Dotnet.Extensions; | ||
|
||
namespace Gauge.Dotnet.Helpers | ||
{ | ||
public class FileHelper | ||
{ | ||
public static IEnumerable<string> GetImplementationFiles() | ||
{ | ||
var classFiles = Directory.EnumerateFiles(Utils.GaugeProjectRoot, "*.cs", | ||
SearchOption.AllDirectories); | ||
return classFiles; | ||
} | ||
|
||
public static string GetImplementationGlobPatterns() | ||
{ | ||
return Path.Combine(Utils.GaugeProjectRoot, "**", "*.cs"); | ||
} | ||
|
||
public static string GetNameSpace() | ||
{ | ||
var gaugeProjectRoot = Utils.GaugeProjectRoot; | ||
return new DirectoryInfo(gaugeProjectRoot).Name.ToValidCSharpIdentifier(); | ||
} | ||
|
||
public static string GetFileName(string suffix, int counter) | ||
{ | ||
var fileName = Path.Combine(Utils.GaugeProjectRoot, $"StepImplementation{suffix}.cs"); | ||
return !File.Exists(fileName) ? fileName : GetFileName((++counter).ToString(), counter); | ||
} | ||
|
||
public static string GetClassName(string filepath) | ||
{ | ||
return Path.GetFileNameWithoutExtension(filepath); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// Copyright 2018 ThoughtWorks, Inc. | ||
// | ||
// This file is part of Gauge-CSharp. | ||
// | ||
// Gauge-CSharp is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// Gauge-CSharp is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with Gauge-CSharp. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
|
||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace Gauge.Dotnet.Helpers | ||
{ | ||
public class ImplementationHelper | ||
{ | ||
public static string CreateImplementationInNewClass(string className, IEnumerable<string> stubs) | ||
{ | ||
return "using System;\n" + | ||
"using Gauge.CSharp.Lib.Attribute;\n" + | ||
"namespace " + FileHelper.GetNameSpace() + "\n" + | ||
"{\n" + | ||
" public class " + className + "\n" + | ||
" {\n" + string.Join(Environment.NewLine, stubs) + | ||
" }\n" + | ||
"}\n"; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Copyright 2018 ThoughtWorks, Inc. | ||
// | ||
// This file is part of Gauge-CSharp. | ||
// | ||
// Gauge-CSharp is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// Gauge-CSharp is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with Gauge-CSharp. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
using System.Collections.Generic; | ||
using System.IO; | ||
using Gauge.Dotnet.Helpers; | ||
using Gauge.Messages; | ||
|
||
namespace Gauge.Dotnet.Processors | ||
{ | ||
public class StubImplementationCodeProcessor : IMessageProcessor | ||
{ | ||
public Message Process(Message request) | ||
{ | ||
var stubs = request.StubImplementationCodeRequest.Codes; | ||
var file = request.StubImplementationCodeRequest.ImplementationFilePath; | ||
var response = new FileDiff(); | ||
if (!File.Exists(file)) | ||
{ | ||
var filepath = FileHelper.GetFileName("", 0); | ||
ImplementInNewClass(response,filepath, stubs); | ||
} | ||
else | ||
{ | ||
var content = File.ReadAllText(file); | ||
if (content == "") | ||
{ | ||
ImplementInNewClass(response, file, stubs); | ||
} | ||
} | ||
return new Message{FileDiff = response}; | ||
} | ||
|
||
private static void ImplementInNewClass(FileDiff fileDiff,string filepath, IEnumerable<string> stubs) | ||
{ | ||
var className = FileHelper.GetClassName(filepath); | ||
var content = ImplementationHelper.CreateImplementationInNewClass(className, stubs); | ||
var item = new TextDiff | ||
{ | ||
Span = new Span | ||
{ | ||
Start = 0, | ||
StartChar = 0, | ||
End = 0, | ||
EndChar = 0 | ||
}, | ||
Content = content | ||
}; | ||
fileDiff.TextDiffs.Add(item); | ||
fileDiff.FilePath = filepath; | ||
} | ||
} | ||
} |