Skip to content

Commit

Permalink
Add Core.Models.Th143.SceneParser
Browse files Browse the repository at this point in the history
  • Loading branch information
y-iihoshi committed Jan 1, 2025
1 parent fb34da2 commit 7ace8e8
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 18 deletions.
42 changes: 26 additions & 16 deletions ThScoreFileConverter.Core.Tests/Models/IntegerParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,18 @@ public void ParseTestDefault()
return $"{name}^2 == {value * value}";
});

var power_a_2 = Regex.Replace("var a = 1;", pattern, evaluator);
Assert.AreEqual("a^2 == 1", power_a_2);
var power_b_2 = Regex.Replace("var b = 23;", pattern, evaluator);
Assert.AreEqual("b^2 == 529", power_b_2);
var power_c_2 = Regex.Replace("var c = 456;", pattern, evaluator);
Assert.AreEqual("c^2 == 207936", power_c_2);
var pairs = new[]
{
("var a = 1;", "a^2 == 1"),
("var b = 23;", "b^2 == 529"),
("var c = 456;", "c^2 == 207936"),
};

foreach (var pair in pairs)
{
var replaced = Regex.Replace(pair.Item1, pattern, evaluator);
Assert.AreEqual(pair.Item2, replaced);
}
}

[TestMethod]
Expand All @@ -40,15 +46,19 @@ public void ParseTest()
return $"{name}^2 == {value * value}";
});

var power_a_2 = Regex.Replace("var a = 1;", pattern, evaluator);
Assert.AreEqual("var a = 1;", power_a_2);
var power_b_2 = Regex.Replace("var b = 2;", pattern, evaluator);
Assert.AreEqual("b^2 == 4", power_b_2);
var power_c_2 = Regex.Replace("var c = 3;", pattern, evaluator);
Assert.AreEqual("c^2 == 9", power_c_2);
var power_d_2 = Regex.Replace("var d = 4;", pattern, evaluator);
Assert.AreEqual("d^2 == 16", power_d_2);
var power_e_2 = Regex.Replace("var e = 5;", pattern, evaluator);
Assert.AreEqual("var e = 5;", power_e_2);
var pairs = new[]
{
("var a = 1;", "var a = 1;"),
("var b = 2;", "b^2 == 4"),
("var c = 3;", "c^2 == 9"),
("var d = 4;", "d^2 == 16"),
("var e = 5;", "var e = 5;"),
};

foreach (var pair in pairs)
{
var replaced = Regex.Replace(pair.Item1, pattern, evaluator);
Assert.AreEqual(pair.Item2, replaced);
}
}
}
42 changes: 42 additions & 0 deletions ThScoreFileConverter.Core.Tests/Models/Th143/SceneParserTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System.Text.RegularExpressions;
using ThScoreFileConverter.Core.Models.Th143;

namespace ThScoreFileConverter.Core.Tests.Models.Th143;

[TestClass]
public class SceneParserTests
{
[TestMethod]
public void ParseTest()
{
var parser = new SceneParser();

var pattern = $@"var (\w+) = ({parser.Pattern});";
var evaluator = new MatchEvaluator(match =>
{
var name = match.Groups[1].Value;
var value = parser.Parse(match.Groups[2]);
return $"{name}^2 == {value * value}";
});

var pairs = new[]
{
("var a0 = 0;", "a0^2 == 100"),
("var a1 = 1;", "a1^2 == 1"),
("var a2 = 2;", "a2^2 == 4"),
("var a3 = 3;", "a3^2 == 9"),
("var a4 = 4;", "a4^2 == 16"),
("var a5 = 5;", "a5^2 == 25"),
("var a6 = 6;", "a6^2 == 36"),
("var a7 = 7;", "a7^2 == 49"),
("var a8 = 8;", "a8^2 == 64"),
("var a9 = 9;", "a9^2 == 81"),
};

foreach (var pair in pairs)
{
var replaced = Regex.Replace(pair.Item1, pattern, evaluator);
Assert.AreEqual(pair.Item2, replaced);
}
}
}
5 changes: 3 additions & 2 deletions ThScoreFileConverter.Core/Models/IntegerParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// </copyright>
//-----------------------------------------------------------------------

using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Text.RegularExpressions;
using CommunityToolkit.Diagnostics;
Expand All @@ -15,7 +16,7 @@ namespace ThScoreFileConverter.Core.Models;
/// Provides a parser for an integer value.
/// </summary>
/// <param name="pattern">The regular expression used for parsing.</param>
public sealed class IntegerParser(string pattern) : IRegexParser<int>
public class IntegerParser([StringSyntax(StringSyntaxAttribute.Regex)] string pattern) : IRegexParser<int>
{
/// <summary>
/// Initializes a new instance of the <see cref="IntegerParser"/> class.
Expand All @@ -29,7 +30,7 @@ public IntegerParser()
public string Pattern { get; } = pattern;

/// <inheritdoc/>
public int Parse(Group group)
public virtual int Parse(Group group)
{
Guard.IsNotNull(group);
return int.Parse(group.Value, CultureInfo.InvariantCulture);
Expand Down
35 changes: 35 additions & 0 deletions ThScoreFileConverter.Core/Models/Th143/SceneParser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//-----------------------------------------------------------------------
// <copyright file="SceneParser.cs" company="None">
// Copyright (c) IIHOSHI Yoshinori.
// Licensed under the BSD-2-Clause license. See LICENSE.txt file in the project root for full license information.
// </copyright>
//-----------------------------------------------------------------------

using System.Text.RegularExpressions;

namespace ThScoreFileConverter.Core.Models.Th143;

/// <summary>
/// Provides the parser of ISC scenes.
/// </summary>
public sealed class SceneParser : IntegerParser
{
/// <summary>
/// Initializes a new instance of the <see cref="SceneParser"/> class.
/// </summary>
public SceneParser()
: base(@"\d")
{
}

/// <summary>
/// Converts from the group matched with the pattern to a value indicating a scene.
/// </summary>
/// <param name="group">The group matched by <see cref="IntegerParser.Pattern"/>.</param>
/// <returns>The parsed value indicating a scene.</returns>
public override int Parse(Group group)
{
var scene = base.Parse(group);
return scene == 0 ? 10 : scene;
}
}

0 comments on commit 7ace8e8

Please sign in to comment.