public void SerializeUTC()
        {
            DateTimeTestClass c = new DateTimeTestClass();

            c.DateTimeField       = new DateTime(2008, 12, 12, 12, 12, 12, 0, DateTimeKind.Utc).ToLocalTime();
            c.DateTimeOffsetField = new DateTime(2008, 12, 12, 12, 12, 12, 0, DateTimeKind.Utc).ToLocalTime();
            c.PreField            = "Pre";
            c.PostField           = "Post";
            string json = JsonConvert.SerializeObject(c, new IsoDateTimeConverter()
            {
                DateTimeStyles = DateTimeStyles.AssumeUniversal
            });

            Assert.AreEqual(@"{""PreField"":""Pre"",""DateTimeField"":""2008-12-12T12:12:12Z"",""DateTimeOffsetField"":""2008-12-12T12:12:12+00:00"",""PostField"":""Post""}", json);

            //test the other edge case too
            c.DateTimeField       = new DateTime(2008, 1, 1, 1, 1, 1, 0, DateTimeKind.Utc).ToLocalTime();
            c.DateTimeOffsetField = new DateTime(2008, 1, 1, 1, 1, 1, 0, DateTimeKind.Utc).ToLocalTime();
            c.PreField            = "Pre";
            c.PostField           = "Post";
            json = JsonConvert.SerializeObject(c, new IsoDateTimeConverter()
            {
                DateTimeStyles = DateTimeStyles.AssumeUniversal
            });
            Assert.AreEqual(@"{""PreField"":""Pre"",""DateTimeField"":""2008-01-01T01:01:01Z"",""DateTimeOffsetField"":""2008-01-01T01:01:01+00:00"",""PostField"":""Post""}", json);
        }
Ejemplo n.º 2
0
        public void ToHttpQueryString_should_return_datetime_in_given_format()

        {
            DateTime time         = new DateTime(2022, 1, 22, 10, 10, 10);
            string   timeInFormat = HttpUtility.UrlEncode("2022/01/22");
            string   format       = "yyyy/MM/dd";

            DateTimeTestClass testClass = new DateTimeTestClass()
            {
                NullableDateTime       = time,
                DateTime               = time,
                DateTimeOffset         = new DateTimeOffset(time),
                NullableDateTimeOffset = new DateTimeOffset(time),
                NullDateTime           = null,
                NullDateTimeOffset     = null
            };

            string query = testClass.ToQueryString(new HttpQueryStringOption()
            {
                DateTimeFormat = format
            });


            query.Should().Be(
                $"{nameof(DateTimeTestClass.DateTime)}={timeInFormat}&" +
                $"{nameof(DateTimeTestClass.NullableDateTime)}={timeInFormat}&" +
                $"{nameof(DateTimeTestClass.DateTimeOffset)}={timeInFormat}&" +
                $"{nameof(DateTimeTestClass.NullableDateTimeOffset)}={timeInFormat}"
                );
        }
        public void SerializeShouldChangeNonUTCDates()
        {
            DateTime localDateTime = new DateTime(2008, 1, 1, 1, 1, 1, 0, DateTimeKind.Local);

            DateTimeTestClass c = new DateTimeTestClass();

            c.DateTimeField = localDateTime;
            c.PreField      = "Pre";
            c.PostField     = "Post";
            string json = JsonConvert.SerializeObject(c, new IsoDateTimeConverter()
            {
                DateTimeStyles = DateTimeStyles.AssumeUniversal
            });                                                                                                                     //note that this fails without the Utc converter...

            c.DateTimeField = new DateTime(2008, 1, 1, 1, 1, 1, 0, DateTimeKind.Utc);
            string json2 = JsonConvert.SerializeObject(c, new IsoDateTimeConverter()
            {
                DateTimeStyles = DateTimeStyles.AssumeUniversal
            });

            TimeSpan offset = localDateTime.GetUtcOffset();

            // if the current timezone is utc then local already equals utc
            if (offset == TimeSpan.Zero)
            {
                Assert.AreEqual(json, json2);
            }
            else
            {
                Assert.AreNotEqual(json, json2);
            }
        }
Ejemplo n.º 4
0
        public void Should_Use_Custom_Boundaries_for_DateTime_Value_Injection()
        {
            //Create an instance of our test class
            var testInstance = new DateTimeTestClass();

            var dateTimeMax = DateTime.UtcNow.AddYears(1);
            var dateTimeMin = DateTime.UtcNow.AddYears(-1);

            //Create an instance of our IntSelector and set some custom bounds
            var selector =
                new DateTimeSelector().SetMax(() => Faker.Generators.DateTimes.GetDateTime(dateTimeMax, dateTimeMax))
                                 .SetMin(() => Faker.Generators.DateTimes.GetDateTime(dateTimeMin, dateTimeMin));

            //Iterate over the test object's properties
            foreach (var property in testInstance.GetType().GetProperties())
            {
                Assert.IsTrue(selector.CanBind(property),
                              string.Format("should have been able to bind to property {0}", property.Name));

                //Inject the value into this property on our test instance class
                selector.Generate(testInstance, property);

                //Get the value out of the property
                var fieldValue = (DateTime)property.GetValue(testInstance, null);
                Assert.IsNotNull(fieldValue);
                Assert.AreNotEqual(fieldValue, default(DateTime));
                Assert.IsTrue(fieldValue <= dateTimeMax && fieldValue >= dateTimeMin, "Custom range should have worked");
            }
        }
Ejemplo n.º 5
0
        public void Should_Use_Custom_Boundaries_for_DateTime_Value_Injection()
        {
            //Create an instance of our test class
            var testInstance = new DateTimeTestClass();

            var dateTimeMax = DateTime.UtcNow.AddYears(1);
            var dateTimeMin = DateTime.UtcNow.AddYears(-1);

            //Create an instance of our IntSelector and set some custom bounds
            var selector =
                new DateTimeSelector().SetMax(() => Faker.Generators.DateTimes.GetDateTime(dateTimeMax, dateTimeMax))
                .SetMin(() => Faker.Generators.DateTimes.GetDateTime(dateTimeMin, dateTimeMin));

            //Iterate over the test object's properties
            foreach (var property in testInstance.GetType().GetProperties())
            {
                Assert.IsTrue(selector.CanBind(property),
                              string.Format("should have been able to bind to property {0}", property.Name));

                //Inject the value into this property on our test instance class
                selector.Generate(testInstance, property);

                //Get the value out of the property
                var fieldValue = (DateTime)property.GetValue(testInstance, null);
                Assert.IsNotNull(fieldValue);
                Assert.AreNotEqual(fieldValue, default(DateTime));
                Assert.IsTrue(fieldValue <= dateTimeMax && fieldValue >= dateTimeMin, "Custom range should have worked");
            }
        }
 public void DeserializeNullToNonNullable()
 {
     ExceptionAssert.Throws <Exception>(() =>
     {
         DateTimeTestClass c2 =
             JsonConvert.DeserializeObject <DateTimeTestClass>(@"{""PreField"":""Pre"",""DateTimeField"":null,""DateTimeOffsetField"":null,""PostField"":""Post""}", new JavaScriptDateTimeConverter());
     }, "Cannot convert null value to System.DateTime. Path 'DateTimeField', line 1, position 38.");
 }
        public void DeserializeEmptyStringIntoDateTimeWithEmptyStringDefaultValue()
        {
            DateTimeTestClass c = JsonConvert.DeserializeObject <DateTimeTestClass>(@"{DateTimeField:""""}", new JsonSerializerSettings {
                NullValueHandling = NullValueHandling.Ignore
            });

            Assert.AreEqual(c.DateTimeField, default(DateTime));
        }
Ejemplo n.º 8
0
        public void DeserializeNullIntoDateTime()
        {
            DateTimeTestClass c = JsonConvertX.DeserializeObject <DateTimeTestClass>(@"{DateTimeField:null}", new JsonSerializerSettings {
                NullValueHandling = NullValueHandling.Ignore
            });

            Assert.AreEqual(c.DateTimeField, default(DateTime));
        }
Ejemplo n.º 9
0
 public void DeserializeNullToNonNullable()
 {
     DateTimeTestClass c2 =
         JsonConvert.DeserializeObject <DateTimeTestClass>(@"{""PreField"":""Pre"",""DateTimeField"":null,""DateTimeOffsetField"":null,""PostField"":""Post""}", new IsoDateTimeConverter()
     {
         DateTimeStyles = DateTimeStyles.AssumeUniversal
     });
 }
        public void DeSerialize()
        {
            DateTimeTestClass c =
                JavaScriptConvert.DeserializeObject <DateTimeTestClass>(@"{""PreField"":""Pre"",""DateTimeField"":""@1229083932012@"",""PostField"":""Post""}", new AspNetAjaxDateTimeConverter());

            Assert.AreEqual(new DateTime(2008, 12, 12, 12, 12, 12, 12), c.DateTimeField);
            Assert.AreEqual("Pre", c.PreField);
            Assert.AreEqual("Post", c.PostField);
        }
Ejemplo n.º 11
0
        public void DateTimeKindAttribute_Apply_ParameterIsNotNull_DateTimeIsNull_DoesNothing()
        {
            DateTimeKindAttribute converter = new DateTimeKindAttribute(DateTimeKind.Utc);

            Assert.True(converter.Kind == DateTimeKind.Utc);

            DateTime          dt = new DateTime(2018, 1, 1, 12, 30, 00, DateTimeKind.Local);
            DateTimeTestClass dateTimeTestClass = new DateTimeTestClass(dt);

            DateTimeKindAttribute.Apply(dateTimeTestClass);
        }
 public void DeserializeNullToNonNullable()
 {
     ExceptionAssert.Throws <JsonSerializationException>(() =>
     {
         DateTimeTestClass c2 =
             JsonConvert.DeserializeObject <DateTimeTestClass>(@"{""PreField"":""Pre"",""DateTimeField"":null,""DateTimeOffsetField"":null,""PostField"":""Post""}", new IsoDateTimeConverter()
         {
             DateTimeStyles = DateTimeStyles.AssumeUniversal
         });
     }, "Cannot convert null value to System.DateTime. Path 'DateTimeField', line 1, position 38.");
 }
        public void Serialize()
        {
            DateTimeTestClass c = new DateTimeTestClass();

            c.DateTimeField = new DateTime(2008, 12, 12, 12, 12, 12, 12);
            c.PreField      = "Pre";
            c.PostField     = "Post";

            string json = JavaScriptConvert.SerializeObject(c, new AspNetAjaxDateTimeConverter());

            Assert.AreEqual(@"{""PreField"":""Pre"",""DateTimeField"":""@1229083932012@"",""PostField"":""Post""}", json);
        }
Ejemplo n.º 14
0
    public void SerializeUTC()
    {
      DateTimeTestClass c = new DateTimeTestClass();
      c.DateTimeField = new DateTime(2008, 12, 12, 12, 12, 12, 0, DateTimeKind.Utc).ToLocalTime();
      c.PreField = "Pre";
      c.PostField = "Post";
      string json = JavaScriptConvert.SerializeObject(c, new IsoDateTimeConverter() { DateTimeStyles = DateTimeStyles.AssumeUniversal });
      Assert.AreEqual(@"{""PreField"":""Pre"",""DateTimeField"":""2008-12-12T12:12:12.0000000Z"",""PostField"":""Post""}", json);

      //test the other edge case too
      c.DateTimeField = new DateTime(2008, 1, 1, 1, 1, 1, 0, DateTimeKind.Utc).ToLocalTime();
      c.PreField = "Pre";
      c.PostField = "Post";
      json = JavaScriptConvert.SerializeObject(c, new IsoDateTimeConverter() { DateTimeStyles = DateTimeStyles.AssumeUniversal });
      Assert.AreEqual(@"{""PreField"":""Pre"",""DateTimeField"":""2008-01-01T01:01:01.0000000Z"",""PostField"":""Post""}", json);
    }
Ejemplo n.º 15
0
        public void SerializeShouldChangeNonUTCDates()
        {
            DateTimeTestClass c = new DateTimeTestClass();

            c.DateTimeField = new DateTime(2008, 1, 1, 1, 1, 1, 0, DateTimeKind.Local);
            c.PreField      = "Pre";
            c.PostField     = "Post";
            string json = JsonConvert.SerializeObject(c, new IsoDateTimeConverter()
            {
                DateTimeStyles = DateTimeStyles.AssumeUniversal
            });                                                                                                                     //note that this fails without the Utc converter...

            c.DateTimeField = new DateTime(2008, 1, 1, 1, 1, 1, 0, DateTimeKind.Utc);
            string json2 = JsonConvert.SerializeObject(c, new IsoDateTimeConverter()
            {
                DateTimeStyles = DateTimeStyles.AssumeUniversal
            });

            Assert.AreNotEqual(json, json2);
        }
Ejemplo n.º 16
0
        public void DateTime_Selector_Injects_All_DateTime_Values()
        {
            var dateTimeSelector  = new DateTimeSelector();
            var dateTimeTestClass = new DateTimeTestClass();

            //Iterate over all of the properties in the fullNameClass object...
            foreach (var property in dateTimeTestClass.GetType().GetProperties())
            {
                //Inject the value into the property
                dateTimeSelector.Generate(dateTimeTestClass, property);
            }

            //Iterate over all of the properties again
            foreach (var property in dateTimeTestClass.GetType().GetProperties())
            {
                var fieldValue = (DateTime)property.GetValue(dateTimeTestClass, null);

                Assert.IsAssignableFrom <DateTime>(fieldValue, "Should be type of DateTime...");
                Assert.AreNotEqual(fieldValue, default(DateTime));
            }
        }
Ejemplo n.º 17
0
        public void DateTime_Selector_Injects_All_DateTime_Values()
        {
            var dateTimeSelector = new DateTimeSelector();
            var dateTimeTestClass = new DateTimeTestClass();

            //Iterate over all of the properties in the fullNameClass object...
            foreach (var property in dateTimeTestClass.GetType().GetProperties())
            {
                //Inject the value into the property
                dateTimeSelector.Generate(dateTimeTestClass, property);
            }

            //Iterate over all of the properties again
            foreach (var property in dateTimeTestClass.GetType().GetProperties())
            {
                var fieldValue = (DateTime)property.GetValue(dateTimeTestClass, null);

                Assert.IsAssignableFrom<DateTime>(fieldValue, "Should be type of DateTime...");
                Assert.AreNotEqual(fieldValue, default(DateTime));
            }
        }
Ejemplo n.º 18
0
        public void DeserializeUTC()
        {
            DateTimeTestClass c =
                JsonConvert.DeserializeObject <DateTimeTestClass>(@"{""PreField"":""Pre"",""DateTimeField"":""2008-12-12T12:12:12Z"",""DateTimeOffsetField"":""2008-12-12T12:12:12Z"",""PostField"":""Post""}", new IsoDateTimeConverter()
            {
                DateTimeStyles = DateTimeStyles.AssumeUniversal
            });

            Assert.AreEqual(new DateTime(2008, 12, 12, 12, 12, 12, 0, DateTimeKind.Utc).ToLocalTime(), c.DateTimeField);
            Assert.AreEqual(new DateTimeOffset(2008, 12, 12, 12, 12, 12, 0, TimeSpan.Zero), c.DateTimeOffsetField);
            Assert.AreEqual("Pre", c.PreField);
            Assert.AreEqual("Post", c.PostField);

            DateTimeTestClass c2 =
                JsonConvert.DeserializeObject <DateTimeTestClass>(@"{""PreField"":""Pre"",""DateTimeField"":""2008-01-01T01:01:01Z"",""DateTimeOffsetField"":""2008-01-01T01:01:01Z"",""PostField"":""Post""}", new IsoDateTimeConverter()
            {
                DateTimeStyles = DateTimeStyles.AssumeUniversal
            });

            Assert.AreEqual(new DateTime(2008, 1, 1, 1, 1, 1, 0, DateTimeKind.Utc).ToLocalTime(), c2.DateTimeField);
            Assert.AreEqual(new DateTimeOffset(2008, 1, 1, 1, 1, 1, 0, TimeSpan.Zero), c2.DateTimeOffsetField);
            Assert.AreEqual("Pre", c2.PreField);
            Assert.AreEqual("Post", c2.PostField);
        }
Ejemplo n.º 19
0
 public void Initialization()
 {
     this.dt = new DateTimeTestClass();
 }
Ejemplo n.º 20
0
 public void SerializeShouldChangeNonUTCDates()
 {
     DateTimeTestClass c = new DateTimeTestClass();
       c.DateTimeField = new DateTime(2008, 1, 1, 1, 1, 1, 0, DateTimeKind.Local);
       c.PreField = "Pre";
       c.PostField = "Post";
       string json = JavaScriptConvert.SerializeObject(c, new IsoDateTimeConverter() { DateTimeStyles = DateTimeStyles.AssumeUniversal }); //note that this fails without the Utc converter...
       c.DateTimeField = new DateTime(2008, 1, 1, 1, 1, 1, 0, DateTimeKind.Utc);
       string json2 = JavaScriptConvert.SerializeObject(c, new IsoDateTimeConverter() { DateTimeStyles = DateTimeStyles.AssumeUniversal });
       Assert.AreNotEqual(json, json2);
 }
Ejemplo n.º 21
0
 public void DeserializeNullToNonNullable()
 {
     DateTimeTestClass c2 =
         JsonConvert.DeserializeObject <DateTimeTestClass>(@"{""PreField"":""Pre"",""DateTimeField"":null,""DateTimeOffsetField"":null,""PostField"":""Post""}", new JavaScriptDateTimeConverter());
 }