Advertisement
Guest User

Untitled

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