Debian Codegraph @

.cargo/vendor/wasm-bindgen/tests/wasm/result.js

raw
1 const wasm = require('wasm-bindgen-test.js');
2 const assert = require('assert');
3
4 exports.error_new = function(message) {
5 return new Error(message)
6 }
7
8 exports.call_ok = function() {
9 assert.doesNotThrow(() => {
10 let five = wasm.return_my_ok();
11 assert.strictEqual(five, 5);
12 })
13 }
14
15 exports.call_err = function() {
16 assert.throws(() => wasm.return_my_err(), {
17 message: "MyError::Variant"
18 });
19 }
20
21 function check_inflight(struct) {
22 assert.strictEqual(struct.is_inflight(), false);
23 }
24
25 exports.all_struct_methods = function() {
26 let struct;
27 assert.throws(() => wasm.Struct.new_err(), {
28 message: "MyError::Variant"
29 });
30 assert.doesNotThrow(() => {
31 struct = wasm.Struct.new();
32 });
33 check_inflight(struct);
34 assert.doesNotThrow(() => {
35 let five = struct.return_ok();
36 assert.strictEqual(five, 5);
37 });
38 check_inflight(struct);
39 assert.throws(() => struct.return_err(), {
40 message: "MyError::Variant"
41 });
42 check_inflight(struct);
43 }
44
45 exports.call_return_string = function() {
46 assert.doesNotThrow(() => {
47 let ok = wasm.return_string();
48 assert.strictEqual(ok, "string here");
49 })
50 }
51
52 exports.call_jsvalue_ok = function() {
53 assert.doesNotThrow(() => {
54 let five = wasm.return_jsvalue_ok();
55 assert.strictEqual(five, 5);
56 })
57 }
58
59 exports.call_jsvalue_err = function() {
60 try {
61 wasm.return_jsvalue_err();
62 assert.fail("should have thrown");
63 } catch (e) {
64 assert.strictEqual(e, -1);
65 }
66 }
67
68 exports.call_string_ok = function() {
69 assert.doesNotThrow(() => {
70 let ok = wasm.return_string_ok();
71 assert.strictEqual(ok, "Ok");
72 })
73 }
74
75 exports.call_string_err = function() {
76 // the behaviour of Result<String, _> is so finicky that it's not obvious
77 // how to to reproduce reliably but also pass the test suite.
78 assert.throws(() => e = wasm.return_string_err(), e => {
79 // one thing we can do (uncomment to test)
80 // is to throw an error in here.
81 // throw new Error("should not cause a SIGBUS in Node")
82 return e === "Er";
83 });
84 }
85
86 exports.call_enum_ok = function() {
87 assert.doesNotThrow(() => {
88 let ok = wasm.return_enum_ok();
89 assert.strictEqual(ok, 2);
90 })
91 }
92
93 exports.call_enum_err = function() {
94 assert.throws(() => {
95 wasm.return_enum_err();
96 }, {
97 message: "MyError::Variant"
98 })
99 }
100
101 exports.call_unit = function() {
102 assert.doesNotThrow(() => {
103 wasm.return_unit_ok();
104 });
105 assert.throws(() => {
106 wasm.return_unit_err();
107 }, {
108 message: "MyError::Variant"
109 });
110 }
111
112 exports.call_option = function() {
113 assert.doesNotThrow(() => {
114 let o = wasm.return_option_ok_some();
115 assert.strictEqual(o, 10.0);
116 });
117 assert.doesNotThrow(() => {
118 let o = wasm.return_option_ok_none();
119 assert.strictEqual(o, undefined);
120 });
121 assert.throws(() => {
122 wasm.return_option_err();
123 }, {
124 message: "MyError::Variant"
125 });
126 }