Skip to content

Commit

Permalink
fixes for 1.0.4 release (#5)
Browse files Browse the repository at this point in the history
* Merge pull request #2 from chrisjbreisch/dev (#3)

* small naming fixes, unit tests

* Merge pull request #2 from chrisjbreisch/dev (#4)
  • Loading branch information
chrisjbreisch authored May 10, 2022
1 parent 3fcc4d3 commit 69e10e9
Show file tree
Hide file tree
Showing 9 changed files with 387 additions and 136 deletions.
155 changes: 155 additions & 0 deletions Trs80.Level1Basic.Extensions.Test/StringExtensionsTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Trs80.Level1Basic.Common.Extensions;

namespace Trs80.Level1Basic.Extensions.Test
{
[TestClass]
public class StringExtensionsTest
{
private const string testString = "four Score-and_seven_YeArs AGO";
private const string testOneWordString = "fOUr";
private const string testUpperOneLetterString = "F";
private const string testLowerOneLetterString = "f";

[TestMethod]
public void Can_Convert_String_To_Pascal_Case()
{
string? result = testString.ToPascalCase();
result.Should().Be("FourScoreAndSevenYearsAgo");
}

[TestMethod]
public void Can_Convert_String_To_Camel_Case()
{
string? result = testString.ToCamelCase();
result.Should().Be("fourScoreAndSevenYearsAgo");
}

[TestMethod]
public void Can_Convert_String_To_Snake_Case()
{
string? result = testString.ToSnakeCase();
result.Should().Be("four_score_and_seven_years_ago");
}

[TestMethod]
public void Can_Convert_String_To_Caps_Case()
{
string? result = testString.ToCapsCase();
result.Should().Be("FOUR_SCORE_AND_SEVEN_YEARS_AGO");
}

[TestMethod]
public void Can_Convert_String_To_Kebab_Case()
{
string? result = testString.ToKebabCase();
result.Should().Be("four-score-and-seven-years-ago");
}

[TestMethod]
public void Can_Convert_One_Word_String_To_Pascal_Case()
{
string? result = testOneWordString.ToPascalCase();
result.Should().Be("Four");
}

[TestMethod]
public void Can_Convert_One_Word_String_To_Camel_Case()
{
string? result = testOneWordString.ToCamelCase();
result.Should().Be("four");
}

[TestMethod]
public void Can_Convert_One_Word_String_To_Snake_Case()
{
string? result = testOneWordString.ToSnakeCase();
result.Should().Be("four");
}

[TestMethod]
public void Can_Convert_One_Word_String_To_Caps_Case()
{
string? result = testOneWordString.ToCapsCase();
result.Should().Be("FOUR");
}

[TestMethod]
public void Can_Convert_One_Word_String_To_Kebab_Case()
{
string? result = testOneWordString.ToKebabCase();
result.Should().Be("four");
}

[TestMethod]
public void Can_Convert_One_Letter_Upper_String_To_Pascal_Case()
{
string? result = testUpperOneLetterString.ToPascalCase();
result.Should().Be("F");
}

[TestMethod]
public void Can_Convert_One_Letter_Upper_String_To_Camel_Case()
{
string? result = testUpperOneLetterString.ToCamelCase();
result.Should().Be("f");
}

[TestMethod]
public void Can_Convert_One_Letter_Upper_String_To_Snake_Case()
{
string? result = testUpperOneLetterString.ToSnakeCase();
result.Should().Be("f");
}

[TestMethod]
public void Can_Convert_One_Letter_Upper_String_To_Caps_Case()
{
string? result = testUpperOneLetterString.ToCapsCase();
result.Should().Be("F");
}

[TestMethod]
public void Can_Convert_One_Letter_Upper_String_To_Kebab_Case()
{
string? result = testUpperOneLetterString.ToKebabCase();
result.Should().Be("f");
}

[TestMethod]
public void Can_Convert_One_Letter_Lower_String_To_Pascal_Case()
{
string? result = testLowerOneLetterString.ToPascalCase();
result.Should().Be("F");
}

[TestMethod]
public void Can_Convert_One_Letter_Lower_String_To_Camel_Case()
{
string? result = testLowerOneLetterString.ToCamelCase();
result.Should().Be("f");
}

[TestMethod]
public void Can_Convert_One_Letter_Lower_String_To_Snake_Case()
{
string? result = testLowerOneLetterString.ToSnakeCase();
result.Should().Be("f");
}

[TestMethod]
public void Can_Convert_One_Letter_Lower_String_To_Caps_Case()
{
string? result = testLowerOneLetterString.ToCapsCase();
result.Should().Be("F");
}

[TestMethod]
public void Can_Convert_One_Letter_Lower_String_To_Kebab_Case()
{
string? result = testLowerOneLetterString.ToKebabCase();
result.Should().Be("f");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.6.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.10" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.10" />
<PackageReference Include="coverlet.collector" Version="3.1.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Trs80.Level1Basic.Extensions\Trs80.Level1Basic.Common.csproj" />
</ItemGroup>

</Project>
74 changes: 69 additions & 5 deletions Trs80.Level1Basic.Extensions/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text;

namespace Trs80.Level1Basic.Common.Extensions;

Expand All @@ -9,21 +11,83 @@ public static class StringExtensions
{
public static string ToPascalCase(this string text)
{
if (string.IsNullOrEmpty(text)) return text;
if (text.Length == 1) return text.ToUpper();
string result = HandleBaseCases(text, true);
if (!string.IsNullOrEmpty(result)) return result;

string[] words = text.Split(new[] { '-', '_' }, StringSplitOptions.RemoveEmptyEntries);
string[] words = SplitWords(text);

if (words.Length > 1)
return string.Concat(words.Select(word => word[..1].ToUpper() + word[1..].ToLower()));

return words[0][..1].ToUpper() + words[0][1..];
return words[0][..1].ToUpper() + words[0][1..].ToLower();
}

private static string[] SplitWords(string text)
{
string[] words = text.Split(new[] { '-', '_', ' ' }, StringSplitOptions.RemoveEmptyEntries);
return words;
}

public static string ToCamelCase(this string text)
{
string result = ToPascalCase(text);
string result = HandleBaseCases(text, false);
if (!string.IsNullOrEmpty(result)) return result;

result = ToPascalCase(text);

return result[..1].ToLower() + result[1..];
}

public static string ToSnakeCase(this string text)
{
string result = HandleBaseCases(text, false);
if (!string.IsNullOrEmpty(result)) return result;

string[] words = SplitWords(text);

return CasedStringWithSeparator(words, false, '_');
}

public static string ToCapsCase(this string text)
{
string result = HandleBaseCases(text, true);
if (!string.IsNullOrEmpty(result)) return result;

string[] words = SplitWords(text);

return CasedStringWithSeparator(words, true, '_');
}

public static string ToKebabCase(this string text)
{
string result = HandleBaseCases(text, false);
if (!string.IsNullOrEmpty(result)) return result;

string[] words = SplitWords(text);

return CasedStringWithSeparator(words, false, '-');
}

private static string HandleBaseCases(this string text, bool isUpper)
{
if (string.IsNullOrEmpty(text)) return text;
return text.Length == 1 ? text.ConvertCase(isUpper) : string.Empty;
}
private static string ConvertCase(this string word, bool isUpper)
{
return isUpper ? word.ToUpper() : word.ToLower();
}

private static string CasedStringWithSeparator(IReadOnlyList<string> words, bool isUpper, char separator)
{
if (words.Count == 1) return words[0].ConvertCase(isUpper);

StringBuilder sb = new();

for (int i = 0; i < words.Count - 1; i++)
sb.Append(words[i].ConvertCase(isUpper) + separator);

sb.Append(words[^1].ConvertCase(isUpper));
return sb.ToString();
}
}
2 changes: 1 addition & 1 deletion Trs80.Level1Basic.GenerateAst/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ private static void DefineVisitorInterface(string outputDir, string baseName, Li
string returnType = baseName.Contains("Statement") ? "void" : "dynamic";

string typeName = type.Split(":")[0].Trim();
writer.WriteLine($" {returnType} Visit{typeName}{baseName}({typeName} root);");
writer.WriteLine($" {returnType} Visit{typeName}{baseName}({typeName} {baseName.ToCamelCase()});");
}
writer.WriteLine("}");
WriteEnd(writer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"profiles": {
"GenerateAst": {
"commandName": "Project",
"commandLineArgs": "..\\..\\..\\..\\Trs80.Level1Basic.Interpreter\\Parser"
"commandLineArgs": "..\\..\\..\\..\\Trs80.Level1Basic.VirtualMachine\\Parser"
}
}
}
Loading

0 comments on commit 69e10e9

Please sign in to comment.