8 changed files with 134 additions and 112 deletions
@ -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); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -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,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()); |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
Loading…
Reference in new issue