stringmatcher.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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 string matchers like containsString,
  16. * startsWith, endsWith, etc.
  17. */
  18. goog.provide('goog.labs.testing.AnyStringMatcher');
  19. goog.provide('goog.labs.testing.ContainsStringMatcher');
  20. goog.provide('goog.labs.testing.EndsWithMatcher');
  21. goog.provide('goog.labs.testing.EqualToIgnoringWhitespaceMatcher');
  22. goog.provide('goog.labs.testing.EqualsMatcher');
  23. goog.provide('goog.labs.testing.RegexMatcher');
  24. goog.provide('goog.labs.testing.StartsWithMatcher');
  25. goog.provide('goog.labs.testing.StringContainsInOrderMatcher');
  26. goog.require('goog.asserts');
  27. goog.require('goog.labs.testing.Matcher');
  28. goog.require('goog.string');
  29. /**
  30. * Matches any string value.
  31. *
  32. * @constructor @struct @implements {goog.labs.testing.Matcher} @final
  33. */
  34. goog.labs.testing.AnyStringMatcher = function() {};
  35. /** @override */
  36. goog.labs.testing.AnyStringMatcher.prototype.matches = function(actualValue) {
  37. return goog.isString(actualValue);
  38. };
  39. /** @override */
  40. goog.labs.testing.AnyStringMatcher.prototype.describe = function(actualValue) {
  41. return '<' + actualValue + '> is not a string';
  42. };
  43. /**
  44. * The ContainsString matcher.
  45. *
  46. * @param {string} value The expected string.
  47. *
  48. * @constructor
  49. * @struct
  50. * @implements {goog.labs.testing.Matcher}
  51. * @final
  52. */
  53. goog.labs.testing.ContainsStringMatcher = function(value) {
  54. /**
  55. * @type {string}
  56. * @private
  57. */
  58. this.value_ = value;
  59. };
  60. /**
  61. * Determines if input string contains the expected string.
  62. *
  63. * @override
  64. */
  65. goog.labs.testing.ContainsStringMatcher.prototype.matches = function(
  66. actualValue) {
  67. goog.asserts.assertString(actualValue);
  68. return goog.string.contains(actualValue, this.value_);
  69. };
  70. /**
  71. * @override
  72. */
  73. goog.labs.testing.ContainsStringMatcher.prototype.describe = function(
  74. actualValue) {
  75. return actualValue + ' does not contain ' + this.value_;
  76. };
  77. /**
  78. * The EndsWith matcher.
  79. *
  80. * @param {string} value The expected string.
  81. *
  82. * @constructor
  83. * @struct
  84. * @implements {goog.labs.testing.Matcher}
  85. * @final
  86. */
  87. goog.labs.testing.EndsWithMatcher = function(value) {
  88. /**
  89. * @type {string}
  90. * @private
  91. */
  92. this.value_ = value;
  93. };
  94. /**
  95. * Determines if input string ends with the expected string.
  96. *
  97. * @override
  98. */
  99. goog.labs.testing.EndsWithMatcher.prototype.matches = function(actualValue) {
  100. goog.asserts.assertString(actualValue);
  101. return goog.string.endsWith(actualValue, this.value_);
  102. };
  103. /**
  104. * @override
  105. */
  106. goog.labs.testing.EndsWithMatcher.prototype.describe = function(actualValue) {
  107. return actualValue + ' does not end with ' + this.value_;
  108. };
  109. /**
  110. * The EqualToIgnoringWhitespace matcher.
  111. *
  112. * @param {string} value The expected string.
  113. *
  114. * @constructor
  115. * @struct
  116. * @implements {goog.labs.testing.Matcher}
  117. * @final
  118. */
  119. goog.labs.testing.EqualToIgnoringWhitespaceMatcher = function(value) {
  120. /**
  121. * @type {string}
  122. * @private
  123. */
  124. this.value_ = value;
  125. };
  126. /**
  127. * Determines if input string contains the expected string.
  128. *
  129. * @override
  130. */
  131. goog.labs.testing.EqualToIgnoringWhitespaceMatcher.prototype.matches = function(
  132. actualValue) {
  133. goog.asserts.assertString(actualValue);
  134. var string1 = goog.string.collapseWhitespace(actualValue);
  135. return goog.string.caseInsensitiveCompare(this.value_, string1) === 0;
  136. };
  137. /**
  138. * @override
  139. */
  140. goog.labs.testing.EqualToIgnoringWhitespaceMatcher.prototype.describe =
  141. function(actualValue) {
  142. return actualValue + ' is not equal(ignoring whitespace) to ' + this.value_;
  143. };
  144. /**
  145. * The Equals matcher.
  146. *
  147. * @param {string} value The expected string.
  148. *
  149. * @constructor
  150. * @struct
  151. * @implements {goog.labs.testing.Matcher}
  152. * @final
  153. */
  154. goog.labs.testing.EqualsMatcher = function(value) {
  155. /**
  156. * @type {string}
  157. * @private
  158. */
  159. this.value_ = value;
  160. };
  161. /**
  162. * Determines if input string is equal to the expected string.
  163. *
  164. * @override
  165. */
  166. goog.labs.testing.EqualsMatcher.prototype.matches = function(actualValue) {
  167. goog.asserts.assertString(actualValue);
  168. return this.value_ === actualValue;
  169. };
  170. /**
  171. * @override
  172. */
  173. goog.labs.testing.EqualsMatcher.prototype.describe = function(actualValue) {
  174. return actualValue + ' is not equal to ' + this.value_;
  175. };
  176. /**
  177. * The MatchesRegex matcher.
  178. *
  179. * @param {!RegExp} regex The expected regex.
  180. *
  181. * @constructor
  182. * @struct
  183. * @implements {goog.labs.testing.Matcher}
  184. * @final
  185. */
  186. goog.labs.testing.RegexMatcher = function(regex) {
  187. /**
  188. * @type {!RegExp}
  189. * @private
  190. */
  191. this.regex_ = regex;
  192. };
  193. /**
  194. * Determines if input string is equal to the expected string.
  195. *
  196. * @override
  197. */
  198. goog.labs.testing.RegexMatcher.prototype.matches = function(actualValue) {
  199. goog.asserts.assertString(actualValue);
  200. return this.regex_.test(actualValue);
  201. };
  202. /**
  203. * @override
  204. */
  205. goog.labs.testing.RegexMatcher.prototype.describe = function(actualValue) {
  206. return actualValue + ' does not match ' + this.regex_;
  207. };
  208. /**
  209. * The StartsWith matcher.
  210. *
  211. * @param {string} value The expected string.
  212. *
  213. * @constructor
  214. * @struct
  215. * @implements {goog.labs.testing.Matcher}
  216. * @final
  217. */
  218. goog.labs.testing.StartsWithMatcher = function(value) {
  219. /**
  220. * @type {string}
  221. * @private
  222. */
  223. this.value_ = value;
  224. };
  225. /**
  226. * Determines if input string starts with the expected string.
  227. *
  228. * @override
  229. */
  230. goog.labs.testing.StartsWithMatcher.prototype.matches = function(actualValue) {
  231. goog.asserts.assertString(actualValue);
  232. return goog.string.startsWith(actualValue, this.value_);
  233. };
  234. /**
  235. * @override
  236. */
  237. goog.labs.testing.StartsWithMatcher.prototype.describe = function(actualValue) {
  238. return actualValue + ' does not start with ' + this.value_;
  239. };
  240. /**
  241. * The StringContainsInOrdermatcher.
  242. *
  243. * @param {Array<string>} values The expected string values.
  244. *
  245. * @constructor
  246. * @struct
  247. * @implements {goog.labs.testing.Matcher}
  248. * @final
  249. */
  250. goog.labs.testing.StringContainsInOrderMatcher = function(values) {
  251. /**
  252. * @type {Array<string>}
  253. * @private
  254. */
  255. this.values_ = values;
  256. };
  257. /**
  258. * Determines if input string contains, in order, the expected array of strings.
  259. *
  260. * @override
  261. */
  262. goog.labs.testing.StringContainsInOrderMatcher.prototype.matches = function(
  263. actualValue) {
  264. goog.asserts.assertString(actualValue);
  265. var currentIndex, previousIndex = 0;
  266. for (var i = 0; i < this.values_.length; i++) {
  267. currentIndex = goog.string.contains(actualValue, this.values_[i]);
  268. if (currentIndex < 0 || currentIndex < previousIndex) {
  269. return false;
  270. }
  271. previousIndex = currentIndex;
  272. }
  273. return true;
  274. };
  275. /**
  276. * @override
  277. */
  278. goog.labs.testing.StringContainsInOrderMatcher.prototype.describe = function(
  279. actualValue) {
  280. return actualValue + ' does not contain the expected values in order.';
  281. };
  282. /** @return {!goog.labs.testing.AnyStringMatcher} */
  283. function anyString() {
  284. return new goog.labs.testing.AnyStringMatcher();
  285. }
  286. /**
  287. * Matches a string containing the given string.
  288. *
  289. * @param {string} value The expected value.
  290. *
  291. * @return {!goog.labs.testing.ContainsStringMatcher} A
  292. * ContainsStringMatcher.
  293. */
  294. function containsString(value) {
  295. return new goog.labs.testing.ContainsStringMatcher(value);
  296. }
  297. /**
  298. * Matches a string that ends with the given string.
  299. *
  300. * @param {string} value The expected value.
  301. *
  302. * @return {!goog.labs.testing.EndsWithMatcher} A
  303. * EndsWithMatcher.
  304. */
  305. function endsWith(value) {
  306. return new goog.labs.testing.EndsWithMatcher(value);
  307. }
  308. /**
  309. * Matches a string that equals (ignoring whitespace) the given string.
  310. *
  311. * @param {string} value The expected value.
  312. *
  313. * @return {!goog.labs.testing.EqualToIgnoringWhitespaceMatcher} A
  314. * EqualToIgnoringWhitespaceMatcher.
  315. */
  316. function equalToIgnoringWhitespace(value) {
  317. return new goog.labs.testing.EqualToIgnoringWhitespaceMatcher(value);
  318. }
  319. /**
  320. * Matches a string that equals the given string.
  321. *
  322. * @param {string} value The expected value.
  323. *
  324. * @return {!goog.labs.testing.EqualsMatcher} A EqualsMatcher.
  325. */
  326. function equals(value) {
  327. return new goog.labs.testing.EqualsMatcher(value);
  328. }
  329. /**
  330. * Matches a string against a regular expression.
  331. *
  332. * @param {!RegExp} regex The expected regex.
  333. *
  334. * @return {!goog.labs.testing.RegexMatcher} A RegexMatcher.
  335. */
  336. function matchesRegex(regex) {
  337. return new goog.labs.testing.RegexMatcher(regex);
  338. }
  339. /**
  340. * Matches a string that starts with the given string.
  341. *
  342. * @param {string} value The expected value.
  343. *
  344. * @return {!goog.labs.testing.StartsWithMatcher} A
  345. * StartsWithMatcher.
  346. */
  347. function startsWith(value) {
  348. return new goog.labs.testing.StartsWithMatcher(value);
  349. }
  350. /**
  351. * Matches a string that contains the given strings in order.
  352. *
  353. * @param {Array<string>} values The expected value.
  354. *
  355. * @return {!goog.labs.testing.StringContainsInOrderMatcher} A
  356. * StringContainsInOrderMatcher.
  357. */
  358. function stringContainsInOrder(values) {
  359. return new goog.labs.testing.StringContainsInOrderMatcher(values);
  360. }