Example #1
0
        private void AssertKnownValue(TimeSpan expected, Duration valueToSerialize, Duration valueToDeserialize = null)
        {
            if (valueToDeserialize == null)
            {
                valueToDeserialize = valueToSerialize; // assume they are te same
            }
            var obj = new HasDuration {
                Value = valueToSerialize
            };

            Assert.Equal(expected, ChangeType <HasDuration, HasTimeSpan>(runtime, obj).Value);
            Assert.Equal(expected, ChangeType <HasDuration, HasTimeSpan>(dynamicMethod, obj).Value);
            Assert.Equal(expected, ChangeType <HasDuration, HasTimeSpan>(fullyCompiled, obj).Value);

            var obj2 = new HasTimeSpan {
                Value = expected
            };
            var other = ChangeType <HasTimeSpan, HasDuration>(runtime, obj2).Value ?? new Duration();

            Assert.Equal(valueToDeserialize.Seconds, other.Seconds);
            Assert.Equal(valueToDeserialize.Nanos, other.Nanos);

            other = ChangeType <HasTimeSpan, HasDuration>(dynamicMethod, obj2).Value ?? new Duration();
            Assert.Equal(valueToDeserialize.Seconds, other.Seconds);
            Assert.Equal(valueToDeserialize.Nanos, other.Nanos);

            other = ChangeType <HasTimeSpan, HasDuration>(fullyCompiled, obj2).Value ?? new Duration();
            Assert.Equal(valueToDeserialize.Seconds, other.Seconds);
            Assert.Equal(valueToDeserialize.Nanos, other.Nanos);
        }
Example #2
0
        private void TimeSpan_WellKnownEquiv(TypeModel model, TimeSpan time)
        {
            var seconds = time.TotalSeconds > 0 ? (long)Math.Floor(time.TotalSeconds) : Math.Ceiling(time.TotalSeconds);
            var nanos   = (int)(((time.Ticks % TimeSpan.TicksPerSecond) * 1000000)
                                / TimeSpan.TicksPerMillisecond);

            // convert forwards and compare
            var hazTs = new HasTimeSpan {
                Value = time
            };
            var hazD = ChangeType <HasTimeSpan, HasDuration>(model, hazTs);

            Assert.Equal(seconds, hazD.Value?.Seconds ?? 0);
            Assert.Equal(nanos, hazD.Value?.Nanos ?? 0);

            // and back again
            hazTs = ChangeType <HasDuration, HasTimeSpan>(model, hazD);
            Assert.Equal(time, hazTs.Value);
        }
Example #3
0
        public void TimeSpan_NullStaysNull()
        {
            var orig = new HasTimeSpan();

            Assert.Null(orig.Value);

            var hd = ChangeType <HasTimeSpan, HasDuration>(runtime, orig);

            Assert.Null(hd.Value);
            var clone = ChangeType <HasDuration, HasTimeSpan>(runtime, hd);

            Assert.Null(clone.Value);

            hd = ChangeType <HasTimeSpan, HasDuration>(dynamicMethod, orig);
            Assert.Null(hd.Value);
            clone = ChangeType <HasDuration, HasTimeSpan>(dynamicMethod, hd);
            Assert.Null(clone.Value);

            hd = ChangeType <HasTimeSpan, HasDuration>(fullyCompiled, orig);
            Assert.Null(hd.Value);
            clone = ChangeType <HasDuration, HasTimeSpan>(fullyCompiled, hd);
            Assert.Null(clone.Value);
        }