Backed out changeset d05089c1e269 (bug 1822297) for causing Documentation related...
[gecko.git] / js / src / jsapi-tests / testJSON.cpp
blob5c33e863680fa2023a35f5a6b0eacd00fb9104c5
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include <limits>
8 #include <string.h>
10 #include "js/Array.h" // JS::IsArrayObject
11 #include "js/Exception.h"
12 #include "js/friend/ErrorMessages.h" // JSMSG_*
13 #include "js/JSON.h"
14 #include "js/MemoryFunctions.h"
15 #include "js/Printf.h"
16 #include "js/PropertyAndElement.h" // JS_GetProperty
17 #include "jsapi-tests/tests.h"
19 using namespace JS;
21 static bool Cmp(const char16_t* buffer, uint32_t length,
22 const char16_t* expected) {
23 uint32_t i = 0;
24 while (*expected) {
25 if (i >= length || *expected != *buffer) {
26 return false;
28 ++expected;
29 ++buffer;
30 ++i;
32 return i == length;
35 static bool Callback(const char16_t* buffer, uint32_t length, void* data) {
36 const char16_t** data_ = static_cast<const char16_t**>(data);
37 const char16_t* expected = *data_;
38 *data_ = nullptr;
39 return Cmp(buffer, length, expected);
42 BEGIN_TEST(testToJSON_same) {
43 // Primitives
44 Rooted<Value> input(cx);
45 input = JS::TrueValue();
46 CHECK(TryToJSON(cx, input, u"true"));
48 input = JS::FalseValue();
49 CHECK(TryToJSON(cx, input, u"false"));
51 input = JS::NullValue();
52 CHECK(TryToJSON(cx, input, u"null"));
54 input.setInt32(0);
55 CHECK(TryToJSON(cx, input, u"0"));
57 input.setInt32(1);
58 CHECK(TryToJSON(cx, input, u"1"));
60 input.setInt32(-1);
61 CHECK(TryToJSON(cx, input, u"-1"));
63 input.setDouble(1);
64 CHECK(TryToJSON(cx, input, u"1"));
66 input.setDouble(1.75);
67 CHECK(TryToJSON(cx, input, u"1.75"));
69 input.setDouble(9e9);
70 CHECK(TryToJSON(cx, input, u"9000000000"));
72 input.setDouble(std::numeric_limits<double>::infinity());
73 CHECK(TryToJSON(cx, input, u"null"));
74 return true;
77 bool TryToJSON(JSContext* cx, Handle<Value> value, const char16_t* expected) {
79 Rooted<Value> v(cx, value);
80 const char16_t* expected_ = expected;
81 CHECK(JS_Stringify(cx, &v, nullptr, JS::NullHandleValue, Callback,
82 &expected_));
83 CHECK_NULL(expected_);
86 const char16_t* expected_ = expected;
87 CHECK(JS::ToJSON(cx, value, nullptr, JS::NullHandleValue, Callback,
88 &expected_));
89 CHECK_NULL(expected_);
91 return true;
93 END_TEST(testToJSON_same)
95 BEGIN_TEST(testToJSON_different) {
96 Rooted<Symbol*> symbol(cx, NewSymbol(cx, nullptr));
97 Rooted<Value> value(cx, SymbolValue(symbol));
99 CHECK(JS::ToJSON(cx, value, nullptr, JS::NullHandleValue, UnreachedCallback,
100 nullptr));
101 const char16_t* expected = u"null";
102 CHECK(JS_Stringify(cx, &value, nullptr, JS::NullHandleValue, Callback,
103 &expected));
104 CHECK_NULL(expected);
105 return true;
108 static bool UnreachedCallback(const char16_t*, uint32_t, void*) {
109 MOZ_CRASH("Should not call the callback");
112 END_TEST(testToJSON_different)