Ejemplo n.º 1
0
        public void UniValue_Common()
        {
            Console.WriteLine("Test 1: Types");
            var test = new Dictionary <string, UniValue>();

            test.Add("a", 123);
            test.Add("b", "test");
            test.Add("c", DateTime.Now);
            test.Add("d", 4.2);
            test.Add("e", new byte[] { 1, 2, 3 });
            test.Add("f", UniValue.Create(new Uri("http://localhost")));
            test.Add("g", UniValue.Create(new { id = 123, name = "tester", date = DateTime.Now, obj = new { a = 1, b = 2, c = 3, d = new int[] { 1, 2, 3 } } }));
            test.Add("h", new int[] { 1, 2, 3 });
            test.Add("i", new string[] { "a", "b", "c" });
            StringBuilder sb = new StringBuilder(); sb.Append("xyz");

            test.Add("j", sb);

            /*ResultValue a = 123;
             * ResultValue b = "test";
             * ResultValue c = DateTime.Now;
             * ResultValue d = 4.2;
             * ResultValue e = new byte[] { 1, 2, 3 };
             * ResultValue f = new Uri("http://localhost");*/

            foreach (var key in test.Keys)
            {
                Console.WriteLine("{0} = {1}", key, test[key]);
            }

            Console.WriteLine("-------------------------------------");

            Console.WriteLine("Test 2: Parse");

            Console.WriteLine("JSON");
            try
            {
                var r = UniValue.ParseJson("test,1,#2,$3");
                Assert.Fail("Invalid parse: ERROR");
            }
            catch
            {
                Console.WriteLine("Invalid parse: OK");
            }
            try
            {
                UniValue r = UniValue.Empty;
                if (!UniValue.TryParseJson("test,1,#2,$3", out r))
                {
                    Console.WriteLine("Try parse #1: OK");
                }
                else
                {
                    Assert.Fail("Try parse #1: ERROR");
                }
                if (UniValue.TryParseJson("[1,2,3]", out r))
                {
                    Console.WriteLine("Try parse #2: OK / {0}", r);
                }
                else
                {
                    Assert.Fail("Try parse #2: ERROR");
                }
            }
            catch
            {
                Assert.Fail("Try parse: ERROR");
            }

            Console.WriteLine("-------------------------------------");

            Console.WriteLine("XML");
            try
            {
                var r = UniValue.ParseXml("test,1,#2,$3");
                Assert.Fail("Invalid parse: ERROR");
            }
            catch
            {
                Console.WriteLine("Invalid parse: OK");
            }
            try
            {
                UniValue r = UniValue.Empty;
                if (!UniValue.TryParseXml("test,1,#2,$3", out r))
                {
                    Console.WriteLine("Try parse #1: OK");
                }
                else
                {
                    Assert.Fail("Try parse #1: ERROR");
                }
                if (UniValue.TryParseXml("<items><item>1</item><item>2</item><item>3</item></items>", out r))
                {
                    Console.WriteLine("Try parse #2: OK / {0}", r);
                }
                else
                {
                    Assert.Fail("Try parse #2: ERROR");
                }
            }
            catch
            {
                Assert.Fail("Try parse: ERROR");
            }

            Console.WriteLine("-------------------------------------");

            Console.WriteLine("PARAMETERS");
            try
            {
                var r = UniValue.ParseParameters("test,1,#2,$3\r\n");
                Assert.Fail("Invalid parse: ERROR");
            }
            catch
            {
                Console.WriteLine("Invalid parse: OK");
            }
            try
            {
                UniValue r = UniValue.Empty;
                if (!UniValue.TryParseParameters("test,1,#2,$3\r\n", out r))
                {
                    Console.WriteLine("Try parse #1: OK");
                }
                else
                {
                    Assert.Fail("Try parse #1: ERROR");
                }
                if (UniValue.TryParseParameters("a=1&b=2&c=3", out r))
                {
                    Console.WriteLine("Try parse #2: OK / {0}", r);
                }
                else
                {
                    Assert.Fail("Try parse #2: ERROR");
                }
            }
            catch
            {
                Assert.Fail("Try parse: ERROR");
            }

            Console.WriteLine("-------------------------------------");

            Console.WriteLine("Test 3");

            Console.WriteLine("ToString");

            UniValue r2  = "world!";
            string   ttt = "Hello, " + r2;

            Console.WriteLine("IsString: {0}", r2.IsString);
            if (!r2.IsString)
            {
                Assert.Fail();
            }
            Console.WriteLine(ttt);
            Console.WriteLine("OK");

            Console.WriteLine("ToInt32");

            r2 = 123;
            int num = (int)r2;

            num = Convert.ToInt32(r2);

            r2  = "ff";
            num = Convert.ToInt32(r2.ToString(), 16);

            r2  = "123";
            num = (int)r2;

            r2  = 123.123;
            num = (int)r2;

            r2  = "123.123";
            num = (int)r2;

            r2  = "123,123";
            num = (int)r2;

            try
            {
                r2  = "abc";
                num = (int)r2;
                Assert.Fail();
            }
            catch
            {
            }
            Console.WriteLine("OK");

            Console.WriteLine("ToDouble");
            r2 = 123;
            double num2 = (double)r2;

            r2   = "123";
            num2 = (double)r2;

            r2   = "123.123";
            num2 = (double)r2;

            r2   = "123,123";
            num2 = (double)r2;

            try
            {
                r2   = "abc";
                num2 = (double)r2;
                Assert.Fail();
            }
            catch
            {
            }
            Console.WriteLine("OK");

            Console.WriteLine("ToDateTime");

            r2 = "Thu Dec 04, 2014";
            Convert.ToDateTime(r2);

            Console.WriteLine("OK");
        }
Ejemplo n.º 2
0
        public void Json()
        {
#if NET35
            Assert.Throws <ArgumentException>(() => UniValue.ParseJson("test,1,#2,$3"));
#elif NET40
            Assert.Throws <ArgumentException>(() => UniValue.ParseJson("test,1,#2,$3"));
#else
            Assert.ThrowsAny <ArgumentException>(() => UniValue.ParseJson("test,1,#2,$3"));
#endif

            UniValue r = UniValue.Empty;

            Assert.False(UniValue.TryParseJson("test,1,#2,$3", out r));
            Assert.True(UniValue.TryParseJson("[1,2,3]", out r));

            int i = 0;
            int j = 0;

            var testValue  = new string[] { "1", "2", "3" };
            var testValue2 = new string[] { "test", "test2", "test3" };
            var testValue3 = new string[] { "1", "2" };
            var testValue4 = new string[] { "hi", "hello" };

            UniValue value = UniValue.ParseJson("{data: [{id: 1, name: 'test'}, {id: 2, name: 'test2'}, {id: 3, name: 'test3', list: [{x:1,y:'hi'}, {x:2, y:'hello'}] }] }");

            foreach (UniValue itm in value["data"])
            {
                if (itm["list"].HasValue)
                {
                    foreach (UniValue itm2 in itm["list"])
                    {
                        Assert.True(testValue3[j] == itm2["x"] && testValue4[j] == itm2["y"]);

                        j++;
                    }
                }

                Assert.True(testValue[i] == itm["id"] && testValue2[i] == itm["name"]);

                i++;
            }

            value = UniValue.ParseJson
                    (
                @"{
            array: [1, 2, 3, 4, 5, 6, 7, 8, 9],
            array2: ['a', 'b', 1, 2, 3],
            data: 
            [
              {id: 1, name: 'test'}, 
              {id: 2, name: 'test2'}, 
              {
                id: 3, name: 'test3', list: 
                [
                  {x:1,y:'hi'}, 
                  {x:2, y:'hello'},
                  {
                    x:3, y:'test', 
                    array: [1,2,3], 
                    sub: 
                    [
                      {id: 123, text: 'tesssst'},
                      {id: 124, text: 'xyz'},
                      {id: 125, text: 'aaaa', list: [{sid: 1}, {sid: 2}]},
                      {id: 127, text: 'zyx', arr: ['a', 'b', 'c']}
                    ]
                  }
                ] 
              }
            ] 
          }"
                    );

            var array = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

            i = j = 0;

            foreach (UniValue itm in value["array"])
            {
                Assert.True(itm == array[i]);
                Assert.True(itm.Equals(array[i]));
                Assert.Equal(array[i], (int)itm);

                i++;
            }

            var array2 = new object[] { "a", "b", 1, 2, 3 };

            i = j = 0;

            foreach (UniValue itm in value["array2"])
            {
                Assert.True(itm.Equals(array2[i]));
                Assert.Equal(array2[i].ToString(), itm.ToString());

                i++;
            }

            var names = new string[] { "test", "test2", "test3" };

            i = j = 0;

            foreach (UniValue itm in value["data"])
            {
                Assert.Equal(names[i], itm["name"].ToString());

                if (itm["list"].HasValue)
                {
                    foreach (UniValue itm2 in itm["list"])
                    {
                        if (itm2["array"].HasValue)
                        {
                            j = 0;

                            foreach (UniValue itm3 in itm2["array"])
                            {
                                Assert.Equal <int>(j + 1, (int)itm3);
                                Assert.True(itm3 == j + 1);
                                Assert.True(itm3.Equals(j + 1));

                                j++;
                            }
                        }

                        if (itm2["sub"].HasValue)
                        {
                            foreach (UniValue itm3 in itm2["sub"])
                            {
                                if (itm3["arr"].HasValue)
                                {
                                    var arr = new string[] { "a", "b", "c" };

                                    j = 0;

                                    foreach (UniValue itm4 in itm3["arr"])
                                    {
                                        Assert.Equal(arr[j], itm4);
                                        Assert.True(itm4 == arr[j]);
                                        Assert.True(itm4.Equals(arr[j]));

                                        j++;
                                    }
                                }

                                if (itm3["list"].HasValue)
                                {
                                    j = 0;

                                    foreach (UniValue itm4 in itm3["list"])
                                    {
                                        Assert.Equal(j + 1, (int)itm4["sid"]);
                                        Assert.True(itm4["sid"] == j + 1);
                                        Assert.True(itm4["sid"].Equals(j + 1));

                                        j++;
                                    }
                                }
                            }
                        }
                    }
                }

                i++;
            }
        }