Esempio n. 1
0
        public void TryParse_from_invalid_json_literals()
        {
            XenMessage msg;

            // Acting & Asserting
            Assert.IsFalse(XenMessage.TryParse(null, out msg), "The nullString check failed.");
            Assert.IsFalse(XenMessage.TryParse("", out msg), "The empty check failed.");
            Assert.IsFalse(XenMessage.TryParse("   ", out msg), "The only whitespace check failed.");
            Assert.IsFalse(XenMessage.TryParse("random", out msg), "The random string check failed.");

            Assert.IsFalse(XenMessage.TryParse("{}", out msg), "The obj schema check failed.");
            Assert.IsFalse(XenMessage.TryParse("{'property':'value'}", out msg), "Valid json obj; invalid schema check failed.");
        }
Esempio n. 2
0
        public void TryParse_from_valid_json_literal()
        {
            var json = @"{  
                            'stringProperty': 'Michael Davis',
                            'intProperty': 31,
                            'time': '2015-05-03T00:00:00',
                            'action': 'SomeXenMessage',
                            'messageId': '123'  
                        }";

            // Act
            XenMessage baseMsg;
            var        baseParsed = XenMessage.TryParse(json, out baseMsg);

            Assert.IsTrue(baseParsed, "The json literal wasn't parsed.");
            Assert.IsNotNull(baseMsg, "The baseMsg was null.");

            Assert.IsTrue
            (
                baseMsg.Action == nameof(SomeXenMessage) &&
                baseMsg.MessageId == "123" &&
                baseMsg.Time == new DateTime(2015, 5, 3),
                "The XenMessage comparison failed."
            );

            // Act
            SomeXenMessage subclassMsg;
            var            subclassParsed = XenMessage.TryParse(json, out subclassMsg);

            Assert.IsTrue(subclassParsed, "The subclass did not parse.");
            Assert.IsNotNull(subclassMsg, "The subclass message is null.");

            // Act
            SomeXenMessage genericMsg;
            var            genericParsed = XenMessage.TryParse(json, out genericMsg);

            Assert.IsTrue(genericParsed, "The generic did not parse.");
            Assert.IsNotNull(genericMsg, "The generic is null.");

            // Act
            object typedMessage;
            var    typedPassed = XenMessage.TryParse(typeof(SomeXenMessage), json, out typedMessage);

            Assert.IsTrue(typedPassed, "The typed message did not parse.");
            Assert.IsNotNull(typedMessage, "The typed message is null.");
            Assert.IsInstanceOf <SomeXenMessage>(typedMessage, "The object was not the correct type.");
        }