コード例 #1
0
        public void TypedAccessors_Dates()
        {
            var bean = new Bean();

            bean.Put("x", new DateTime(2011, 11, 11));
            Assert.Equal(2011, bean.Get <DateTime>("x").Year);

            bean.Put("x", "2012-12-12");
            Assert.Equal(2012, bean.Get <DateTime>("x").Year);

            bean.Put("x", "?");
            Assert.Equal(new DateTime(), bean.Get <DateTime>("x"));
        }
コード例 #2
0
        public void TypedAccessors_Conversion()
        {
            AssertExtensions.WithCulture("ru", delegate() {
                var bean = new Bean();

                bean.Put("x", "3.14");
                Assert.Equal(3.14, bean.Get <double?>("x"));
                Assert.Equal(3.14M, bean.Get <decimal>("x"));

                bean.Put("x", "abc");
                Assert.Equal(0, bean.Get <int>("x"));
                Assert.Null(bean.Get <int?>("x"));
            });
        }
コード例 #3
0
        public void TypedAccessors()
        {
            var bean = new Bean();

            Assert.Equal(0, bean.Get <int>("x"));
            Assert.Null(bean.Get <int?>("x"));

            bean.Put("x", 0);
            Assert.Equal(0, bean.Get <int>("x"));
            Assert.Equal(0, bean.Get <int?>("x"));

            bean.Put("x", null);
            Assert.Equal(0, bean.Get <int>("x"));
            Assert.Null(bean.Get <int?>("x"));

            bean.Put("x", new Nullable <int>(1));
            Assert.Equal(1, bean.Get <int>("x"));
        }
コード例 #4
0
        public void TypedAccessors_NonConvertile()
        {
            var guid = Guid.NewGuid();
            var bean = new Bean();

            bean.Put("p", guid);
            Assert.Equal(guid, bean.Get <Guid>("p"));
            Assert.Equal(guid, bean.Get <Guid?>("p"));
        }
コード例 #5
0
        public void TypedAccessors_Enums()
        {
            var bean = new Bean();

            bean.Put("x", DayOfWeek.Thursday);
            Assert.Equal(DayOfWeek.Thursday, bean.Get <DayOfWeek>("x"));

            bean.Put("x", "THURSDAY");
            Assert.Equal(DayOfWeek.Thursday, bean.Get <DayOfWeek>("x"));

            bean.Put("x", (ulong)DayOfWeek.Thursday);
            Assert.Equal(DayOfWeek.Thursday, bean.Get <DayOfWeek>("x"));

            bean.Put("x", "?");
            Assert.Equal(default(DayOfWeek), bean.Get <DayOfWeek>("x"));

            bean.Put("x", 4);
            Assert.Equal(DayOfWeek.Thursday, bean.Get <DayOfWeek?>("x"));
        }
コード例 #6
0
ファイル: BeanTests.cs プロジェクト: edgarborja/LimeBean
 public void Export()
 {
     var bean = new Bean();
     bean["id"] = 123;
     bean.Put("a", 1).Put("b", "abc");
     AssertExtensions.Equivalent(bean.Export(), new Dictionary<string, object> {
         { "id", 123 }, { "a", 1 }, { "b", "abc" }
     });
     Assert.NotSame(bean.Export(), bean.Export());
 }
コード例 #7
0
        public void PutFromDictionary()
        {
            var bean = new Bean();

            bean.Put(new Dictionary <string, object> {
                { "a", 1 }, { "b", 1 }, { "c", 1 }
            });

            Assert.Equal(1, bean["a"]);
            Assert.Equal(1, bean["b"]);
            Assert.Equal(1, bean["c"]);

            bean.Put(new Dictionary <string, object> {
                { "b", 2 }, { "c", null }
            });

            Assert.Equal(1, bean["a"]);
            Assert.Equal(2, bean["b"]);
            Assert.Null(bean["c"]);
        }
コード例 #8
0
        public void Export()
        {
            var bean = new Bean();

            bean["id"] = 123;
            bean.Put("a", 1).Put("b", "abc");
            AssertExtensions.Equivalent(bean.Export(), new Dictionary <string, object> {
                { "id", 123 }, { "a", 1 }, { "b", "abc" }
            });
            Assert.NotSame(bean.Export(), bean.Export());
        }
コード例 #9
0
        void TypedAccessors(Bean bean)
        {
            /// ## Typed Accessors
            /// To access bean properties in a strongly-typed fashion, use the `Get<T>` method:
#if CODE
            bean.Get <string>("title");
            bean.Get <decimal>("price");
            bean.Get <bool?>("someFlag");
#endif
            /// And there is a companion `Put` method which is chainable:
#if CODE
            bean
            .Put("name", "Jane Doe")
            .Put("comment", null);
#endif
            /// See also: [Custom Bean Classes](#custom-bean-classes)
        }
コード例 #10
0
        void BeanOptions(BeanApi api)
        {
            /// ## Bean Options
            /// You can configure the BeanAPI to dispense new Beans with some default options
            ///
            /// **.ValidateGetColumns**
#if CODE
            // Sets whether a Bean throws `ColumnNotFoundException` if
            // you request a column which isn't stored in the Bean. True by default
            api.BeanOptions.ValidateGetColumns = true;

            Bean bean = api.Dispense("books");
            bean.Put("ColumnOne", 1);              // Add a single column
            int one = bean.Get <int>("ColumnOne"); // OK
            int two = bean.Get <int>("ColumnTwo"); // throws ColumnNotFoundException
#endif
        }
コード例 #11
0
        public void Dispense_ValidateGetColumns_Test()
        {
            IBeanFactory factory = new BeanFactory();
            object       one;
            object       two;
            Bean         bean;

            Func <bool, Bean> make = validateColumns => {
                factory.Options.ValidateGetColumns = false;
                Bean b = factory.Dispense("test");
                Assert.Equal(typeof(Bean), b.GetType());
                Assert.Equal(false, b.ValidateGetColumns);
                b.Put("one", 1);
                return(b);
            };

            // With ValidateGetColumns switched off
            bean = make(false);
            one  = (int)bean["one"];
            Assert.Equal(1, one);
            one = bean.Get <int>("one");
            Assert.Equal(1, one);
            two = bean.Get <int>("two");
            Assert.Equal(0, two);
            two = bean["two"];
            Assert.Equal(null, two);

            // With ValidateGetColumns switched on
            bean = make(true);
            one  = (int)bean["one"];
            Assert.Equal(1, one);
            one = bean.Get <int>("one");
            Assert.Equal(1, one);
            try {
                two = bean["two"];
            } catch (Exception e) {
                Assert.IsType(typeof(ColumnNotFoundException), e);
            }
            try {
                two = bean.Get <int>("two");
            } catch (Exception e) {
                Assert.IsType(typeof(ColumnNotFoundException), e);
            }
        }
コード例 #12
0
        public void ExportWithIgnorelist()
        {
            var bean = new Bean
            {
                ["id"] = 123
            };

            bean.Put("a", 1).Put("b", "abc");

            AssertExtensions.Equivalent(bean.Export("id"), new Dictionary <string, object> {
                { "a", 1 }, { "b", "abc" }
            });
            AssertExtensions.Equivalent(bean.Export("id,b"), new Dictionary <string, object> {
                { "a", 1 }
            });
            AssertExtensions.Equivalent(bean.Export("id,a,b"), new Dictionary <string, object>());

            Assert.NotSame(bean.Export("id"), bean.Export("id"));
            Assert.NotSame(bean.Export("id,b"), bean.Export("id,b"));
            Assert.NotSame(bean.Export("id,a,b"), bean.Export("id,a,b"));
        }
コード例 #13
0
ファイル: Body.cs プロジェクト: edgarborja/LimeBean
 void TypedAccessors(Bean bean)
 {
     /// ## Typed Accessors
     /// To access bean properties in a strongly-typed fashion, use the `Get<T>` method:
     #if CODE
     bean.Get<string>("title");
     bean.Get<decimal>("price");
     bean.Get<bool?>("someFlag");
     #endif
     /// And there is a companion `Put` method which is chainable:
     #if CODE
     bean
         .Put("name", "Jane Doe")
         .Put("comment", null);
     #endif
     /// See also: [Custom Bean Classes](#custom-bean-classes)
 }
コード例 #14
0
ファイル: BeanTests.cs プロジェクト: edgarborja/LimeBean
        public void TypedAccessors_Conversion()
        {
            AssertExtensions.WithCulture("ru", delegate() {
                var bean = new Bean();

                bean.Put("x", "3.14");
                Assert.Equal(3.14, bean.Get<double?>("x"));
                Assert.Equal(3.14M, bean.Get<decimal>("x"));

                bean.Put("x", "abc");
                Assert.Equal(0, bean.Get<int>("x"));
                Assert.Null(bean.Get<int?>("x"));
            });
        }
コード例 #15
0
ファイル: BeanTests.cs プロジェクト: edgarborja/LimeBean
        public void TypedAccessors_Dates()
        {
            var bean = new Bean();

            bean.Put("x", new DateTime(2011, 11, 11));
            Assert.Equal(2011, bean.Get<DateTime>("x").Year);

            bean.Put("x", "2012-12-12");
            Assert.Equal(2012, bean.Get<DateTime>("x").Year);

            bean.Put("x", "?");
            Assert.Equal(new DateTime(), bean.Get<DateTime>("x"));
        }
コード例 #16
0
ファイル: BeanTests.cs プロジェクト: edgarborja/LimeBean
        public void TypedAccessors_Enums()
        {
            var bean = new Bean();

            bean.Put("x", DayOfWeek.Thursday);
            Assert.Equal(DayOfWeek.Thursday, bean.Get<DayOfWeek>("x"));

            bean.Put("x", "THURSDAY");
            Assert.Equal(DayOfWeek.Thursday, bean.Get<DayOfWeek>("x"));

            bean.Put("x", (ulong)DayOfWeek.Thursday);
            Assert.Equal(DayOfWeek.Thursday, bean.Get<DayOfWeek>("x"));

            bean.Put("x", "?");
            Assert.Equal(default(DayOfWeek), bean.Get<DayOfWeek>("x"));

            bean.Put("x", 4);
            Assert.Equal(DayOfWeek.Thursday, bean.Get<DayOfWeek?>("x"));
        }
コード例 #17
0
ファイル: BeanTests.cs プロジェクト: edgarborja/LimeBean
 public void TypedAccessors_NonConvertile()
 {
     var guid = Guid.NewGuid();
     var bean = new Bean();
     bean.Put("p", guid);
     Assert.Equal(guid, bean.Get<Guid>("p"));
     Assert.Equal(guid, bean.Get<Guid?>("p"));
 }
コード例 #18
0
ファイル: BeanTests.cs プロジェクト: edgarborja/LimeBean
        public void TypedAccessors()
        {
            var bean = new Bean();

            Assert.Equal(0, bean.Get<int>("x"));
            Assert.Null(bean.Get<int?>("x"));

            bean.Put("x", 0);
            Assert.Equal(0, bean.Get<int>("x"));
            Assert.Equal(0, bean.Get<int?>("x"));

            bean.Put("x", null);
            Assert.Equal(0, bean.Get<int>("x"));
            Assert.Null(bean.Get<int?>("x"));

            bean.Put("x", new Nullable<int>(1));
            Assert.Equal(1, bean.Get<int>("x"));
        }