From 5e18c3f4b8838abc09982d5ca46af0bdf36dd7e9 Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Sun, 6 Sep 2015 18:48:22 +0200 Subject: [PATCH] Move grid length parsing to GridLengths. --- .../GridLengthTests.cs | 93 +++++++++++++++++++ src/Perspex.Controls/ColumnDefinitions.cs | 3 +- src/Perspex.Controls/GridLength.cs | 38 ++++++++ .../Parsers/GridLengthsParser.cs | 52 ----------- src/Perspex.Controls/Perspex.Controls.csproj | 1 - src/Perspex.Controls/RowDefinitions.cs | 3 +- .../Parsers/GridLengthsParserTests.cs | 54 ----------- .../Perspex.Controls.UnitTests.csproj | 2 +- 8 files changed, 134 insertions(+), 112 deletions(-) create mode 100644 Tests/Perspex.Controls.UnitTests/GridLengthTests.cs delete mode 100644 src/Perspex.Controls/Parsers/GridLengthsParser.cs delete mode 100644 tests/Perspex.Controls.UnitTests/Parsers/GridLengthsParserTests.cs diff --git a/Tests/Perspex.Controls.UnitTests/GridLengthTests.cs b/Tests/Perspex.Controls.UnitTests/GridLengthTests.cs new file mode 100644 index 0000000000..f2c81a9e88 --- /dev/null +++ b/Tests/Perspex.Controls.UnitTests/GridLengthTests.cs @@ -0,0 +1,93 @@ +// ----------------------------------------------------------------------- +// +// Copyright 2015 MIT Licence. See licence.md for more information. +// +// ----------------------------------------------------------------------- + +namespace Perspex.Controls.UnitTests +{ + using System; + using System.Linq; + using Xunit; + + public class GridLengthTests + { + [Fact] + public void Parse_Should_Parse_Auto() + { + var result = GridLength.Parse("Auto"); + + Assert.Equal(GridLength.Auto, result); + } + + [Fact] + public void Parse_Should_Parse_Auto_Lowercase() + { + var result = GridLength.Parse("auto"); + + Assert.Equal(GridLength.Auto, result); + } + + [Fact] + public void Parse_Should_Parse_Star() + { + var result = GridLength.Parse("*"); + + Assert.Equal(new GridLength(1, GridUnitType.Star), result); + } + + [Fact] + public void Parse_Should_Parse_Star_Value() + { + var result = GridLength.Parse("2*"); + + Assert.Equal(new GridLength(2, GridUnitType.Star), result); + } + + [Fact] + public void Parse_Should_Parse_Pixel_Value() + { + var result = GridLength.Parse("2"); + + Assert.Equal(new GridLength(2, GridUnitType.Pixel), result); + } + + [Fact] + public void Parse_Should_Throw_FormatException_For_Invalid_String() + { + Assert.Throws(() => GridLength.Parse("2x")); + } + + [Fact] + public void ParseLengths_Accepts_Comma_Separators() + { + var result = GridLength.ParseLengths("*,Auto,2*,4").ToList(); + + Assert.Equal( + new[] + { + new GridLength(1, GridUnitType.Star), + GridLength.Auto, + new GridLength(2, GridUnitType.Star), + new GridLength(4, GridUnitType.Pixel), + }, + result); + } + + [Fact] + public void ParseLengths_Accepts_Space_Separators() + { + var result = GridLength.ParseLengths("* Auto 2* 4").ToList(); + + Assert.Equal( + new[] + { + new GridLength(1, GridUnitType.Star), + GridLength.Auto, + new GridLength(2, GridUnitType.Star), + new GridLength(4, GridUnitType.Pixel), + }, + result); + } + } +} diff --git a/src/Perspex.Controls/ColumnDefinitions.cs b/src/Perspex.Controls/ColumnDefinitions.cs index ce020377eb..05dac937ed 100644 --- a/src/Perspex.Controls/ColumnDefinitions.cs +++ b/src/Perspex.Controls/ColumnDefinitions.cs @@ -8,7 +8,6 @@ namespace Perspex.Controls { using System.Linq; using Perspex.Collections; - using Perspex.Controls.Parsers; /// /// A collection of s. @@ -28,7 +27,7 @@ namespace Perspex.Controls /// A string representation of the column definitions. public ColumnDefinitions(string s) { - this.AddRange(GridLengthsParser.Parse(s).Select(x => new ColumnDefinition(x))); + this.AddRange(GridLength.ParseLengths(s).Select(x => new ColumnDefinition(x))); } } } \ No newline at end of file diff --git a/src/Perspex.Controls/GridLength.cs b/src/Perspex.Controls/GridLength.cs index 1e477b3417..9a7e605923 100644 --- a/src/Perspex.Controls/GridLength.cs +++ b/src/Perspex.Controls/GridLength.cs @@ -7,6 +7,8 @@ namespace Perspex.Controls { using System; + using System.Collections.Generic; + using System.Linq; /// /// Defines the valid units for a . @@ -192,5 +194,41 @@ namespace Perspex.Controls string s = this.value.ToString(); return this.IsStar ? s + "*" : s; } + + /// + /// Parses a string to return a . + /// + /// The string. + /// The . + public static GridLength Parse(string s) + { + s = s.ToUpperInvariant(); + + if (s == "AUTO") + { + return GridLength.Auto; + } + else if (s.EndsWith("*")) + { + var valueString = s.Substring(0, s.Length - 1).Trim(); + var value = valueString.Length > 0 ? double.Parse(valueString) : 1; + return new GridLength(value, GridUnitType.Star); + } + else + { + var value = double.Parse(s); + return new GridLength(value, GridUnitType.Pixel); + } + } + + /// + /// Parses a string to return a collection of s. + /// + /// The string. + /// The . + public static IEnumerable ParseLengths(string s) + { + return s.Split(new[] { ',', ' ' }).Select(x => Parse(x)); + } } } \ No newline at end of file diff --git a/src/Perspex.Controls/Parsers/GridLengthsParser.cs b/src/Perspex.Controls/Parsers/GridLengthsParser.cs deleted file mode 100644 index 64df91d723..0000000000 --- a/src/Perspex.Controls/Parsers/GridLengthsParser.cs +++ /dev/null @@ -1,52 +0,0 @@ -// ----------------------------------------------------------------------- -// -// Copyright 2014 MIT Licence. See licence.md for more information. -// -// ----------------------------------------------------------------------- - -namespace Perspex.Controls.Parsers -{ - using System; - using System.Collections.Generic; - using System.Linq; - - /// - /// Parses a string of s for and - /// . - /// - public static class GridLengthsParser - { - /// - /// Parses a string of s. - /// - /// The string. - /// A collection of s. - public static IEnumerable Parse(string s) - { - var parts = s.Split(',').Select(x => x.ToUpperInvariant().Trim()); - - foreach (var part in parts) - { - if (part == "AUTO") - { - yield return GridLength.Auto; - } - else if (part.EndsWith("*")) - { - var valueString = part.Substring(0, part.Length - 1).Trim(); - var value = valueString.Length > 0 ? double.Parse(valueString) : 1; - yield return new GridLength(value, GridUnitType.Star); - } - else if (part.EndsWith("PX")) - { - var value = double.Parse(part.Substring(0, part.Length - 2)); - yield return new GridLength(value, GridUnitType.Pixel); - } - else - { - throw new FormatException("Invalid grid length: " + part); - } - } - } - } -} diff --git a/src/Perspex.Controls/Perspex.Controls.csproj b/src/Perspex.Controls/Perspex.Controls.csproj index c095c37e8a..209290d807 100644 --- a/src/Perspex.Controls/Perspex.Controls.csproj +++ b/src/Perspex.Controls/Perspex.Controls.csproj @@ -50,7 +50,6 @@ - diff --git a/src/Perspex.Controls/RowDefinitions.cs b/src/Perspex.Controls/RowDefinitions.cs index 6a975a2297..f9a22b5186 100644 --- a/src/Perspex.Controls/RowDefinitions.cs +++ b/src/Perspex.Controls/RowDefinitions.cs @@ -8,7 +8,6 @@ namespace Perspex.Controls { using System.Linq; using Perspex.Collections; - using Perspex.Controls.Parsers; /// /// A collection of s. @@ -28,7 +27,7 @@ namespace Perspex.Controls /// A string representation of the row definitions. public RowDefinitions(string s) { - this.AddRange(GridLengthsParser.Parse(s).Select(x => new RowDefinition(x))); + this.AddRange(GridLength.ParseLengths(s).Select(x => new RowDefinition(x))); } } } \ No newline at end of file diff --git a/tests/Perspex.Controls.UnitTests/Parsers/GridLengthsParserTests.cs b/tests/Perspex.Controls.UnitTests/Parsers/GridLengthsParserTests.cs deleted file mode 100644 index 340923056b..0000000000 --- a/tests/Perspex.Controls.UnitTests/Parsers/GridLengthsParserTests.cs +++ /dev/null @@ -1,54 +0,0 @@ -// ----------------------------------------------------------------------- -// -// Copyright 2015 MIT Licence. See licence.md for more information. -// -// ----------------------------------------------------------------------- - -namespace Perspex.Controls.UnitTests.Parsers -{ - using System; - using System.Linq; - using Perspex.Controls.Parsers; - using Xunit; - - public class GridLengthsParserTests - { - [Fact] - public void Parser_Should_Correctly_Parse_Grid_Lengths() - { - var s = "*,Auto,2*,4px"; - var result = GridLengthsParser.Parse(s); - - Assert.Equal( - new[] - { - new GridLength(1, GridUnitType.Star), - GridLength.Auto, - new GridLength(2, GridUnitType.Star), - new GridLength(4, GridUnitType.Pixel), - }, - result); - } - - [Fact] - public void Parser_Should_Throw_For_Invalid_Star_Value() - { - var s = "*,Auto,x*,4px"; - Assert.Throws(() => GridLengthsParser.Parse(s).ToList()); - } - - [Fact] - public void Parser_Should_Throw_For_Invalid_Unit_Value() - { - var s = "*,Auto,4ab,4px"; - Assert.Throws(() => GridLengthsParser.Parse(s).ToList()); - } - - [Fact] - public void Parser_Should_Throw_For_Empty_Entry() - { - var s = "*,Auto,,4px"; - Assert.Throws(() => GridLengthsParser.Parse(s).ToList()); - } - } -} diff --git a/tests/Perspex.Controls.UnitTests/Perspex.Controls.UnitTests.csproj b/tests/Perspex.Controls.UnitTests/Perspex.Controls.UnitTests.csproj index 0736d59726..610cef7365 100644 --- a/tests/Perspex.Controls.UnitTests/Perspex.Controls.UnitTests.csproj +++ b/tests/Perspex.Controls.UnitTests/Perspex.Controls.UnitTests.csproj @@ -91,11 +91,11 @@ + -