Simon Hunt | f306972 | 2014-12-16 18:15:37 -0800 | [diff] [blame] | 1 | /* |
Simon Hunt | 8ead3a2 | 2015-01-06 11:00:15 -0800 | [diff] [blame] | 2 | * Copyright 2014,2015 Open Networking Laboratory |
Simon Hunt | f306972 | 2014-12-16 18:15:37 -0800 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /* |
Simon Hunt | dc6362a | 2014-12-18 19:55:23 -0800 | [diff] [blame] | 18 | ONOS GUI -- Util -- General Purpose Functions - Unit Tests |
Simon Hunt | f306972 | 2014-12-16 18:15:37 -0800 | [diff] [blame] | 19 | */ |
Yuta HIGUCHI | 4f39bcd | 2014-12-18 20:46:14 -0800 | [diff] [blame] | 20 | describe('factory: fw/util/fn.js', function() { |
Simon Hunt | f306972 | 2014-12-16 18:15:37 -0800 | [diff] [blame] | 21 | var fs, |
| 22 | someFunction = function () {}, |
| 23 | someArray = [1, 2, 3], |
| 24 | someObject = { foo: 'bar'}, |
| 25 | someNumber = 42, |
| 26 | someString = 'xyyzy', |
Simon Hunt | 59df0b2 | 2014-12-17 10:32:25 -0800 | [diff] [blame] | 27 | someDate = new Date(), |
| 28 | stringArray = ['foo', 'bar']; |
Simon Hunt | f306972 | 2014-12-16 18:15:37 -0800 | [diff] [blame] | 29 | |
Simon Hunt | dc6362a | 2014-12-18 19:55:23 -0800 | [diff] [blame] | 30 | beforeEach(module('onosUtil')); |
Simon Hunt | f306972 | 2014-12-16 18:15:37 -0800 | [diff] [blame] | 31 | |
| 32 | beforeEach(inject(function (FnService) { |
| 33 | fs = FnService; |
| 34 | })); |
| 35 | |
Simon Hunt | f306972 | 2014-12-16 18:15:37 -0800 | [diff] [blame] | 36 | |
| 37 | // === Tests for isF() |
| 38 | it('isF(): null for undefined', function () { |
| 39 | expect(fs.isF(undefined)).toBeNull(); |
| 40 | }); |
| 41 | it('isF(): null for null', function () { |
| 42 | expect(fs.isF(null)).toBeNull(); |
| 43 | }); |
| 44 | it('isF(): the reference for function', function () { |
| 45 | expect(fs.isF(someFunction)).toBe(someFunction); |
| 46 | }); |
| 47 | it('isF(): null for string', function () { |
| 48 | expect(fs.isF(someString)).toBeNull(); |
| 49 | }); |
| 50 | it('isF(): null for number', function () { |
| 51 | expect(fs.isF(someNumber)).toBeNull(); |
| 52 | }); |
| 53 | it('isF(): null for Date', function () { |
| 54 | expect(fs.isF(someDate)).toBeNull(); |
| 55 | }); |
| 56 | it('isF(): null for array', function () { |
| 57 | expect(fs.isF(someArray)).toBeNull(); |
| 58 | }); |
| 59 | it('isF(): null for object', function () { |
| 60 | expect(fs.isF(someObject)).toBeNull(); |
| 61 | }); |
| 62 | |
| 63 | |
| 64 | // === Tests for isA() |
| 65 | it('isA(): null for undefined', function () { |
| 66 | expect(fs.isA(undefined)).toBeNull(); |
| 67 | }); |
| 68 | it('isA(): null for null', function () { |
| 69 | expect(fs.isA(null)).toBeNull(); |
| 70 | }); |
| 71 | it('isA(): null for function', function () { |
| 72 | expect(fs.isA(someFunction)).toBeNull(); |
| 73 | }); |
| 74 | it('isA(): null for string', function () { |
| 75 | expect(fs.isA(someString)).toBeNull(); |
| 76 | }); |
| 77 | it('isA(): null for number', function () { |
| 78 | expect(fs.isA(someNumber)).toBeNull(); |
| 79 | }); |
| 80 | it('isA(): null for Date', function () { |
| 81 | expect(fs.isA(someDate)).toBeNull(); |
| 82 | }); |
| 83 | it('isA(): the reference for array', function () { |
| 84 | expect(fs.isA(someArray)).toBe(someArray); |
| 85 | }); |
| 86 | it('isA(): null for object', function () { |
| 87 | expect(fs.isA(someObject)).toBeNull(); |
| 88 | }); |
| 89 | |
| 90 | |
| 91 | // === Tests for isS() |
| 92 | it('isS(): null for undefined', function () { |
| 93 | expect(fs.isS(undefined)).toBeNull(); |
| 94 | }); |
| 95 | it('isS(): null for null', function () { |
| 96 | expect(fs.isS(null)).toBeNull(); |
| 97 | }); |
| 98 | it('isS(): null for function', function () { |
| 99 | expect(fs.isS(someFunction)).toBeNull(); |
| 100 | }); |
| 101 | it('isS(): the reference for string', function () { |
| 102 | expect(fs.isS(someString)).toBe(someString); |
| 103 | }); |
| 104 | it('isS(): null for number', function () { |
| 105 | expect(fs.isS(someNumber)).toBeNull(); |
| 106 | }); |
| 107 | it('isS(): null for Date', function () { |
| 108 | expect(fs.isS(someDate)).toBeNull(); |
| 109 | }); |
| 110 | it('isS(): null for array', function () { |
| 111 | expect(fs.isS(someArray)).toBeNull(); |
| 112 | }); |
| 113 | it('isS(): null for object', function () { |
| 114 | expect(fs.isS(someObject)).toBeNull(); |
| 115 | }); |
| 116 | |
| 117 | |
| 118 | // === Tests for isO() |
| 119 | it('isO(): null for undefined', function () { |
| 120 | expect(fs.isO(undefined)).toBeNull(); |
| 121 | }); |
| 122 | it('isO(): null for null', function () { |
| 123 | expect(fs.isO(null)).toBeNull(); |
| 124 | }); |
| 125 | it('isO(): null for function', function () { |
| 126 | expect(fs.isO(someFunction)).toBeNull(); |
| 127 | }); |
| 128 | it('isO(): null for string', function () { |
| 129 | expect(fs.isO(someString)).toBeNull(); |
| 130 | }); |
| 131 | it('isO(): null for number', function () { |
| 132 | expect(fs.isO(someNumber)).toBeNull(); |
| 133 | }); |
| 134 | it('isO(): null for Date', function () { |
| 135 | expect(fs.isO(someDate)).toBeNull(); |
| 136 | }); |
| 137 | it('isO(): null for array', function () { |
| 138 | expect(fs.isO(someArray)).toBeNull(); |
| 139 | }); |
| 140 | it('isO(): the reference for object', function () { |
| 141 | expect(fs.isO(someObject)).toBe(someObject); |
| 142 | }); |
Simon Hunt | 59df0b2 | 2014-12-17 10:32:25 -0800 | [diff] [blame] | 143 | |
| 144 | // === Tests for contains() |
| 145 | it('contains(): false for improper args', function () { |
| 146 | expect(fs.contains()).toBeFalsy(); |
| 147 | }); |
| 148 | it('contains(): false for non-array', function () { |
| 149 | expect(fs.contains(null, 1)).toBeFalsy(); |
| 150 | }); |
Simon Hunt | dc6362a | 2014-12-18 19:55:23 -0800 | [diff] [blame] | 151 | it('contains(): true for contained item', function () { |
Simon Hunt | 59df0b2 | 2014-12-17 10:32:25 -0800 | [diff] [blame] | 152 | expect(fs.contains(someArray, 1)).toBeTruthy(); |
| 153 | expect(fs.contains(stringArray, 'bar')).toBeTruthy(); |
| 154 | }); |
Simon Hunt | dc6362a | 2014-12-18 19:55:23 -0800 | [diff] [blame] | 155 | it('contains(): false for non-contained item', function () { |
Simon Hunt | 59df0b2 | 2014-12-17 10:32:25 -0800 | [diff] [blame] | 156 | expect(fs.contains(someArray, 109)).toBeFalsy(); |
| 157 | expect(fs.contains(stringArray, 'zonko')).toBeFalsy(); |
| 158 | }); |
Simon Hunt | 51fc40b | 2015-01-06 13:56:12 -0800 | [diff] [blame] | 159 | |
| 160 | // === Tests for areFunctions() |
| 161 | it('areFunctions(): false for non-array', function () { |
| 162 | expect(fs.areFunctions({}, 'not-an-array')).toBeFalsy(); |
| 163 | }); |
| 164 | it('areFunctions(): true for empty-array', function () { |
| 165 | expect(fs.areFunctions({}, [])).toBeTruthy(); |
| 166 | }); |
| 167 | it('areFunctions(): true for some api', function () { |
| 168 | expect(fs.areFunctions({ |
| 169 | a: function () {}, |
| 170 | b: function () {} |
| 171 | }, ['b', 'a'])).toBeTruthy(); |
| 172 | }); |
| 173 | it('areFunctions(): false for some other api', function () { |
| 174 | expect(fs.areFunctions({ |
| 175 | a: function () {}, |
| 176 | b: 'not-a-function' |
| 177 | }, ['b', 'a'])).toBeFalsy(); |
| 178 | }); |
| 179 | it('areFunctions(): extraneous stuff ignored', function () { |
| 180 | expect(fs.areFunctions({ |
| 181 | a: function () {}, |
| 182 | b: function () {}, |
| 183 | c: 1, |
| 184 | d: 'foo' |
| 185 | }, ['a', 'b'])).toBeTruthy(); |
| 186 | }); |
| 187 | |
| 188 | |
Simon Hunt | f306972 | 2014-12-16 18:15:37 -0800 | [diff] [blame] | 189 | }); |