Guest User

Untitled

a guest
Dec 10th, 2018
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.11 KB | None | 0 0
  1. // 3: template strings - tagged
  2. // To do: make all tests pass, leave the asserts unchanged!
  3. // Follow the hints of the failure messages!
  4.  
  5. describe('tagged template strings, are an advanced form of template strings', function() {
  6.  
  7. it('syntax: prefix the template string with a function to call (without "()" around it)', function() {
  8. function tagFunction(s) {
  9. return s.toString();
  10. }
  11. var evaluated = tagFunction `template string`;
  12. assert.equal(evaluated, 'template string');
  13. });
  14.  
  15. describe('the function can access each part of the template', function() {
  16.  
  17. describe('the 1st parameter - receives only the pure strings of the template string', function() {
  18.  
  19. function tagFunction(strings) {
  20. return strings;
  21. }
  22.  
  23. it('the strings are an array', function() {
  24. var result = ['template string'];
  25. assert.deepEqual(tagFunction`template string`, result);
  26. });
  27.  
  28. it('expressions are NOT passed to it', function() {
  29. var tagged = tagFunction `one${23}two`;
  30. assert.deepEqual(tagged, ['one', 'two']);
  31. });
  32.  
  33. });
  34.  
  35. describe('the 2nd and following parameters - contain the values of the processed substitution', function() {
  36.  
  37. var one = 1;
  38. var two = 2;
  39. var three = 3;
  40. it('the 2nd parameter contains the first expression`s value', function() {
  41. function firstValueOnly(strings, firstValue) {
  42. return firstValue;
  43. }
  44. assert.equal(firstValueOnly`uno ${one}, dos ${two}`, 1);
  45. });
  46.  
  47. it('the 3rd parameter contains the second expression`s value', function() {
  48. function firstValueOnly(strings, firstValue, secondValue) {
  49. return secondValue;
  50. }
  51. assert.equal(firstValueOnly`uno ${one}, dos ${two}`, 2);
  52. });
  53.  
  54. it('using ES6 rest syntax, all values can be accessed via one variable', function() {
  55. function valuesOnly(stringsArray, ...allValues) { // using the new ES6 rest syntax
  56. return allValues;
  57. }
  58. assert.deepEqual(valuesOnly`uno=${one}, dos=${two}, tres=${three}`, [1, 2, 3]);
  59. });
  60.  
  61. });
  62. });
  63.  
  64. });
Add Comment
Please, Sign In to add comment