summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/IndexedDB/idbkeyrange-includes.htm
blob: 96769ae0a909f1234893331e4d3c4c8ceb832873 (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
128
129
130
131
132
133
134
135
136
137
138
139
140
<!doctype html>
<meta charset=utf-8>
<title>IndexedDB: IDBKeyRange.includes()</title>
<meta name="help" href="https://w3c.github.io/IndexedDB/#keyrange">
<script src=/resources/testharness.js></script>
<script src=/resources/testharnessreport.js></script>
<script>

test(function() {
  var range = IDBKeyRange.bound(12, 34);
  assert_throws_js(TypeError, function() { range.includes(); },
      'throws if key is not specified');

  assert_throws_dom('DataError', function() { range.includes(undefined); },
      'throws if key is undefined');
  assert_throws_dom('DataError', function() { range.includes(null); },
      'throws if key is null');
  assert_throws_dom('DataError', function() { range.includes({}); },
      'throws if key is not valid type');
  assert_throws_dom('DataError', function() { range.includes(NaN); },
      'throws if key is not valid number');
  assert_throws_dom('DataError', function() { range.includes(new Date(NaN)); },
      'throws if key is not valid date');
  assert_throws_dom('DataError', function() {
      var a = []; a[0] = a; range.includes(a);
  }, 'throws if key is not valid array');
}, "IDBKeyRange.includes() with invalid input");

test(function() {
  var closedRange = IDBKeyRange.bound(5, 20);
  assert_true(!!closedRange.includes, "IDBKeyRange has a .includes");
  assert_true(closedRange.includes(7), "in range");
  assert_false(closedRange.includes(1), "below range");
  assert_false(closedRange.includes(42), "above range");
  assert_true(closedRange.includes(5.01), "at the lower end of the range");
  assert_true(closedRange.includes(19.99), "at the upper end of the range");
  assert_false(closedRange.includes(4.99), "right below range");
  assert_false(closedRange.includes(21.01), "right above range");

  assert_true(closedRange.includes(5), "lower boundary");
  assert_true(closedRange.includes(20), "upper boundary");
}, "IDBKeyRange.includes() with a closed range");

test(function() {
  var closedRange = IDBKeyRange.bound(5, 20, true, true);
  assert_true(closedRange.includes(7), "in range");
  assert_false(closedRange.includes(1), "below range");
  assert_false(closedRange.includes(42), "above range");
  assert_true(closedRange.includes(5.01), "at the lower end of the range");
  assert_true(closedRange.includes(19.99), "at the upper end of the range");
  assert_false(closedRange.includes(4.99), "right below range");
  assert_false(closedRange.includes(21.01), "right above range");

  assert_false(closedRange.includes(5), "lower boundary");
  assert_false(closedRange.includes(20), "upper boundary");
}, "IDBKeyRange.includes() with an open range");

test(function() {
  var range = IDBKeyRange.bound(5, 20, true);
  assert_true(range.includes(7), "in range");
  assert_false(range.includes(1), "below range");
  assert_false(range.includes(42), "above range");
  assert_true(range.includes(5.01), "at the lower end of the range");
  assert_true(range.includes(19.99), "at the upper end of the range");
  assert_false(range.includes(4.99), "right below range");
  assert_false(range.includes(21.01), "right above range");

  assert_false(range.includes(5), "lower boundary");
  assert_true(range.includes(20), "upper boundary");
}, "IDBKeyRange.includes() with a lower-open upper-closed range");

test(function() {
  var range = IDBKeyRange.bound(5, 20, false, true);
  assert_true(range.includes(7), "in range");
  assert_false(range.includes(1), "below range");
  assert_false(range.includes(42), "above range");
  assert_true(range.includes(5.01), "at the lower end of the range");
  assert_true(range.includes(19.99), "at the upper end of the range");
  assert_false(range.includes(4.99), "right below range");
  assert_false(range.includes(21.01), "right above range");

  assert_true(range.includes(5), "lower boundary");
  assert_false(range.includes(20), "upper boundary");
}, "IDBKeyRange.includes() with a lower-closed upper-open range");

test(function() {
  var onlyRange = IDBKeyRange.only(42);
  assert_true(onlyRange.includes(42), "in range");
  assert_false(onlyRange.includes(1), "below range");
  assert_false(onlyRange.includes(9000), "above range");
  assert_false(onlyRange.includes(41), "right below range");
  assert_false(onlyRange.includes(43), "right above range");
}, "IDBKeyRange.includes() with an only range");

test(function() {
  var range = IDBKeyRange.lowerBound(5);
  assert_false(range.includes(4), 'value before closed lower bound');
  assert_true(range.includes(5), 'value at closed lower bound');
  assert_true(range.includes(6), 'value after closed lower bound');
  assert_true(range.includes(42), 'value way after open lower bound');
}, "IDBKeyRange.includes() with an closed lower-bounded range");

test(function() {
  var range = IDBKeyRange.lowerBound(5, true);
  assert_false(range.includes(4), 'value before open lower bound');
  assert_false(range.includes(5), 'value at open lower bound');
  assert_true(range.includes(6), 'value after open lower bound');
  assert_true(range.includes(42), 'value way after open lower bound');
}, "IDBKeyRange.includes() with an open lower-bounded range");

test(function() {
  var range = IDBKeyRange.upperBound(5);
  assert_true(range.includes(-42), 'value way before closed upper bound');
  assert_true(range.includes(4), 'value before closed upper bound');
  assert_true(range.includes(5), 'value at closed upper bound');
  assert_false(range.includes(6), 'value after closed upper bound');
}, "IDBKeyRange.includes() with an closed upper-bounded range");

test(function() {
  var range = IDBKeyRange.upperBound(5, true);
  assert_true(range.includes(-42), 'value way before closed upper bound');
  assert_true(range.includes(4), 'value before open upper bound');
  assert_false(range.includes(5), 'value at open upper bound');
  assert_false(range.includes(6), 'value after open upper bound');
}, "IDBKeyRange.includes() with an open upper-bounded range");

test(function(t) {
    assert_true(IDBKeyRange.bound(new Date(0), new Date())
        .includes(new Date(102729600000)));
    assert_false(IDBKeyRange.bound(new Date(0), new Date(1e11))
        .includes(new Date(1e11 + 1)));

    assert_true(IDBKeyRange.bound('a', 'c').includes('b'));
    assert_false(IDBKeyRange.bound('a', 'c').includes('d'));

    assert_true(IDBKeyRange.bound([], [[], []]).includes([[]]));
    assert_false(IDBKeyRange.bound([], [[]]).includes([[[]]]));
}, 'IDBKeyRange.includes() with non-numeric keys');

</script>