Annotate

tests/test_util_multitable.lua @ 2365:0bd8408a491c

prosody: Fixed a possible nil concatenation.
author Waqas Hussain <waqas20@gmail.com>
date Wed, 16 Dec 2009 00:11:54 +0500
parent 1523:841d61be198f
child 2923:b7049746bd29
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1523
841d61be198f Remove version number from copyright headers
Matthew Wild <mwild1@gmail.com>
parents: 896
diff changeset
1 -- Prosody IM
797
6ea01e05b004 Add tests for util.multitable
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2 -- Copyright (C) 2008-2009 Matthew Wild
6ea01e05b004 Add tests for util.multitable
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 -- Copyright (C) 2008-2009 Waqas Hussain
6ea01e05b004 Add tests for util.multitable
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 --
6ea01e05b004 Add tests for util.multitable
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 -- This project is MIT/X11 licensed. Please see the
6ea01e05b004 Add tests for util.multitable
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 -- COPYING file in the source package for more information.
6ea01e05b004 Add tests for util.multitable
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 --
6ea01e05b004 Add tests for util.multitable
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8
6ea01e05b004 Add tests for util.multitable
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9
6ea01e05b004 Add tests for util.multitable
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 function new(new, multitable)
6ea01e05b004 Add tests for util.multitable
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 mt = new();
6ea01e05b004 Add tests for util.multitable
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 assert_table(mt, "Multitable is a table");
6ea01e05b004 Add tests for util.multitable
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 assert_function(mt.add, "Multitable has method add");
6ea01e05b004 Add tests for util.multitable
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 assert_function(mt.get, "Multitable has method get");
6ea01e05b004 Add tests for util.multitable
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 assert_function(mt.remove, "Multitable has method remove");
6ea01e05b004 Add tests for util.multitable
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16
6ea01e05b004 Add tests for util.multitable
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 get(mt.get, multitable);
6ea01e05b004 Add tests for util.multitable
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 end
6ea01e05b004 Add tests for util.multitable
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19
6ea01e05b004 Add tests for util.multitable
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 function get(get, multitable)
6ea01e05b004 Add tests for util.multitable
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 local function has_items(list, ...)
6ea01e05b004 Add tests for util.multitable
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 local should_have = {};
6ea01e05b004 Add tests for util.multitable
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 if select('#', ...) > 0 then
6ea01e05b004 Add tests for util.multitable
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 assert_table(list, "has_items: list is table", 3);
6ea01e05b004 Add tests for util.multitable
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 else
6ea01e05b004 Add tests for util.multitable
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 assert_is_not(list and #list > 0, "No items, and no list");
6ea01e05b004 Add tests for util.multitable
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 return true, "has-all";
6ea01e05b004 Add tests for util.multitable
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 end
6ea01e05b004 Add tests for util.multitable
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 for n=1,select('#', ...) do should_have[select(n, ...)] = true; end
6ea01e05b004 Add tests for util.multitable
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 for n, item in ipairs(list) do
6ea01e05b004 Add tests for util.multitable
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 if not should_have[item] then return false, "too-many"; end
6ea01e05b004 Add tests for util.multitable
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 should_have[item] = nil;
6ea01e05b004 Add tests for util.multitable
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 end
6ea01e05b004 Add tests for util.multitable
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 if next(should_have) then
6ea01e05b004 Add tests for util.multitable
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 return false, "not-enough";
6ea01e05b004 Add tests for util.multitable
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 end
6ea01e05b004 Add tests for util.multitable
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 return true, "has-all";
6ea01e05b004 Add tests for util.multitable
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 end
6ea01e05b004 Add tests for util.multitable
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 local function assert_has_all(message, list, ...)
6ea01e05b004 Add tests for util.multitable
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 return assert_equal(select(2, has_items(list, ...)), "has-all", message or "List has all expected items, and no more", 2);
6ea01e05b004 Add tests for util.multitable
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 end
6ea01e05b004 Add tests for util.multitable
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42
6ea01e05b004 Add tests for util.multitable
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 mt = multitable.new();
6ea01e05b004 Add tests for util.multitable
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44
6ea01e05b004 Add tests for util.multitable
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 local trigger1, trigger2, trigger3 = {}, {}, {};
6ea01e05b004 Add tests for util.multitable
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 local item1, item2, item3 = {}, {}, {};
6ea01e05b004 Add tests for util.multitable
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47
6ea01e05b004 Add tests for util.multitable
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 assert_has_all("Has no items with trigger1", mt:get(trigger1));
6ea01e05b004 Add tests for util.multitable
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49
6ea01e05b004 Add tests for util.multitable
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50
6ea01e05b004 Add tests for util.multitable
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 mt:add(1, 2, 3, item1);
6ea01e05b004 Add tests for util.multitable
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52
6ea01e05b004 Add tests for util.multitable
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 assert_has_all("Has item1 for 1, 2, 3", mt:get(1, 2, 3), item1);
6ea01e05b004 Add tests for util.multitable
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54
6ea01e05b004 Add tests for util.multitable
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 -- Doesn't support nil
6ea01e05b004 Add tests for util.multitable
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 --[[ mt:add(nil, item1);
6ea01e05b004 Add tests for util.multitable
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 mt:add(nil, item2);
6ea01e05b004 Add tests for util.multitable
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58 mt:add(nil, item3);
6ea01e05b004 Add tests for util.multitable
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59
6ea01e05b004 Add tests for util.multitable
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60 assert_has_all("Has all items with (nil)", mt:get(nil), item1, item2, item3);
6ea01e05b004 Add tests for util.multitable
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 ]]
6ea01e05b004 Add tests for util.multitable
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62 end