| 1 |
/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 |
|
| 5 |
#include "jsapi.h" // JS_NewPlainObject |
| 6 |
#include "js/Result.h" // Error, Ok, Result |
| 7 |
#include "js/String.h" // JS_NewStringCopyZ |
| 8 |
#include "jsapi-tests/tests.h" |
| 9 |
|
| 10 |
JS::Result<> SimpleSuccess(JSContext*) { return JS::Ok(); } |
| 11 |
|
| 12 |
JS::Result<> SimpleFailure(JSContext*) { return JS::Result<>{JS::Error()}; } |
| 13 |
|
| 14 |
JS::Result<int> PODSuccess(JSContext*) { return 42; } |
| 15 |
|
| 16 |
JS::Result<int> PODFailure(JSContext*) { return JS::Result<int>{JS::Error()}; } |
| 17 |
|
| 18 |
JS::Result<JSObject*> ObjectSuccess(JSContext* cx) { |
| 19 |
JS::RootedObject obj{cx, JS_NewPlainObject(cx)}; |
| 20 |
MOZ_RELEASE_ASSERT(obj); |
| 21 |
return obj.get(); |
| 22 |
} |
| 23 |
|
| 24 |
JS::Result<JSObject*> ObjectFailure(JSContext*) { |
| 25 |
return JS::Result<JSObject*>{JS::Error()}; |
| 26 |
} |
| 27 |
|
| 28 |
JS::Result<JSString*> StringSuccess(JSContext* cx) { |
| 29 |
JS::RootedString str{cx, JS_NewStringCopyZ(cx, "foo")}; |
| 30 |
MOZ_RELEASE_ASSERT(str); |
| 31 |
return str.get(); |
| 32 |
} |
| 33 |
|
| 34 |
JS::Result<JSString*> StringFailure(JSContext*) { |
| 35 |
return JS::Result<JSString*>{JS::Error()}; |
| 36 |
} |
| 37 |
|
| 38 |
BEGIN_TEST(testResult_SimpleSuccess) { |
| 39 |
JS::Result<> result = SimpleSuccess(cx); |
| 40 |
CHECK(result.isOk()); |
| 41 |
return true; |
| 42 |
} |
| 43 |
END_TEST(testResult_SimpleSuccess) |
| 44 |
|
| 45 |
BEGIN_TEST(testResult_SimpleFailure) { |
| 46 |
JS::Result<> result = SimpleFailure(cx); |
| 47 |
CHECK(result.isErr()); |
| 48 |
return true; |
| 49 |
} |
| 50 |
END_TEST(testResult_SimpleFailure) |
| 51 |
|
| 52 |
BEGIN_TEST(testResult_PODSuccess) { |
| 53 |
JS::Result<int> result = PODSuccess(cx); |
| 54 |
CHECK(result.isOk()); |
| 55 |
CHECK_EQUAL(result.unwrap(), 42); |
| 56 |
CHECK_EQUAL(result.inspect(), 42); |
| 57 |
return true; |
| 58 |
} |
| 59 |
END_TEST(testResult_PODSuccess) |
| 60 |
|
| 61 |
BEGIN_TEST(testResult_PODFailure) { |
| 62 |
JS::Result<int> result = PODFailure(cx); |
| 63 |
CHECK(result.isErr()); |
| 64 |
return true; |
| 65 |
} |
| 66 |
END_TEST(testResult_PODFailure) |
| 67 |
|
| 68 |
BEGIN_TEST(testResult_ObjectSuccess) { |
| 69 |
JS::Result<JSObject*> result = ObjectSuccess(cx); |
| 70 |
CHECK(result.isOk()); |
| 71 |
CHECK(result.inspect() != nullptr); |
| 72 |
CHECK(result.unwrap() != nullptr); |
| 73 |
return true; |
| 74 |
} |
| 75 |
END_TEST(testResult_ObjectSuccess) |
| 76 |
|
| 77 |
BEGIN_TEST(testResult_ObjectFailure) { |
| 78 |
JS::Result<JSObject*> result = ObjectFailure(cx); |
| 79 |
CHECK(result.isErr()); |
| 80 |
return true; |
| 81 |
} |
| 82 |
END_TEST(testResult_ObjectFailure) |
| 83 |
|
| 84 |
BEGIN_TEST(testResult_StringSuccess) { |
| 85 |
JS::Result<JSString*> result = StringSuccess(cx); |
| 86 |
CHECK(result.isOk()); |
| 87 |
CHECK(result.inspect() != nullptr); |
| 88 |
CHECK(result.unwrap() != nullptr); |
| 89 |
return true; |
| 90 |
} |
| 91 |
END_TEST(testResult_StringSuccess) |
| 92 |
|
| 93 |
BEGIN_TEST(testResult_StringFailure) { |
| 94 |
JS::Result<JSString*> result = StringFailure(cx); |
| 95 |
CHECK(result.isErr()); |
| 96 |
return true; |
| 97 |
} |
| 98 |
END_TEST(testResult_StringFailure) |