aboutsummaryrefslogtreecommitdiffstats
path: root/spec
diff options
context:
space:
mode:
authorMatthew Wild <mwild1@gmail.com>2018-09-21 14:27:46 +0100
committerMatthew Wild <mwild1@gmail.com>2018-09-21 14:27:46 +0100
commit5947c028a9e5d586796b273538795bc1cc14785e (patch)
tree24eec4723dfbfd740e1a6bc75f204e7066265922 /spec
parent74cb650790e018d95e8f1cb3735b369620845c22 (diff)
downloadprosody-5947c028a9e5d586796b273538795bc1cc14785e.tar.gz
prosody-5947c028a9e5d586796b273538795bc1cc14785e.zip
util.iterators: Add sorted_pairs() method
Diffstat (limited to 'spec')
-rw-r--r--spec/util_iterators_spec.lua30
1 files changed, 30 insertions, 0 deletions
diff --git a/spec/util_iterators_spec.lua b/spec/util_iterators_spec.lua
index d00058f4..c819b2f0 100644
--- a/spec/util_iterators_spec.lua
+++ b/spec/util_iterators_spec.lua
@@ -11,4 +11,34 @@ describe("util.iterators", function ()
assert.same(output, expect);
end);
end);
+
+ describe("sorted_pairs", function ()
+ it("should produce sorted pairs", function ()
+ local orig = { b = 1, c = 2, a = "foo", d = false };
+ local n, last_key = 0, nil;
+ for k, v in iter.sorted_pairs(orig) do
+ n = n + 1;
+ if last_key then
+ assert(k > last_key, "Expected "..k.." > "..last_key)
+ end
+ last_key = k;
+ end
+ assert.equal("d", last_key);
+ assert.equal(4, n);
+ end);
+
+ it("should allow a custom sort function", function ()
+ local orig = { b = 1, c = 2, a = "foo", d = false };
+ local n, last_key = 0, nil;
+ for k, v in iter.sorted_pairs(orig, function (a, b) return a > b end) do
+ n = n + 1;
+ if last_key then
+ assert(k < last_key, "Expected "..k.." > "..last_key)
+ end
+ last_key = k;
+ end
+ assert.equal("a", last_key);
+ assert.equal(4, n);
+ end);
+ end);
end);