Skip to content

Commit

Permalink
Change the metadata object from class to struct.
Browse files Browse the repository at this point in the history
Here's the reason:
1. technically, metadata should not be chagned after initialized and assign the value.
2. easier to compare the object, means there's no need to write lots of code to check if two metadata are the same in the test case everytime.
  • Loading branch information
andy840119 committed Jul 21, 2024
1 parent 69199f3 commit 4e7cbf0
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 22 deletions.
3 changes: 1 addition & 2 deletions LrcParser.Tests/Parser/Kar/Lines/KarLyricParserTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ public void TestDecode(string lyric, string text, string[] timeTags)
};
var actual = Decode(lyric);

Assert.That(actual.Text, Is.EqualTo(expected.Text));
Assert.That(actual.TimeTags, Is.EqualTo(expected.TimeTags));
Assert.That(actual, Is.EqualTo(expected));
}

[TestCase("帰り道は", new[] { "[0,start]:17970", "[1,start]:18370", "[2,start]:18550", "[3,start]:18940", "[3,end]:19220" }, "[00:17.97]帰[00:18.37]り[00:18.55]道[00:18.94]は[00:19.22]")]
Expand Down
6 changes: 1 addition & 5 deletions LrcParser.Tests/Parser/Kar/Lines/KarRubyParserTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,7 @@ public void TestDecode(string rubyTag, string parent, string ruby, string[] time
};
var actual = Decode(rubyTag);

Assert.That(actual.Ruby, Is.EqualTo(expected.Ruby));
Assert.That(actual.Parent, Is.EqualTo(expected.Parent));
Assert.That(actual.TimeTags, Is.EqualTo(expected.TimeTags));
Assert.That(actual.StartTime, Is.EqualTo(expected.StartTime));
Assert.That(actual.EndTime, Is.EqualTo(expected.EndTime));
Assert.That(actual, Is.EqualTo(expected));
}

[TestCase("帰", "かえ", new string[] { }, 53190, 84770, "@Ruby1=帰,かえ,[00:53.19],[01:24.77]")]
Expand Down
4 changes: 3 additions & 1 deletion LrcParser.Tests/Parser/Lines/BaseSingleLineParserTest.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
// 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.Parser.Lines;

namespace LrcParser.Tests.Parser.Lines;

public class BaseSingleLineParserTest<TParser, TModel> where TParser : SingleLineParser<TModel>, new() where TModel : class
public class BaseSingleLineParserTest<TParser, TModel>
where TParser : SingleLineParser<TModel>, new() where TModel : struct, IEquatable<TModel>
{
protected bool CanDecode(string text) => new TParser().CanDecode(text);

Expand Down
3 changes: 1 addition & 2 deletions LrcParser.Tests/Parser/Lrc/Lines/LrcLyricParserTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ public void TestDecode(string lyric, string text, string[] timeTags)
};
var actual = Decode(lyric);

Assert.That(actual.Text, Is.EqualTo(expected.Text));
Assert.That(actual.TimeTags, Is.EqualTo(expected.TimeTags));
Assert.That(actual, Is.EqualTo(expected));
}

[TestCase("帰り道は", new[] { "[0,start]:17970", "[1,start]:18370", "[2,start]:18550", "[3,start]:18940", "[3,end]:19220" }, "[00:17.97]帰[00:18.37]り[00:18.55]道[00:18.94]は[00:19.22]")]
Expand Down
30 changes: 24 additions & 6 deletions LrcParser.Tests/Parser/LyricParserTest.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// 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 System.Collections.Generic;
using System.Linq;
using LrcParser.Model;
Expand Down Expand Up @@ -45,13 +46,13 @@ public TestLyricParser()

protected override Song PostProcess(List<object> values)
{
var lines = values.OfType<string>();
var lines = values.OfType<Line>();

return new Song
{
Lyrics = lines.Select(l => new Lyric
{
Text = l,
Text = l.Text,
}).ToList(),
};
}
Expand All @@ -60,14 +61,31 @@ protected override IEnumerable<object> PreProcess(Song song)
=> song.Lyrics.Select(lyric => lyric.Text);
}

private class TestLineParser : SingleLineParser<string>
private class TestLineParser : SingleLineParser<Line>
{
public override bool CanDecode(string text)
=> true;

public override string Decode(string text) => text;
public override Line Decode(string text) => new()
{
Text = text,
};

public override string Encode(Line component, int index)
=> $"index:{index}, value: {component.Text}";
}

private struct Line : IEquatable<Line>
{
public Line()
{
}

public string Text { get; set; } = string.Empty;

public override string Encode(string component, int index)
=> $"index:{index}, value: {component}";
public bool Equals(Line other)
{
return Text == other.Text;
}
}
}
11 changes: 10 additions & 1 deletion LrcParser/Parser/Kar/Metadata/KarLyric.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@

namespace LrcParser.Parser.Kar.Metadata;

public class KarLyric
public struct KarLyric : IEquatable<KarLyric>
{
public KarLyric()
{
}

/// <summary>
/// Text
/// </summary>
Expand All @@ -16,4 +20,9 @@ public class KarLyric
/// Time tags
/// </summary>
public SortedDictionary<TextIndex, int> TimeTags { get; set; } = new();

public bool Equals(KarLyric other)
{
return Text == other.Text && TimeTags.SequenceEqual(other.TimeTags);
}
}
19 changes: 16 additions & 3 deletions LrcParser/Parser/Kar/Metadata/KarRuby.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@ namespace LrcParser.Parser.Kar.Metadata;
/// @Ruby25=時,じか,,[00:38:45]
/// @Ruby49=時,とき,[00:38:45],[01:04:49]
/// </example>
public class KarRuby
public struct KarRuby : IEquatable<KarRuby>
{
public KarRuby()
{
}

/// <summary>
/// Parent kanji
/// </summary>
Expand All @@ -33,10 +37,19 @@ public class KarRuby
/// <summary>
/// Start position
/// </summary>
public int? StartTime { get; set; }
public int? StartTime { get; set; } = null;

/// <summary>
/// End position
/// </summary>
public int? EndTime { get; set; }
public int? EndTime { get; set; } = null;

public bool Equals(KarRuby other)
{
return Parent == other.Parent
&& Ruby == other.Ruby
&& TimeTags.SequenceEqual(other.TimeTags)
&& StartTime == other.StartTime
&& EndTime == other.EndTime;
}
}
2 changes: 1 addition & 1 deletion LrcParser/Parser/Lines/SingleLineParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace LrcParser.Parser.Lines;
/// Base component pass string
/// </inheritdoc>
/// <typeparam name="T">Encode and decode object type</typeparam>
public abstract class SingleLineParser<T> : ISingleLineParser where T : class
public abstract class SingleLineParser<T> : ISingleLineParser where T : struct, IEquatable<T>
{
public abstract bool CanDecode(string text);

Expand Down
11 changes: 10 additions & 1 deletion LrcParser/Parser/Lrc/Metadata/LrcLyric.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@

namespace LrcParser.Parser.Lrc.Metadata;

public class LrcLyric
public struct LrcLyric : IEquatable<LrcLyric>
{
public LrcLyric()
{
}

/// <summary>
/// Text
/// </summary>
Expand All @@ -16,4 +20,9 @@ public class LrcLyric
/// Time tags
/// </summary>
public SortedDictionary<TextIndex, int> TimeTags { get; set; } = new();

public bool Equals(LrcLyric other)
{
return Text == other.Text && TimeTags.SequenceEqual(other.TimeTags);
}
}

0 comments on commit 4e7cbf0

Please sign in to comment.