Skip to content

Commit 2a714a6

Browse files
committed
Output interface, All rules and parameters is IContextDependent
1 parent fb78bd7 commit 2a714a6

34 files changed

+237
-48
lines changed

src/SimpleStateMachine.StructuralSearch.Tests/FindRuleParserTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ public void FindRuleExprParsingShouldBeEqualsCustomResult(int number, string rul
6262
Assert.Equal(_ruleStr, customResult.ToLower());
6363
}
6464
[Theory]
65-
[InlineData("FindRule/NullUnionOperator.txt", "$sign$ In \"Is\",\"==\",\"!=\",\"is not\"", "$value$ In $value1$,\"$value1$\\.Value\",$value2$,\"$value2$\\.Value\"")]
66-
[InlineData("FindRule/AssignmentNullUnionOperator.txt", "$sign$ In \"Is\",\"==\",\"!=\",\"is not\"")]
65+
[InlineData("FindRule/NullUnionOperator.txt", "$sign$ In \"is\",\"==\",\"!=\",\"is not\"", "$value$ In $value1$,\"$value1$\\.Value\",$value2$,\"$value2$\\.Value\"")]
66+
[InlineData("FindRule/AssignmentNullUnionOperator.txt", "$sign$ In \"is\",\"==\",\"!=\",\"is not\"")]
6767
public void FindRuleParsingFromFileShouldBeSuccess(string filePath, params string[] customResult)
6868
{
6969
var ruleStr = File.ReadAllText(filePath);

src/SimpleStateMachine.StructuralSearch.Tests/StructurSearchParserTests.cs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,26 @@ public static void StructuralSearchShouldBeSuccess(string exampleName, string ex
2626

2727
[Theory]
2828
// [InlineData("AssignmentNullUnionOperator")]
29-
[InlineData("NullUnionOperator", "Examples/NullUnionOperator.cs", 2)]
30-
[InlineData("TernaryOperator", "Examples/TernaryOperator.cs", 3)]
31-
public static void StructuralSearchShouldBe(string exampleName, string exampleFilePath, int matchesCount)
29+
[InlineData("NullUnionOperator", "Examples/NullUnionOperator.cs", "Examples/Test.cs", 2)]
30+
[InlineData("TernaryOperator", "Examples/TernaryOperator.cs", "", 3)]
31+
public static void StructuralSearchShouldBe(string exampleName, string inputFilePath, string outputFilePath, int matchesCount)
3232
{
33+
// TODO fix NullUnion example
34+
3335
var config = ConfigurationMock.GetConfigurationFromFiles(exampleName);
3436
var parser = new StructuralSearchParser(config);
3537

36-
var fileInfo = new FileInfo(exampleFilePath);
37-
var input = Input.File(fileInfo);
38+
var inputFileInfo = new FileInfo(inputFilePath);
39+
var input = Input.File(inputFileInfo);
3840
IParsingContext context = new ParsingContext(input);
3941
var matches = parser.Parse(ref context);
4042
matches = parser.ApplyFindRule(ref context, matches);
4143
matches = parser.ApplyReplaceRule(ref context, matches);
42-
parser.Replace(ref context, matches);
44+
var replaceMatches = parser.GetReplaceMatches(ref context, matches);
4345

46+
var outputFileInfo = new FileInfo(outputFilePath);
47+
var output = Output.File(outputFileInfo);
48+
output.Replace(input, replaceMatches);
4449
Assert.Equal(matches.Count(), matchesCount);
4550
}
4651
}

src/SimpleStateMachine.StructuralSearch.Tests/Mock/EmptyParsingContext.cs renamed to src/SimpleStateMachine.StructuralSearch/EmptyParsingContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using System.Collections.Generic;
22

3-
namespace SimpleStateMachine.StructuralSearch.Tests.Mock
3+
namespace SimpleStateMachine.StructuralSearch
44
{
55
public class EmptyParsingContext : IParsingContext
66
{

src/SimpleStateMachine.StructuralSearch/Input/EmptyInput.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public Result<char, T> ParseBy<T>(Parser<char, T> parser)
99
throw new System.NotImplementedException();
1010
}
1111

12-
public void ReplaceAsync(Match<string> match, string value)
12+
public void Replace(Match<string> match, string value)
1313
{
1414
throw new System.NotImplementedException();
1515
}

src/SimpleStateMachine.StructuralSearch/Input/FileInput.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public Result<char, T> ParseBy<T>(Parser<char, T> parser)
1919
return parser.Parse(FileInfo.OpenText());
2020
}
2121

22-
public void ReplaceAsync(Match<string> match, string value)
22+
public void Replace(Match<string> match, string value)
2323
{
2424
var text = File.ReadAllText(Path);
2525
text = text.Replace(match.Value, value);

src/SimpleStateMachine.StructuralSearch/Input/IInput.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace SimpleStateMachine.StructuralSearch
55
public interface IInput
66
{
77
Result<char, T> ParseBy<T>(Parser<char, T> parser);
8-
void ReplaceAsync(Match<string> match, string value);
8+
void Replace(Match<string> match, string value);
99

1010
string Extension { get; }
1111
string Path { get; }
File renamed without changes.

src/SimpleStateMachine.StructuralSearch/Input/StringInput.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public Result<char, T> ParseBy<T>(Parser<char, T> parser)
1616
return parser.Parse(Input);
1717
}
1818

19-
public void ReplaceAsync(Match<string> match, string value)
19+
public void Replace(Match<string> match, string value)
2020
{
2121
throw new System.NotImplementedException();
2222
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System.Collections.Generic;
2+
using System.IO;
3+
using System.Linq;
4+
5+
namespace SimpleStateMachine.StructuralSearch;
6+
7+
public class FileOutput : IOutput
8+
{
9+
public readonly FileInfo FileInfo;
10+
11+
public FileOutput(FileInfo fileInfo)
12+
{
13+
FileInfo = fileInfo;
14+
}
15+
16+
public void Replace(IInput input, IEnumerable<ReplaceMatch> replaceMatches)
17+
{
18+
var text = replaceMatches
19+
.Aggregate(input.Data, (current, replaceMatch) =>
20+
current.Replace(replaceMatch.Match.Value, replaceMatch.Value));
21+
22+
File.WriteAllText(Path, text);
23+
}
24+
25+
public string Extension => FileInfo.Extension;
26+
public string Path => System.IO.Path.GetFullPath(FileInfo.FullName);
27+
public string Name => System.IO.Path.GetFileNameWithoutExtension(FileInfo.Name);
28+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
using System.Collections.Generic;
2+
3+
namespace SimpleStateMachine.StructuralSearch;
4+
5+
public interface IOutput
6+
{
7+
void Replace(IInput input, IEnumerable<ReplaceMatch> replaceMatches);
8+
}

0 commit comments

Comments
 (0)