Example #1
0
        private MathDomain Create(JArray jArray)
        {
            MathDomain domain = new MathDomain();

            foreach (var item in jArray)
            {
                if (int.TryParse(item.ToString(), out var parsedValue))
                {
                    domain.AddSegment(new ValueDomainSegment(parsedValue));
                }
                else
                {
                    var model = JsonConvert.DeserializeObject <RangeDomainModel>(
                        item.ToString(),
                        new JsonSerializerSettings()
                    {
                        NullValueHandling = NullValueHandling.Ignore
                    }
                        );

                    domain.AddSegment(new RangeDomainSegment(_random, model.Min, model.Max, model.Increment));
                }
            }

            return(domain);
        }
        public void ShouldWriteJsonAndDeserializeIntoSameObject()
        {
            MathDomain         original = new MathDomain();
            ValueDomainSegment vds      = new ValueDomainSegment(1024);
            RangeDomainSegment rds      = new RangeDomainSegment(new Random800_90(), 0, 1024, 128);

            original.AddSegment(vds);
            original.AddSegment(rds);

            var serializedObject = JsonConvert.SerializeObject(
                original,
                new JsonSerializerSettings
            {
                Converters = new List <JsonConverter> {
                    new DomainConverter()
                },
                NullValueHandling = NullValueHandling.Ignore
            }
                );

            var deserializedObject =
                JsonConvert.DeserializeObject <MathDomain>(
                    serializedObject,
                    new DomainConverter()
                    );

            var vdsReObjectified = deserializedObject.DomainSegments.OfType <ValueDomainSegment>().First();
            var rdsReObjectified = deserializedObject.DomainSegments.OfType <RangeDomainSegment>().First();

            Assert.AreEqual(original.DomainSegments.Count(), deserializedObject.DomainSegments.Count(), "count");
            Assert.AreEqual(vds.ToString(), vdsReObjectified.ToString(), nameof(vds));
            Assert.AreEqual(rds.ToString(), rdsReObjectified.ToString(), nameof(rds));
        }
Example #3
0
        public void AddSegmentShouldAddSegmentToCollection()
        {
            var segmentCount = _subject.DomainSegments.Count();

            _subject.AddSegment(_mockDomainSegment.Object);

            Assert.AreEqual(segmentCount + 1, _subject.DomainSegments.Count());
        }
        public void ShouldWriteJsonAndDeserializeIntoSameObject()
        {
            MathDomain         original = new MathDomain();
            ValueDomainSegment vds      = new ValueDomainSegment(1024);
            RangeDomainSegment rds      = new RangeDomainSegment(new Random800_90(), 0, 1024, 128);

            original.AddSegment(vds);
            original.AddSegment(rds);

            var serializedObject = JsonSerializer.Serialize(original, _jsonSerializerOptions);

            var deserializedObject = JsonSerializer.Deserialize <MathDomain>(serializedObject, _jsonSerializerOptions);

            Assert.IsNotNull(deserializedObject, $"{nameof(deserializedObject)} was null.");

            var vdsReObjectified = deserializedObject.DomainSegments.OfType <ValueDomainSegment>().First();
            var rdsReObjectified = deserializedObject.DomainSegments.OfType <RangeDomainSegment>().First();

            Assert.AreEqual(original.DomainSegments.Count(), deserializedObject.DomainSegments.Count(), "count");
            Assert.AreEqual(vds.ToString(), vdsReObjectified.ToString(), nameof(vds));
            Assert.AreEqual(rds.ToString(), rdsReObjectified.ToString(), nameof(rds));
        }
Example #5
0
 public void Setup()
 {
     _mockDomainSegment = new Mock <IDomainSegment>();
     _subject           = new MathDomain();
     _subject.AddSegment(_mockDomainSegment.Object);
 }