dictionarymatcher_test.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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.dictionaryMatcherTest');
  15. goog.setTestOnly('goog.labs.testing.dictionaryMatcherTest');
  16. /** @suppress {extraRequire} */
  17. goog.require('goog.labs.testing.HasEntryMatcher');
  18. goog.require('goog.labs.testing.MatcherError');
  19. goog.require('goog.labs.testing.assertThat');
  20. goog.require('goog.testing.jsunit');
  21. function testHasEntries() {
  22. var obj1 = {x: 1, y: 2, z: 3};
  23. goog.labs.testing.assertThat(
  24. obj1, hasEntries({x: 1, y: 2}), 'obj1 has entries: {x:1, y:2}');
  25. assertMatcherError(function() {
  26. goog.labs.testing.assertThat(obj1, hasEntries({z: 5, a: 4}));
  27. }, 'hasEntries should throw exception when it fails');
  28. }
  29. function testHasEntry() {
  30. var obj1 = {x: 1, y: 2, z: 3};
  31. goog.labs.testing.assertThat(obj1, hasEntry('x', 1), 'obj1 has entry: {x:1}');
  32. assertMatcherError(function() {
  33. goog.labs.testing.assertThat(obj1, hasEntry('z', 5));
  34. }, 'hasEntry should throw exception when it fails');
  35. }
  36. function testHasKey() {
  37. var obj1 = {x: 1};
  38. goog.labs.testing.assertThat(obj1, hasKey('x'), 'obj1 has key x');
  39. assertMatcherError(function() {
  40. goog.labs.testing.assertThat(obj1, hasKey('z'));
  41. }, 'hasKey should throw exception when it fails');
  42. }
  43. function testHasValue() {
  44. var obj1 = {x: 1};
  45. goog.labs.testing.assertThat(obj1, hasValue(1), 'obj1 has value 1');
  46. assertMatcherError(function() {
  47. goog.labs.testing.assertThat(obj1, hasValue(2));
  48. }, 'hasValue should throw exception when it fails');
  49. }
  50. function assertMatcherError(callable, errorString) {
  51. var e = assertThrows(errorString || 'callable throws exception', callable);
  52. assertTrue(e instanceof goog.labs.testing.MatcherError);
  53. }