Skip to content

Commit

Permalink
Rewrite time-tag convertor and it's test usnig regular expression.
Browse files Browse the repository at this point in the history
  • Loading branch information
andy840119 committed Dec 12, 2020
1 parent fa4680e commit 46c4a9e
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 101 deletions.
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);
}
}
}

This file was deleted.

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);
}
}
}

This file was deleted.

0 comments on commit 46c4a9e

Please sign in to comment.