Example #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 }));
        }
Example #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));
        }
 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);
 }
Example #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));
        }
Example #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));
        }
Example #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));
        }
Example #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));
        }
Example #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));
        }
Example #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));
        }
Example #11
0
        public string TestFormatNumberWithFormatString(object value, string format, string cultureName)
        {
            var attribute = new ValueAttribute()
            {
                CultureName  = cultureName,
                FormatString = format
            };

            return(attribute.Format(value));
        }
Example #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);
 }
Example #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));
        }
Example #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));
        }
Example #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)));
        }
Example #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)));
        }
Example #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));
        }
Example #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));
        }
Example #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));
        }
Example #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);
        }
Example #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)));
            }
        }
Example #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));
 }
Example #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));
 }
Example #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));
 }
Example #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)));
 }
Example #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));
 }
Example #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);
 }
Example #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));
 }
Example #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));
 }
Example #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));
 }
Example #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));
 }
Example #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)));
 }
Example #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)));
      }
 }
Example #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));
 }
Example #35
0
 public LazyParser(ValueAttribute attribute)
 {
     this.attribute = attribute;
 }
Example #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 });
 }