Wed, 31 Dec 2014 13:27:57 +0100
Ignore runtime configuration files generated during quality assurance.
michael@0 | 1 | #include "TestJSON.h" |
michael@0 | 2 | |
michael@0 | 3 | #include "IPDLUnitTests.h" // fail etc. |
michael@0 | 4 | |
michael@0 | 5 | #define test_assert(_cond, _msg) \ |
michael@0 | 6 | if (!(_cond)) fail(_msg) |
michael@0 | 7 | |
michael@0 | 8 | namespace mozilla { |
michael@0 | 9 | namespace _ipdltest { |
michael@0 | 10 | |
michael@0 | 11 | static nsString |
michael@0 | 12 | String(const char* const str) |
michael@0 | 13 | { |
michael@0 | 14 | return NS_ConvertUTF8toUTF16(str); |
michael@0 | 15 | } |
michael@0 | 16 | |
michael@0 | 17 | static void |
michael@0 | 18 | Array123(InfallibleTArray<JSONVariant>& a123) |
michael@0 | 19 | { |
michael@0 | 20 | a123.AppendElement(1); a123.AppendElement(2); a123.AppendElement(3); |
michael@0 | 21 | |
michael@0 | 22 | test_assert(a123 == a123, "operator== is broken"); |
michael@0 | 23 | } |
michael@0 | 24 | |
michael@0 | 25 | template<class HandleT> |
michael@0 | 26 | JSONVariant |
michael@0 | 27 | MakeTestVariant(HandleT* handle) |
michael@0 | 28 | { |
michael@0 | 29 | // In JS syntax: |
michael@0 | 30 | // |
michael@0 | 31 | // return [ |
michael@0 | 32 | // undefined, null, true, 1.25, "test string", |
michael@0 | 33 | // handle, |
michael@0 | 34 | // [ 1, 2, 3 ], |
michael@0 | 35 | // { "undefined" : undefined, |
michael@0 | 36 | // "null" : null, |
michael@0 | 37 | // "true" : true, |
michael@0 | 38 | // "1.25" : 1.25, |
michael@0 | 39 | // "string" : "string" |
michael@0 | 40 | // "handle" : handle, |
michael@0 | 41 | // "array" : [ 1, 2, 3 ] |
michael@0 | 42 | // } |
michael@0 | 43 | // ] |
michael@0 | 44 | // |
michael@0 | 45 | InfallibleTArray<JSONVariant> outer; |
michael@0 | 46 | |
michael@0 | 47 | outer.AppendElement(void_t()); |
michael@0 | 48 | outer.AppendElement(null_t()); |
michael@0 | 49 | outer.AppendElement(true); |
michael@0 | 50 | outer.AppendElement(1.25); |
michael@0 | 51 | outer.AppendElement(String("test string")); |
michael@0 | 52 | |
michael@0 | 53 | outer.AppendElement(handle); |
michael@0 | 54 | |
michael@0 | 55 | InfallibleTArray<JSONVariant> tmp; |
michael@0 | 56 | Array123(tmp); |
michael@0 | 57 | outer.AppendElement(tmp); |
michael@0 | 58 | |
michael@0 | 59 | InfallibleTArray<KeyValue> obj; |
michael@0 | 60 | obj.AppendElement(KeyValue(String("undefined"), void_t())); |
michael@0 | 61 | obj.AppendElement(KeyValue(String("null"), null_t())); |
michael@0 | 62 | obj.AppendElement(KeyValue(String("true"), true)); |
michael@0 | 63 | obj.AppendElement(KeyValue(String("1.25"), 1.25)); |
michael@0 | 64 | obj.AppendElement(KeyValue(String("string"), String("value"))); |
michael@0 | 65 | obj.AppendElement(KeyValue(String("handle"), handle)); |
michael@0 | 66 | InfallibleTArray<JSONVariant> tmp2; |
michael@0 | 67 | Array123(tmp2); |
michael@0 | 68 | obj.AppendElement(KeyValue(String("array"), tmp2)); |
michael@0 | 69 | |
michael@0 | 70 | outer.AppendElement(obj); |
michael@0 | 71 | |
michael@0 | 72 | test_assert(outer == outer, "operator== is broken"); |
michael@0 | 73 | |
michael@0 | 74 | return JSONVariant(outer); |
michael@0 | 75 | } |
michael@0 | 76 | |
michael@0 | 77 | //----------------------------------------------------------------------------- |
michael@0 | 78 | // parent |
michael@0 | 79 | |
michael@0 | 80 | void |
michael@0 | 81 | TestJSONParent::Main() |
michael@0 | 82 | { |
michael@0 | 83 | if (!SendStart()) |
michael@0 | 84 | fail("sending Start"); |
michael@0 | 85 | } |
michael@0 | 86 | |
michael@0 | 87 | |
michael@0 | 88 | bool |
michael@0 | 89 | TestJSONParent::RecvTest(const JSONVariant& i, |
michael@0 | 90 | JSONVariant* o) |
michael@0 | 91 | { |
michael@0 | 92 | test_assert(i == i, "operator== is broken"); |
michael@0 | 93 | test_assert(i == MakeTestVariant(mKid), "inparam mangled en route"); |
michael@0 | 94 | |
michael@0 | 95 | *o = i; |
michael@0 | 96 | |
michael@0 | 97 | test_assert(i == *o, "operator= is broken"); |
michael@0 | 98 | |
michael@0 | 99 | return true; |
michael@0 | 100 | } |
michael@0 | 101 | |
michael@0 | 102 | |
michael@0 | 103 | //----------------------------------------------------------------------------- |
michael@0 | 104 | // child |
michael@0 | 105 | |
michael@0 | 106 | bool |
michael@0 | 107 | TestJSONChild::RecvStart() |
michael@0 | 108 | { |
michael@0 | 109 | if (!SendPTestHandleConstructor()) |
michael@0 | 110 | fail("sending Handle ctor"); |
michael@0 | 111 | |
michael@0 | 112 | JSONVariant i(MakeTestVariant(mKid)); |
michael@0 | 113 | test_assert(i == i, "operator== is broken"); |
michael@0 | 114 | test_assert(i == MakeTestVariant(mKid), "copy ctor is broken"); |
michael@0 | 115 | |
michael@0 | 116 | JSONVariant o; |
michael@0 | 117 | if (!SendTest(i, &o)) |
michael@0 | 118 | fail("sending Test"); |
michael@0 | 119 | |
michael@0 | 120 | test_assert(i == o, "round-trip mangled input data"); |
michael@0 | 121 | test_assert(o == MakeTestVariant(mKid), "outparam mangled en route"); |
michael@0 | 122 | |
michael@0 | 123 | Close(); |
michael@0 | 124 | return true; |
michael@0 | 125 | } |
michael@0 | 126 | |
michael@0 | 127 | |
michael@0 | 128 | } // namespace _ipdltest |
michael@0 | 129 | } // namespace mozilla |