Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/SimpleStateMachine.StructuralSearch.Sandbox/Program.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
using System;
using System.Linq;
using System.Text.Json;
using System.Text.RegularExpressions;
using Pidgin;
using SimpleStateMachine.StructuralSearch.Configurations;
using SimpleStateMachine.StructuralSearch.Extensions;
using SimpleStateMachine.StructuralSearch.Rules;
using YamlDotNet.Serialization;
using YamlDotNet.Serialization.NamingConventions;
using static Pidgin.Parser;
using String = System.String;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

<ItemGroup>
<PackageReference Include="Pidgin" Version="3.0.0" />
<PackageReference Include="YamlDotNet" Version="11.2.1" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
Configurations:
- FindTemplate: |-
if($var$ $sign$ null)
{
$var$ = $value$;
}
FindRules:
- $sign$ In ("Is", "==")
ReplaceTemplate: |-
$var$ ??= $value$;
ReplaceRules:



- FindTemplate: |-
if($condition$)
return $value1$;
else
return $value2$;
FindRules:
ReplaceTemplate: |-
return $condition$? $value1$ : $value2$;
ReplaceRules:



- FindTemplate: |-
if($value1$ $sign$ null)
{
$var$ = $value1$;
}
else
{
$var$ = $value2$;
}
FindRules:
- $sign$ In ("Is", "==")
ReplaceTemplate: |-
$var$ = $value1$ ?? $value2$;
ReplaceRules:
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
Configurations:
- FindTemplate: |-
if($var$ $sign$ null)
{
$var$ = $value$;
}
FindRules:
- $sign$ In ("Is", "==")
ReplaceTemplate: |-
$var$ ??= $value$;



- FindTemplate: |-
if($condition$)
return $value1$;
else
return $value2$;
ReplaceTemplate: |-
return $condition$? $value1$ : $value2$;



- FindTemplate: |-
if($value1$ $sign$ null)
{
$var$ = $value1$;
}
else
{
$var$ = $value2$;
}
FindRules:
- $sign$ In ("Is", "==")
ReplaceTemplate: |-
$var$ = $value1$ ?? $value2$;
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System.Collections.Generic;
using System.IO;
using System.Text.Json;
using Pidgin;
using SimpleStateMachine.StructuralSearch.Configurations;
using Xunit;
using YamlDotNet.Serialization;
using YamlDotNet.Serialization.NamingConventions;

namespace SimpleStateMachine.StructuralSearch.Tests
{
public class ConfigurationFileTests
{
[Theory]
[InlineData("ConfigurationFile/ShortConfig.yml")]
[InlineData("ConfigurationFile/FullConfig.yml")]
public void ConfigurationFileParsingShouldBeSuccess(string filePath)
{
var yml = File.ReadAllText(filePath);
var deserializer = new DeserializerBuilder()
.WithNamingConvention(PascalCaseNamingConvention.Instance)
.Build();

var cfg = deserializer.Deserialize<ConfigurationsFile>(yml);
}

// private ConfigurationsFile Mock()
// {
// var configurationFile = new ConfigurationsFile();
//
// }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
$sign$ In ("Is", "==")
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
if($var1$ $sign$ $value1$)
if($var1$ $sign$ $some1$)
{
$var$ = $value1$;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
if($condition$)
return $value1$;
else
return $value2$;
19 changes: 11 additions & 8 deletions src/SimpleStateMachine.StructuralSearch.Tests/FindTemplateTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,30 @@ namespace SimpleStateMachine.StructuralSearch.Tests
public class FindTemplateTests
{
[Theory]
[InlineData("FindTemplate/IfElseTemplate.txt")]
[InlineData("FindTemplate/IfValueIsNullTemplate.txt")]
[InlineData("FindTemplate/NestedParenthesisedTemplate.txt")]
[InlineData("FindTemplate/IfElseFindTemplate.txt")]
[InlineData("FindTemplate/IfValueIsNullFindTemplate.txt")]
[InlineData("FindTemplate/NestedParenthesisedFindTemplate.txt")]
[InlineData("FindTemplate/TernaryOperatorFindTemplate.txt")]
public void TemplateParsingShouldBeSuccess(string templatePath)
{
var findTemplate = File.ReadAllText(templatePath);
var template = StructuralSearch.ParseFindTemplate(findTemplate);
var template = StructuralSearch.ParseFindTemplate(findTemplate, new ParsingContext());

Assert.NotNull(template);
}

[Theory]
[InlineData("FindTemplate/IfElseTemplate.txt", "Source/IfElseSource.txt")]
[InlineData("FindTemplate/IfValueIsNullTemplate.txt", "Source/IfValueIsNullSource.txt")]
[InlineData("FindTemplate/NestedParenthesisedTemplate.txt", "Source/NestedParenthesisedSource.txt")]
[InlineData("FindTemplate/IfElseFindTemplate.txt", "Source/IfElseSource.txt")]
[InlineData("FindTemplate/IfValueIsNullFindTemplate.txt", "Source/IfValueIsNullSource.txt")]
[InlineData("FindTemplate/NestedParenthesisedFindTemplate.txt", "Source/NestedParenthesisedSource.txt")]
[InlineData("FindTemplate/TernaryOperatorFindTemplate.txt", "Source/TernaryOperatorSource.txt")]
public void SourceParsingBeFindTemplateShouldBeSuccess(string templatePath, string sourcePath)
{
var findTemplate = File.ReadAllText(templatePath);
var source = File.ReadAllText(sourcePath);

var template = StructuralSearch.ParseFindTemplate(findTemplate);
var context = new ParsingContext();
var template = StructuralSearch.ParseFindTemplate(findTemplate, context);
var result = template.ParseOrThrow(source);

Assert.NotNull(template);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace SimpleStateMachine.StructuralSearch.Tests.Mock
{
public class EmptyParsingContext : IParsingContext
{
public bool TryGetPlaceholder(string name, out string value)
{
throw new System.NotImplementedException();
}

public void AddPlaceholder(string name, string value)
{
throw new System.NotImplementedException();
}

public string GetPlaceholder(string name)
{
return name;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,32 @@ public class PlaceholderParserTests
[Fact]
public void FindTemplateShouldBeNotEmpty()
{
Assert.Throws<ParseException>(() => StructuralSearch.ParseFindTemplate(string.Empty));
Assert.Throws<ParseException>(() => StructuralSearch.ParseFindTemplate(string.Empty, new ParsingContext()));
}

[Theory]
[InlineData("($test$)", "(value )", "value ")]
[InlineData("($test$ )", "(value )", "value")]
[InlineData("($test$)", "(value (test))", "value (test)")]
[InlineData("($test$)", "(value (test) )", "value (test) ")]
public void TemplateParsingShouldBeSuccess(string template, string source, string result)
{
var templateParser = StructuralSearch.ParseFindTemplate(template);
var context = new ParsingContext();
var templateParser = StructuralSearch.ParseFindTemplate(template, context);
var res = templateParser.ParseOrThrow(source);
var value = context.GetPlaceholder("test");

// var templateStr = File.ReadAllText(templatePath);
// var template = StructuralSearch.ParseTemplate(templateStr);
//
// Assert.NotNull(template);
Assert.Equal(value, result);
}

[Theory]
[InlineData("$var$;$var2$;", "test;;;test;;;", "value ")]
public void TemplateParsingShouldBeSuccess2(string template, string source, string result)
{
var templateParser = StructuralSearch.ParseFindTemplate(template);
var context = new ParsingContext();
var templateParser = StructuralSearch.ParseFindTemplate(template, context);
var res = templateParser.ParseOrThrow(source);


// var templateStr = File.ReadAllText(templatePath);
// var template = StructuralSearch.ParseTemplate(templateStr);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
temp == 125 ? 12 : 15;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
temp ??= 12;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
return temp == 125? 12 : 15;
Original file line number Diff line number Diff line change
@@ -1 +1 @@
$var1$ $sign$ $value1$ ? $value1$ : $value2$;
$var1$ $sign$ $value1$ ? $value2$ : $value3$;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
return $condition$? $value1$ : $value2$;
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System.IO;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using SimpleStateMachine.StructuralSearch.Tests.Mock;
using Xunit;

namespace SimpleStateMachine.StructuralSearch.Tests
Expand All @@ -9,14 +11,44 @@ public class ReplaceTemplateTests
[Theory]
[InlineData("ReplaceTemplate/IfElseReplaceTemplate.txt", 14)]
[InlineData("ReplaceTemplate/IfValueIsNullReplaceTemplate.txt", 6)]
public void TemplateParsingShouldBeSuccess(string templatePath, int stepsCount)
[InlineData("ReplaceTemplate/TernaryOperatorReplaceTemplate.txt", 11)]
public void TemplateParsingShouldHaveStepCount(string templatePath, int stepsCount)
{
var replaceTemplate = File.ReadAllText(templatePath);
var replaceBuilder = StructuralSearch.ParseReplaceTemplate(replaceTemplate);
var result = replaceBuilder.Build();
var result = replaceBuilder.Build(new EmptyParsingContext());

Assert.NotNull(replaceTemplate);
Assert.Equal(replaceBuilder.Steps.Count(), stepsCount);
}

[Theory]
[InlineData("ReplaceTemplate/IfElseReplaceTemplate.txt", "ReplaceResult/IfElseReplaceResult.txt",
new[] { "var1", "sign", "value1", "value2", "value3" },
new[] { "temp", "==", "125", "12", "15" })]
[InlineData("ReplaceTemplate/IfValueIsNullReplaceTemplate.txt", "ReplaceResult/IfValueIsNullReplaceResult.txt",
new[] { "var", "value" },
new[] { "temp", "12" })]
[InlineData("ReplaceTemplate/TernaryOperatorReplaceTemplate.txt", "ReplaceResult/TernaryOperatorReplaceResult.txt",
new[] { "condition", "value1", "value2" },
new[] { "temp == 125", "12", "15" })]
public void ReplaceBuildShouldBeSuccess(string templatePath, string resultPath, string[] keys, string[] values)
{
var replaceTemplate = File.ReadAllText(templatePath);
var replaceResult = File.ReadAllText(resultPath);
var replaceBuilder = StructuralSearch.ParseReplaceTemplate(replaceTemplate);

var parsingContext = new ParsingContext();
for (int i = 0; i < keys.Length; i++)
{
parsingContext.AddPlaceholder(keys[i], values[i]);
}

var result = replaceBuilder.Build(parsingContext);

Assert.NotNull(replaceTemplate);
Assert.NotNull(replaceResult);
Assert.Equal(replaceResult, result);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="YamlDotNet" Version="11.2.1" />
</ItemGroup>

<ItemGroup>
Expand All @@ -34,6 +35,16 @@
<Content Include="ReplaceTemplate\*.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="ReplaceResult\*.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="ConfigurationFile\*">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>

<ItemGroup>
<Folder Include="ReplaceRule" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
if(temp = 125)
if(temp == 125)
{
test = 12;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
if(temp == 125)
return 12;
else
return 15;
13 changes: 13 additions & 0 deletions src/SimpleStateMachine.StructuralSearch.Tests/Test.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using YamlDotNet.Serialization;
using System.Text.RegularExpressions;

namespace SimpleStateMachine.StructuralSearch.Tests
{
public class Test: INamingConvention
{
public string Apply(string value)
{
throw new System.NotImplementedException();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.Collections.Generic;

namespace SimpleStateMachine.StructuralSearch.Configurations
{
public class Configuration
{
public string FindTemplate { get; set; }

public List<string> FindRules { get; set; }

public string ReplaceTemplate { get; set; }

public List<string> ReplaceRules { get; set; }
}
}
Loading