コード例 #1
0
ファイル: Program.cs プロジェクト: isaksky/edn-dot-net
        public static void TestCollections()
        {
            var testNetCollections = new List <IDictionary <string, SortedSet <int> > >()
            {
                new Dictionary <string, SortedSet <int> >()
                {
                    { "key1", new SortedSet <int>()
                      {
                          1, 2, 3
                      } },
                    { "key2", new SortedSet <int>()
                      {
                          4, 5, 6
                      } },
                },

                new Dictionary <string, SortedSet <int> >()
                {
                    { "key3", new SortedSet <int>()
                      {
                          1, 2, 3
                      } },
                    { "key4", new SortedSet <int>()
                      {
                          4, 5, 6
                      } },
                }
            };


            string s = EDNWriter.EDNWriterFuncs.writeString(testNetCollections);

            var testNetRet = EDNReader.EDNReaderFuncs.readString(s);

            var firstDict = (IDictionary <object, object>)((IEnumerable <object>)testNetRet.First()).First();

            foreach (var kvp in firstDict)
            {
                Funcs.Assert(kvp.Value is EDNSet);
            }

            Funcs.Assert(firstDict.Contains(new KeyValuePair <object, object>("key1", new EDNSet(new object[] { 1L, 2L, 3L }))));
            Funcs.Assert(firstDict.Count() == 2);

            var testMap = new EDNMap(new List <object>()
            {
                null, new SortedSet <int>()
                {
                    1, 2, 3
                },
                "key6", new SortedSet <int>()
                {
                    4, 5, 6
                },
                "key7", 1
            });

            foreach (var kvp in testMap)
            {
                Funcs.Assert(kvp.Value != null && kvp.Value != kvp.Key);
            }

            Funcs.Assert(testMap[null] is SortedSet <int>);
            Funcs.Assert(testMap.Count() == 3);
            Funcs.Assert(testMap.Where(kvp => (string)kvp.Key == "key7").Count() == 1);
            Funcs.Assert(testMap.Select(kvp => (string)kvp.Key == "key7").First() == false);
            Funcs.Assert(testMap.ContainsKey(null));
            Funcs.Assert(testMap.ContainsKey("key6"));
            Funcs.Assert(testMap.ContainsKey("missingkey") == false);

            Funcs.Assert(testMap.Keys.Count() == testMap.Values.Count());
            Funcs.Assert(testMap.Values.Count() == 3);
            Funcs.Assert(testMap.Values.Contains(1));
            Funcs.Assert(testMap.Values.Contains(999) == false);
            Funcs.Assert(testMap.Keys.Contains(null));
            Funcs.Assert(testMap.Keys.Contains("key6"));

            Funcs.Assert(testMap.ToList().Count(kvp => kvp.Key == null) == 1);

            //test array
            s          = EDNWriter.EDNWriterFuncs.writeString(testNetCollections.ToArray());
            testNetRet = EDNReader.EDNReaderFuncs.readString(s);

            Funcs.Assert(testNetRet.First() is EDNVector);

            //ET Test using KeyValue pairs outside of dictionaties.
            var lstkv = new List <KeyValuePair <Int32, string[]> >()
            {
                new KeyValuePair <int, string[]>(1, new string[] { "fred", "wilma" })
                ,
                new KeyValuePair <int, string[]>(2, new string[] { "again", "whatever" })
            };

            s          = EDNWriter.EDNWriterFuncs.writeString(lstkv);
            testNetRet = EDNReader.EDNReaderFuncs.readString(s);
            //ET Make sure all the items in the list are EDNVectors
            Funcs.Assert(testNetRet.First() is EDNList &&
                         ((EDNList)testNetRet.First()).All(l => l is EDNVector));
            // ET Each EDNVector should have a list with pairs where the 1st element is an int and the second is a EDNVector of strings
            var vecenum = ((EDNList)testNetRet.First())
                          .Cast <EDNVector>()
                          .Select(x => x.Select((v, i) => new { Index = i, Value = v })
                                  .GroupBy(a => a.Index / 2)
                                  .Select(y => y.Select(v => v.Value).ToList()))
                          .Select(mlst => mlst.All(lst => lst[0] is Int32 && lst[1] is EDNVector && lst.All(d => d is String)))
                          .All(y => y);

            //Test string to keyword conversion
            EDNKeyword kw  = ":testns/keyname";
            EDNKeyword kw2 = "testns/keyname";

            Funcs.Assert(ReferenceEquals(kw, kw2));
        }