Skip to content

Commit

Permalink
Processing stub implementation request. #8
Browse files Browse the repository at this point in the history
  • Loading branch information
BugDiver committed Jul 6, 2018
1 parent f622567 commit ae02f9b
Show file tree
Hide file tree
Showing 7 changed files with 241 additions and 35 deletions.
67 changes: 67 additions & 0 deletions integration-test/ImplementCodeProcessorTests.cs
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);
}
}
}
40 changes: 8 additions & 32 deletions src/GaugeGrpcConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

using System.Threading.Tasks;
using Gauge.CSharp.Core;
using Gauge.Dotnet.Helpers;
using Gauge.Messages;
using Grpc.Core;

Expand All @@ -43,26 +44,16 @@ public override Task<Empty> CacheFile(CacheFileRequest request, ServerCallContex
public override Task<ImplementationFileGlobPatternResponse> GetGlobPatterns(Empty request,
ServerCallContext context)
{
var response = new ImplementationFileGlobPatternResponse
{
GlobPatterns =
{
$"{Utils.GaugeProjectRoot}/**/*.cs"
}
};
var response = new ImplementationFileGlobPatternResponse();
response.GlobPatterns.Add(FileHelper.GetImplementationGlobPatterns());
return Task.FromResult(response);
}

public override Task<ImplementationFileListResponse> GetImplementationFiles(Empty request,
ServerCallContext context)
{
var response = new ImplementationFileListResponse
{
ImplementationFilePaths =
{
"StepImplementation.cs"
}
};
var response = new ImplementationFileListResponse();
response.ImplementationFilePaths.AddRange(FileHelper.GetImplementationFiles());
return Task.FromResult(response);
}

Expand Down Expand Up @@ -90,24 +81,9 @@ public override Task<StepPositionsResponse> GetStepPositions(StepPositionsReques

public override Task<FileDiff> ImplementStub(StubImplementationCodeRequest request, ServerCallContext context)
{
return Task.FromResult(new FileDiff
{
FilePath = "StepImplementation.cs",
TextDiffs =
{
new TextDiff
{
Span = new Span
{
Start = 0,
StartChar = 0,
End = 10,
EndChar = 10
},
Content = "Hello Word"
}
}
});
var respone = _factory.GetProcessor(Message.Types.MessageType.StubImplementationCodeRequest)
.Process(new Message {StubImplementationCodeRequest = request});
return Task.FromResult(respone.FileDiff);
}

public override Task<Empty> KillProcess(KillProcessRequest request, ServerCallContext context)
Expand Down
57 changes: 57 additions & 0 deletions src/Helpers/FileHelper.cs
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);
}
}
}
38 changes: 38 additions & 0 deletions src/Helpers/ImplementationHelper.cs
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";
}
}
}
5 changes: 3 additions & 2 deletions src/MessageProcessorFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,9 @@ public void InitializeExecutionMessageHandlers(IReflectionWrapper reflectionWrap
{Message.Types.MessageType.StepValidateRequest, new StepValidationProcessor(_stepRegistry)},
{Message.Types.MessageType.StepNameRequest, new StepNameProcessor(_stepRegistry)},
{Message.Types.MessageType.RefactorRequest, new RefactorProcessor(_stepRegistry)},
{Message.Types.MessageType.CacheFileRequest, new CacheFileRequestProcessor(_loader)},
{Message.Types.MessageType.StepPositionsRequest, new StepPositionsRequestProcessor(_stepRegistry)}
{Message.Types.MessageType.CacheFileRequest, new CacheFileProcessor(_loader)},
{Message.Types.MessageType.StepPositionsRequest, new StepPositionsProcessor(_stepRegistry)},
{Message.Types.MessageType.StubImplementationCodeRequest, new StubImplementationCodeProcessor()}
};
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Processors/StepValidationProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public Message Process(Message request)
return GetStepValidateResponseMessage(isValid, request, errorType, errorMessage, suggestion);
}

private static string GetSuggestion(ProtoStepValue stepValue)
private string GetSuggestion(ProtoStepValue stepValue)
{
var name = stepValue.StepValue.ToValidCSharpIdentifier();
return "\t\t[Step(\"" + stepValue.ParameterizedStepValue + "\")]\n" +
Expand Down
67 changes: 67 additions & 0 deletions src/Processors/StubImplementationCodeProcessor.cs
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;
}
}
}

0 comments on commit ae02f9b

Please sign in to comment.