From 36d22d82aa202bb199967e9512281e9a53db42c9 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 7 Apr 2024 21:33:14 +0200 Subject: Adding upstream version 115.7.0esr. Signed-off-by: Daniel Baumann --- js/src/jsapi-tests/testResult.cpp | 98 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 js/src/jsapi-tests/testResult.cpp (limited to 'js/src/jsapi-tests/testResult.cpp') diff --git a/js/src/jsapi-tests/testResult.cpp b/js/src/jsapi-tests/testResult.cpp new file mode 100644 index 0000000000..5d8d0f88ea --- /dev/null +++ b/js/src/jsapi-tests/testResult.cpp @@ -0,0 +1,98 @@ +/* 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 "jsapi.h" // JS_NewPlainObject +#include "js/Result.h" // Error, Ok, Result +#include "js/String.h" // JS_NewStringCopyZ +#include "jsapi-tests/tests.h" + +JS::Result<> SimpleSuccess(JSContext*) { return JS::Ok(); } + +JS::Result<> SimpleFailure(JSContext*) { return JS::Result<>{JS::Error()}; } + +JS::Result PODSuccess(JSContext*) { return 42; } + +JS::Result PODFailure(JSContext*) { return JS::Result{JS::Error()}; } + +JS::Result ObjectSuccess(JSContext* cx) { + JS::RootedObject obj{cx, JS_NewPlainObject(cx)}; + MOZ_RELEASE_ASSERT(obj); + return obj.get(); +} + +JS::Result ObjectFailure(JSContext*) { + return JS::Result{JS::Error()}; +} + +JS::Result StringSuccess(JSContext* cx) { + JS::RootedString str{cx, JS_NewStringCopyZ(cx, "foo")}; + MOZ_RELEASE_ASSERT(str); + return str.get(); +} + +JS::Result StringFailure(JSContext*) { + return JS::Result{JS::Error()}; +} + +BEGIN_TEST(testResult_SimpleSuccess) { + JS::Result<> result = SimpleSuccess(cx); + CHECK(result.isOk()); + return true; +} +END_TEST(testResult_SimpleSuccess) + +BEGIN_TEST(testResult_SimpleFailure) { + JS::Result<> result = SimpleFailure(cx); + CHECK(result.isErr()); + return true; +} +END_TEST(testResult_SimpleFailure) + +BEGIN_TEST(testResult_PODSuccess) { + JS::Result result = PODSuccess(cx); + CHECK(result.isOk()); + CHECK_EQUAL(result.unwrap(), 42); + CHECK_EQUAL(result.inspect(), 42); + return true; +} +END_TEST(testResult_PODSuccess) + +BEGIN_TEST(testResult_PODFailure) { + JS::Result result = PODFailure(cx); + CHECK(result.isErr()); + return true; +} +END_TEST(testResult_PODFailure) + +BEGIN_TEST(testResult_ObjectSuccess) { + JS::Result result = ObjectSuccess(cx); + CHECK(result.isOk()); + CHECK(result.inspect() != nullptr); + CHECK(result.unwrap() != nullptr); + return true; +} +END_TEST(testResult_ObjectSuccess) + +BEGIN_TEST(testResult_ObjectFailure) { + JS::Result result = ObjectFailure(cx); + CHECK(result.isErr()); + return true; +} +END_TEST(testResult_ObjectFailure) + +BEGIN_TEST(testResult_StringSuccess) { + JS::Result result = StringSuccess(cx); + CHECK(result.isOk()); + CHECK(result.inspect() != nullptr); + CHECK(result.unwrap() != nullptr); + return true; +} +END_TEST(testResult_StringSuccess) + +BEGIN_TEST(testResult_StringFailure) { + JS::Result result = StringFailure(cx); + CHECK(result.isErr()); + return true; +} +END_TEST(testResult_StringFailure) -- cgit v1.2.3