public void Move() { var matchRegExResponse = _findObjectsRegExService.MatchRegex(_model.FileContent); if (!matchRegExResponse.HasMatched) { return; } var regex = new Regex(@"(.*,\d,)(-?\d*(?:\.\d*)?)_(-?\d*(?:\.\d*)?)_(-?\d*(?:\.\d*)?)(,.*)", RegexOptions.Multiline); var objects = matchRegExResponse.Content.Split("%"); for (var i = 0; i < objects.Length; i++) { if (objects[i].Contains("co_wire", System.StringComparison.OrdinalIgnoreCase)) { objects[i] = _moveCoWireObjectService.For(objects[i]); } else { var match = regex.Match(objects[i]); var leadingValue = match.Groups[1].Value; var x = _parseAndAddFloatValue.For(match.Groups[2].Value, _model.MoveXAxisValue); var y = _parseAndAddFloatValue.For(match.Groups[3].Value, _model.MoveYAxisValue); var z = _parseAndAddFloatValue.For(match.Groups[4].Value, _model.MoveZAxisValue); var trailingValue = match.Groups[5].Value; objects[i] = objects[i].Replace(match.Value, $"{leadingValue}{x}_{y}_{z}{trailingValue}"); } } var result = string.Join("%", objects); _model.FileContent = _findObjectsRegExService.Replace(_model.FileContent, matchRegExResponse.Prefix + result + matchRegExResponse.Suffix, matchRegExResponse.MatchingRegEx); }
public void Move_RegExMatchesMultipleObjects_ReplacesCoordinates( [Frozen] IMainModel model, [Frozen] IFindObjectsRegExService findObjectsRegExService, [Frozen] IMoveCoWireObjectService moveCoWireObjectService, [Frozen] IParseAndAddFloatValue parseAndAddFloatValue, MoveObjectsService sut, string dummyContent, RegexServiceResponseModel regexServiceResponseModel, string dummyRegex) { //Arrange var source = Resources.Services_Move_MultipleObjects_Source; var expectedResult = Resources.Services_Move_MultipleObjects_ExpectedResult; model.FileContent.Returns(dummyContent); regexServiceResponseModel.HasMatched = true; regexServiceResponseModel.Content = source; regexServiceResponseModel.MatchingRegEx = dummyRegex; findObjectsRegExService.MatchRegex(dummyContent).Returns(regexServiceResponseModel); moveCoWireObjectService.For(Arg.Any <string>()).Returns("co_wire"); var coordinateCounter = 0; parseAndAddFloatValue.For(Arg.Any <string>(), Arg.Any <float>()).Returns((ci) => { switch (coordinateCounter) { case 0: coordinateCounter++; return("X"); case 1: coordinateCounter++; return("Y"); default: coordinateCounter = 0; return("Z"); } ; }); //Act sut.Move(); //Assert findObjectsRegExService.ReceivedCalls().Should().HaveCount(2); findObjectsRegExService.Received(1).MatchRegex(dummyContent); findObjectsRegExService.Received(1).Replace(dummyContent, expectedResult, regexServiceResponseModel.MatchingRegEx); }
public void Move_RegExServiceDoesNotMatch_Returns( [Frozen] IMainModel model, [Frozen] IFindObjectsRegExService findObjectsRegExService, [Frozen] IParseAndAddFloatValue parseAndAddFloatValue, MoveObjectsService sut, string dummyContent) { //Arrange model.FileContent.Returns(dummyContent); findObjectsRegExService.MatchRegex(dummyContent).Returns(new RegexServiceResponseModel()); //Act sut.Move(); //Assert model.ReceivedCalls().Should().HaveCount(1); findObjectsRegExService.ReceivedCalls().Should().HaveCount(1); findObjectsRegExService.Received(1).MatchRegex(dummyContent); parseAndAddFloatValue.ReceivedCalls().Should().BeEmpty(); }
public void Move_RegExMatchDoesNotContainPercentChar_ReplacesOneLine( [Frozen] IMainModel model, [Frozen] IFindObjectsRegExService findObjectsRegExService, [Frozen] IParseAndAddFloatValue parseAndAddFloatValue, MoveObjectsService sut, string dummyContent, RegexServiceResponseModel regexServiceResponseModel, string dummyRegex, float dummyX, float dummyY, float dummyZ) { //Arrange var source = "table_square_0,1,16.48622_-0.2120953_61.54293,-1.347026E-05_270_-1.018759E-06,-1,1,0,0,0,-1,,-1,kcc0:h0:s0:v49:lr50:hr20,0,,"; var expectedResult = "table_square_0,1,X_Y_Z,-1.347026E-05_270_-1.018759E-06,-1,1,0,0,0,-1,,-1,kcc0:h0:s0:v49:lr50:hr20,0,,"; model.FileContent.Returns(dummyContent); model.MoveXAxisValue.Returns(dummyX); model.MoveYAxisValue.Returns(dummyY); model.MoveZAxisValue.Returns(dummyZ); regexServiceResponseModel.HasMatched = true; regexServiceResponseModel.Content = source; regexServiceResponseModel.MatchingRegEx = dummyRegex; findObjectsRegExService.MatchRegex(dummyContent).Returns(regexServiceResponseModel); parseAndAddFloatValue.For("16.48622", dummyX).Returns("X"); parseAndAddFloatValue.For("-0.2120953", dummyY).Returns("Y"); parseAndAddFloatValue.For("61.54293", dummyZ).Returns("Z"); //Act sut.Move(); //Assert model.ReceivedCalls().Should().HaveCount(6); findObjectsRegExService.ReceivedCalls().Should().HaveCount(2); findObjectsRegExService.Received(1).MatchRegex(dummyContent); findObjectsRegExService.Received(1).Replace(dummyContent, expectedResult, regexServiceResponseModel.MatchingRegEx); }