1 local iter
= require
"util.iterators";
3 describe("util.iterators", function ()
4 describe("join", function ()
5 it("should produce a joined iterator", function ()
6 local expect
= { "a", "b", "c", 1, 2, 3 };
8 for x
in iter
.join(iter
.values({"a", "b", "c"})):append(iter
.values({1, 2, 3})) do
9 table.insert(output
, x
);
11 assert.same(output
, expect
);
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
22 assert(k
> last_key
, "Expected "..k
.." > "..last_key
)
24 assert.equal(orig
[k
], v
);
27 assert.equal("d", last_key
);
31 it("should allow a custom sort function", function ()
32 local orig
= { b
= 1, c
= 2, a
= "foo", d
= false };
33 local n
, last_key
= 0, nil;
34 for k
, v
in iter
.sorted_pairs(orig
, function (a
, b
) return a
> b
end) do
37 assert(k
< last_key
, "Expected "..k
.." > "..last_key
)
39 assert.equal(orig
[k
], v
);
42 assert.equal("a", last_key
);