1/** 2 * @fileoverview Tests for checks.js. 3 */ 4goog.module('protobuf.internal.checksTest'); 5 6const {CHECK_TYPE, checkDefAndNotNull, checkFunctionExists} = goog.require('protobuf.internal.checks'); 7 8describe('checkDefAndNotNull', () => { 9 it('throws if undefined', () => { 10 let value; 11 if (CHECK_TYPE) { 12 expect(() => checkDefAndNotNull(value)).toThrow(); 13 } else { 14 expect(checkDefAndNotNull(value)).toBeUndefined(); 15 } 16 }); 17 18 it('throws if null', () => { 19 const value = null; 20 if (CHECK_TYPE) { 21 expect(() => checkDefAndNotNull(value)).toThrow(); 22 } else { 23 expect(checkDefAndNotNull(value)).toBeNull(); 24 } 25 }); 26 27 it('does not throw if empty string', () => { 28 const value = ''; 29 expect(checkDefAndNotNull(value)).toEqual(''); 30 }); 31}); 32 33describe('checkFunctionExists', () => { 34 it('throws if the function is undefined', () => { 35 let foo = /** @type {function()} */ (/** @type {*} */ (undefined)); 36 if (CHECK_TYPE) { 37 expect(() => checkFunctionExists(foo)).toThrow(); 38 } else { 39 checkFunctionExists(foo); 40 } 41 }); 42 43 it('throws if the property is defined but not a function', () => { 44 let foo = /** @type {function()} */ (/** @type {*} */ (1)); 45 if (CHECK_TYPE) { 46 expect(() => checkFunctionExists(foo)).toThrow(); 47 } else { 48 checkFunctionExists(foo); 49 } 50 }); 51 52 it('does not throw if the function is defined', () => { 53 function foo(x) { 54 return x; 55 } 56 checkFunctionExists(foo); 57 }); 58});