Exemple #1
0
        public void Can_Deserialize_TimeSpan()
        {
            TimeSpanTestStructure payload = GetPayLoad <TimeSpanTestStructure>("timespans.txt");

            Assert.AreEqual(new TimeSpan(468006), payload.Tick);
            Assert.AreEqual(new TimeSpan(0, 0, 0, 0, 125), payload.Millisecond);
            Assert.AreEqual(new TimeSpan(0, 0, 8), payload.Second);
            Assert.AreEqual(new TimeSpan(0, 55, 2), payload.Minute);
            Assert.AreEqual(new TimeSpan(21, 30, 7), payload.Hour);
            Assert.Null(payload.NullableWithoutValue);
            Assert.NotNull(payload.NullableWithValue);
            Assert.AreEqual(new TimeSpan(21, 30, 7), payload.NullableWithValue.Value);
            Assert.AreEqual(new TimeSpan(0, 0, 10), payload.IsoSecond);
            Assert.AreEqual(new TimeSpan(0, 3, 23), payload.IsoMinute);
            Assert.AreEqual(new TimeSpan(5, 4, 9), payload.IsoHour);
            Assert.AreEqual(new TimeSpan(1, 19, 27, 13), payload.IsoDay);
            // 2 months + 4 days = 64 days
            Assert.AreEqual(new TimeSpan(64, 3, 14, 19), payload.IsoMonth);
            // 1 year = 365 days
            Assert.AreEqual(new TimeSpan(365, 9, 27, 48), payload.IsoYear);
        }
Exemple #2
0
        public void Can_Deserialize_TimeSpan()
        {
            CultureInfo culture           = CultureInfo.InvariantCulture;
            XDocument   doc               = new XDocument(culture);
            TimeSpan?   nullTimespan      = null;
            TimeSpan?   nullValueTimeSpan = new TimeSpan(21, 30, 7);
            XElement    root              = new XElement("Person");

            root.Add(new XElement("Tick", new TimeSpan(468006)));
            root.Add(new XElement("Millisecond", new TimeSpan(0, 0, 0, 0, 125)));
            root.Add(new XElement("Second", new TimeSpan(0, 0, 8)));
            root.Add(new XElement("Minute", new TimeSpan(0, 55, 2)));
            root.Add(new XElement("Hour", new TimeSpan(21, 30, 7)));
            root.Add(new XElement("NullableWithoutValue", nullTimespan));
            root.Add(new XElement("NullableWithValue", nullValueTimeSpan));

            doc.Add(root);

            RestResponse response = new RestResponse
            {
                Content = doc.ToString()
            };
            XmlDeserializer d = new XmlDeserializer
            {
                Culture = culture,
            };
            TimeSpanTestStructure payload = d.Deserialize <TimeSpanTestStructure>(response);

            Assert.AreEqual(new TimeSpan(468006), payload.Tick);
            Assert.AreEqual(new TimeSpan(0, 0, 0, 0, 125), payload.Millisecond);
            Assert.AreEqual(new TimeSpan(0, 0, 8), payload.Second);
            Assert.AreEqual(new TimeSpan(0, 55, 2), payload.Minute);
            Assert.AreEqual(new TimeSpan(21, 30, 7), payload.Hour);
            Assert.Null(payload.NullableWithoutValue);
            Assert.NotNull(payload.NullableWithValue);
            Assert.AreEqual(new TimeSpan(21, 30, 7), payload.NullableWithValue.Value);
        }