1 //! dox
2 
3 #![deny(missing_docs)] // test that documenting public bindings is enough
4 
5 use wasm_bindgen::prelude::*;
6 use wasm_bindgen_test::*;
7 
8 #[wasm_bindgen(module = "tests/wasm/import_class.js")]
9 extern "C" {
math_log(f: f64) -> f6410     fn math_log(f: f64) -> f64;
11 
12     #[wasm_bindgen(js_namespace = StaticFunction)]
bar() -> u3213     fn bar() -> u32;
14 
15     #[derive(Clone)]
16     type Construct;
17     #[wasm_bindgen(js_namespace = Construct)]
create() -> Construct18     fn create() -> Construct;
19     #[wasm_bindgen(method)]
get_internal_string(this: &Construct) -> String20     fn get_internal_string(this: &Construct) -> String;
21     #[wasm_bindgen(method)]
append_to_internal_string(this: &Construct, s: &str)22     fn append_to_internal_string(this: &Construct, s: &str);
23     #[wasm_bindgen(method)]
assert_internal_string(this: &Construct, s: &str)24     fn assert_internal_string(this: &Construct, s: &str);
25 
26     type NewConstructors;
27     #[wasm_bindgen(constructor)]
new(arg: i32) -> NewConstructors28     fn new(arg: i32) -> NewConstructors;
29     #[wasm_bindgen(method)]
get(this: &NewConstructors) -> i3230     fn get(this: &NewConstructors) -> i32;
31 
32     #[wasm_bindgen(js_name = default)]
33     type RenamedTypes;
34     #[wasm_bindgen(constructor, js_class = default)]
new(arg: i32) -> RenamedTypes35     fn new(arg: i32) -> RenamedTypes;
36     #[wasm_bindgen(method, js_class = default)]
get(this: &RenamedTypes) -> i3237     fn get(this: &RenamedTypes) -> i32;
38 
switch_methods_a()39     fn switch_methods_a();
switch_methods_b()40     fn switch_methods_b();
41     type SwitchMethods;
42     #[wasm_bindgen(constructor)]
43     #[wasm_bindgen(final)]
new() -> SwitchMethods44     fn new() -> SwitchMethods;
45     #[wasm_bindgen(js_namespace = SwitchMethods, final)]
a()46     fn a();
switch_methods_called() -> bool47     fn switch_methods_called() -> bool;
48     #[wasm_bindgen(method, final)]
b(this: &SwitchMethods)49     fn b(this: &SwitchMethods);
50 
51     type Properties;
52     #[wasm_bindgen(constructor)]
new() -> Properties53     fn new() -> Properties;
54     #[wasm_bindgen(getter, method)]
a(this: &Properties) -> i3255     fn a(this: &Properties) -> i32;
56     #[wasm_bindgen(setter, method)]
set_a(this: &Properties, a: i32)57     fn set_a(this: &Properties, a: i32);
58 
59     type RenameProperties;
60     #[wasm_bindgen(constructor)]
new() -> RenameProperties61     fn new() -> RenameProperties;
62     #[wasm_bindgen(getter = a, method)]
test(this: &RenameProperties) -> i3263     fn test(this: &RenameProperties) -> i32;
64     #[wasm_bindgen(getter, method, js_name = a)]
test2(this: &RenameProperties) -> i3265     fn test2(this: &RenameProperties) -> i32;
66     #[wasm_bindgen(setter = a, method)]
another(this: &RenameProperties, a: i32)67     fn another(this: &RenameProperties, a: i32);
68     #[wasm_bindgen(setter, method, js_name = a)]
another2(this: &RenameProperties, a: i32)69     fn another2(this: &RenameProperties, a: i32);
70 
71     /// dox
72     pub type AssertImportDenyDocsWorks;
73     /// dox
74     #[wasm_bindgen(constructor)]
new() -> AssertImportDenyDocsWorks75     pub fn new() -> AssertImportDenyDocsWorks;
76     /// dox
77     #[wasm_bindgen(getter = a, method)]
test(this: &AssertImportDenyDocsWorks) -> i3278     pub fn test(this: &AssertImportDenyDocsWorks) -> i32;
79     /// dox
foo()80     pub fn foo();
81 
82     pub type Options;
83     #[wasm_bindgen(constructor)]
new() -> Options84     fn new() -> Options;
take_none(val: Option<Options>)85     fn take_none(val: Option<Options>);
take_some(val: Option<Options>)86     fn take_some(val: Option<Options>);
return_null() -> Option<Options>87     fn return_null() -> Option<Options>;
return_undefined() -> Option<Options>88     fn return_undefined() -> Option<Options>;
return_some() -> Option<Options>89     fn return_some() -> Option<Options>;
run_rust_option_tests()90     fn run_rust_option_tests();
91 
92     type CatchConstructors;
93     #[wasm_bindgen(constructor, catch)]
new(x: u32) -> Result<CatchConstructors, JsValue>94     fn new(x: u32) -> Result<CatchConstructors, JsValue>;
95 
96     type StaticStructural;
97     #[wasm_bindgen(static_method_of = StaticStructural, structural)]
static_structural(a: u32) -> u3298     fn static_structural(a: u32) -> u32;
99 
100     #[derive(Clone)]
101     type InnerClass;
102     #[wasm_bindgen(js_namespace = ["nestedNamespace", "InnerClass"])]
inner_static_function(a: u32) -> u32103     fn inner_static_function(a: u32) -> u32;
104     #[wasm_bindgen(js_namespace = ["nestedNamespace", "InnerClass"])]
create_inner_instance() -> InnerClass105     fn create_inner_instance() -> InnerClass;
106     #[wasm_bindgen(method)]
get_internal_int(this: &InnerClass) -> u32107     fn get_internal_int(this: &InnerClass) -> u32;
108     #[wasm_bindgen(method)]
append_to_internal_int(this: &InnerClass, i: u32)109     fn append_to_internal_int(this: &InnerClass, i: u32);
110     #[wasm_bindgen(method)]
assert_internal_int(this: &InnerClass, i: u32)111     fn assert_internal_int(this: &InnerClass, i: u32);
112 }
113 
114 #[wasm_bindgen]
115 extern "C" {
116     #[wasm_bindgen(js_namespace = Math)]
random() -> f64117     fn random() -> f64;
118     #[wasm_bindgen(js_namespace = Math)]
log(a: f64) -> f64119     fn log(a: f64) -> f64;
120 }
121 
122 #[wasm_bindgen_test]
simple()123 fn simple() {
124     random();
125     assert_eq!(log(1.0), math_log(1.0));
126 }
127 
128 #[wasm_bindgen_test]
import_class()129 fn import_class() {
130     assert_eq!(bar(), 2);
131 }
132 
133 #[wasm_bindgen_test]
construct()134 fn construct() {
135     let f = Construct::create();
136     assert_eq!(f.get_internal_string(), "this");
137     assert_eq!(f.clone().get_internal_string(), "this");
138     f.append_to_internal_string(" foo");
139     f.assert_internal_string("this foo");
140 }
141 
142 #[wasm_bindgen_test]
new_constructors()143 fn new_constructors() {
144     let f = NewConstructors::new(1);
145     assert_eq!(f.get(), 2);
146 }
147 
148 #[wasm_bindgen_test]
rename_type()149 fn rename_type() {
150     let f = RenamedTypes::new(1);
151     assert_eq!(f.get(), 2);
152 }
153 
154 #[wasm_bindgen_test]
155 #[cfg(ignored)] // TODO: fix this before landing
switch_methods()156 fn switch_methods() {
157     assert!(!switch_methods_called());
158     SwitchMethods::a();
159     assert!(switch_methods_called());
160 
161     switch_methods_a();
162 
163     assert!(!switch_methods_called());
164     SwitchMethods::a();
165     assert!(switch_methods_called());
166 
167     assert!(!switch_methods_called());
168     SwitchMethods::new().b();
169     assert!(switch_methods_called());
170 
171     switch_methods_b();
172 
173     assert!(!switch_methods_called());
174     SwitchMethods::new().b();
175     assert!(!switch_methods_called());
176 }
177 
178 #[wasm_bindgen_test]
properties()179 fn properties() {
180     let a = Properties::new();
181     assert_eq!(a.a(), 1);
182     a.set_a(2);
183     assert_eq!(a.a(), 2);
184 }
185 
186 #[wasm_bindgen_test]
rename_setter_getter()187 fn rename_setter_getter() {
188     let x: fn() -> RenameProperties = RenameProperties::new;
189     let a = x();
190     assert_eq!(a.test(), 1);
191     a.another(2);
192     assert_eq!(a.test(), 2);
193     a.another2(3);
194     assert_eq!(a.test2(), 3);
195 }
196 
197 /// dox
198 #[wasm_bindgen]
199 pub struct AssertDenyDocsWorks {
200     /// dox
201     pub a: u32,
202     _b: i64,
203 }
204 
205 /// dox
206 #[wasm_bindgen]
assert_deny_docs_works()207 pub fn assert_deny_docs_works() {}
208 
209 #[wasm_bindgen_test]
options()210 fn options() {
211     take_none(None);
212     take_some(Some(Options::new()));
213     assert!(return_null().is_none());
214     assert!(return_undefined().is_none());
215     assert!(return_some().is_some());
216     run_rust_option_tests();
217 }
218 
219 /// doc
220 #[wasm_bindgen]
rust_take_none(a: Option<Options>)221 pub fn rust_take_none(a: Option<Options>) {
222     assert!(a.is_none());
223 }
224 
225 /// doc
226 #[wasm_bindgen]
rust_take_some(a: Option<Options>)227 pub fn rust_take_some(a: Option<Options>) {
228     assert!(a.is_some());
229 }
230 
231 /// doc
232 #[wasm_bindgen]
rust_return_none() -> Option<Options>233 pub fn rust_return_none() -> Option<Options> {
234     None
235 }
236 
237 /// doc
238 #[wasm_bindgen]
rust_return_some() -> Option<Options>239 pub fn rust_return_some() -> Option<Options> {
240     Some(Options::new())
241 }
242 
243 #[wasm_bindgen_test]
catch_constructors()244 fn catch_constructors() {
245     assert!(CatchConstructors::new(0).is_err());
246     assert!(CatchConstructors::new(1).is_ok());
247 }
248 
249 #[wasm_bindgen_test]
static_structural()250 fn static_structural() {
251     assert_eq!(StaticStructural::static_structural(30), 33);
252 }
253 
254 #[wasm_bindgen_test]
nested_namespace()255 fn nested_namespace() {
256     assert_eq!(InnerClass::inner_static_function(15), 20);
257 
258     let f = InnerClass::create_inner_instance();
259     assert_eq!(f.get_internal_int(), 3);
260     assert_eq!(f.clone().get_internal_int(), 3);
261     f.append_to_internal_int(5);
262     f.assert_internal_int(8);
263 }
264