numbermatcher.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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. /**
  15. * @fileoverview Provides the built-in number matchers like lessThan,
  16. * greaterThan, etc.
  17. */
  18. goog.provide('goog.labs.testing.AnyNumberMatcher');
  19. goog.provide('goog.labs.testing.CloseToMatcher');
  20. goog.provide('goog.labs.testing.EqualToMatcher');
  21. goog.provide('goog.labs.testing.GreaterThanEqualToMatcher');
  22. goog.provide('goog.labs.testing.GreaterThanMatcher');
  23. goog.provide('goog.labs.testing.LessThanEqualToMatcher');
  24. goog.provide('goog.labs.testing.LessThanMatcher');
  25. goog.require('goog.asserts');
  26. goog.require('goog.labs.testing.Matcher');
  27. /**
  28. * Matches any number value.
  29. *
  30. * @constructor @struct @implements {goog.labs.testing.Matcher} @final
  31. */
  32. goog.labs.testing.AnyNumberMatcher = function() {};
  33. /** @override */
  34. goog.labs.testing.AnyNumberMatcher.prototype.matches = function(actualValue) {
  35. return goog.isNumber(actualValue);
  36. };
  37. /** @override */
  38. goog.labs.testing.AnyNumberMatcher.prototype.describe = function(actualValue) {
  39. return '<' + actualValue + '> is not a number';
  40. };
  41. /**
  42. * The GreaterThan matcher.
  43. *
  44. * @param {number} value The value to compare.
  45. *
  46. * @constructor
  47. * @struct
  48. * @implements {goog.labs.testing.Matcher}
  49. * @final
  50. */
  51. goog.labs.testing.GreaterThanMatcher = function(value) {
  52. /**
  53. * @type {number}
  54. * @private
  55. */
  56. this.value_ = value;
  57. };
  58. /**
  59. * Determines if input value is greater than the expected value.
  60. *
  61. * @override
  62. */
  63. goog.labs.testing.GreaterThanMatcher.prototype.matches = function(actualValue) {
  64. goog.asserts.assertNumber(actualValue);
  65. return actualValue > this.value_;
  66. };
  67. /**
  68. * @override
  69. */
  70. goog.labs.testing.GreaterThanMatcher.prototype.describe = function(
  71. actualValue) {
  72. goog.asserts.assertNumber(actualValue);
  73. return actualValue + ' is not greater than ' + this.value_;
  74. };
  75. /**
  76. * The lessThan matcher.
  77. *
  78. * @param {number} value The value to compare.
  79. *
  80. * @constructor
  81. * @struct
  82. * @implements {goog.labs.testing.Matcher}
  83. * @final
  84. */
  85. goog.labs.testing.LessThanMatcher = function(value) {
  86. /**
  87. * @type {number}
  88. * @private
  89. */
  90. this.value_ = value;
  91. };
  92. /**
  93. * Determines if the input value is less than the expected value.
  94. *
  95. * @override
  96. */
  97. goog.labs.testing.LessThanMatcher.prototype.matches = function(actualValue) {
  98. goog.asserts.assertNumber(actualValue);
  99. return actualValue < this.value_;
  100. };
  101. /**
  102. * @override
  103. */
  104. goog.labs.testing.LessThanMatcher.prototype.describe = function(actualValue) {
  105. goog.asserts.assertNumber(actualValue);
  106. return actualValue + ' is not less than ' + this.value_;
  107. };
  108. /**
  109. * The GreaterThanEqualTo matcher.
  110. *
  111. * @param {number} value The value to compare.
  112. *
  113. * @constructor
  114. * @struct
  115. * @implements {goog.labs.testing.Matcher}
  116. * @final
  117. */
  118. goog.labs.testing.GreaterThanEqualToMatcher = function(value) {
  119. /**
  120. * @type {number}
  121. * @private
  122. */
  123. this.value_ = value;
  124. };
  125. /**
  126. * Determines if the input value is greater than equal to the expected value.
  127. *
  128. * @override
  129. */
  130. goog.labs.testing.GreaterThanEqualToMatcher.prototype.matches = function(
  131. actualValue) {
  132. goog.asserts.assertNumber(actualValue);
  133. return actualValue >= this.value_;
  134. };
  135. /**
  136. * @override
  137. */
  138. goog.labs.testing.GreaterThanEqualToMatcher.prototype.describe = function(
  139. actualValue) {
  140. goog.asserts.assertNumber(actualValue);
  141. return actualValue + ' is not greater than equal to ' + this.value_;
  142. };
  143. /**
  144. * The LessThanEqualTo matcher.
  145. *
  146. * @param {number} value The value to compare.
  147. *
  148. * @constructor
  149. * @struct
  150. * @implements {goog.labs.testing.Matcher}
  151. * @final
  152. */
  153. goog.labs.testing.LessThanEqualToMatcher = function(value) {
  154. /**
  155. * @type {number}
  156. * @private
  157. */
  158. this.value_ = value;
  159. };
  160. /**
  161. * Determines if the input value is less than or equal to the expected value.
  162. *
  163. * @override
  164. */
  165. goog.labs.testing.LessThanEqualToMatcher.prototype.matches = function(
  166. actualValue) {
  167. goog.asserts.assertNumber(actualValue);
  168. return actualValue <= this.value_;
  169. };
  170. /**
  171. * @override
  172. */
  173. goog.labs.testing.LessThanEqualToMatcher.prototype.describe = function(
  174. actualValue) {
  175. goog.asserts.assertNumber(actualValue);
  176. return actualValue + ' is not less than equal to ' + this.value_;
  177. };
  178. /**
  179. * The EqualTo matcher.
  180. *
  181. * @param {number} value The value to compare.
  182. *
  183. * @constructor
  184. * @struct
  185. * @implements {goog.labs.testing.Matcher}
  186. * @final
  187. */
  188. goog.labs.testing.EqualToMatcher = function(value) {
  189. /**
  190. * @type {number}
  191. * @private
  192. */
  193. this.value_ = value;
  194. };
  195. /**
  196. * Determines if the input value is equal to the expected value.
  197. *
  198. * @override
  199. */
  200. goog.labs.testing.EqualToMatcher.prototype.matches = function(actualValue) {
  201. goog.asserts.assertNumber(actualValue);
  202. return actualValue === this.value_;
  203. };
  204. /**
  205. * @override
  206. */
  207. goog.labs.testing.EqualToMatcher.prototype.describe = function(actualValue) {
  208. goog.asserts.assertNumber(actualValue);
  209. return actualValue + ' is not equal to ' + this.value_;
  210. };
  211. /**
  212. * The CloseTo matcher.
  213. *
  214. * @param {number} value The value to compare.
  215. * @param {number} range The range to check within.
  216. *
  217. * @constructor
  218. * @struct
  219. * @implements {goog.labs.testing.Matcher}
  220. * @final
  221. */
  222. goog.labs.testing.CloseToMatcher = function(value, range) {
  223. /**
  224. * @type {number}
  225. * @private
  226. */
  227. this.value_ = value;
  228. /**
  229. * @type {number}
  230. * @private
  231. */
  232. this.range_ = range;
  233. };
  234. /**
  235. * Determines if input value is within a certain range of the expected value.
  236. *
  237. * @override
  238. */
  239. goog.labs.testing.CloseToMatcher.prototype.matches = function(actualValue) {
  240. goog.asserts.assertNumber(actualValue);
  241. return Math.abs(this.value_ - actualValue) < this.range_;
  242. };
  243. /**
  244. * @override
  245. */
  246. goog.labs.testing.CloseToMatcher.prototype.describe = function(actualValue) {
  247. goog.asserts.assertNumber(actualValue);
  248. return actualValue + ' is not close to(' + this.range_ + ') ' + this.value_;
  249. };
  250. /** @return {!goog.labs.testing.AnyNumberMatcher} */
  251. function anyNumber() {
  252. return new goog.labs.testing.AnyNumberMatcher();
  253. }
  254. /**
  255. * @param {number} value The expected value.
  256. *
  257. * @return {!goog.labs.testing.GreaterThanMatcher} A GreaterThanMatcher.
  258. */
  259. function greaterThan(value) {
  260. return new goog.labs.testing.GreaterThanMatcher(value);
  261. }
  262. /**
  263. * @param {number} value The expected value.
  264. *
  265. * @return {!goog.labs.testing.GreaterThanEqualToMatcher} A
  266. * GreaterThanEqualToMatcher.
  267. */
  268. function greaterThanEqualTo(value) {
  269. return new goog.labs.testing.GreaterThanEqualToMatcher(value);
  270. }
  271. /**
  272. * @param {number} value The expected value.
  273. *
  274. * @return {!goog.labs.testing.LessThanMatcher} A LessThanMatcher.
  275. */
  276. function lessThan(value) {
  277. return new goog.labs.testing.LessThanMatcher(value);
  278. }
  279. /**
  280. * @param {number} value The expected value.
  281. *
  282. * @return {!goog.labs.testing.LessThanEqualToMatcher} A LessThanEqualToMatcher.
  283. */
  284. function lessThanEqualTo(value) {
  285. return new goog.labs.testing.LessThanEqualToMatcher(value);
  286. }
  287. /**
  288. * @param {number} value The expected value.
  289. *
  290. * @return {!goog.labs.testing.EqualToMatcher} An EqualToMatcher.
  291. */
  292. function equalTo(value) {
  293. return new goog.labs.testing.EqualToMatcher(value);
  294. }
  295. /**
  296. * @param {number} value The expected value.
  297. * @param {number} range The maximum allowed difference from the expected value.
  298. *
  299. * @return {!goog.labs.testing.CloseToMatcher} A CloseToMatcher.
  300. */
  301. function closeTo(value, range) {
  302. return new goog.labs.testing.CloseToMatcher(value, range);
  303. }