diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 01:47:29 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 01:47:29 +0000 |
commit | 0ebf5bdf043a27fd3dfb7f92e0cb63d88954c44d (patch) | |
tree | a31f07c9bcca9d56ce61e9a1ffd30ef350d513aa /js/src/jsapi-tests/testFrontendJSON.cpp | |
parent | Initial commit. (diff) | |
download | firefox-esr-0ebf5bdf043a27fd3dfb7f92e0cb63d88954c44d.tar.xz firefox-esr-0ebf5bdf043a27fd3dfb7f92e0cb63d88954c44d.zip |
Adding upstream version 115.8.0esr.upstream/115.8.0esr
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'js/src/jsapi-tests/testFrontendJSON.cpp')
-rw-r--r-- | js/src/jsapi-tests/testFrontendJSON.cpp | 200 |
1 files changed, 200 insertions, 0 deletions
diff --git a/js/src/jsapi-tests/testFrontendJSON.cpp b/js/src/jsapi-tests/testFrontendJSON.cpp new file mode 100644 index 0000000000..d4d109a1b5 --- /dev/null +++ b/js/src/jsapi-tests/testFrontendJSON.cpp @@ -0,0 +1,200 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include <string> + +#include "js/JSON.h" +#include "jsapi-tests/tests.h" + +using namespace JS; + +BEGIN_FRONTEND_TEST(testIsValidJSONLatin1) { + const char* source; + + source = "true"; + CHECK(IsValidJSON(reinterpret_cast<const JS::Latin1Char*>(source), + strlen(source))); + + source = "false"; + CHECK(IsValidJSON(reinterpret_cast<const JS::Latin1Char*>(source), + strlen(source))); + + source = "null"; + CHECK(IsValidJSON(reinterpret_cast<const JS::Latin1Char*>(source), + strlen(source))); + + source = "0"; + CHECK(IsValidJSON(reinterpret_cast<const JS::Latin1Char*>(source), + strlen(source))); + + source = "1"; + CHECK(IsValidJSON(reinterpret_cast<const JS::Latin1Char*>(source), + strlen(source))); + + source = "-1"; + CHECK(IsValidJSON(reinterpret_cast<const JS::Latin1Char*>(source), + strlen(source))); + + source = "1.75"; + CHECK(IsValidJSON(reinterpret_cast<const JS::Latin1Char*>(source), + strlen(source))); + + source = "9000000000"; + CHECK(IsValidJSON(reinterpret_cast<const JS::Latin1Char*>(source), + strlen(source))); + + source = "\"foo\""; + CHECK(IsValidJSON(reinterpret_cast<const JS::Latin1Char*>(source), + strlen(source))); + + source = "[]"; + CHECK(IsValidJSON(reinterpret_cast<const JS::Latin1Char*>(source), + strlen(source))); + + source = "[1, true]"; + CHECK(IsValidJSON(reinterpret_cast<const JS::Latin1Char*>(source), + strlen(source))); + + source = "{}"; + CHECK(IsValidJSON(reinterpret_cast<const JS::Latin1Char*>(source), + strlen(source))); + + source = "{\"key\": 10}"; + CHECK(IsValidJSON(reinterpret_cast<const JS::Latin1Char*>(source), + strlen(source))); + + source = "{\"key\": 10, \"prop\": 20}"; + CHECK(IsValidJSON(reinterpret_cast<const JS::Latin1Char*>(source), + strlen(source))); + + source = "1 "; + CHECK(IsValidJSON(reinterpret_cast<const JS::Latin1Char*>(source), + strlen(source))); + + // Invalid cases. + + source = ""; + CHECK(!IsValidJSON(reinterpret_cast<const JS::Latin1Char*>(source), + strlen(source))); + + source = "1 1"; + CHECK(!IsValidJSON(reinterpret_cast<const JS::Latin1Char*>(source), + strlen(source))); + + source = ".1"; + CHECK(!IsValidJSON(reinterpret_cast<const JS::Latin1Char*>(source), + strlen(source))); + + source = "undefined"; + CHECK(!IsValidJSON(reinterpret_cast<const JS::Latin1Char*>(source), + strlen(source))); + + source = "TRUE"; + CHECK(!IsValidJSON(reinterpret_cast<const JS::Latin1Char*>(source), + strlen(source))); + + source = "'foo'"; + CHECK(!IsValidJSON(reinterpret_cast<const JS::Latin1Char*>(source), + strlen(source))); + + source = "["; + CHECK(!IsValidJSON(reinterpret_cast<const JS::Latin1Char*>(source), + strlen(source))); + + source = "{"; + CHECK(!IsValidJSON(reinterpret_cast<const JS::Latin1Char*>(source), + strlen(source))); + + source = "/a/"; + CHECK(!IsValidJSON(reinterpret_cast<const JS::Latin1Char*>(source), + strlen(source))); + + return true; +} + +END_TEST(testIsValidJSONLatin1) + +BEGIN_FRONTEND_TEST(testIsValidJSONTwoBytes) { + const char16_t* source; + + source = u"true"; + CHECK(IsValidJSON(source, std::char_traits<char16_t>::length(source))); + + source = u"false"; + CHECK(IsValidJSON(source, std::char_traits<char16_t>::length(source))); + + source = u"null"; + CHECK(IsValidJSON(source, std::char_traits<char16_t>::length(source))); + + source = u"0"; + CHECK(IsValidJSON(source, std::char_traits<char16_t>::length(source))); + + source = u"1"; + CHECK(IsValidJSON(source, std::char_traits<char16_t>::length(source))); + + source = u"-1"; + CHECK(IsValidJSON(source, std::char_traits<char16_t>::length(source))); + + source = u"1.75"; + CHECK(IsValidJSON(source, std::char_traits<char16_t>::length(source))); + + source = u"9000000000"; + CHECK(IsValidJSON(source, std::char_traits<char16_t>::length(source))); + + source = u"\"foo\""; + CHECK(IsValidJSON(source, std::char_traits<char16_t>::length(source))); + + source = u"[]"; + CHECK(IsValidJSON(source, std::char_traits<char16_t>::length(source))); + + source = u"[1, true]"; + CHECK(IsValidJSON(source, std::char_traits<char16_t>::length(source))); + + source = u"{}"; + CHECK(IsValidJSON(source, std::char_traits<char16_t>::length(source))); + + source = u"{\"key\": 10}"; + CHECK(IsValidJSON(source, std::char_traits<char16_t>::length(source))); + + source = u"{\"key\": 10, \"prop\": 20}"; + CHECK(IsValidJSON(source, std::char_traits<char16_t>::length(source))); + + source = u"1 "; + CHECK(IsValidJSON(source, std::char_traits<char16_t>::length(source))); + + // Invalid cases. + + source = u""; + CHECK(!IsValidJSON(source, std::char_traits<char16_t>::length(source))); + + source = u"1 1"; + CHECK(!IsValidJSON(source, std::char_traits<char16_t>::length(source))); + + source = u".1"; + CHECK(!IsValidJSON(source, std::char_traits<char16_t>::length(source))); + + source = u"undefined"; + CHECK(!IsValidJSON(source, std::char_traits<char16_t>::length(source))); + + source = u"TRUE"; + CHECK(!IsValidJSON(source, std::char_traits<char16_t>::length(source))); + + source = u"'foo'"; + CHECK(!IsValidJSON(source, std::char_traits<char16_t>::length(source))); + + source = u"["; + CHECK(!IsValidJSON(source, std::char_traits<char16_t>::length(source))); + + source = u"{"; + CHECK(!IsValidJSON(source, std::char_traits<char16_t>::length(source))); + + source = u"/a/"; + CHECK(!IsValidJSON(source, std::char_traits<char16_t>::length(source))); + + return true; +} + +END_TEST(testIsValidJSONTwoBytes) |