using System; using System.Collections.Generic; using System.IO; using System.Linq; using UnitTestSharp; namespace Vellum.UnitTests { public class SortedDictionaryConverterTests : TestFixture { public static SaveSession Session() { return SaveConvertersTests.TestConverters().CreateSession(); } public static SortedDictionary ThroughLuaText( SortedDictionary value) { SaveSession session = Session(); Type type = typeof(SortedDictionary); SaveNode node = SaveConvertersTests.ThroughLuaText(session.ToNode(value, type)); return (SortedDictionary)session.FromNode(node, type); } public class WriteTests : TestFixture { public void Entries_AreAMapUnderEntries() { var value = new SortedDictionary { { "b", 2 }, { "a", 1 } }; SaveNode node = Session().ToNode(value, typeof(SortedDictionary)); CheckEqual(new[] { "a", "b" }, node.RequireEntry("entries").RequireMap().Select(entry => entry.Key.Text).ToArray()); } public void DefaultOrdering_IsLeftOut() { SaveNode node = Session().ToNode(new SortedDictionary(), typeof(SortedDictionary)); CheckEqual(new[] { "entries" }, node.Entries.Keys.ToArray()); } public void NamedOrdering_IsSavedBesideTheEntries() { var value = new SortedDictionary(StringComparer.Ordinal); SaveNode node = Session().ToNode(value, typeof(SortedDictionary)); CheckEqual("Ordinal", node.RequireEntry("comparer").Text); } public void OrderingThatCantBeSaved_Throws() { var value = new SortedDictionary(StringComparer.CurrentCulture); CheckThrow(typeof(NotSupportedException)); Session().ToNode(value, typeof(SortedDictionary)); } public void NullDictionary_IsNull() { CheckEqual(SaveNode.Kinds.Null, Session().ToNode(null, typeof(SortedDictionary)).Kind); } } public class ReadTests : TestFixture { public void DictionaryHoldingItself_ReadsAsADictionaryHoldingItself() { var loaded = (SortedDictionary)new SaveConverters(new SaveConverter[0]) .CreateSession() .FromNode(DictionaryConverterTests.ReadTests.NodeHoldingItself(), typeof(SortedDictionary)); CheckTrue(ReferenceEquals(loaded, loaded["self"])); } public void TextKeys_RoundTrip() { var value = new SortedDictionary { { "b", 2 }, { "a", 1 } }; SortedDictionary loaded = ThroughLuaText(value); CheckEqual(new[] { "a", "b" }, loaded.Keys.ToArray()); CheckEqual(2, loaded["b"]); } public void ConvertedKeys_RoundTrip() { var value = new SortedDictionary { { 5, new Point(1.5, 0) }, { -3, new Point(0, 2) } }; SortedDictionary loaded = ThroughLuaText(value); CheckEqual(new[] { -3, 5 }, loaded.Keys.ToArray()); CheckEqual(1.5, loaded[5].X); } public void Ordering_RoundTrips() { var value = new SortedDictionary(StringComparer.OrdinalIgnoreCase) { { "a", 1 } }; SortedDictionary loaded = ThroughLuaText(value); CheckTrue(loaded.Comparer == StringComparer.OrdinalIgnoreCase); CheckEqual(1, loaded["A"]); } public void EmptyDictionary_RoundTrips() { CheckEqual(0, ThroughLuaText(new SortedDictionary()).Count); } public void NullForADictionary_IsNull() { CheckNull(Session().FromNode(SaveNode.Null, typeof(SortedDictionary))); } public void KeysTheOrderingCallsEqual_RecordsTheSecond() { SaveNode node = SaveNode.NewTable(); node.Entries["comparer"] = SaveNode.OfText("OrdinalIgnoreCase"); node.Entries["entries"] = SaveNode.NewTable(); node.Entries["entries"].Entries["a"] = SaveNode.OfText("1"); node.Entries["entries"].Entries["A"] = SaveNode.OfText("2"); SaveSession session = Session(); var loaded = (SortedDictionary)session.FromNode(node, typeof(SortedDictionary)); CheckEqual(1, loaded.Count); CheckEqual(1, session.Sandbox.Issues.Count); } public void MissingEntries_Throws() { CheckThrow(typeof(InvalidDataException)); Session().FromNode(SaveNode.NewTable(), typeof(SortedDictionary)); } } public class TryListChildrenTests : TestFixture { public void EachKeyAndValue_AreListedInPairsAsTheirTypes() { var value = new SortedDictionary { { "a", new Waypoint { Name = "x" } } }; SaveNode node = Session().ToNode(value, typeof(SortedDictionary)); var children = new ChildList(); CheckTrue(new SortedDictionaryConverter().TryListChildren(node, children)); IReadOnlyList> pairs = node.RequireEntry("entries").RequireMap(); CheckEqual(2, children.Count); CheckTrue(ReferenceEquals(pairs[0].Key, children.NodeAt(0))); CheckTrue(ReferenceEquals(pairs[0].Value, children.NodeAt(1))); CheckEqual(typeof(Waypoint), children.TypeAt(1)); } public void MissingEntries_ReturnsFalse() { CheckFalse(new SortedDictionaryConverter().TryListChildren( SaveNode.NewTable(), new ChildList())); } public void EntriesThatAreNotAMap_ReturnsFalse() { SaveNode node = SaveNode.NewTable(); node.Entries["entries"] = SaveNode.OfText("3"); CheckFalse(new SortedDictionaryConverter().TryListChildren(node, new ChildList())); } } public class AllocateTests : TestFixture { public void SavedValue_GivesAnEmptyCollection() { SaveSession session = SaveConvertersTests.Session(); SaveNode node = session.ToNode(new SortedDictionary { { 3, "c" }, { 1, "a" } }); CheckEqual(0, new SortedDictionaryConverter().Allocate(node, session).Count); } public void NodeOfTheWrongShape_Throws() { CheckThrow(typeof(InvalidDataException)); new SortedDictionaryConverter().Allocate(SaveNode.OfText("3"), SaveConvertersTests.Session()); } } public class FillTests : TestFixture { public void SavedValue_AddsWhatReadLoads() { SaveSession session = SaveConvertersTests.Session(); SaveNode node = session.ToNode(new SortedDictionary { { 3, "c" }, { 1, "a" } }); var converter = new SortedDictionaryConverter(); SortedDictionary filled = converter.Allocate(node, session); converter.Fill(filled, node, session); CheckTrue(converter.Read(node, SaveConvertersTests.Session()).SequenceEqual(filled)); } } public class FillNeedsFilledPartTests : TestFixture { public void TheHashedOrSortedType_IsTrue() { CheckTrue(new SortedDictionaryConverter().FillNeedsFilledPart(typeof(int))); } public void AnotherType_IsFalse() { CheckFalse(new SortedDictionaryConverter().FillNeedsFilledPart(typeof(string))); } } } }