Skip to content

Commit

Permalink
Add more checking
Browse files Browse the repository at this point in the history
  • Loading branch information
daxpedda committed Nov 28, 2024
1 parent 67531f5 commit 94172f4
Show file tree
Hide file tree
Showing 38 changed files with 237 additions and 303 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ jobs:
- run: rustup update --no-self-update stable && rustup default stable
- run: rustup target add wasm32-unknown-unknown
- run: cargo clippy --no-deps --all-features -p wasm-bindgen-backend -- -D warnings
- run: cargo clippy --no-deps --all-features -p wasm-bindgen -- -D warnings
- run: cargo clippy --no-deps --all-features -p wasm-bindgen-cli -- -D warnings
- run: cargo clippy --no-deps --all-features -p wasm-bindgen-cli-support -- -D warnings
- run: cargo clippy --no-deps --all-features -p example-tests -- -D warnings
Expand Down Expand Up @@ -89,8 +90,8 @@ jobs:
- uses: actions/checkout@v4
- run: rustup update --no-self-update stable && rustup default stable
- run: rustup target add wasm32-unknown-unknown
- run: cargo clippy --no-deps --all-features --target wasm32-unknown-unknown -p js-sys -- -D warnings
- run: cargo clippy --no-deps --all-features --target wasm32-unknown-unknown -p web-sys -- -D warnings
- run: cargo clippy --no-deps --all-features --target wasm32-unknown-unknown -p js-sys --all-targets -- -D warnings
- run: cargo clippy --no-deps --all-features --target wasm32-unknown-unknown -p web-sys --all-targets -- -D warnings

# Run `cargo clippy` over crates that support `no_std`
clippy_no_std:
Expand Down
10 changes: 0 additions & 10 deletions crates/cli-support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -812,13 +812,3 @@ where
pairs.sort_by_key(|(k, _)| *k);
pairs.into_iter()
}

/// Like `sorted_iter`, but produces mutable references to the values
fn sorted_iter_mut<K, V>(map: &mut HashMap<K, V>) -> impl Iterator<Item = (&K, &mut V)>
where
K: Ord,
{
let mut pairs = map.iter_mut().collect::<Vec<_>>();
pairs.sort_by_key(|(k, _)| *k);
pairs.into_iter()
}
22 changes: 10 additions & 12 deletions crates/js-sys/tests/wasm/Array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ macro_rules! js_array {

macro_rules! array {
($($e:expr),*) => ({
let mut __x = Vec::new();
$(__x.push(JsValue::from($e));)*
__x
vec![$(JsValue::from($e)),*]
})
}

Expand All @@ -38,7 +36,7 @@ fn from_iter() {

assert_eq!(
to_rust(
&vec![JsValue::from("a"), JsValue::from("b"), JsValue::from("c"),]
&[JsValue::from("a"), JsValue::from("b"), JsValue::from("c")]
.iter()
.collect()
),
Expand All @@ -53,7 +51,7 @@ fn from_iter() {
);

assert_eq!(
to_rust(&vec![array.clone(),].iter().collect()),
to_rust(&[array.clone()].iter().collect()),
vec![JsValue::from(array)],
);

Expand All @@ -74,7 +72,7 @@ fn from_iter() {
let v = vec!["a", "b", "c"];

assert_eq!(
to_rust(&Array::from_iter(v.into_iter().map(|s| JsValue::from(s)))),
to_rust(&Array::from_iter(v.into_iter().map(JsValue::from))),
vec!["a", "b", "c"],
);
}
Expand Down Expand Up @@ -290,6 +288,7 @@ fn sort() {
}

#[wasm_bindgen_test]
#[allow(clippy::cmp_owned)]
fn some() {
let array = js_array!["z", 1, "y", 2];
assert!(array.some(&mut |e| e == JsValue::from(2)));
Expand Down Expand Up @@ -553,7 +552,7 @@ fn find_last_index() {
fn to_locale_string() {
let output = js_array![1, "a", Date::new(&"21 Dec 1997 14:12:00 UTC".into())]
.to_locale_string(&"en".into(), &JsValue::undefined());
assert!(String::from(output).len() > 0);
assert!(!String::from(output).is_empty());
}

#[wasm_bindgen_test]
Expand All @@ -567,7 +566,7 @@ fn for_each() {
res
}

assert_eq!(sum_indices_of_evens(&js_array![2, 4, 6, 8]), 0 + 1 + 2 + 3);
assert_eq!(sum_indices_of_evens(&js_array![2, 4, 6, 8]), 1 + 2 + 3);
assert_eq!(sum_indices_of_evens(&js_array![1, 3, 5, 7]), 0);
assert_eq!(sum_indices_of_evens(&js_array![3, 5, 7, 10]), 3);
}
Expand All @@ -578,7 +577,7 @@ fn set_length() {
array.set_length(3);
assert_eq!(
array.iter().collect::<Vec<_>>(),
[1.0, 2.0, 3.0].map(|x| JsValue::from_f64(x))
[1.0, 2.0, 3.0].map(JsValue::from_f64)
);

array.set_length(7);
Expand All @@ -587,7 +586,7 @@ fn set_length() {
[1.0, 2.0, 3.0]
.iter()
.copied()
.map(|x| JsValue::from_f64(x))
.map(JsValue::from_f64)
.chain([JsValue::UNDEFINED; 4])
.collect::<Vec<_>>()
);
Expand Down Expand Up @@ -620,8 +619,7 @@ fn test_array_view_mut_raw<ElemT: std::cmp::PartialEq + std::fmt::Debug, ArrT>(
let start: u8 = 10;
let len: usize = 32;
let end: u8 = start + len as u8;
let mut buffer: Vec<ElemT> = Vec::new();
buffer.reserve(len);
let mut buffer: Vec<ElemT> = Vec::with_capacity(len);
unsafe {
let array: ArrT = sut(buffer.as_mut_ptr(), len);
populate_array(
Expand Down
4 changes: 2 additions & 2 deletions crates/js-sys/tests/wasm/Boolean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ use wasm_bindgen_test::*;
#[allow(deprecated)]
#[wasm_bindgen_test]
fn new_undefined() {
assert_eq!(Boolean::new(&JsValue::undefined()).value_of(), false);
assert!(!Boolean::new(&JsValue::undefined()).value_of());
}

#[allow(deprecated)]
#[wasm_bindgen_test]
fn new_truly() {
assert_eq!(Boolean::new(&JsValue::from("foo")).value_of(), true);
assert!(Boolean::new(&JsValue::from("foo")).value_of());
}

#[allow(deprecated)]
Expand Down
10 changes: 5 additions & 5 deletions crates/js-sys/tests/wasm/Intl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,15 +118,15 @@ fn relative_time_format() {
let opts = Object::new();

let c = Intl::RelativeTimeFormat::new(&locales, &opts);
assert!(c.format(1_f64.into(), &"seconds").is_string());
assert!(c.format(1_f64, "seconds").is_string());
assert!(c
.format_to_parts(1_f64.into(), &"seconds")
.format_to_parts(1_f64, "seconds")
.is_instance_of::<Array>());
assert!(c.resolved_options().is_instance_of::<Object>());

assert_eq!(c.format(1_f64.into(), &"seconds"), "in 1 second");
assert_eq!(c.format(1.5.into(), &"seconds"), "in 1.5 seconds");
assert_eq!(c.format((-1.5).into(), &"seconds"), "1.5 seconds ago");
assert_eq!(c.format(1_f64, "seconds"), "in 1 second");
assert_eq!(c.format(1.5, "seconds"), "in 1.5 seconds");
assert_eq!(c.format(-1.5, "seconds"), "1.5 seconds ago");

let a = Intl::RelativeTimeFormat::supported_locales_of(&locales, &opts);
assert!(a.is_instance_of::<Array>());
Expand Down
75 changes: 34 additions & 41 deletions crates/js-sys/tests/wasm/JsString.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![cfg(test)]

use js_sys::*;
use wasm_bindgen::prelude::*;
use wasm_bindgen_test::*;
Expand Down Expand Up @@ -69,9 +71,9 @@ fn ends_with() {
let js = JsString::from(s);

// TODO: remove third parameter once we have optional parameters
assert_eq!(js.ends_with("question.", s.len() as i32), true);
assert_eq!(js.ends_with("to be", s.len() as i32), false);
assert_eq!(js.ends_with("to be", 19), true);
assert!(js.ends_with("question.", s.len() as i32));
assert!(!js.ends_with("to be", s.len() as i32));
assert!(js.ends_with("to be", 19));
}

#[wasm_bindgen_test]
Expand All @@ -93,7 +95,7 @@ fn from_char_code() {
let codes_u16: Vec<u16> = codes
.into_iter()
.map(|code| {
assert!(code <= u32::from(u16::max_value()));
assert!(code <= u32::from(u16::MAX));
code as u16
})
.collect();
Expand Down Expand Up @@ -121,23 +123,23 @@ fn from_code_point() {
);
assert_eq!(JsString::from_code_point(&codes).unwrap(), "☃★♲你");

assert!(!JsString::from_code_point1(0x10FFFF).is_err());
assert!(JsString::from_code_point1(0x10FFFF).is_ok());
assert!(JsString::from_code_point1(0x110000).is_err());
assert!(JsString::from_code_point1(u32::max_value()).is_err());
assert!(JsString::from_code_point1(u32::MAX).is_err());
}

#[wasm_bindgen_test]
fn includes() {
let str = JsString::from("Blue Whale");

// TODO: remove second parameter once we have optional parameters
assert_eq!(str.includes("Blue", 0), true);
assert_eq!(str.includes("Blute", 0), false);
assert_eq!(str.includes("Whale", 0), true);
assert_eq!(str.includes("Whale", 5), true);
assert_eq!(str.includes("Whale", 7), false);
assert_eq!(str.includes("", 0), true);
assert_eq!(str.includes("", 16), true);
assert!(str.includes("Blue", 0));
assert!(!str.includes("Blute", 0));
assert!(str.includes("Whale", 0));
assert!(str.includes("Whale", 5));
assert!(!str.includes("Whale", 7));
assert!(str.includes("", 0));
assert!(str.includes("", 16));
}

#[wasm_bindgen_test]
Expand Down Expand Up @@ -225,12 +227,12 @@ fn match_all() {
.unwrap();

let obj = &result[0];
assert_eq!(Reflect::get(obj.as_ref(), &"0".into()).unwrap(), "The");
assert_eq!(Reflect::get(obj.as_ref(), &"1".into()).unwrap(), "he");
assert_eq!(Reflect::get(obj, &"0".into()).unwrap(), "The");
assert_eq!(Reflect::get(obj, &"1".into()).unwrap(), "he");

let obj = &result[1];
assert_eq!(Reflect::get(obj.as_ref(), &"0".into()).unwrap(), "It");
assert_eq!(Reflect::get(obj.as_ref(), &"1".into()).unwrap(), "t");
assert_eq!(Reflect::get(obj, &"0".into()).unwrap(), "It");
assert_eq!(Reflect::get(obj, &"1".into()).unwrap(), "t");

let result: Vec<_> = JsString::from("foo")
.match_all(&re)
Expand All @@ -249,29 +251,20 @@ fn match_all() {

let obj = &result[0];
assert_eq!(
Reflect::get(obj.as_ref(), &"0".into()).unwrap(),
Reflect::get(obj, &"0".into()).unwrap(),
"see Chapter 3.4.5.1"
);
assert_eq!(
Reflect::get(obj.as_ref(), &"1".into()).unwrap(),
"Chapter 3.4.5.1"
);
assert_eq!(Reflect::get(obj.as_ref(), &"2".into()).unwrap(), ".1");
assert_eq!(Reflect::get(obj.as_ref(), &"index".into()).unwrap(), 22);
assert_eq!(Reflect::get(obj.as_ref(), &"input".into()).unwrap(), s);
assert_eq!(Reflect::get(obj, &"1".into()).unwrap(), "Chapter 3.4.5.1");
assert_eq!(Reflect::get(obj, &"2".into()).unwrap(), ".1");
assert_eq!(Reflect::get(obj, &"index".into()).unwrap(), 22);
assert_eq!(Reflect::get(obj, &"input".into()).unwrap(), s);

let obj = &result[1];
assert_eq!(
Reflect::get(obj.as_ref(), &"0".into()).unwrap(),
"see Chapter 3.1.4"
);
assert_eq!(
Reflect::get(obj.as_ref(), &"1".into()).unwrap(),
"Chapter 3.1.4"
);
assert_eq!(Reflect::get(obj.as_ref(), &"2".into()).unwrap(), ".4");
assert_eq!(Reflect::get(obj.as_ref(), &"index".into()).unwrap(), 48);
assert_eq!(Reflect::get(obj.as_ref(), &"input".into()).unwrap(), s);
assert_eq!(Reflect::get(obj, &"0".into()).unwrap(), "see Chapter 3.1.4");
assert_eq!(Reflect::get(obj, &"1".into()).unwrap(), "Chapter 3.1.4");
assert_eq!(Reflect::get(obj, &"2".into()).unwrap(), ".4");
assert_eq!(Reflect::get(obj, &"index".into()).unwrap(), 48);
assert_eq!(Reflect::get(obj, &"input".into()).unwrap(), s);
}

#[wasm_bindgen_test]
Expand Down Expand Up @@ -512,17 +505,17 @@ fn to_locale_lower_case() {
let js = JsString::from("Mozilla");
assert_eq!(js.to_locale_lower_case(None), "mozilla");
let s = JsString::from("\u{0130}");
assert_eq!(s.to_locale_lower_case(Some("tr".into())), "i");
assert_ne!(s.to_locale_lower_case(Some("en-US".into())), "i");
assert_eq!(s.to_locale_lower_case(Some("tr")), "i");
assert_ne!(s.to_locale_lower_case(Some("en-US")), "i");
}

#[wasm_bindgen_test]
fn to_locale_upper_case() {
let js = JsString::from("mozilla");
assert_eq!(js.to_locale_upper_case(None), "MOZILLA");
let s = JsString::from("i\u{0307}");
assert_eq!(s.to_locale_upper_case(Some("lt".into())), "I");
assert_ne!(s.to_locale_upper_case(Some("en-US".into())), "I");
assert_eq!(s.to_locale_upper_case(Some("lt")), "I");
assert_ne!(s.to_locale_upper_case(Some("en-US")), "I");
}

#[wasm_bindgen_test]
Expand Down Expand Up @@ -573,7 +566,7 @@ fn value_of() {
fn raw() {
let call_site = Object::new();
let raw = Array::of3(&"foo".into(), &"bar".into(), &"123".into());
Reflect::set(&call_site.as_ref(), &"raw".into(), &raw.into()).unwrap();
Reflect::set(call_site.as_ref(), &"raw".into(), &raw.into()).unwrap();
assert_eq!(
JsString::raw_2(&call_site, "5", "JavaScript").unwrap(),
"foo5barJavaScript123"
Expand Down
12 changes: 6 additions & 6 deletions crates/js-sys/tests/wasm/Map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ fn delete() {
let map = Map::new();
map.set(&"foo".into(), &"bar".into());
assert_eq!(map.size(), 1);
assert_eq!(map.delete(&"foo".into()), true);
assert_eq!(map.delete(&"bar".into()), false);
assert!(map.delete(&"foo".into()));
assert!(!map.delete(&"bar".into()));
assert_eq!(map.size(), 0);
}

Expand Down Expand Up @@ -63,8 +63,8 @@ fn get() {
fn has() {
let map = Map::new();
map.set(&"foo".into(), &"bar".into());
assert_eq!(map.has(&"foo".into()), true);
assert_eq!(map.has(&"bar".into()), false);
assert!(map.has(&"foo".into()));
assert!(!map.has(&"bar".into()));
}

#[wasm_bindgen_test]
Expand All @@ -76,8 +76,8 @@ fn new() {
fn set() {
let map = Map::new();
let new = map.set(&"foo".into(), &"bar".into());
assert_eq!(map.has(&"foo".into()), true);
assert_eq!(new.has(&"foo".into()), true);
assert!(map.has(&"foo".into()));
assert!(new.has(&"foo".into()));
}

#[wasm_bindgen_test]
Expand Down
6 changes: 3 additions & 3 deletions crates/js-sys/tests/wasm/MapIterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ fn entries() {
let entries = map.entries();

let next = entries.next().unwrap();
assert_eq!(next.done(), false);
assert!(!next.done());
assert!(next.value().is_object());
assert_eq!(Reflect::get(&next.value(), &0.into()).unwrap(), "uno");
assert_eq!(Reflect::get(&next.value(), &1.into()).unwrap(), 1);
Expand All @@ -27,7 +27,7 @@ fn keys() {
let keys = map.keys();

let next = keys.next().unwrap();
assert_eq!(next.done(), false);
assert!(!next.done());
assert_eq!(next.value(), "uno");

let next = keys.next().unwrap();
Expand All @@ -43,7 +43,7 @@ fn values() {
let values = map.values();

let next = values.next().unwrap();
assert_eq!(next.done(), false);
assert!(!next.done());
assert_eq!(next.value(), 1);

let next = values.next().unwrap();
Expand Down
Loading

0 comments on commit 94172f4

Please sign in to comment.