| É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 | function inject_message(msg) | 
|  | 20 | if msg == 'fail' then | 
|  | 21 | error('fail') | 
|  | 22 | end | 
|  | 23 | end | 
|  | 24 |  | 
|  | 25 | function inject_payload(payload_type, payload_name, data) | 
|  | 26 | if data == 'fail' then | 
|  | 27 | error('fail') | 
|  | 28 | end | 
|  | 29 | end | 
|  | 30 |  | 
|  | 31 | local lma_utils = require('lma_utils') | 
|  | 32 |  | 
|  | 33 | TestLmaUtils = {} | 
|  | 34 |  | 
|  | 35 | function TestLmaUtils:test_safe_json_encode_with_valid_data() | 
|  | 36 | local ret = lma_utils.safe_json_encode({}) | 
|  | 37 | assertEquals(ret, '{}') | 
|  | 38 | end | 
|  | 39 |  | 
|  | 40 | function TestLmaUtils:test_safe_inject_message_without_error() | 
|  | 41 | local ret, msg = lma_utils.safe_inject_message({}) | 
|  | 42 | assertEquals(ret, 0) | 
|  | 43 | assertEquals(msg, nil) | 
|  | 44 | end | 
|  | 45 |  | 
|  | 46 | function TestLmaUtils:test_safe_inject_message_with_error() | 
|  | 47 | local ret, msg = lma_utils.safe_inject_message('fail') | 
|  | 48 | assertEquals(ret, -1) | 
|  | 49 | assert(msg:match(': fail')) | 
|  | 50 | end | 
|  | 51 |  | 
|  | 52 | function TestLmaUtils:test_safe_inject_payload_without_error() | 
|  | 53 | local ret, msg = lma_utils.safe_inject_payload('txt', 'foo', {}) | 
|  | 54 | assertEquals(ret, 0) | 
|  | 55 | assertEquals(msg, nil) | 
|  | 56 | end | 
|  | 57 |  | 
|  | 58 | function TestLmaUtils:test_safe_inject_payload_with_error() | 
|  | 59 | local ret, msg = lma_utils.safe_inject_payload('txt', 'foo', 'fail') | 
|  | 60 | assertEquals(ret, -1) | 
|  | 61 | assert(msg:match(': fail')) | 
|  | 62 | end | 
|  | 63 |  | 
|  | 64 | function TestLmaUtils:test_truncate_with_small_string() | 
|  | 65 | local ret = lma_utils.truncate('foo', 10, '<BR/>') | 
|  | 66 | assertEquals(ret, 'foo') | 
|  | 67 | end | 
|  | 68 |  | 
|  | 69 | function TestLmaUtils:test_truncate_with_large_string() | 
|  | 70 | local ret = lma_utils.truncate('foo and long string', 10, '<BR/>') | 
|  | 71 | assertEquals(ret, 'foo and lo') | 
|  | 72 | end | 
|  | 73 |  | 
|  | 74 | function TestLmaUtils:test_truncate_with_one_delimiter() | 
|  | 75 | local ret = lma_utils.truncate('foo<BR/>longstring', 10, '<BR/>') | 
|  | 76 | assertEquals(ret, 'foo') | 
|  | 77 | end | 
|  | 78 |  | 
|  | 79 | function TestLmaUtils:test_truncate_with_several_delimiters_1() | 
|  | 80 | local ret = lma_utils.truncate('foo<BR/>bar<BR/>longstring', 10, '<BR/>') | 
|  | 81 | assertEquals(ret, 'foo') | 
|  | 82 | end | 
|  | 83 |  | 
|  | 84 | function TestLmaUtils:test_truncate_with_several_delimiters_2() | 
|  | 85 | local ret = lma_utils.truncate('foo<BR/>ba<BR/>longstring', 10, '<BR/>') | 
|  | 86 | assertEquals(ret, 'foo<BR/>ba') | 
|  | 87 | end | 
|  | 88 |  | 
|  | 89 | function TestLmaUtils:test_truncate_with_several_delimiters_3() | 
|  | 90 | local ret = lma_utils.truncate('foo<BR/>ba<BR/>long<BR/>string', 12, '<BR/>') | 
|  | 91 | assertEquals(ret, 'foo<BR/>ba') | 
|  | 92 | end | 
|  | 93 |  | 
| Simon Pasquier | e9d98ee | 2016-12-16 14:42:01 +0100 | [diff] [blame] | 94 | function TestLmaUtils:test_convert_to_sec() | 
|  | 95 | assertEquals(lma_utils.convert_to_sec(1000000001), 1) | 
|  | 96 | assertEquals(lma_utils.convert_to_sec(1999999999), 1) | 
|  | 97 | assertEquals(lma_utils.convert_to_sec(2000000001), 2) | 
|  | 98 | end | 
|  | 99 |  | 
| Simon Pasquier | 6f2f452 | 2017-06-26 11:06:23 +0200 | [diff] [blame] | 100 | function TestLmaUtils:test_convert_to_bool() | 
|  | 101 | assertEquals(lma_utils.convert_to_bool(true), true) | 
|  | 102 | assertEquals(lma_utils.convert_to_bool(0), false) | 
|  | 103 | assertEquals(lma_utils.convert_to_bool(1), true) | 
|  | 104 | assertEquals(lma_utils.convert_to_bool("false"), false) | 
|  | 105 | assertEquals(lma_utils.convert_to_bool("tRue"), true) | 
|  | 106 | assertEquals(lma_utils.convert_to_bool(nil), false) | 
|  | 107 | assertEquals(lma_utils.convert_to_bool(nil, true), true) | 
|  | 108 | assertEquals(lma_utils.convert_to_bool("false", true), false) | 
|  | 109 | end | 
|  | 110 |  | 
| Éric Lemoine | 7127271 | 2016-11-08 12:53:51 +0000 | [diff] [blame] | 111 | lu = LuaUnit | 
|  | 112 | lu:setVerbosity( 1 ) | 
|  | 113 | os.exit( lu:run() ) |