Ejemplo n.º 1
0
        public void StringToBoolConversionStrategyTest()
        {
            var    strategy = new StringToBoolConversionStrategy();
            object newValue;

            Assert.IsFalse(strategy.TryConvert(new object(), typeof(object), out newValue));
            Assert.IsFalse(strategy.TryConvert(true, typeof(string), out newValue));
            Assert.IsFalse(strategy.TryConvert(null, typeof(bool), out newValue));
            Assert.IsFalse(strategy.TryConvert(5, typeof(bool), out newValue));

            Assert.IsTrue(strategy.TryConvert("true", typeof(bool), out newValue));
            Assert.AreEqual(true, newValue);

            Assert.IsTrue(strategy.TryConvert(" true ", typeof(bool), out newValue));
            Assert.AreEqual(true, newValue);

            Assert.IsTrue(strategy.TryConvert("false", typeof(bool), out newValue));
            Assert.AreEqual(false, newValue);

            Assert.IsTrue(strategy.TryConvert("TrUe", typeof(bool), out newValue));
            Assert.AreEqual(true, newValue);

            Assert.IsTrue(strategy.TryConvert("yes", typeof(bool), out newValue));
            Assert.AreEqual(true, newValue);

            Assert.IsTrue(strategy.TryConvert("ok", typeof(bool), out newValue));
            Assert.AreEqual(true, newValue);

            Assert.IsTrue(strategy.TryConvert("hello", typeof(bool), out newValue));
            Assert.AreEqual(false, newValue);
        }
Ejemplo n.º 2
0
        public void StringToBoolConversionStrategyTest()
        {
            var strategy = new StringToBoolConversionStrategy();

            Assert.IsFalse(strategy.CanConvert(typeof(object), typeof(object)));
            Assert.IsFalse(strategy.CanConvert(typeof(bool), typeof(string)));
            Assert.IsTrue(strategy.CanConvert(typeof(string), typeof(bool)));

            var val = strategy.Convert(typeof(string), typeof(bool), "true", null);

            Assert.AreEqual(true, val);

            val = strategy.Convert(typeof(string), typeof(bool), "false", null);
            Assert.AreEqual(false, val);

            val = strategy.Convert(typeof(string), typeof(bool), "TrUe", null);
            Assert.AreEqual(true, val);

            val = strategy.Convert(typeof(string), typeof(bool), "yes", null);
            Assert.AreEqual(true, val);

            val = strategy.Convert(typeof(string), typeof(bool), "ok", null);
            Assert.AreEqual(true, val);
        }