public void Simpledyntest() { var numbers = Enumerable.Range(0, 5000); var pairs = numbers.ToDictionary(x => "K" + x.ToString(), x => x.ToString()); dynamic ds = new DynamicStrings(pairs); var coo = (int)ds.K999; Assert.IsTrue(coo.GetType() == typeof(int)); //A needless tautology Assert.AreEqual(999, coo); }
public void Customconvtest() { var cust = new SampleCustomTypeConverter(); var numbers = Enumerable.Range(0, 5000); //evens are normal, odds get an odd CUSTOM! prefix on the value portion var pairs = numbers.ToDictionary(x => "K" + x.ToString(), x => ((x % 2 == 0) ? "" : "CUSTOM!") + x.ToString()); dynamic ds = new DynamicStrings(pairs, null, DynamicStringOptions.None, new TypeConverter[] { cust }); var coo = (SampleCustomType)ds.K999; Assert.AreEqual("CUSTOM!999", coo.Somestring); var smally = (short)ds.K888; Assert.AreEqual(888, smally); }
public void Multidyntest() { var utcnow = DateTime.UtcNow; var dto = DateTimeOffset.UtcNow; var pairs = new Dictionary <string, string>() { ["Someint"] = "123456789", ["Somestring"] = "yes just a string", ["Somedate"] = utcnow.ToString("o"), ["Somedto"] = dto.ToString(), ["Somebool"] = "TruE", ["Somenull"] = null, ["Anotherint"] = "123" }; dynamic ds = new DynamicStrings(pairs, StringComparer.OrdinalIgnoreCase, //let's ignore case DynamicStringOptions.None, new TypeConverter[] { new DateTimeOffsetConverter() }); //datetimeoffset seems to need an explicit converter // vv Here are the casts from dynamic vv \\ int someint = ds.Someint; string somestring = ds.Somestring; var somedate = ((DateTime)(ds.Somedate)).ToUniversalTime(); DateTimeOffset somedto = ds.Somedto; bool somebool = ds.Somebool; object somenull = ds.Somenull; //Nullables currently NOT supported\\ //int? anotherint = ds.Anotherint; //Nullable test; non null case //int? anothernull = ds.Somenull; //nullable. null case Assert.AreEqual(123456789, someint); Assert.AreEqual("yes just a string", somestring); Assert.AreEqual(utcnow, somedate); Assert.IsTrue(Math.Abs((dto - somedto).TotalMilliseconds) < 2000, "was expecting dto and somedto essentially the same"); Assert.AreEqual(true, somebool); Assert.IsNull(somenull, "somenull"); //Nullables currently NOT supported\\ //Assert.AreEqual(123, anotherint); //Assert.IsNull(anothernull, "anothernull"); }
public void Castertest() { var numbers = Enumerable.Range(0, 5000); var pairs = numbers.ToDictionary(x => "K" + x.ToString(), x => x.ToString()); dynamic ds = new DynamicStrings(pairs); var coo = (int)ds.K999; Assert.AreEqual(999, coo); IDictionary <string, string> id = ds; DynamicStrings dss = ds; Assert.IsTrue(id.ContainsKey("K111"), "we should have had key 111"); Assert.ThrowsException <NotSupportedException>(() => { //we don't support mutability etc. id.Add("somekey", "someval"); }); var memnames = dss.GetDynamicMemberNames().ToDictionary(x => x, x => true); Assert.AreEqual(pairs.Count, memnames.Count, "count of GetDynamicMemberNames"); }
public void TestOfEmptiness() { var pairs = new Dictionary <string, string>() { ["Nothing"] = "" }; //e2d empty to default dynamic ds_e2d = new DynamicStrings(pairs, StringComparer.OrdinalIgnoreCase, //let's ignore case DynamicStringOptions.EmptyStringMeansDefault); //baseline case: cast of empty to most things should fail dynamic ds_none = new DynamicStrings(pairs, StringComparer.OrdinalIgnoreCase, //let's ignore case DynamicStringOptions.None); //autoforce to default here Assert.AreEqual(0, (int)ds_e2d.Nothing); //here we're not forcing; expect bad format Assert.ThrowsException <FormatException>(() => { int shouldfail = ds_none.Nothing; }); //empty string to string is actually OK Assert.AreEqual(String.Empty, (string)ds_none.Nothing); //for good measure we try a property that's not even there Assert.ThrowsException <RuntimeBinderException>(() => { int shouldfailagain = ds_none.NotEvenHere; }); }