Guest User

Untitled

a guest
Jul 17th, 2018
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.05 KB | None | 0 0
  1. // 63: String - `includes()`
  2. // To do: make all tests pass, leave the assert lines unchanged!
  3.  
  4. describe('`string.includes()` finds string within another string', function() {
  5.  
  6. describe('find a single character', function() {
  7. it('in a three char string', function() {
  8. const searchString = 'x';
  9. assert.equal('xyz'.includes(searchString), true);
  10. });
  11. it('reports false if character was not found', function() {
  12. const expected = false;
  13. assert.equal('xyz'.includes('abc'), expected);
  14. });
  15. });
  16.  
  17. describe('find a string', function() {
  18. it('that matches exactly', function() {
  19. const findSome = findMe => findMe.includes('xyz');
  20. assert.equal(findSome('xyz'), true);
  21. });
  22. });
  23.  
  24. describe('search for an empty string, is always true', function() {
  25. it('in an empty string', function() {
  26. const emptyString = '';
  27. assert.equal(''.includes(emptyString), true);
  28. });
  29. it('in `abc`', function() {
  30. const actual = 'abc'.includes('abc');
  31. assert.equal(actual, true);
  32. });
  33. });
  34.  
  35. describe('special/corner cases', function() {
  36. it('search for `undefined` in a string fails', function() {
  37. const findInAbc = (what) => 'abc'.includes();
  38. assert.equal(findInAbc(void 0), false);
  39. });
  40. it('searches case-sensitive', function() {
  41. const findInAbc = (what) => 'abc'.includes(what);
  42. assert.equal(findInAbc('A'), false);
  43. });
  44. it('must NOT be a regular expression', function() {
  45. const regExp = /([A-Z])\w+/g;
  46. assert.throws(() => {''.includes(regExp)});
  47. });
  48. describe('coerces the searched "thing" into a string', function() {
  49. it('e.g. from a number', function() {
  50. const actual = '123'.includes(2);
  51. assert.equal(actual, true);
  52. });
  53. it('e.g. from an array', function() {
  54. const actual = '123'.includes([3]);
  55. assert.equal(actual, true);
  56. });
  57. it('e.g. from an object, with a `toString()` method', function() {
  58. const objWithToString = {toString: () => 1};
  59. assert.equal('123'.includes(objWithToString), true);
  60. });
  61. });
  62. });
  63.  
  64. describe('takes a position from where to start searching', function() {
  65. it('does not find `a` after position 1 in `abc`', function() {
  66. const position = 1;
  67. assert.equal('abc'.includes('a', position), false);
  68. });
  69. it('even the position gets coerced', function() {
  70. const findAtPosition = (pos) => 'xyz'.includes('z', pos);
  71. assert.equal(findAtPosition('2'), true);
  72. });
  73. describe('invalid positions get converted to 0', function() {
  74. it('e.g. `undefined`', function() {
  75. const findAtPosition = (pos) => 'xyz'.includes('x', pos);
  76. assert.equal(findAtPosition(void 0), true);
  77. });
  78. it('negative numbers', function() {
  79. const findAtPosition = (pos) => 'xyz'.includes('x', pos);
  80. assert.equal(findAtPosition(-2), true);
  81. });
  82. it('NaN', function() {
  83. const findAtPosition = (pos) => 'xyz'.includes('x', pos);
  84. assert.equal(findAtPosition(NaN), true);
  85. });
  86. });
  87. });
  88.  
  89. });
Add Comment
Please, Sign In to add comment