コード例 #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 }));
        }
コード例 #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));
        }
コード例 #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));
 }
コード例 #4
0
 public string TestFormatNumberWithFormatString(object value, string format, string cultureName)
 {
     var attribute = new ValueAttribute()
      {
     CultureName = cultureName,
     FormatString = format
      };
      return attribute.Format(value);
 }
コード例 #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));
        }
コード例 #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));
        }
コード例 #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));
        }
コード例 #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));
        }
コード例 #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));
        }
コード例 #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));
        }
コード例 #11
0
        public string TestFormatNumberWithFormatString(object value, string format, string cultureName)
        {
            var attribute = new ValueAttribute()
            {
                CultureName  = cultureName,
                FormatString = format
            };

            return(attribute.Format(value));
        }
コード例 #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);
 }
コード例 #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));
        }
コード例 #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));
        }
コード例 #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)));
        }
コード例 #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)));
        }
コード例 #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));
        }
コード例 #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));
        }
コード例 #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));
        }
コード例 #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);
        }
コード例 #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)));
            }
        }
コード例 #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));
 }
コード例 #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));
 }
コード例 #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));
 }
コード例 #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)));
 }
コード例 #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));
 }
コード例 #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);
 }
コード例 #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));
 }
コード例 #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));
 }
コード例 #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));
 }
コード例 #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));
 }
コード例 #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)));
 }
コード例 #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)));
      }
 }
コード例 #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));
 }
コード例 #35
0
ファイル: LazyParser.cs プロジェクト: lanicon/TextTables
 public LazyParser(ValueAttribute attribute)
 {
     this.attribute = attribute;
 }
コード例 #36
0
ファイル: ValueAttribute.cs プロジェクト: kos59125/TextTables
 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 });
 }