-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rewrite time-tag convertor and it's test usnig regular expression.
- Loading branch information
1 parent
fa4680e
commit 46c4a9e
Showing
4 changed files
with
92 additions
and
101 deletions.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
osu.Game.Rulesets.Karaoke.Tests/IO/Serialization/Converters/TimeTagConverterTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Copyright (c) andy840119 <[email protected]>. Licensed under the GPL Licence. | ||
// See the LICENCE file in the repository root for full licence text. | ||
|
||
using Newtonsoft.Json; | ||
using NUnit.Framework; | ||
using osu.Framework.Graphics.Sprites; | ||
using osu.Game.Rulesets.Karaoke.IO.Serialization.Converters; | ||
using osu.Game.Rulesets.Karaoke.Objects; | ||
|
||
namespace osu.Game.Rulesets.Karaoke.Tests.IO.Serialization.Converters | ||
{ | ||
[TestFixture] | ||
public class TimeTagConverterTest : BaseSingleConverterTest<TimeTagConverter> | ||
{ | ||
[TestCase(1, TimeTagIndex.IndexState.Start, 1000, "[1,start]:1000")] | ||
[TestCase(1, TimeTagIndex.IndexState.End, 1000, "[1,end]:1000")] | ||
[TestCase(-1, TimeTagIndex.IndexState.Start, 1000, "[-1,start]:1000")] // Should not check index is out of range in here. | ||
[TestCase(1, TimeTagIndex.IndexState.Start, -1000, "[1,start]:-1000")] // Should not check if time is negative. | ||
[TestCase(1, TimeTagIndex.IndexState.Start, null, "[1,start]:")] | ||
public void TestSerialize(int index, TimeTagIndex.IndexState state, int? time, string json) | ||
{ | ||
var timeTag = new TimeTag(new TimeTagIndex(index, state), time); | ||
|
||
var result = JsonConvert.SerializeObject(timeTag, CreateSettings()); | ||
Assert.AreEqual(result, $"\"{json}\""); | ||
} | ||
|
||
[TestCase("[1,start]:1000", 1, TimeTagIndex.IndexState.Start, 1000)] | ||
[TestCase("[1,end]:1000", 1, TimeTagIndex.IndexState.End, 1000)] | ||
[TestCase("[-1,start]:1000", -1, TimeTagIndex.IndexState.Start, 1000)] // Should not check index is out of range in here. | ||
[TestCase("[1,start]:-1000", 1, TimeTagIndex.IndexState.Start, -1000)] // Should not check if time is negative. | ||
[TestCase("[1,start]:", 1, TimeTagIndex.IndexState.Start, null)] | ||
[TestCase("", TimeTagIndex.IndexState.Start, 0, null)] // Test deal with format is not right below. | ||
[TestCase("[1,???]:", 0, TimeTagIndex.IndexState.Start, null)] | ||
[TestCase("[1,]", 0, TimeTagIndex.IndexState.Start, null)] | ||
[TestCase("[,start]", 0, TimeTagIndex.IndexState.Start, null)] | ||
[TestCase("[]", 0, TimeTagIndex.IndexState.Start, null)] | ||
public void TestDeserialize(string json, int index, TimeTagIndex.IndexState state, int? time) | ||
{ | ||
var result = JsonConvert.DeserializeObject<TimeTag>($"\"{json}\"", CreateSettings()); | ||
var actual = new TimeTag(new TimeTagIndex(index, state), time); | ||
Assert.AreEqual(result.Index, actual.Index); | ||
Assert.AreEqual(result.Time, actual.Time); | ||
} | ||
} | ||
} |
45 changes: 0 additions & 45 deletions
45
osu.Game.Rulesets.Karaoke.Tests/IO/Serialization/Converters/TimeTagsConverterTest.cs
This file was deleted.
Oops, something went wrong.
46 changes: 46 additions & 0 deletions
46
osu.Game.Rulesets.Karaoke/IO/Serialization/Converters/TimeTagConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Copyright (c) andy840119 <[email protected]>. Licensed under the GPL Licence. | ||
// See the LICENCE file in the repository root for full licence text. | ||
|
||
using System; | ||
using System.Text.RegularExpressions; | ||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Linq; | ||
using osu.Framework.Graphics.Sprites; | ||
using osu.Game.Rulesets.Karaoke.Objects; | ||
|
||
namespace osu.Game.Rulesets.Karaoke.IO.Serialization.Converters | ||
{ | ||
public class TimeTagConverter : JsonConverter<TimeTag> | ||
{ | ||
public override TimeTag ReadJson(JsonReader reader, Type objectType, TimeTag existingValue, bool hasExistingValue, JsonSerializer serializer) | ||
{ | ||
var obj = JToken.Load(reader); | ||
var value = obj.Value<string>(); | ||
|
||
if (value == null || value == "") | ||
return new TimeTag(new TimeTagIndex()); | ||
|
||
var regux = new Regex("(?<index>[-0-9]+),(?<state>start|end)]:(?<time>[-0-9]+|s*|)"); | ||
var result = regux.Match(value); | ||
if (!result.Success) | ||
return new TimeTag(new TimeTagIndex()); | ||
|
||
var index = int.Parse(result.Groups["index"]?.Value); | ||
var state = result.Groups["state"]?.Value == "start" ? TimeTagIndex.IndexState.Start : TimeTagIndex.IndexState.End; | ||
var timeStr = result.Groups["time"]?.Value; | ||
var time = timeStr == "" ? default(int?) : int.Parse(timeStr); | ||
|
||
return new TimeTag(new TimeTagIndex(index, state), time); | ||
} | ||
|
||
public override void WriteJson(JsonWriter writer, TimeTag value, JsonSerializer serializer) | ||
{ | ||
var tag = value.Index; | ||
var state = tag.State == TimeTagIndex.IndexState.Start ? "start" : "end"; | ||
var time = value.Time; | ||
|
||
var str = $"[{tag.Index},{state}]:{time}"; | ||
writer.WriteValue(str); | ||
} | ||
} | ||
} |
56 changes: 0 additions & 56 deletions
56
osu.Game.Rulesets.Karaoke/IO/Serialization/Converters/TimeTagsConverter.cs
This file was deleted.
Oops, something went wrong.