blob: 5d8d0f88eaa92ef0af766c07439f8963947c6857 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
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<int> PODSuccess(JSContext*) { return 42; }
JS::Result<int> PODFailure(JSContext*) { return JS::Result<int>{JS::Error()}; }
JS::Result<JSObject*> ObjectSuccess(JSContext* cx) {
JS::RootedObject obj{cx, JS_NewPlainObject(cx)};
MOZ_RELEASE_ASSERT(obj);
return obj.get();
}
JS::Result<JSObject*> ObjectFailure(JSContext*) {
return JS::Result<JSObject*>{JS::Error()};
}
JS::Result<JSString*> StringSuccess(JSContext* cx) {
JS::RootedString str{cx, JS_NewStringCopyZ(cx, "foo")};
MOZ_RELEASE_ASSERT(str);
return str.get();
}
JS::Result<JSString*> StringFailure(JSContext*) {
return JS::Result<JSString*>{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<int> 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<int> result = PODFailure(cx);
CHECK(result.isErr());
return true;
}
END_TEST(testResult_PODFailure)
BEGIN_TEST(testResult_ObjectSuccess) {
JS::Result<JSObject*> 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<JSObject*> result = ObjectFailure(cx);
CHECK(result.isErr());
return true;
}
END_TEST(testResult_ObjectFailure)
BEGIN_TEST(testResult_StringSuccess) {
JS::Result<JSString*> 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<JSString*> result = StringFailure(cx);
CHECK(result.isErr());
return true;
}
END_TEST(testResult_StringFailure)
|