7 changed files with 130 additions and 5 deletions
@ -0,0 +1,53 @@ |
|||||
|
// -----------------------------------------------------------------------
|
||||
|
// <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); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,54 @@ |
|||||
|
// -----------------------------------------------------------------------
|
||||
|
// <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()); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue