private static Parse GenerateParseLazy(Type lazyParserType, ValueAttribute attribute) { var parse = typeof(IParser).GetMethod("Parse", new[] { typeof(string), typeof(IFormatProvider) }); var instance = Activator.CreateInstance(lazyParserType, attribute); return((value, provider) => parse.Invoke(instance, new object[] { value, provider })); }
public void TestParseWithImplicitParser() { var expected = new TestClass(1); var attribute = new ValueAttribute(); var actual = attribute.Parse("1", typeof(TestClass)); Assert.That(actual, Is.EqualTo(expected)); }
public void TestParseBoolean() { bool expected = true; var expectedType = typeof(Boolean); var attribute = new ValueAttribute(); var actual = attribute.Parse("True", expectedType); Assert.That(actual.GetType(), Is.EqualTo(expectedType)); Assert.That(actual, Is.EqualTo(expected)); }
public string TestFormatNumberWithFormatString(object value, string format, string cultureName) { var attribute = new ValueAttribute() { CultureName = cultureName, FormatString = format }; return attribute.Format(value); }
public void TestParseUInt64() { ulong expected = 1; var expectedType = typeof(UInt64); var attribute = new ValueAttribute(); var actual = attribute.Parse("1", expectedType); Assert.That(actual.GetType(), Is.EqualTo(expectedType)); Assert.That(actual, Is.EqualTo(expected)); }
public void TestParseInt32() { int expected = 1; var expectedType = typeof(Int32); var attribute = new ValueAttribute(); var actual = attribute.Parse("1", expectedType); Assert.That(actual.GetType(), Is.EqualTo(expectedType)); Assert.That(actual, Is.EqualTo(expected)); }
public void TestParseString() { var expected = "Foo"; var expectedType = typeof(String); var attribute = new ValueAttribute(); var actual = attribute.Parse("Foo", expectedType); Assert.That(actual.GetType(), Is.EqualTo(expectedType)); Assert.That(actual, Is.EqualTo(expected)); }
public void TestParseChar() { char expected = 'K'; var expectedType = typeof(Char); var attribute = new ValueAttribute(); var actual = attribute.Parse("K", expectedType); Assert.That(actual.GetType(), Is.EqualTo(expectedType)); Assert.That(actual, Is.EqualTo(expected)); }
public void TestParseDouble() { double expected = 1.5; var expectedType = typeof(Double); var attribute = new ValueAttribute(); var actual = attribute.Parse("1.5", expectedType); Assert.That(actual.GetType(), Is.EqualTo(expectedType)); Assert.That(actual, Is.EqualTo(expected)); }
public void TestParseEnum() { var expected = TestEnum.TestValue; var expectedType = typeof(TestEnum); var attribute = new ValueAttribute(); var actual = attribute.Parse("testvalue", expectedType); Assert.That(actual.GetType(), Is.EqualTo(expectedType)); Assert.That(actual, Is.EqualTo(expected)); }
public string TestFormatNumberWithFormatString(object value, string format, string cultureName) { var attribute = new ValueAttribute() { CultureName = cultureName, FormatString = format }; return(attribute.Format(value)); }
public string TestFormatDateTimeWithFormatString(string format, string cultureName) { var datetime = new DateTime(2000, 1, 23, 4, 56, 7, 890); var attribute = new ValueAttribute() { CultureName = cultureName, FormatString = format }; return attribute.Format(datetime); }
public string TestFormatDateTimeWithFormatString(string format, string cultureName) { var datetime = new DateTime(2000, 1, 23, 4, 56, 7, 890); var attribute = new ValueAttribute() { CultureName = cultureName, FormatString = format }; return(attribute.Format(datetime)); }
public void TestParseWithNumberStyle() { var attribute = new ValueAttribute() { NumberStyle = NumberStyles.Integer }; var type = typeof(Double); Assert.That(attribute.Parse("1", type), Is.EqualTo(Double.Parse("1"))); Assert.That(attribute.Parse("1.0", type), Throws.TypeOf(typeof(FormatException))); }
public void TestParseExactDateTimeFailure(string format) { var expected = new DateTime(2000, 1, 23, 4, 56, 7, 890); var expectedType = typeof(DateTime); var attribute = new ValueAttribute() { FormatString = format }; Assert.That(attribute.Parse("2000-01-23 04:56:07.890", expectedType), Throws.TypeOf(typeof(FormatException))); }
public void TestParseWithDateTimeStyle() { var expected = DateTime.Parse("2000-01-01 00:00"); var attribute = new ValueAttribute() { CultureName = "ja-JP", // JST is 9 hours ahead of UTC DateTimeStyle = DateTimeStyles.AdjustToUniversal | DateTimeStyles.AssumeLocal }; var actual = attribute.Parse("2000-01-01 09:00", typeof(DateTime)); Assert.That(actual, Is.EqualTo(expected)); }
public void TestParseExactGuidSuccess(string format) { var expected = Guid.Empty; var expectedType = typeof(Guid); var attribute = new ValueAttribute() { FormatString = format }; var actual = attribute.Parse("00000000-0000-0000-0000-000000000000", expectedType); Assert.That(actual.GetType(), Is.EqualTo(expectedType)); Assert.That(actual, Is.EqualTo(expected)); }
public void TestParseExactDateTimeSuccess(string format) { var expected = new DateTime(2000, 1, 23, 4, 56, 7, 890); var expectedType = typeof(DateTime); var attribute = new ValueAttribute() { FormatString = format }; var actual = attribute.Parse("2000-01-23 04:56:07.890", expectedType); Assert.That(actual.GetType(), Is.EqualTo(expectedType)); Assert.That(actual, Is.EqualTo(expected)); }
public void TestParseLazy() { var expected = 1; var expectedType = typeof(Lazy <Int32>); var attribute = new ValueAttribute(); var actual = attribute.Parse("1", expectedType); Assert.That(actual.GetType(), Is.EqualTo(expectedType)); var lazy = (Lazy <Int32>)actual; Assert.That(lazy.IsValueCreated, Is.False); Assert.That(lazy.Value, Is.EqualTo(expected)); Assert.That(lazy.IsValueCreated, Is.True); }
public void TestParseExactGuidFailure(string format) { var attribute = new ValueAttribute() { FormatString = format }; try { // Calls ParseExact method via reflection. // The error is wraped by TargetInvocationException attribute.Parse(Guid.Empty.ToString("D"), typeof(Guid)); } catch (TargetInvocationException ex) { Assert.That(ex.InnerException.GetType(), Is.EqualTo(typeof(FormatException))); } }
public void TestParseByte() { byte expected = 1; var expectedType = typeof(Byte); var attribute = new ValueAttribute(); var actual = attribute.Parse("1", expectedType); Assert.That(actual.GetType(), Is.EqualTo(expectedType)); Assert.That(actual, Is.EqualTo(expected)); }
public void TestParseSingle() { float expected = 1.5f; var expectedType = typeof(Single); var attribute = new ValueAttribute(); var actual = attribute.Parse("1.5", expectedType); Assert.That(actual.GetType(), Is.EqualTo(expectedType)); Assert.That(actual, Is.EqualTo(expected)); }
public void TestParseLazy() { var expected = 1; var expectedType = typeof(Lazy<Int32>); var attribute = new ValueAttribute(); var actual = attribute.Parse("1", expectedType); Assert.That(actual.GetType(), Is.EqualTo(expectedType)); var lazy = (Lazy<Int32>)actual; Assert.That(lazy.IsValueCreated, Is.False); Assert.That(lazy.Value, Is.EqualTo(expected)); Assert.That(lazy.IsValueCreated, Is.True); }
public void TestParseExactGuidFailure(string format) { var attribute = new ValueAttribute() { FormatString = format }; try { // Calls ParseExact method via reflection. // The error is wraped by TargetInvocationException attribute.Parse(Guid.Empty.ToString("D"), typeof(Guid)); } catch (TargetInvocationException ex) { Assert.That(ex.InnerException.GetType (), Is.EqualTo(typeof(FormatException))); } }
public LazyParser(ValueAttribute attribute) { this.attribute = attribute; }
private static Parse GenerateParseLazy(Type lazyParserType, ValueAttribute attribute) { var parse = typeof(IParser).GetMethod("Parse", new[] { typeof(string), typeof(IFormatProvider) }); var instance = Activator.CreateInstance(lazyParserType, attribute); return (value, provider) => parse.Invoke(instance, new object[] { value, provider }); }