Skip to content

Commit c50424a

Browse files
committed
Move text processing methods to separate class
1 parent 4f003d4 commit c50424a

File tree

5 files changed

+62
-43
lines changed

5 files changed

+62
-43
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System.Text.RegularExpressions;
2+
3+
namespace SimpleCodeGenerator.Core
4+
{
5+
public static class TextProcessing
6+
{
7+
public static string SanitizeStringForVariableName(string input)
8+
{
9+
if (string.IsNullOrWhiteSpace(input))
10+
return "";
11+
12+
string variableName = Regex.Replace(input, "[^0-9A-Za-z_]", string.Empty);
13+
14+
if (int.TryParse(variableName[0].ToString(), out int _))
15+
{
16+
variableName = $"_{variableName}";
17+
}
18+
19+
return variableName;
20+
}
21+
22+
public static string EscapeSummaryText(string text)
23+
{
24+
return $"<![CDATA[{text}]]>";
25+
}
26+
}
27+
}

src/Assets/SimpleCodeGenerator/Core/TextProcessing.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Assets/SimpleCodeGenerator/Editor/CodeGenerator.cs

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ public static void GenerateStringDictionary(IEnumerable<StringDictionaryItem> va
5151

5252
for (int i = 0; i < valueArray.Length; i++)
5353
{
54-
valueArray[i].Key = SanitizeStringForVariableName(valueArray[i].Key);
55-
valueArray[i].Summary = EscapeSummaryText(valueArray[i].Summary);
54+
valueArray[i].Key = TextProcessing.SanitizeStringForVariableName(valueArray[i].Key);
55+
valueArray[i].Summary = TextProcessing.EscapeSummaryText(valueArray[i].Summary);
5656
}
5757

5858
var template = Template.FindBuiltInTemplate("StringDictionary");
@@ -81,7 +81,7 @@ public static void GenerateEnum(IEnumerable<string> values, string namespaceName
8181

8282
for (int i = 0; i < valueArray.Length; i++)
8383
{
84-
valueArray[i] = SanitizeStringForVariableName(valueArray[i]);
84+
valueArray[i] = TextProcessing.SanitizeStringForVariableName(valueArray[i]);
8585
}
8686

8787
var template = Template.FindBuiltInTemplate("Enum");
@@ -149,25 +149,5 @@ private static string GetRelativePathToBuiltInTemplate(string templateName)
149149
{
150150
return Template.GetAbsolutePathToBuiltInTemplate(templateName) - Application.dataPath;
151151
}
152-
153-
public static string SanitizeStringForVariableName(string input)
154-
{
155-
if (string.IsNullOrWhiteSpace(input))
156-
return "";
157-
158-
string variableName = Regex.Replace(input, "[^0-9A-Za-z_]", string.Empty);
159-
160-
if (int.TryParse(variableName[0].ToString(), out int _))
161-
{
162-
variableName = $"_{variableName}";
163-
}
164-
165-
return variableName;
166-
}
167-
168-
public static string EscapeSummaryText(string text)
169-
{
170-
return $"<![CDATA[{text}]]>";
171-
}
172152
}
173153
}

tests/CodeGeneratorTests.cs

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using FluentAssertions;
2+
using SimpleCodeGenerator.Core;
23
using SimpleCodeGenerator.Editor;
34

45
namespace tests;
@@ -82,24 +83,4 @@ public Task GenerateFromTemplate_RenderFromValidTemplateInstance_TemplateIsRende
8283
settings.UseDirectory("Verify");
8384
return Verify(result, settings);
8485
}
85-
86-
[Theory]
87-
[InlineData("test", "test")]
88-
[InlineData("test123", "test123")]
89-
[InlineData("1test", "_1test")]
90-
[InlineData("1 test", "_1test")]
91-
[InlineData("hello world", "helloworld")]
92-
[InlineData(" hello world", "helloworld")]
93-
[InlineData(" hello world", "helloworld")]
94-
[InlineData(" hello 1 world", "hello1world")]
95-
[InlineData(" ", "")]
96-
[InlineData(" ", "")]
97-
[InlineData("/test", "test")]
98-
[InlineData("test%$&§!?`+-#", "test")]
99-
public void SanitizeStringForVariableName_SanitizeStrings_StringAreSanitizedCorrectly(string input, string expected)
100-
{
101-
string result = CodeGenerator.SanitizeStringForVariableName(input);
102-
103-
result.Should().Be(expected);
104-
}
10586
}

tests/TextProcessingTests.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using FluentAssertions;
2+
using SimpleCodeGenerator.Core;
3+
using SimpleCodeGenerator.Editor;
4+
5+
namespace tests;
6+
7+
public class TextProcessingTests
8+
{
9+
[Theory]
10+
[InlineData("test", "test")]
11+
[InlineData("test123", "test123")]
12+
[InlineData("1test", "_1test")]
13+
[InlineData("1 test", "_1test")]
14+
[InlineData("hello world", "helloworld")]
15+
[InlineData(" hello world", "helloworld")]
16+
[InlineData(" hello world", "helloworld")]
17+
[InlineData(" hello 1 world", "hello1world")]
18+
[InlineData(" ", "")]
19+
[InlineData(" ", "")]
20+
[InlineData("/test", "test")]
21+
[InlineData("test%$&§!?`+-#", "test")]
22+
public void SanitizeStringForVariableName_SanitizeStrings_StringAreSanitizedCorrectly(string input, string expected)
23+
{
24+
string result = TextProcessing.SanitizeStringForVariableName(input);
25+
26+
result.Should().Be(expected);
27+
}
28+
}

0 commit comments

Comments
 (0)