summaryrefslogtreecommitdiffstats
path: root/vendor/js-sys/tests/wasm/Set.rs
blob: 045e192e6dde538688c4bf47accd55ce30d6d07f (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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
use js_sys::*;
use wasm_bindgen::prelude::*;
use wasm_bindgen::JsCast;
use wasm_bindgen_test::*;

fn set2vec(s: &Set) -> Vec<JsValue> {
    let mut result = Vec::new();
    s.for_each(&mut |x, _, _| result.push(x));
    return result;
}

#[wasm_bindgen_test]
fn add() {
    let set = Set::new(&JsValue::undefined());
    set.add(&100.into());
    assert_eq!(set.size(), 1);
    assert_eq!(set2vec(&set)[0], 100);
}

#[wasm_bindgen_test]
fn clear() {
    let set = Set::new(&JsValue::undefined());
    set.add(&1.into());
    set.add(&2.into());
    set.add(&3.into());
    assert_eq!(set.size(), 3);
    set.clear();
    assert_eq!(set.size(), 0);
}

#[wasm_bindgen_test]
fn delete() {
    let set = Set::new(&JsValue::undefined());
    set.add(&1.into());
    set.add(&2.into());
    set.add(&3.into());

    assert!(set.delete(&3.into()));
    assert!(!set.delete(&3.into()));
    assert!(!set.delete(&4.into()));
}

#[wasm_bindgen_test]
fn for_each() {
    let set = Set::new(&JsValue::undefined());
    set.add(&1.into());
    set.add(&2.into());
    set.add(&3.into());

    let v = set2vec(&set);
    assert_eq!(v.len(), 3);
    assert!(v.iter().any(|v| *v == 1));
    assert!(v.iter().any(|v| *v == 2));
    assert!(v.iter().any(|v| *v == 3));
}

#[wasm_bindgen_test]
fn has() {
    let set = Set::new(&JsValue::undefined());
    set.add(&1.into());
    set.add(&2.into());
    set.add(&3.into());

    assert!(set.has(&1.into()));
    assert!(!set.has(&1.3.into()));
}

#[wasm_bindgen_test]
fn new() {
    assert_eq!(Set::new(&JsValue::undefined()).size(), 0);
}

#[wasm_bindgen_test]
fn size() {
    let set = Set::new(&JsValue::undefined());
    assert_eq!(set.size(), 0);
    set.add(&1.into());
    assert_eq!(set.size(), 1);
    set.add(&2.into());
    assert_eq!(set.size(), 2);
    set.add(&3.into());
    assert_eq!(set.size(), 3);
}

#[wasm_bindgen_test]
fn set_inheritance() {
    let set = Set::new(&JsValue::undefined());
    assert!(set.is_instance_of::<Set>());
    assert!(set.is_instance_of::<Object>());
    let _: &Object = set.as_ref();
}

#[wasm_bindgen_test]
fn keys() {
    let set = Set::new(&JsValue::undefined());
    set.add(&1.into());
    set.add(&2.into());
    set.add(&3.into());

    let list = set
        .keys()
        .into_iter()
        .map(|e| e.unwrap())
        .collect::<Vec<_>>();
    assert_eq!(list.len(), 3);
    assert!(list.iter().any(|l| *l == 1));
    assert!(list.iter().any(|l| *l == 2));
    assert!(list.iter().any(|l| *l == 3));
}

#[wasm_bindgen_test]
fn values() {
    let set = Set::new(&JsValue::undefined());
    set.add(&1.into());
    set.add(&2.into());
    set.add(&3.into());

    let list = set
        .values()
        .into_iter()
        .map(|e| e.unwrap())
        .collect::<Vec<_>>();
    assert_eq!(list.len(), 3);
    assert!(list.iter().any(|l| *l == 1));
    assert!(list.iter().any(|l| *l == 2));
    assert!(list.iter().any(|l| *l == 3));
}