Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonCropp committed Dec 31, 2024
1 parent 3d18715 commit 04f806f
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 0 deletions.
57 changes: 57 additions & 0 deletions src/Verify.Tests/DateFormatTokenizerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
public class DateFormatTokenizerTests
{
[Fact]
public void Tokenize_ShouldHandleSimpleFormat()
{
var format = "yyyy-MM-dd";
var expectedTokens = new List<string> { "yyyy", "-", "MM", "-", "dd" };

var tokens = DateFormatTokenizer.Tokenize(format);

Assert.Equal(expectedTokens, tokens);
}

[Fact]
public void Tokenize_ShouldHandleLiteralStrings()
{
var format = "yyyy-MM-dd 'at' HH:mm";
var expectedTokens = new List<string> { "yyyy", "-", "MM", "-", "dd", " 'at' ", "HH", ":", "mm" };

var tokens = DateFormatTokenizer.Tokenize(format);

Assert.Equal(expectedTokens, tokens);
}

[Fact]
public void Tokenize_ShouldHandleEscapedCharacters()
{
var format = "yyyy-MM-dd\\THH:mm:ss";
var expectedTokens = new List<string> { "yyyy", "-", "MM", "-", "dd", "T", "HH", ":", "mm", ":", "ss" };

var tokens = DateFormatTokenizer.Tokenize(format);

Assert.Equal(expectedTokens, tokens);
}

[Fact]
public void Tokenize_ShouldHandleMixedLiteralsAndEscapes()
{
var format = "yyyy-MM-dd 'at' HH\\:mm\\:ss";
var expectedTokens = new List<string> { "yyyy", "-", "MM", "-", "dd", " 'at' ", "HH", ":", "mm", ":", "ss" };

var tokens = DateFormatTokenizer.Tokenize(format);

Assert.Equal(expectedTokens, tokens);
}

[Fact]
public void Tokenize_ShouldHandleEmptyFormat()
{
var format = "";
var expectedTokens = new List<string>();

var tokens = DateFormatTokenizer.Tokenize(format);

Assert.Equal(expectedTokens, tokens);
}
}
78 changes: 78 additions & 0 deletions src/Verify/Serialization/Scrubbers/DateTimeFormatTokenizer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@

public static class DateFormatTokenizer
{
public static List<string> Tokenize(string format)
{
var tokens = new List<string>();
var currentToken = string.Empty;
var inLiteral = false;
var escapeNext = false;

foreach (var c in format)
{
if (escapeNext)
{
currentToken += c;
escapeNext = false;
continue;
}

if (c == '\\')
{
escapeNext = true;
continue;
}

if (c is '\'' or '\"')
{
if (inLiteral)
{
tokens.Add(currentToken + c);
currentToken = string.Empty;
inLiteral = false;
}
else
{
if (!string.IsNullOrEmpty(currentToken))
{
tokens.Add(currentToken);
}
currentToken = c.ToString();
inLiteral = true;
}
}
else if (inLiteral)
{
currentToken += c;
}
else if (char.IsLetter(c))
{
if (currentToken.Length > 0 && currentToken[0] != c)
{
tokens.Add(currentToken);
currentToken = c.ToString();
}
else
{
currentToken += c;
}
}
else
{
if (!string.IsNullOrEmpty(currentToken))
{
tokens.Add(currentToken);
currentToken = string.Empty;
}
tokens.Add(c.ToString());
}
}

if (!string.IsNullOrEmpty(currentToken))
{
tokens.Add(currentToken);
}

return tokens;
}
}

0 comments on commit 04f806f

Please sign in to comment.