Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor the TextIndexTest.cs #57

Merged
merged 3 commits into from
Dec 6, 2023
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
24 changes: 24 additions & 0 deletions LrcParser.Tests/Helper/TestCaseTextIndexHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright (c) karaoke.dev <[email protected]>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

using System.Text.RegularExpressions;
using LrcParser.Extension;
using LrcParser.Model;

namespace LrcParser.Tests.Helper;

public static class TestCaseTextIndexHelper
{
public static TextIndex ParseTextIndex(string str)
{
var regex = new Regex("(?<index>[-0-9]+),(?<state>start|end)");
var result = regex.Match(str);
if (!result.Success)
throw new RegexMatchTimeoutException(nameof(str));

int index = result.GetGroupValue<int>("index");
var state = result.GetGroupValue<string>("state") == "start" ? IndexState.Start : IndexState.End;

return new TextIndex(index, state);
}
}
84 changes: 38 additions & 46 deletions LrcParser.Tests/Model/TextIndexTest.cs
Original file line number Diff line number Diff line change
@@ -1,74 +1,66 @@
// Copyright (c) karaoke.dev <[email protected]>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

using System;
using LrcParser.Model;
using LrcParser.Tests.Helper;
using NUnit.Framework;

namespace LrcParser.Tests.Model;

[TestFixture]
public class TextIndexTest
{
[TestCase(1, 1)]
[TestCase(1.5, 1.5)]
[TestCase(-1.5, -1.5)]
public void TestOperatorEqual(double tone1, double tone2)
[TestCase("1,start")]
[TestCase("1,end")]
[TestCase("-1,start")]
public void TestOperatorEqual(string textIndex1)
{
Assert.AreEqual(numberToTextIndex(tone1), numberToTextIndex(tone2));
Assert.AreEqual(TestCaseTextIndexHelper.ParseTextIndex(textIndex1), TestCaseTextIndexHelper.ParseTextIndex(textIndex1));
}

[TestCase(-1, 1)]
[TestCase(1, -1)]
[TestCase(1.5, -1.5)]
[TestCase(1.5, 1)]
[TestCase(-1.5, -1)]
[TestCase(-1.5, -2)]
public void TestOperatorNotEqual(double tone1, double tone2)
[TestCase("-1,start", "1,start")]
[TestCase("1,start", "-1,start")]
[TestCase("1,end", "-1,end")]
[TestCase("1,end", "1,start")]
[TestCase("-2,end", "-1,start")]
[TestCase("-2,end", "-2,start")]
public void TestOperatorNotEqual(string textIndex1, string textIndex2)
{
Assert.AreNotEqual(numberToTextIndex(tone1), numberToTextIndex(tone2));
Assert.AreNotEqual(TestCaseTextIndexHelper.ParseTextIndex(textIndex1), TestCaseTextIndexHelper.ParseTextIndex(textIndex2));
}

[TestCase(1, 0, true)]
[TestCase(1, 0.5, true)]
[TestCase(1, 1, false)]
[TestCase(1, 1.5, false)]
public void TestOperatorGreater(double tone1, double tone2, bool match)
[TestCase("1,start", "0,start", true)]
[TestCase("1,start", "0,end", true)]
[TestCase("1,start", "1,start", false)]
[TestCase("1,start", "1,end", false)]
public void TestOperatorGreater(string textIndex1, string textIndex2, bool match)
{
Assert.AreEqual(numberToTextIndex(tone1) > numberToTextIndex(tone2), match);
Assert.AreEqual(TestCaseTextIndexHelper.ParseTextIndex(textIndex1) > TestCaseTextIndexHelper.ParseTextIndex(textIndex2), match);
}

[TestCase(1, 0, true)]
[TestCase(1, 0.5, true)]
[TestCase(1, 1, true)]
[TestCase(1, 1.5, false)]
public void TestOperatorGreaterOrEqual(double tone1, double tone2, bool match)
[TestCase("1,start", "0,start", true)]
[TestCase("1,start", "0,end", true)]
[TestCase("1,start", "1,start", true)]
[TestCase("1,start", "1,end", false)]
public void TestOperatorGreaterOrEqual(string textIndex1, string textIndex2, bool match)
{
Assert.AreEqual(numberToTextIndex(tone1) >= numberToTextIndex(tone2), match);
Assert.AreEqual(TestCaseTextIndexHelper.ParseTextIndex(textIndex1) >= TestCaseTextIndexHelper.ParseTextIndex(textIndex2), match);
}

[TestCase(-1, 0, true)]
[TestCase(-1, -0.5, true)]
[TestCase(-1, -1, false)]
[TestCase(-1, -1.5, false)]
public void TestOperatorLess(double tone1, double tone2, bool match)
[TestCase("-1,start", "0,start", true)]
[TestCase("-1,start", "-1,end", true)]
[TestCase("-1,start", "-1,start", false)]
[TestCase("-1,start", "-2,end", false)]
public void TestOperatorLess(string textIndex1, string textIndex2, bool match)
{
Assert.AreEqual(numberToTextIndex(tone1) < numberToTextIndex(tone2), match);
Assert.AreEqual(TestCaseTextIndexHelper.ParseTextIndex(textIndex1) < TestCaseTextIndexHelper.ParseTextIndex(textIndex2), match);
}

[TestCase(-1, 0, true)]
[TestCase(-1, -0.5, true)]
[TestCase(-1, -1, true)]
[TestCase(-1, -1.5, false)]
public void TestOperatorLessOrEqual(double tone1, double tone2, bool match)
[TestCase("-1,start", "0,start", true)]
[TestCase("-1,start", "-1,end", true)]
[TestCase("-1,start", "-1,start", true)]
[TestCase("-1,start", "-2,end", false)]
public void TestOperatorLessOrEqual(string textIndex1, string textIndex2, bool match)
{
Assert.AreEqual(numberToTextIndex(tone1) <= numberToTextIndex(tone2), match);
}

private static TextIndex numberToTextIndex(double tone)
{
var half = Math.Abs(tone) % 1 == 0.5;
var scale = tone < 0 ? (int)tone - (half ? 1 : 0) : (int)tone;
return new TextIndex(scale, half ? IndexState.End : IndexState.Start);
Assert.AreEqual(TestCaseTextIndexHelper.ParseTextIndex(textIndex1) <= TestCaseTextIndexHelper.ParseTextIndex(textIndex2), match);
}
}
4 changes: 2 additions & 2 deletions LrcParser.Tests/Utils/TextIndexUtilsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ public void TestToGapIndex(int index, IndexState state, int expected)

[TestCase(IndexState.Start, 1, -1, 1)]
[TestCase(IndexState.End, 1, -1, -1)]
[TestCase(IndexState.Start, "1", "-1", "1")]
[TestCase(IndexState.End, "1", "-1", "-1")]
[TestCase(IndexState.Start, "start", "end", "start")]
[TestCase(IndexState.End, "start", "end", "end")]
public void TestGetValueByState(IndexState state, object startValue, object endValue, object expected)
{
var textIndex = new TextIndex(0, state);
Expand Down
11 changes: 11 additions & 0 deletions LrcParser/Utils/TextIndexUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,17 @@ internal static int ToCharIndex(TextIndex index)
internal static int ToGapIndex(TextIndex index)
=> GetValueByState(index, index.Index, index.Index + 1);

/// <summary>
/// Get the value by state.
/// If the state is start, then return start value.
/// If the state is end, then return end value.
/// </summary>
/// <param name="index"></param>
/// <param name="startValue"></param>
/// <param name="endValue"></param>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
/// <exception cref="ArgumentOutOfRangeException"></exception>
internal static T GetValueByState<T>(TextIndex index, T startValue, T endValue) =>
index.State switch
{
Expand Down
Loading