Software /
code /
prosody
Annotate
spec/util_cache_spec.lua @ 12912:44a78985471f
mod_saslauth: Support for SASL handlers forcing a specific resource
The token layer supports tokens that are tied to a given resource.
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Wed, 01 Mar 2023 12:56:08 +0000 |
parent | 12771:e9fcc69ea508 |
child | 13174:8ec7b7d6556f |
rev | line source |
---|---|
8236
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
1 |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
2 local cache = require "util.cache"; |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
3 |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
4 describe("util.cache", function() |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
5 describe("#new()", function() |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
6 it("should work", function() |
12771
e9fcc69ea508
util.cache: Add some missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
12571
diff
changeset
|
7 do |
e9fcc69ea508
util.cache: Add some missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
12571
diff
changeset
|
8 local c = cache.new(1); |
e9fcc69ea508
util.cache: Add some missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
12571
diff
changeset
|
9 assert.is_not_nil(c); |
e9fcc69ea508
util.cache: Add some missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
12571
diff
changeset
|
10 |
e9fcc69ea508
util.cache: Add some missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
12571
diff
changeset
|
11 assert.has_error(function () |
e9fcc69ea508
util.cache: Add some missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
12571
diff
changeset
|
12 cache.new(0); |
e9fcc69ea508
util.cache: Add some missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
12571
diff
changeset
|
13 end); |
e9fcc69ea508
util.cache: Add some missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
12571
diff
changeset
|
14 assert.has_error(function () |
e9fcc69ea508
util.cache: Add some missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
12571
diff
changeset
|
15 cache.new(-1); |
e9fcc69ea508
util.cache: Add some missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
12571
diff
changeset
|
16 end); |
e9fcc69ea508
util.cache: Add some missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
12571
diff
changeset
|
17 assert.has_error(function () |
e9fcc69ea508
util.cache: Add some missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
12571
diff
changeset
|
18 cache.new("foo"); |
e9fcc69ea508
util.cache: Add some missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
12571
diff
changeset
|
19 end); |
e9fcc69ea508
util.cache: Add some missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
12571
diff
changeset
|
20 end |
8236
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
21 |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
22 local c = cache.new(5); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
23 |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
24 local function expect_kv(key, value, actual_key, actual_value) |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
25 assert.are.equal(key, actual_key, "key incorrect"); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
26 assert.are.equal(value, actual_value, "value incorrect"); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
27 end |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
28 |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
29 expect_kv(nil, nil, c:head()); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
30 expect_kv(nil, nil, c:tail()); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
31 |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
32 assert.are.equal(c:count(), 0); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
33 |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
34 c:set("one", 1) |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
35 assert.are.equal(c:count(), 1); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
36 expect_kv("one", 1, c:head()); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
37 expect_kv("one", 1, c:tail()); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
38 |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
39 c:set("two", 2) |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
40 expect_kv("two", 2, c:head()); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
41 expect_kv("one", 1, c:tail()); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
42 |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
43 c:set("three", 3) |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
44 expect_kv("three", 3, c:head()); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
45 expect_kv("one", 1, c:tail()); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
46 |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
47 c:set("four", 4) |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
48 c:set("five", 5); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
49 assert.are.equal(c:count(), 5); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
50 expect_kv("five", 5, c:head()); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
51 expect_kv("one", 1, c:tail()); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
52 |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
53 c:set("foo", nil); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
54 assert.are.equal(c:count(), 5); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
55 expect_kv("five", 5, c:head()); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
56 expect_kv("one", 1, c:tail()); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
57 |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
58 assert.are.equal(c:get("one"), 1); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
59 expect_kv("five", 5, c:head()); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
60 expect_kv("one", 1, c:tail()); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
61 |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
62 assert.are.equal(c:get("two"), 2); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
63 assert.are.equal(c:get("three"), 3); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
64 assert.are.equal(c:get("four"), 4); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
65 assert.are.equal(c:get("five"), 5); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
66 |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
67 assert.are.equal(c:get("foo"), nil); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
68 assert.are.equal(c:get("bar"), nil); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
69 |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
70 c:set("six", 6); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
71 assert.are.equal(c:count(), 5); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
72 expect_kv("six", 6, c:head()); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
73 expect_kv("two", 2, c:tail()); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
74 |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
75 assert.are.equal(c:get("one"), nil); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
76 assert.are.equal(c:get("two"), 2); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
77 assert.are.equal(c:get("three"), 3); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
78 assert.are.equal(c:get("four"), 4); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
79 assert.are.equal(c:get("five"), 5); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
80 assert.are.equal(c:get("six"), 6); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
81 |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
82 c:set("three", nil); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
83 assert.are.equal(c:count(), 4); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
84 |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
85 assert.are.equal(c:get("one"), nil); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
86 assert.are.equal(c:get("two"), 2); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
87 assert.are.equal(c:get("three"), nil); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
88 assert.are.equal(c:get("four"), 4); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
89 assert.are.equal(c:get("five"), 5); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
90 assert.are.equal(c:get("six"), 6); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
91 |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
92 c:set("seven", 7); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
93 assert.are.equal(c:count(), 5); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
94 |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
95 assert.are.equal(c:get("one"), nil); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
96 assert.are.equal(c:get("two"), 2); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
97 assert.are.equal(c:get("three"), nil); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
98 assert.are.equal(c:get("four"), 4); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
99 assert.are.equal(c:get("five"), 5); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
100 assert.are.equal(c:get("six"), 6); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
101 assert.are.equal(c:get("seven"), 7); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
102 |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
103 c:set("eight", 8); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
104 assert.are.equal(c:count(), 5); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
105 |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
106 assert.are.equal(c:get("one"), nil); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
107 assert.are.equal(c:get("two"), nil); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
108 assert.are.equal(c:get("three"), nil); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
109 assert.are.equal(c:get("four"), 4); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
110 assert.are.equal(c:get("five"), 5); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
111 assert.are.equal(c:get("six"), 6); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
112 assert.are.equal(c:get("seven"), 7); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
113 assert.are.equal(c:get("eight"), 8); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
114 |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
115 c:set("four", 4); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
116 assert.are.equal(c:count(), 5); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
117 |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
118 assert.are.equal(c:get("one"), nil); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
119 assert.are.equal(c:get("two"), nil); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
120 assert.are.equal(c:get("three"), nil); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
121 assert.are.equal(c:get("four"), 4); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
122 assert.are.equal(c:get("five"), 5); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
123 assert.are.equal(c:get("six"), 6); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
124 assert.are.equal(c:get("seven"), 7); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
125 assert.are.equal(c:get("eight"), 8); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
126 |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
127 c:set("nine", 9); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
128 assert.are.equal(c:count(), 5); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
129 |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
130 assert.are.equal(c:get("one"), nil); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
131 assert.are.equal(c:get("two"), nil); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
132 assert.are.equal(c:get("three"), nil); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
133 assert.are.equal(c:get("four"), 4); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
134 assert.are.equal(c:get("five"), nil); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
135 assert.are.equal(c:get("six"), 6); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
136 assert.are.equal(c:get("seven"), 7); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
137 assert.are.equal(c:get("eight"), 8); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
138 assert.are.equal(c:get("nine"), 9); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
139 |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
140 do |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
141 local keys = { "nine", "four", "eight", "seven", "six" }; |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
142 local values = { 9, 4, 8, 7, 6 }; |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
143 local i = 0; |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
144 for k, v in c:items() do |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
145 i = i + 1; |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
146 assert.are.equal(k, keys[i]); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
147 assert.are.equal(v, values[i]); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
148 end |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
149 assert.are.equal(i, 5); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
150 |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
151 c:set("four", "2+2"); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
152 assert.are.equal(c:count(), 5); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
153 |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
154 assert.are.equal(c:get("one"), nil); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
155 assert.are.equal(c:get("two"), nil); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
156 assert.are.equal(c:get("three"), nil); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
157 assert.are.equal(c:get("four"), "2+2"); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
158 assert.are.equal(c:get("five"), nil); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
159 assert.are.equal(c:get("six"), 6); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
160 assert.are.equal(c:get("seven"), 7); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
161 assert.are.equal(c:get("eight"), 8); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
162 assert.are.equal(c:get("nine"), 9); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
163 end |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
164 |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
165 do |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
166 local keys = { "four", "nine", "eight", "seven", "six" }; |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
167 local values = { "2+2", 9, 8, 7, 6 }; |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
168 local i = 0; |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
169 for k, v in c:items() do |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
170 i = i + 1; |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
171 assert.are.equal(k, keys[i]); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
172 assert.are.equal(v, values[i]); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
173 end |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
174 assert.are.equal(i, 5); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
175 |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
176 c:set("foo", nil); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
177 assert.are.equal(c:count(), 5); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
178 |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
179 assert.are.equal(c:get("one"), nil); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
180 assert.are.equal(c:get("two"), nil); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
181 assert.are.equal(c:get("three"), nil); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
182 assert.are.equal(c:get("four"), "2+2"); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
183 assert.are.equal(c:get("five"), nil); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
184 assert.are.equal(c:get("six"), 6); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
185 assert.are.equal(c:get("seven"), 7); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
186 assert.are.equal(c:get("eight"), 8); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
187 assert.are.equal(c:get("nine"), 9); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
188 end |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
189 |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
190 do |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
191 local keys = { "four", "nine", "eight", "seven", "six" }; |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
192 local values = { "2+2", 9, 8, 7, 6 }; |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
193 local i = 0; |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
194 for k, v in c:items() do |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
195 i = i + 1; |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
196 assert.are.equal(k, keys[i]); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
197 assert.are.equal(v, values[i]); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
198 end |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
199 assert.are.equal(i, 5); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
200 |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
201 c:set("four", nil); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
202 |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
203 assert.are.equal(c:get("one"), nil); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
204 assert.are.equal(c:get("two"), nil); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
205 assert.are.equal(c:get("three"), nil); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
206 assert.are.equal(c:get("four"), nil); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
207 assert.are.equal(c:get("five"), nil); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
208 assert.are.equal(c:get("six"), 6); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
209 assert.are.equal(c:get("seven"), 7); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
210 assert.are.equal(c:get("eight"), 8); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
211 assert.are.equal(c:get("nine"), 9); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
212 end |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
213 |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
214 do |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
215 local keys = { "nine", "eight", "seven", "six" }; |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
216 local values = { 9, 8, 7, 6 }; |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
217 local i = 0; |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
218 for k, v in c:items() do |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
219 i = i + 1; |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
220 assert.are.equal(k, keys[i]); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
221 assert.are.equal(v, values[i]); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
222 end |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
223 assert.are.equal(i, 4); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
224 end |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
225 |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
226 do |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
227 local evicted_key, evicted_value; |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
228 local c2 = cache.new(3, function (_key, _value) |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
229 evicted_key, evicted_value = _key, _value; |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
230 end); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
231 local function set(k, v, should_evict_key, should_evict_value) |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
232 evicted_key, evicted_value = nil, nil; |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
233 c2:set(k, v); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
234 assert.are.equal(evicted_key, should_evict_key); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
235 assert.are.equal(evicted_value, should_evict_value); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
236 end |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
237 set("a", 1) |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
238 set("a", 1) |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
239 set("a", 1) |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
240 set("a", 1) |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
241 set("a", 1) |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
242 |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
243 set("b", 2) |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
244 set("c", 3) |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
245 set("b", 2) |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
246 set("d", 4, "a", 1) |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
247 set("e", 5, "c", 3) |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
248 end |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
249 |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
250 do |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
251 local evicted_key, evicted_value; |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
252 local c3 = cache.new(1, function (_key, _value) |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
253 evicted_key, evicted_value = _key, _value; |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
254 if _key == "a" then |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
255 -- Sanity check for what we're evicting |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
256 assert.are.equal(_key, "a"); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
257 assert.are.equal(_value, 1); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
258 -- We're going to block eviction of this key/value, so set to nil... |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
259 evicted_key, evicted_value = nil, nil; |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
260 -- Returning false to block eviction |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
261 return false |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
262 end |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
263 end); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
264 local function set(k, v, should_evict_key, should_evict_value) |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
265 evicted_key, evicted_value = nil, nil; |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
266 local ret = c3:set(k, v); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
267 assert.are.equal(evicted_key, should_evict_key); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
268 assert.are.equal(evicted_value, should_evict_value); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
269 return ret; |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
270 end |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
271 set("a", 1) |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
272 set("a", 1) |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
273 set("a", 1) |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
274 set("a", 1) |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
275 set("a", 1) |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
276 |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
277 -- Our on_evict prevents "a" from being evicted, causing this to fail... |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
278 assert.are.equal(set("b", 2), false, "Failed to prevent eviction, or signal result"); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
279 |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
280 expect_kv("a", 1, c3:head()); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
281 expect_kv("a", 1, c3:tail()); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
282 |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
283 -- Check the final state is what we expect |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
284 assert.are.equal(c3:get("a"), 1); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
285 assert.are.equal(c3:get("b"), nil); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
286 assert.are.equal(c3:count(), 1); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
287 end |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
288 |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
289 |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
290 local c4 = cache.new(3, false); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
291 |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
292 assert.are.equal(c4:set("a", 1), true); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
293 assert.are.equal(c4:set("a", 1), true); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
294 assert.are.equal(c4:set("a", 1), true); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
295 assert.are.equal(c4:set("a", 1), true); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
296 assert.are.equal(c4:set("b", 2), true); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
297 assert.are.equal(c4:set("c", 3), true); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
298 assert.are.equal(c4:set("d", 4), false); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
299 assert.are.equal(c4:set("d", 4), false); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
300 assert.are.equal(c4:set("d", 4), false); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
301 |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
302 expect_kv("c", 3, c4:head()); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
303 expect_kv("a", 1, c4:tail()); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
304 |
9323
3259653512e7
util.cache tests: Add annotation to fix [luacheck] warning
Matthew Wild <mwild1@gmail.com>
parents:
8236
diff
changeset
|
305 local c5 = cache.new(3, function (k, v) --luacheck: ignore 212/v |
8236
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
306 if k == "a" then |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
307 return nil; |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
308 elseif k == "b" then |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
309 return true; |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
310 end |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
311 return false; |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
312 end); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
313 |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
314 assert.are.equal(c5:set("a", 1), true); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
315 assert.are.equal(c5:set("a", 1), true); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
316 assert.are.equal(c5:set("a", 1), true); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
317 assert.are.equal(c5:set("a", 1), true); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
318 assert.are.equal(c5:set("b", 2), true); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
319 assert.are.equal(c5:set("c", 3), true); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
320 assert.are.equal(c5:set("d", 4), true); -- "a" evicted (cb returned nil) |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
321 assert.are.equal(c5:set("d", 4), true); -- nop |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
322 assert.are.equal(c5:set("d", 4), true); -- nop |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
323 assert.are.equal(c5:set("e", 5), true); -- "b" evicted (cb returned true) |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
324 assert.are.equal(c5:set("f", 6), false); -- "c" won't evict (cb returned false) |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
325 |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
326 expect_kv("e", 5, c5:head()); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
327 expect_kv("c", 3, c5:tail()); |
11366
618ab9bba1c2
util.cache: Add test for :table (fails on Lua 5.1)
Kim Alvefur <zash@zash.se>
parents:
9323
diff
changeset
|
328 |
618ab9bba1c2
util.cache: Add test for :table (fails on Lua 5.1)
Kim Alvefur <zash@zash.se>
parents:
9323
diff
changeset
|
329 end); |
618ab9bba1c2
util.cache: Add test for :table (fails on Lua 5.1)
Kim Alvefur <zash@zash.se>
parents:
9323
diff
changeset
|
330 |
12571
c4337ff4f1c4
tests: Remove special-casing of Lua 5.1
Kim Alvefur <zash@zash.se>
parents:
11366
diff
changeset
|
331 it(":table works", function () |
11366
618ab9bba1c2
util.cache: Add test for :table (fails on Lua 5.1)
Kim Alvefur <zash@zash.se>
parents:
9323
diff
changeset
|
332 local t = cache.new(3):table(); |
618ab9bba1c2
util.cache: Add test for :table (fails on Lua 5.1)
Kim Alvefur <zash@zash.se>
parents:
9323
diff
changeset
|
333 assert.is.table(t); |
618ab9bba1c2
util.cache: Add test for :table (fails on Lua 5.1)
Kim Alvefur <zash@zash.se>
parents:
9323
diff
changeset
|
334 t["a"] = "1"; |
618ab9bba1c2
util.cache: Add test for :table (fails on Lua 5.1)
Kim Alvefur <zash@zash.se>
parents:
9323
diff
changeset
|
335 assert.are.equal(t["a"], "1"); |
618ab9bba1c2
util.cache: Add test for :table (fails on Lua 5.1)
Kim Alvefur <zash@zash.se>
parents:
9323
diff
changeset
|
336 t["b"] = "2"; |
618ab9bba1c2
util.cache: Add test for :table (fails on Lua 5.1)
Kim Alvefur <zash@zash.se>
parents:
9323
diff
changeset
|
337 assert.are.equal(t["b"], "2"); |
618ab9bba1c2
util.cache: Add test for :table (fails on Lua 5.1)
Kim Alvefur <zash@zash.se>
parents:
9323
diff
changeset
|
338 t["c"] = "3"; |
618ab9bba1c2
util.cache: Add test for :table (fails on Lua 5.1)
Kim Alvefur <zash@zash.se>
parents:
9323
diff
changeset
|
339 assert.are.equal(t["c"], "3"); |
618ab9bba1c2
util.cache: Add test for :table (fails on Lua 5.1)
Kim Alvefur <zash@zash.se>
parents:
9323
diff
changeset
|
340 t["d"] = "4"; |
618ab9bba1c2
util.cache: Add test for :table (fails on Lua 5.1)
Kim Alvefur <zash@zash.se>
parents:
9323
diff
changeset
|
341 assert.are.equal(t["d"], "4"); |
618ab9bba1c2
util.cache: Add test for :table (fails on Lua 5.1)
Kim Alvefur <zash@zash.se>
parents:
9323
diff
changeset
|
342 assert.are.equal(t["a"], nil); |
618ab9bba1c2
util.cache: Add test for :table (fails on Lua 5.1)
Kim Alvefur <zash@zash.se>
parents:
9323
diff
changeset
|
343 |
618ab9bba1c2
util.cache: Add test for :table (fails on Lua 5.1)
Kim Alvefur <zash@zash.se>
parents:
9323
diff
changeset
|
344 local i = spy.new(function () end); |
618ab9bba1c2
util.cache: Add test for :table (fails on Lua 5.1)
Kim Alvefur <zash@zash.se>
parents:
9323
diff
changeset
|
345 for k, v in pairs(t) do |
618ab9bba1c2
util.cache: Add test for :table (fails on Lua 5.1)
Kim Alvefur <zash@zash.se>
parents:
9323
diff
changeset
|
346 i(k,v) |
618ab9bba1c2
util.cache: Add test for :table (fails on Lua 5.1)
Kim Alvefur <zash@zash.se>
parents:
9323
diff
changeset
|
347 end |
618ab9bba1c2
util.cache: Add test for :table (fails on Lua 5.1)
Kim Alvefur <zash@zash.se>
parents:
9323
diff
changeset
|
348 assert.spy(i).was_called(); |
618ab9bba1c2
util.cache: Add test for :table (fails on Lua 5.1)
Kim Alvefur <zash@zash.se>
parents:
9323
diff
changeset
|
349 assert.spy(i).was_called_with("b", "2"); |
618ab9bba1c2
util.cache: Add test for :table (fails on Lua 5.1)
Kim Alvefur <zash@zash.se>
parents:
9323
diff
changeset
|
350 assert.spy(i).was_called_with("c", "3"); |
618ab9bba1c2
util.cache: Add test for :table (fails on Lua 5.1)
Kim Alvefur <zash@zash.se>
parents:
9323
diff
changeset
|
351 assert.spy(i).was_called_with("d", "4"); |
8236
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
352 end); |
12771
e9fcc69ea508
util.cache: Add some missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
12571
diff
changeset
|
353 |
e9fcc69ea508
util.cache: Add some missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
12571
diff
changeset
|
354 local function vs(t) |
e9fcc69ea508
util.cache: Add some missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
12571
diff
changeset
|
355 local vs_ = {}; |
e9fcc69ea508
util.cache: Add some missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
12571
diff
changeset
|
356 for v in t:values() do |
e9fcc69ea508
util.cache: Add some missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
12571
diff
changeset
|
357 vs_[#vs_+1] = v; |
e9fcc69ea508
util.cache: Add some missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
12571
diff
changeset
|
358 end |
e9fcc69ea508
util.cache: Add some missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
12571
diff
changeset
|
359 return vs_; |
e9fcc69ea508
util.cache: Add some missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
12571
diff
changeset
|
360 end |
e9fcc69ea508
util.cache: Add some missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
12571
diff
changeset
|
361 |
e9fcc69ea508
util.cache: Add some missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
12571
diff
changeset
|
362 it(":values works", function () |
e9fcc69ea508
util.cache: Add some missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
12571
diff
changeset
|
363 local t = cache.new(3); |
e9fcc69ea508
util.cache: Add some missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
12571
diff
changeset
|
364 t:set("k1", "v1"); |
e9fcc69ea508
util.cache: Add some missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
12571
diff
changeset
|
365 t:set("k2", "v2"); |
e9fcc69ea508
util.cache: Add some missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
12571
diff
changeset
|
366 assert.same({"v2", "v1"}, vs(t)); |
e9fcc69ea508
util.cache: Add some missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
12571
diff
changeset
|
367 t:set("k3", "v3"); |
e9fcc69ea508
util.cache: Add some missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
12571
diff
changeset
|
368 assert.same({"v3", "v2", "v1"}, vs(t)); |
e9fcc69ea508
util.cache: Add some missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
12571
diff
changeset
|
369 t:set("k4", "v4"); |
e9fcc69ea508
util.cache: Add some missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
12571
diff
changeset
|
370 assert.same({"v4", "v3", "v2"}, vs(t)); |
e9fcc69ea508
util.cache: Add some missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
12571
diff
changeset
|
371 end); |
e9fcc69ea508
util.cache: Add some missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
12571
diff
changeset
|
372 |
e9fcc69ea508
util.cache: Add some missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
12571
diff
changeset
|
373 it(":resize works", function () |
e9fcc69ea508
util.cache: Add some missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
12571
diff
changeset
|
374 local c = cache.new(5); |
e9fcc69ea508
util.cache: Add some missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
12571
diff
changeset
|
375 for i = 1, 5 do |
e9fcc69ea508
util.cache: Add some missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
12571
diff
changeset
|
376 c:set(("k%d"):format(i), ("v%d"):format(i)); |
e9fcc69ea508
util.cache: Add some missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
12571
diff
changeset
|
377 end |
e9fcc69ea508
util.cache: Add some missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
12571
diff
changeset
|
378 assert.same({"v5", "v4", "v3", "v2", "v1"}, vs(c)); |
e9fcc69ea508
util.cache: Add some missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
12571
diff
changeset
|
379 assert.has_error(function () |
e9fcc69ea508
util.cache: Add some missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
12571
diff
changeset
|
380 c:resize(-1); |
e9fcc69ea508
util.cache: Add some missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
12571
diff
changeset
|
381 end); |
e9fcc69ea508
util.cache: Add some missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
12571
diff
changeset
|
382 assert.has_error(function () |
e9fcc69ea508
util.cache: Add some missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
12571
diff
changeset
|
383 c:resize(0); |
e9fcc69ea508
util.cache: Add some missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
12571
diff
changeset
|
384 end); |
e9fcc69ea508
util.cache: Add some missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
12571
diff
changeset
|
385 assert.has_error(function () |
e9fcc69ea508
util.cache: Add some missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
12571
diff
changeset
|
386 c:resize("foo"); |
e9fcc69ea508
util.cache: Add some missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
12571
diff
changeset
|
387 end); |
e9fcc69ea508
util.cache: Add some missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
12571
diff
changeset
|
388 c:resize(3); |
e9fcc69ea508
util.cache: Add some missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
12571
diff
changeset
|
389 assert.same({"v5", "v4", "v3"}, vs(c)); |
e9fcc69ea508
util.cache: Add some missing test cases
Matthew Wild <mwild1@gmail.com>
parents:
12571
diff
changeset
|
390 end); |
8236
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
391 end); |
4878e4159e12
Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
392 end); |