Exemplo n.º 1
0
        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 }));
        }
Exemplo n.º 2
0
        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));
        }
Exemplo n.º 3
0
 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));
 }
Exemplo n.º 4
0
 public string TestFormatNumberWithFormatString(object value, string format, string cultureName)
 {
     var attribute = new ValueAttribute()
      {
     CultureName = cultureName,
     FormatString = format
      };
      return attribute.Format(value);
 }
Exemplo n.º 5
0
        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));
        }
Exemplo n.º 6
0
        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));
        }
Exemplo n.º 7
0
        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));
        }
Exemplo n.º 8
0
        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));
        }
Exemplo n.º 9
0
        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));
        }
Exemplo n.º 10
0
        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));
        }
Exemplo n.º 11
0
        public string TestFormatNumberWithFormatString(object value, string format, string cultureName)
        {
            var attribute = new ValueAttribute()
            {
                CultureName  = cultureName,
                FormatString = format
            };

            return(attribute.Format(value));
        }
Exemplo n.º 12
0
 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);
 }
Exemplo n.º 13
0
        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));
        }
Exemplo n.º 14
0
        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));
        }
Exemplo n.º 15
0
        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)));
        }
Exemplo n.º 16
0
        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)));
        }
Exemplo n.º 17
0
        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));
        }
Exemplo n.º 18
0
        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));
        }
Exemplo n.º 19
0
        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));
        }
Exemplo n.º 20
0
        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);
        }
Exemplo n.º 21
0
        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)));
            }
        }
Exemplo n.º 22
0
 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));
 }
Exemplo n.º 23
0
 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));
 }
Exemplo n.º 24
0
 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));
 }
Exemplo n.º 25
0
 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)));
 }
Exemplo n.º 26
0
 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));
 }
Exemplo n.º 27
0
 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);
 }
Exemplo n.º 28
0
 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));
 }
Exemplo n.º 29
0
 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));
 }
Exemplo n.º 30
0
 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));
 }
Exemplo n.º 31
0
 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));
 }
Exemplo n.º 32
0
 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)));
 }
Exemplo n.º 33
0
 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)));
      }
 }
Exemplo n.º 34
0
 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));
 }
Exemplo n.º 35
0
 public LazyParser(ValueAttribute attribute)
 {
     this.attribute = attribute;
 }
Exemplo n.º 36
0
 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 });
 }