Éric Lemoine | 7127271 | 2016-11-08 12:53:51 +0000 | [diff] [blame] | 1 | -- Copyright 2015 Mirantis, Inc. |
| 2 | -- |
| 3 | -- Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | -- you may not use this file except in compliance with the License. |
| 5 | -- You may obtain a copy of the License at |
| 6 | -- |
| 7 | -- http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | -- |
| 9 | -- Unless required by applicable law or agreed to in writing, software |
| 10 | -- distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | -- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | -- See the License for the specific language governing permissions and |
| 13 | -- limitations under the License. |
| 14 | |
| 15 | EXPORT_ASSERT_TO_GLOBALS=true |
| 16 | require('luaunit') |
| 17 | package.path = package.path .. ";../heka/files/lua/common/?.lua;lua/mocks/?.lua" |
| 18 | |
| 19 | local table_utils = require('table_utils') |
| 20 | |
| 21 | TestTableUtils = {} |
| 22 | |
| 23 | function TestTableUtils:setUp() |
| 24 | self.array = { 'a', 'b', 'c' } |
| 25 | self.dict = { c='C', a='A', b='B' } |
| 26 | end |
| 27 | |
| 28 | function TestTableUtils:test_item_pos_with_match() |
| 29 | assertEquals(table_utils.item_pos('b', self.array), 2) |
| 30 | end |
| 31 | |
| 32 | function TestTableUtils:test_item_pos_without_match() |
| 33 | assertEquals(table_utils.item_pos('z', self.array), nil) |
| 34 | end |
| 35 | |
| 36 | function TestTableUtils:test_item_find_with_match() |
| 37 | assertEquals(table_utils.item_find('b', self.array), true) |
| 38 | end |
| 39 | |
| 40 | function TestTableUtils:test_item_find_without_match() |
| 41 | assertEquals(table_utils.item_find('z', self.array), false) |
| 42 | end |
| 43 | |
| 44 | function TestTableUtils:test_deep_copy() |
| 45 | local copy = table_utils.deepcopy(self.array) |
| 46 | assertEquals(#copy, #self.array) |
| 47 | assertEquals(copy[1], self.array[1]) |
| 48 | assertEquals(copy[2], self.array[2]) |
| 49 | assertEquals(copy[3], self.array[3]) |
| 50 | assert(copy ~= self.array) |
| 51 | end |
| 52 | |
| 53 | function TestTableUtils:test_orderedPairs() |
| 54 | local t = {} |
| 55 | for k,v in table_utils.orderedPairs(self.dict) do |
| 56 | t[#t+1] = { k=k, v=v } |
| 57 | end |
| 58 | assertEquals(#t, 3) |
| 59 | assertEquals(t[1].k, 'a') |
| 60 | assertEquals(t[1].v, 'A') |
| 61 | assertEquals(t[2].k, 'b') |
| 62 | assertEquals(t[2].v, 'B') |
| 63 | assertEquals(t[3].k, 'c') |
| 64 | assertEquals(t[3].v, 'C') |
| 65 | end |
| 66 | |
| 67 | function TestTableUtils:test_table_equal_with_equal_keys_and_values() |
| 68 | assertTrue(table_utils.table_equal({a = 'a', b = 'b'}, {a = 'a', b = 'b'})) |
| 69 | end |
| 70 | |
| 71 | function TestTableUtils:test_table_equal_with_nonequal_values() |
| 72 | assertFalse(table_utils.table_equal({a = 'a', b = 'b'}, {a = 'a', b = 'c'})) |
| 73 | end |
| 74 | |
| 75 | function TestTableUtils:test_table_equal_with_nonequal_keys_1() |
| 76 | assertFalse(table_utils.table_equal({a = 'a', b = 'b'}, {a = 'a', c = 'b'})) |
| 77 | end |
| 78 | |
| 79 | function TestTableUtils:test_table_equal_with_nonequal_keys_2() |
| 80 | assertFalse(table_utils.table_equal({a = 'a', b = 'b'}, |
| 81 | {a = 'a', b = 'b', c = 'c'})) |
| 82 | end |
| 83 | |
| 84 | lu = LuaUnit |
| 85 | lu:setVerbosity( 1 ) |
| 86 | os.exit( lu:run() ) |