Browse Source

Move grid length parsing to GridLengths.

pull/114/head
Steven Kirk 11 years ago
parent
commit
5e18c3f4b8
  1. 93
      Tests/Perspex.Controls.UnitTests/GridLengthTests.cs
  2. 3
      src/Perspex.Controls/ColumnDefinitions.cs
  3. 38
      src/Perspex.Controls/GridLength.cs
  4. 52
      src/Perspex.Controls/Parsers/GridLengthsParser.cs
  5. 1
      src/Perspex.Controls/Perspex.Controls.csproj
  6. 3
      src/Perspex.Controls/RowDefinitions.cs
  7. 54
      tests/Perspex.Controls.UnitTests/Parsers/GridLengthsParserTests.cs
  8. 2
      tests/Perspex.Controls.UnitTests/Perspex.Controls.UnitTests.csproj

93
Tests/Perspex.Controls.UnitTests/GridLengthTests.cs

@ -0,0 +1,93 @@
// -----------------------------------------------------------------------
// <copyright file="GridLengthTests.cs" company="Steven Kirk">
// Copyright 2015 MIT Licence. See licence.md for more information.
// </copyright>
// -----------------------------------------------------------------------
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<FormatException>(() => 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);
}
}
}

3
src/Perspex.Controls/ColumnDefinitions.cs

@ -8,7 +8,6 @@ namespace Perspex.Controls
{
using System.Linq;
using Perspex.Collections;
using Perspex.Controls.Parsers;
/// <summary>
/// A collection of <see cref="ColumnDefinition"/>s.
@ -28,7 +27,7 @@ namespace Perspex.Controls
/// <param name="s">A string representation of the column definitions.</param>
public ColumnDefinitions(string s)
{
this.AddRange(GridLengthsParser.Parse(s).Select(x => new ColumnDefinition(x)));
this.AddRange(GridLength.ParseLengths(s).Select(x => new ColumnDefinition(x)));
}
}
}

38
src/Perspex.Controls/GridLength.cs

@ -7,6 +7,8 @@
namespace Perspex.Controls
{
using System;
using System.Collections.Generic;
using System.Linq;
/// <summary>
/// Defines the valid units for a <see cref="GridLength"/>.
@ -192,5 +194,41 @@ namespace Perspex.Controls
string s = this.value.ToString();
return this.IsStar ? s + "*" : s;
}
/// <summary>
/// Parses a string to return a <see cref="GridLength"/>.
/// </summary>
/// <param name="s">The string.</param>
/// <returns>The <see cref="GridLength"/>.</returns>
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);
}
}
/// <summary>
/// Parses a string to return a collection of <see cref="GridLength"/>s.
/// </summary>
/// <param name="s">The string.</param>
/// <returns>The <see cref="GridLength"/>.</returns>
public static IEnumerable<GridLength> ParseLengths(string s)
{
return s.Split(new[] { ',', ' ' }).Select(x => Parse(x));
}
}
}

52
src/Perspex.Controls/Parsers/GridLengthsParser.cs

@ -1,52 +0,0 @@
// -----------------------------------------------------------------------
// <copyright file="GridLengthsParser.cs" company="Steven Kirk">
// Copyright 2014 MIT Licence. See licence.md for more information.
// </copyright>
// -----------------------------------------------------------------------
namespace Perspex.Controls.Parsers
{
using System;
using System.Collections.Generic;
using System.Linq;
/// <summary>
/// Parses a string of <see cref="GridLength"/>s for <see cref="ColumnDefinitions"/> and
/// <see cref="RowDefinitions"/>.
/// </summary>
public static class GridLengthsParser
{
/// <summary>
/// Parses a string of <see cref="GridLength"/>s.
/// </summary>
/// <param name="s">The string.</param>
/// <returns>A collection of <see cref="GridLength"/>s.</returns>
public static IEnumerable<GridLength> 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);
}
}
}
}
}

1
src/Perspex.Controls/Perspex.Controls.csproj

@ -50,7 +50,6 @@
<Compile Include="ISetLogicalParent.cs" />
<Compile Include="MenuItemAccessKeyHandler.cs" />
<Compile Include="Mixins\SelectableMixin.cs" />
<Compile Include="Parsers\GridLengthsParser.cs" />
<Compile Include="Presenters\IContentPresenter.cs" />
<Compile Include="Primitives\AccessText.cs" />
<Compile Include="Border.cs" />

3
src/Perspex.Controls/RowDefinitions.cs

@ -8,7 +8,6 @@ namespace Perspex.Controls
{
using System.Linq;
using Perspex.Collections;
using Perspex.Controls.Parsers;
/// <summary>
/// A collection of <see cref="RowDefinition"/>s.
@ -28,7 +27,7 @@ namespace Perspex.Controls
/// <param name="s">A string representation of the row definitions.</param>
public RowDefinitions(string s)
{
this.AddRange(GridLengthsParser.Parse(s).Select(x => new RowDefinition(x)));
this.AddRange(GridLength.ParseLengths(s).Select(x => new RowDefinition(x)));
}
}
}

54
tests/Perspex.Controls.UnitTests/Parsers/GridLengthsParserTests.cs

@ -1,54 +0,0 @@
// -----------------------------------------------------------------------
// <copyright file="GridLengthsParserTests.cs" company="Steven Kirk">
// Copyright 2015 MIT Licence. See licence.md for more information.
// </copyright>
// -----------------------------------------------------------------------
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<FormatException>(() => GridLengthsParser.Parse(s).ToList());
}
[Fact]
public void Parser_Should_Throw_For_Invalid_Unit_Value()
{
var s = "*,Auto,4ab,4px";
Assert.Throws<FormatException>(() => GridLengthsParser.Parse(s).ToList());
}
[Fact]
public void Parser_Should_Throw_For_Empty_Entry()
{
var s = "*,Auto,,4px";
Assert.Throws<FormatException>(() => GridLengthsParser.Parse(s).ToList());
}
}
}

2
tests/Perspex.Controls.UnitTests/Perspex.Controls.UnitTests.csproj

@ -91,11 +91,11 @@
<Otherwise />
</Choose>
<ItemGroup>
<Compile Include="GridLengthTests.cs" />
<Compile Include="ContentPresenterTests.cs" />
<Compile Include="BorderTests.cs" />
<Compile Include="GlobalSuppressions.cs" />
<Compile Include="Mixins\SelectableMixinTests.cs" />
<Compile Include="Parsers\GridLengthsParserTests.cs" />
<Compile Include="Primitives\TrackTests.cs" />
<Compile Include="Primitives\PopupTests.cs" />
<Compile Include="DropDownTests.cs" />

Loading…
Cancel
Save