From c666f752fa80632ce6a73c73a478d15fa5e72077 Mon Sep 17 00:00:00 2001 From: Anton Danilkin Date: Mon, 13 Aug 2018 23:18:16 +0300 Subject: [PATCH] Add OptionalAndUnionArguments test --- crates/webidl-tests/simple.js | 9 ++++++++- crates/webidl-tests/simple.rs | 17 +++++++++++++++-- crates/webidl-tests/simple.webidl | 12 +++++++++++- crates/webidl/src/lib.rs | 5 ++++- crates/webidl/src/util.rs | 6 +++++- 5 files changed, 43 insertions(+), 6 deletions(-) diff --git a/crates/webidl-tests/simple.js b/crates/webidl-tests/simple.js index 7dbd45b4..0c63082b 100644 --- a/crates/webidl-tests/simple.js +++ b/crates/webidl-tests/simple.js @@ -67,7 +67,7 @@ global.UndefinedMethod = class UndefinedMethod { } }; -global.OptionalMethod = class OptionalMethod { +global.NullableMethod = class NullableMethod { constructor() {} opt(a) { if (a == undefined) { @@ -107,6 +107,13 @@ global.Indexing = function () { }); }; +global.OptionalAndUnionArguments = class OptionalAndUnionArguments { + constructor() {} + m(a, b = true, c = 123, d = 456) { + return [typeof a, a, typeof b, b, typeof c, c, typeof d, d].join(', '); + } +}; + global.PartialInterface = class PartialInterface { get un() { return 1; diff --git a/crates/webidl-tests/simple.rs b/crates/webidl-tests/simple.rs index 1230bfbc..b06d9dd4 100644 --- a/crates/webidl-tests/simple.rs +++ b/crates/webidl-tests/simple.rs @@ -56,8 +56,8 @@ fn one_method_using_an_undefined_import_doesnt_break_all_other_methods() { } #[wasm_bindgen_test] -fn optional_method() { - let f = OptionalMethod::new().unwrap(); +fn nullable_method() { + let f = NullableMethod::new().unwrap(); assert!(f.opt(Some(15)) == Some(16)); assert!(f.opt(None) == None); } @@ -78,6 +78,19 @@ fn indexing() { assert_eq!(f.get(123), -1); } +#[wasm_bindgen_test] +fn optional_and_union_arguments() { + let f = OptionalAndUnionArguments::new().unwrap(); + assert_eq!(f.m_using_a("abc"), "string, abc, boolean, true, number, 123, number, 456"); + assert_eq!(f.m_using_a_and_b("abc", false), "string, abc, boolean, false, number, 123, number, 456"); + assert_eq!(f.m_using_dom_str_and_bool_and_i16("abc", false, 5), "string, abc, boolean, false, number, 5, number, 456"); + assert_eq!(f.m_using_dom_str_and_bool_and_dom_str("abc", false, "5"), "string, abc, boolean, false, string, 5, number, 456"); + assert_eq!(f.m_using_dom_str_and_bool_and_i16_and_opt_i64("abc", false, 5, Some(10)), "string, abc, boolean, false, number, 5, bigint, 10"); + assert_eq!(f.m_using_dom_str_and_bool_and_i16_and_opt_bool("abc", false, 5, Some(true)), "string, abc, boolean, false, number, 5, boolean, true"); + assert_eq!(f.m_using_dom_str_and_bool_and_dom_str_and_opt_i64("abc", false, "5", Some(10)), "string, abc, boolean, false, string, 5, bigint, 10"); + assert_eq!(f.m_using_dom_str_and_bool_and_dom_str_and_opt_bool("abc", false, "5", Some(true)), "string, abc, boolean, false, string, 5, boolean, true"); +} + #[wasm_bindgen_test] fn unforgeable_is_structural() { let f = Unforgeable::new().unwrap(); diff --git a/crates/webidl-tests/simple.webidl b/crates/webidl-tests/simple.webidl index 8435c024..6df6de63 100644 --- a/crates/webidl-tests/simple.webidl +++ b/crates/webidl-tests/simple.webidl @@ -31,7 +31,7 @@ interface UndefinedMethod { }; [Constructor()] -interface OptionalMethod { +interface NullableMethod { octet? opt(short? a); }; @@ -47,6 +47,16 @@ interface Indexing { deleter void (unsigned long index); }; +[Constructor()] +interface OptionalAndUnionArguments { + DOMString m( + DOMString a, + optional boolean b = true, + optional (short or DOMString) c = 123, + optional (long long or boolean)? d = 456 + ); +}; + [Constructor()] interface Unforgeable { [Unforgeable] readonly attribute short uno; diff --git a/crates/webidl/src/lib.rs b/crates/webidl/src/lib.rs index 7cbae081..23163f9f 100644 --- a/crates/webidl/src/lib.rs +++ b/crates/webidl/src/lib.rs @@ -789,7 +789,10 @@ impl<'src> WebidlParse<'src, &'src str> for weedle::interface::ConstMember<'src> }; let ty = match idl_type.to_syn_type(TypePosition::Return) { - None => return Ok(()), + None => { + warn!("Can not convert const type to syn type: {:?}", idl_type); + return Ok(()); + }, Some(ty) => ty, }; diff --git a/crates/webidl/src/util.rs b/crates/webidl/src/util.rs index eecd403b..0722d2c8 100644 --- a/crates/webidl/src/util.rs +++ b/crates/webidl/src/util.rs @@ -234,7 +234,10 @@ impl<'src> FirstPassRecord<'src> { IdlType::Void => None, ret @ _ => { match ret.to_syn_type(TypePosition::Return) { - None => return Vec::new(), + None => { + warn!("Can not convert return type to syn type: {:?}", ret); + return Vec::new(); + }, Some(ret) => Some(ret), } }, @@ -312,6 +315,7 @@ impl<'src> FirstPassRecord<'src> { let syn_type = if let Some(syn_type) = idl_type.to_syn_type(TypePosition::Argument) { syn_type } else { + warn!("Can not convert argument type to syn type: {:?}", idl_type); continue 'outer; }; let argument_name = rust_ident(&argument_name.to_snake_case());