objectmatcher_test.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // Copyright 2012 The Closure Library Authors. All Rights Reserved.
  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. goog.provide('goog.labs.testing.objectMatcherTest');
  15. goog.setTestOnly('goog.labs.testing.objectMatcherTest');
  16. goog.require('goog.labs.testing.MatcherError');
  17. /** @suppress {extraRequire} */
  18. goog.require('goog.labs.testing.ObjectEqualsMatcher');
  19. goog.require('goog.labs.testing.assertThat');
  20. goog.require('goog.testing.jsunit');
  21. function testAnyObject() {
  22. goog.labs.testing.assertThat({}, anyObject(), 'typeof {} == "object"');
  23. assertMatcherError(function() {
  24. goog.labs.testing.assertThat(null, anyObject());
  25. }, 'typeof null == "object"');
  26. }
  27. function testObjectEquals() {
  28. var obj1 = {x: 1};
  29. var obj2 = obj1;
  30. goog.labs.testing.assertThat(obj1, equalsObject(obj2), 'obj1 equals obj2');
  31. assertMatcherError(function() {
  32. goog.labs.testing.assertThat({x: 1}, equalsObject({}));
  33. }, 'equalsObject does not throw exception on failure');
  34. }
  35. function testInstanceOf() {
  36. function expected() { this.x = 1; }
  37. var input = new expected();
  38. goog.labs.testing.assertThat(
  39. input, instanceOfClass(expected), 'input is an instance of expected');
  40. assertMatcherError(function() {
  41. goog.labs.testing.assertThat(5, instanceOfClass(function() {}));
  42. }, 'instanceOfClass does not throw exception on failure');
  43. }
  44. function testHasProperty() {
  45. goog.labs.testing.assertThat(
  46. {x: 1}, hasProperty('x'), '{x:1} has property x}');
  47. assertMatcherError(function() {
  48. goog.labs.testing.assertThat({x: 1}, hasProperty('y'));
  49. }, 'hasProperty does not throw exception on failure');
  50. }
  51. function testIsNull() {
  52. goog.labs.testing.assertThat(null, isNull(), 'null is null');
  53. assertMatcherError(function() {
  54. goog.labs.testing.assertThat(5, isNull());
  55. }, 'isNull does not throw exception on failure');
  56. }
  57. function testIsNullOrUndefined() {
  58. var x;
  59. goog.labs.testing.assertThat(
  60. undefined, isNullOrUndefined(), 'undefined is null or undefined');
  61. goog.labs.testing.assertThat(
  62. x, isNullOrUndefined(), 'undefined is null or undefined');
  63. x = null;
  64. goog.labs.testing.assertThat(
  65. null, isNullOrUndefined(), 'null is null or undefined');
  66. goog.labs.testing.assertThat(
  67. x, isNullOrUndefined(), 'null is null or undefined');
  68. assertMatcherError(function() {
  69. goog.labs.testing.assertThat(5, isNullOrUndefined());
  70. }, 'isNullOrUndefined does not throw exception on failure');
  71. }
  72. function testIsUndefined() {
  73. var x;
  74. goog.labs.testing.assertThat(
  75. undefined, isUndefined(), 'undefined is undefined');
  76. goog.labs.testing.assertThat(x, isUndefined(), 'undefined is undefined');
  77. assertMatcherError(function() {
  78. goog.labs.testing.assertThat(5, isUndefined());
  79. }, 'isUndefined does not throw exception on failure');
  80. }
  81. function assertMatcherError(callable, errorString) {
  82. var e = assertThrows(errorString || 'callable throws exception', callable);
  83. assertTrue(e instanceof goog.labs.testing.MatcherError);
  84. }