Software /
code /
prosody
Comparison
spec/util_iterators_spec.lua @ 9327:f6f1dec164b5
util.iterators: Add sorted_pairs() method
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Fri, 21 Sep 2018 14:27:46 +0100 |
parent | 8805:82d68951ec2a |
child | 9328:a9592107021b |
comparison
equal
deleted
inserted
replaced
9326:c9c4b8bc53b1 | 9327:f6f1dec164b5 |
---|---|
9 table.insert(output, x); | 9 table.insert(output, x); |
10 end | 10 end |
11 assert.same(output, expect); | 11 assert.same(output, expect); |
12 end); | 12 end); |
13 end); | 13 end); |
14 | |
15 describe("sorted_pairs", function () | |
16 it("should produce sorted pairs", function () | |
17 local orig = { b = 1, c = 2, a = "foo", d = false }; | |
18 local n, last_key = 0, nil; | |
19 for k, v in iter.sorted_pairs(orig) do | |
20 n = n + 1; | |
21 if last_key then | |
22 assert(k > last_key, "Expected "..k.." > "..last_key) | |
23 end | |
24 last_key = k; | |
25 end | |
26 assert.equal("d", last_key); | |
27 assert.equal(4, n); | |
28 end); | |
29 | |
30 it("should allow a custom sort function", function () | |
31 local orig = { b = 1, c = 2, a = "foo", d = false }; | |
32 local n, last_key = 0, nil; | |
33 for k, v in iter.sorted_pairs(orig, function (a, b) return a > b end) do | |
34 n = n + 1; | |
35 if last_key then | |
36 assert(k < last_key, "Expected "..k.." > "..last_key) | |
37 end | |
38 last_key = k; | |
39 end | |
40 assert.equal("a", last_key); | |
41 assert.equal(4, n); | |
42 end); | |
43 end); | |
14 end); | 44 end); |