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")); }
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")); }); }
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")); }
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")); }
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")); }
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()); }
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"]); }
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()); }
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) }
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 }
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); } }
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")); }
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) }
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")); }); }
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")); }
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")); }
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")); }
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")); }