From f1c9073338e32630eca13774e8b9b0143c6feaa9 Mon Sep 17 00:00:00 2001 From: smoogipoo Date: Tue, 14 May 2019 16:16:55 +0900 Subject: [PATCH] Fix commented line check not working with whitespace --- .../Beatmaps/Formats/LegacyDecoderTest.cs | 56 +++++++++++++++++++ osu.Game.Tests/Resources/comments.osu | 9 +++ osu.Game/Beatmaps/Formats/LegacyDecoder.cs | 2 +- 3 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 osu.Game.Tests/Beatmaps/Formats/LegacyDecoderTest.cs create mode 100644 osu.Game.Tests/Resources/comments.osu diff --git a/osu.Game.Tests/Beatmaps/Formats/LegacyDecoderTest.cs b/osu.Game.Tests/Beatmaps/Formats/LegacyDecoderTest.cs new file mode 100644 index 0000000000..b4d219456c --- /dev/null +++ b/osu.Game.Tests/Beatmaps/Formats/LegacyDecoderTest.cs @@ -0,0 +1,56 @@ +// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. +// See the LICENCE file in the repository root for full licence text. + +using System.Collections.Generic; +using System.IO; +using NUnit.Framework; +using osu.Game.Beatmaps.Formats; +using osu.Game.Tests.Resources; + +namespace osu.Game.Tests.Beatmaps.Formats +{ + [TestFixture] + public class LegacyDecoderTest + { + [Test] + public void TestDecodeComments() + { + var decoder = new LineLoggingDecoder(14); + + using (var resStream = TestResources.OpenResource("comments.osu")) + using (var stream = new StreamReader(resStream)) + { + decoder.Decode(stream); + + Assert.That(decoder.ParsedLines, Has.None.EqualTo("// Combo1: 0, 0, 0")); + Assert.That(decoder.ParsedLines, Has.None.EqualTo("//Combo2: 0, 0, 0")); + Assert.That(decoder.ParsedLines, Has.None.EqualTo(" // Combo3: 0, 0, 0")); + Assert.That(decoder.ParsedLines, Has.One.EqualTo("Combo1: 100, 100, 100 // Comment at end of line")); + } + } + + private class LineLoggingDecoder : LegacyDecoder + { + public readonly List ParsedLines = new List(); + + public LineLoggingDecoder(int version) + : base(version) + { + } + + protected override bool ShouldSkipLine(string line) + { + var result = base.ShouldSkipLine(line); + + if (!result) + ParsedLines.Add(line); + + return result; + } + } + + private class TestModel + { + } + } +} diff --git a/osu.Game.Tests/Resources/comments.osu b/osu.Game.Tests/Resources/comments.osu new file mode 100644 index 0000000000..78b48dd804 --- /dev/null +++ b/osu.Game.Tests/Resources/comments.osu @@ -0,0 +1,9 @@ +osu file format v14 + +[Colours] + +// Combo1: 0, 0, 0 +//Combo2: 0, 0, 0 + // Combo3: 0, 0, 0 + +Combo1: 100, 100, 100 // Comment at end of line \ No newline at end of file diff --git a/osu.Game/Beatmaps/Formats/LegacyDecoder.cs b/osu.Game/Beatmaps/Formats/LegacyDecoder.cs index ad5089958c..eb5bcfe824 100644 --- a/osu.Game/Beatmaps/Formats/LegacyDecoder.cs +++ b/osu.Game/Beatmaps/Formats/LegacyDecoder.cs @@ -54,7 +54,7 @@ namespace osu.Game.Beatmaps.Formats } } - protected virtual bool ShouldSkipLine(string line) => string.IsNullOrWhiteSpace(line) || line.StartsWith("//", StringComparison.Ordinal); + protected virtual bool ShouldSkipLine(string line) => string.IsNullOrWhiteSpace(line) || line.AsSpan().TrimStart().StartsWith("//".AsSpan(), StringComparison.Ordinal); protected virtual void ParseLine(T output, Section section, string line) {