public void ToStringUrlEncoded()
        {
            var bag = new KeyValueBag()
            {
                ["Hello"] = "world again",
                ["Age"]   = 40
            };

            Assert.That(bag.ToString("url"), Is.EqualTo("Hello=world+again&Age=40"));
        }
        public void ToStringCmd()
        {
            var bag = new KeyValueBag()
            {
                ["Hello"] = "world",
                ["Age"]   = 40
            };

            Assert.That(bag.ToString("cmd"), Is.EqualTo("-Hello:world -Age:40"));
        }
Esempio n. 3
0
        public void MissesGracefullyWithTypeCheck()
        {
            var obj = new KeyValueBinderTests.SomeClass();

            var bag = new KeyValueBag
            {
                ["Integer"] = "Not an Integer!"
            };

            var reflect = new KeyValueReflection();
            var misses  = reflect.SetValues(bag, obj);

            Assert.That(misses, Is.EqualTo(1));
        }
Esempio n. 4
0
        public void MissesGracefullyWithConversion()
        {
            var obj = new KeyValueBinderTests.SomeClass();

            var bag = new KeyValueBag
            {
                ["Integer"] = "1001"
            };

            var reflect = new KeyValueReflection();
            var misses  = reflect.SetValues(bag, obj);

            Assert.That(obj.Integer, Is.EqualTo(1001));
            Assert.That(misses, Is.EqualTo(0));
        }
Esempio n. 5
0
        public void MissesGracefully()
        {
            var obj = new KeyValueBinderTests.SomeClass();

            var bag = new KeyValueBag
            {
                ["PropDoesNotExist"] = "XXX",
                ["Integer"]          = 543
            };

            var reflect = new KeyValueReflection();
            var misses  = reflect.SetValues(bag, obj);

            Assert.That(obj.Integer, Is.EqualTo(543));
            Assert.That(misses, Is.EqualTo(1));
        }
Esempio n. 6
0
        public void CanSetValues()
        {
            var obj = new KeyValueBinderTests.SomeClass();

            var bag = new KeyValueBag
            {
                ["String"]  = "XXX",
                ["Integer"] = 543
            };

            var reflect = new KeyValueReflection();
            var misses  = reflect.SetValues(bag, obj);

            Assert.That(obj.String, Is.EqualTo("XXX"));
            Assert.That(obj.Integer, Is.EqualTo(543));
            Assert.That(misses, Is.EqualTo(0));
        }
        public static IKeyValueBag FromCommandLine(string[] args)
        {
            var bag = new KeyValueBag();

            foreach (var arg in args)
            {
                if (arg.StartsWith("-"))
                {
                    var idx = arg.IndexOf(':');
                    if (idx > 0)
                    {
                        bag[arg.Substring(1, idx - 1)] = arg.Substring(idx + 1, arg.Length - idx - 1);
                    }
                    else
                    {
                        bag[arg.Remove(0, 1)] = null;
                    }
                }
            }
            return(bag);
        }
        public static IKeyValueBag FromObject(object obj)
        {
            var type = obj.GetType();
            var bag  = new KeyValueBag();

            foreach (var prop in type.GetProperties())
            {
                var value = ReduceNullable(prop.GetValue(obj, null));
                if (value != null)
                {
                    if (IsComplex(value))
                    {
                        bag[prop.Name] = value.ToString();
                    }
                    else
                    {
                        bag[prop.Name] = value;
                    }
                }
            }
            return(bag);
        }