Example #1
0
        public override HttpResponse Handle(HttpRequest req)
        {
            if (req.Headers.ContainsKey("SIMPLEDBADD"))
            {
                if (req.Headers["SIMPLEDBADD"] == "TRUE")
                {
                    count++;
                }
            }

            if (req.Headers.ContainsKey("SIMPLEDBSUB"))
            {
                if (req.Headers["SIMPLEDBSUB"] == "TRUE")
                {
                    count--;
                }
            }
            if (count < 0)
            {
                count = 0;
            }

            var dict = new JsonDict();

            dict.Add("Count", JsonNumber.Create(count));
            return(HttpResponse.Json(dict));
        }
Example #2
0
        public void TestJsonInstantiate()
        {
            Assert.DoesNotThrow(() => new JsonBool(true));
            Assert.DoesNotThrow(() => JsonNumber.Create(25));
            Assert.DoesNotThrow(() => JsonNumber.Create(25L));
            Assert.DoesNotThrow(() => JsonNumber.Create(25.1));
            Assert.DoesNotThrow(() => new JsonString("abc"));
            Assert.DoesNotThrow(() => new JsonList());
            Assert.DoesNotThrow(() => new JsonDict());

            Assert.Throws <ArgumentNullException>(() => new JsonString(null));
            Assert.Throws <ArgumentNullException>(() => new JsonList(null));
            Assert.Throws <ArgumentNullException>(() => new JsonDict(null));

            Assert.Throws <ArgumentException>(() => JsonNumber.Create(double.NaN));
            Assert.Throws <ArgumentException>(() => JsonNumber.Create(double.PositiveInfinity));
            Assert.Throws <ArgumentException>(() => JsonNumber.Create(double.NegativeInfinity));
        }
Example #3
0
 private void assertJsonValueNotEqualComparison(JsonValue value)
 {
     Assert.IsFalse(value.Equals(null));
     if (!(value is JsonBool))
     {
         Assert.IsFalse(value.Equals(new JsonBool(true)));
     }
     if (!(value is JsonString))
     {
         Assert.IsFalse(value.Equals(new JsonString("abc")));
     }
     if (!(value is JsonNumber))
     {
         Assert.IsFalse(value.Equals(JsonNumber.Create(47)));
     }
     if (!(value is JsonList))
     {
         Assert.IsFalse(value.Equals(new JsonList()));
     }
     if (!(value is JsonDict))
     {
         Assert.IsFalse(value.Equals(new JsonDict()));
     }
 }
Example #4
0
        public void TestJsonEquality()
        {
            // Comparison with null
            Assert.IsFalse(new JsonBool(true).Equals(null));
            Assert.IsFalse(new JsonString("thingy").Equals(null));
            Assert.IsFalse(JsonNumber.Create(47).Equals(null));
            Assert.IsFalse(new JsonList().Equals(null));
            Assert.IsFalse(new JsonDict().Equals(null));

            // Comparison with an object outside the hierarchy
            Assert.IsFalse(new JsonBool(true).Equals(new JsonString("abc")));
            Assert.IsFalse(new JsonString("thingy").Equals(StringComparer.Ordinal));
            Assert.IsFalse(JsonNumber.Create(47).Equals(new StringBuilder()));
            Assert.IsFalse(new JsonList().Equals(new JsonDict()));
            Assert.IsFalse(new JsonDict().Equals(new object()));

            // Normal comparisons: bool
            Assert.IsTrue(new JsonBool(true).Equals(new JsonBool(true)));
            Assert.IsFalse(new JsonBool(true).Equals(new JsonBool(false)));
            Assert.IsTrue(new JsonBool(true) == true);
            Assert.IsTrue(new JsonBool(false) != true);

            // Normal comparisons: string
            Assert.IsTrue(new JsonString("abc").Equals(new JsonString("abc")));
            Assert.IsFalse(new JsonString("thingy").Equals(new JsonString("stuff")));
            Assert.IsTrue(new JsonString("abc") == "abc");
            Assert.IsTrue(new JsonString("thingy") != "stuff");

            // Normal comparisons: number
            Assert.IsTrue(JsonNumber.Create(47.0).Equals(JsonNumber.Create(47)));
            Assert.IsTrue(JsonNumber.Create(47.0).Equals(JsonNumber.Create(47L)));
            Assert.IsTrue(JsonNumber.Create(47).Equals(JsonNumber.Create(47L)));
            Assert.IsFalse(JsonNumber.Create(47.47).Equals(JsonNumber.Create(47)));
            Assert.AreEqual((double)JsonNumber.Create(47), 47.0);
            Assert.AreEqual((int)JsonNumber.Create(47.0), 47);
            Assert.AreEqual((int)JsonNumber.Create(47.4), 47);

            // Normal comparisons: list
            Assert.IsTrue(new JsonList().Equals(new JsonList()));
            Assert.IsTrue(new JsonList {
                47, null, "blah", new JsonList {
                    -3
                }, 5.0, -0.12
            }.Equals(new JsonList {
                47, null, "blah", new JsonList {
                    -3
                }, 5, -0.12
            }));
            Assert.IsFalse(new JsonList {
                47, null, "blah", new JsonList {
                    -3
                }, 5.0, -0.12
            }.Equals(new JsonList {
                47, null, "blah", new JsonList {
                    -3.1
                }, 5.0, -0.12
            }));
            Assert.IsFalse(new JsonList {
                47, null, "blah", new JsonList {
                    -3
                }, 5, -0.12
            }.Equals(new JsonList {
                47, null, "blah", new JsonList {
                    -3
                }, 5, -0.12, null
            }));

            // Normal comparisons: dict
            Assert.IsTrue(new JsonDict().Equals(new JsonDict()));
            Assert.IsTrue(new JsonDict {
                { "", 47 }, { "hey", new JsonDict {
                                  { "inner", "self" }
                              } }, { "blah", null }
            }.Equals(new JsonDict {
                { "", 47 }, { "hey", new JsonDict {
                                  { "inner", "self" }
                              } }, { "blah", null }
            }));
            Assert.IsTrue(new JsonDict {
                { "", 47 }, { "hey", new JsonDict {
                                  { "inner", "self" }
                              } }, { "blah", null }
            }.Equals(new JsonDict {
                { "blah", null }, { "hey", new JsonDict {
                                        { "inner", "self" }
                                    } }, { "", 47 }
            }));
            Assert.IsFalse(new JsonDict {
                { "", 47 }, { "hey", new JsonDict {
                                  { "inner", "self" }
                              } }, { "blah", null }
            }.Equals(new JsonDict {
                { "", 47 }, { "hey", new JsonDict {
                                  { "inner", "self" }
                              } }
            }));
            Assert.IsFalse(new JsonDict {
                { "", 47 }, { "hey", new JsonDict {
                                  { "inner", "self" }
                              } }, { "blah", null }
            }.Equals(new JsonDict {
                { "", 47 }, { "hey", new JsonDict {
                                  { "inner", "selfish" }
                              } }, { "blah", null }
            }));
            Assert.IsFalse(new JsonDict {
                { "", 47 }, { "hey", new JsonDict {
                                  { "inner", "self" }
                              } }, { "blah", null }
            }.Equals(new JsonDict {
                { "", 47 }, { "hey!", new JsonDict {
                                  { "inner", "self" }
                              } }, { "blah", null }
            }));

            // Normal comparisons via JsonValue
            assertValueEqual(new JsonBool(true), new JsonBool(true));
            assertValueNotEqual(new JsonBool(true), new JsonBool(false));
            assertValueEqual(new JsonString("abc"), new JsonString("abc"));
            assertValueNotEqual(new JsonString("thingy"), new JsonString("stuff"));
            assertValueEqual(JsonNumber.Create(47.0), JsonNumber.Create(47L));
            assertValueNotEqual(JsonNumber.Create(47.47), JsonNumber.Create(47));
            assertValueEqual(new JsonList {
                47, null, "blah", new JsonList {
                    -3
                }, 5, -0.12
            }, new JsonList {
                47, null, "blah", new JsonList {
                    -3
                }, 5, -0.12
            });
            assertValueNotEqual(new JsonList {
                47, null, "blah", new JsonList {
                    -3
                }, 5.0, -0.12
            }, new JsonList {
                47, null, "blah", new JsonList {
                    -3.1
                }, 5.0, -0.12
            });
            assertValueEqual(new JsonDict {
                { "", 47 }, { "hey", new JsonDict {
                                  { "inner", "self" }
                              } }, { "blah", null }
            }, new JsonDict {
                { "", 47 }, { "hey", new JsonDict {
                                  { "inner", "self" }
                              } }, { "blah", null }
            });
            assertValueNotEqual(new JsonDict {
                { "", 47 }, { "hey", new JsonDict {
                                  { "inner", "self" }
                              } }, { "blah", null }
            }, new JsonDict {
                { "", 47 }, { "hey", new JsonDict {
                                  { "inner", "self" }
                              } }
            });

            // JsonValue comparisons with nulls and objects outside the hierarchy
            assertJsonValueNotEqualComparison(new JsonBool(true));
            assertJsonValueNotEqualComparison(new JsonString("abc"));
            assertJsonValueNotEqualComparison(JsonNumber.Create(47.5));
            assertJsonValueNotEqualComparison(new JsonList());
            assertJsonValueNotEqualComparison(new JsonDict());
        }