checks_test.js 1.5 KB

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