using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Seshat.UnitTests { public class RowTests : UnitTestSharp.TestFixture { public class SerializeToLuaTableTests : UnitTestSharp.TestFixture { public void Basic() { var writer = new CodeWriter(); var map = new Row.StringMap(); map.Map.Add("foo", new Dictionary { {"fizz", "buzz"}, {"if", "then"}, }); map.Scalar.Add("bar", "fuzz"); map.List.Add("bizz", new[] { "1", "2", "3", }); writer.CurrentIndent += 4; Row.SerializeToLuaTable(ref writer, map); var expected = @" { bar = ""fuzz"", bizz = { ""1"", ""2"", ""3"", }, foo = { [ ""fizz"" ] = ""buzz"", [ ""if"" ] = ""then"", }, },"; CheckEqual(expected, writer.FullText); } public void CommentInKey() { var writer = new CodeWriter(); var map = new Row.StringMap(); map.Map.Add("foo", new Dictionary { {"fizz", "buzz"}, }); CheckThrow(typeof(Exception)); Row.SerializeToLuaTable(ref writer, map); } } public class ExtractScalarFromStringMapTests : UnitTestSharp.TestFixture { public void Basic() { var failures = new List(); var value = Row.ExtractScalarFromStringMap( new Dictionary>() { { "foo", new Dictionary { {"0", "bar"}, }}, }, "foo", x => x, () => "", ref failures); CheckEqual("bar", value); CheckEqual(0, failures.Count); } public void Null() { var failures = new List(); var value = Row.ExtractScalarFromStringMap( new Dictionary>() { { "foo", new Dictionary { {"0", "bar"}, }}, }, "bar", x => x, () => "not found", ref failures); CheckEqual("not found", value); CheckEqual(0, failures.Count); } public void NotInMap() { var failures = new List(); var value = Row.ExtractScalarFromStringMap( new Dictionary>() { { "foo", new Dictionary { {"1", "bar"}, }}, }, "foo", x => x, () => "not found", ref failures); CheckEqual("not found", value); CheckEqual(0, failures.Count); } public void ConverterFailure() { var failures = new List(); var value = Row.ExtractScalarFromStringMap( new Dictionary>() { { "foo", new Dictionary { {"0", "bar"}, }}, }, "foo", x => { throw new Exception("Foo"); }, () => "default", ref failures); CheckEqual("default", value); CheckEqual(new [] { @"error : Deserializing foo failed with ""Foo""." }, failures); } } public class ExtractListFromStringMap : UnitTestSharp.TestFixture { public void Basic() { var failures = new List(); var stringMap = new Dictionary> { { "bizz", new Dictionary { { "1", "foo" }, { "2", "bar" }, { "3", "fuzz" }, }} }; var actual = Row.ExtractListFromStringMap>( stringMap, "bizz", x => x.ToList(), () => new List { "" }, ref failures); CheckEqual(new string[] { "foo", "bar", "fuzz" }, actual); CheckEqual(0, failures.Count); } public void MissingKey() { var failures = new List(); var stringMap = new Dictionary> { }; CheckEqual(new string[] { "not found" }, Row.ExtractListFromStringMap( stringMap, "bizz", x => x.ToList(), () => new List { "not found" }, ref failures)); CheckEqual(0, failures.Count); } public void MissingIndices() { var failures = new List(); var stringMap = new Dictionary> { { "bizz", new Dictionary { { "2", "foo" }, { "3", "bar" }, { "4", "fuzz" }, }} }; Row.ExtractListFromStringMap( stringMap, "bizz", x => x.ToList(), () => new List { "" }, ref failures); CheckEqual(new[] { "warning: Attempting to interpret bizz as a list, but missing index 1"}, failures); } public void ConverterFailure() { var failures = new List(); var stringMap = new Dictionary> { { "bizz", new Dictionary { { "1", "foo" }, { "2", "bar" }, { "3", "fuzz" }, }} }; var actual = Row.ExtractListFromStringMap>( stringMap, "bizz", x => { throw new Exception("Foo"); }, () => new List { "1", "2", "3" }, ref failures); CheckEqual(new string[] { "1", "2", "3", }, actual); CheckEqual(new[] { @"error : Deserializing bizz failed with ""Foo""."}, failures); } } public class ExtractMapFromStringMap : UnitTestSharp.TestFixture { public void Basic() { var failures = new List(); var stringMap = new Dictionary> { { "bizz", new Dictionary { { "a", "foo" }, { "b", "bar" }, { "c", "fuzz" }, }} }; var actual = Row.ExtractMapFromStringMap>( stringMap, "bizz", x => x.ToDictionary(y => y.Key, y => y.Value), () => new Dictionary(), ref failures); CheckEqual(new Dictionary { { "a", "foo" }, { "b", "bar" }, { "c", "fuzz" }}, actual); CheckEqual(0, failures.Count); } public void MissingKey() { var failures = new List(); var stringMap = new Dictionary> { { "bizz", new Dictionary { { "a", "foo" }, { "b", "bar" }, { "c", "fuzz" }, }} }; var actual = Row.ExtractMapFromStringMap>( stringMap, "buzz", x => x.ToDictionary(y => y.Key, y => y.Value), () => new Dictionary { {"not", "found" } }, ref failures); CheckEqual(new Dictionary { { "not", "found" } }, actual); CheckEqual(0, failures.Count); } public void ConverterFailure() { var failures = new List(); var stringMap = new Dictionary> { { "bizz", new Dictionary { { "a", "foo" }, { "b", "bar" }, { "c", "fuzz" }, }} }; var actual = Row.ExtractMapFromStringMap>( stringMap, "bizz", x => { throw new Exception("Foo"); }, () => new Dictionary { { "not", "found" } }, ref failures); CheckEqual(new Dictionary { { "not", "found" } }, actual); CheckEqual(new[] { @"error : Deserializing bizz failed with ""Foo""." }, failures); } } } }