/test/javascript_literals.coffee

http://github.com/jashkenas/coffee-script · CoffeeScript · 70 lines · 52 code · 8 blank · 10 comment · 1 complexity · ee82dfaf6f3091458eaeaa6d08b07171 MD5 · raw file

  1. # JavaScript Literals
  2. # -------------------
  3. test "inline JavaScript is evaluated", ->
  4. eq '\\`', `
  5. // Inline JS
  6. "\\\\\`"
  7. `
  8. test "escaped backticks are output correctly", ->
  9. `var a = \`2 + 2 = ${4}\``
  10. eq a, '2 + 2 = 4'
  11. test "backslashes before a newline don’t break JavaScript blocks", ->
  12. `var a = \`To be, or not\\
  13. to be.\``
  14. eq a, '''
  15. To be, or not\\
  16. to be.'''
  17. test "block inline JavaScript is evaluated", ->
  18. ```
  19. var a = 1;
  20. var b = 2;
  21. ```
  22. c = 3
  23. ```var d = 4;```
  24. eq a + b + c + d, 10
  25. test "block inline JavaScript containing backticks", ->
  26. ```
  27. // This is a comment with `backticks`
  28. var a = 42;
  29. var b = `foo ${'bar'}`;
  30. var c = 3;
  31. var d = 'foo`bar`';
  32. ```
  33. eq a + c, 45
  34. eq b, 'foo bar'
  35. eq d, 'foo`bar`'
  36. test "block JavaScript can end with an escaped backtick character", ->
  37. ```var a = \`hello\````
  38. ```
  39. var b = \`world${'!'}\````
  40. eq a, 'hello'
  41. eq b, 'world!'
  42. test "JavaScript block only escapes backslashes followed by backticks", ->
  43. eq `'\\\n'`, '\\\n'
  44. test "escaped JavaScript blocks speed round", ->
  45. # The following has escaped backslashes because they’re required in strings, but the intent is this:
  46. # `hello` → hello;
  47. # `\`hello\`` → `hello`;
  48. # `\`Escaping backticks in JS: \\\`hello\\\`\`` → `Escaping backticks in JS: \`hello\``;
  49. # `Single backslash: \ ` → Single backslash: \ ;
  50. # `Double backslash: \\ ` → Double backslash: \\ ;
  51. # `Single backslash at EOS: \\` → Single backslash at EOS: \;
  52. # `Double backslash at EOS: \\\\` → Double backslash at EOS: \\;
  53. for [input, output] in [
  54. ['`hello`', 'hello;']
  55. ['`\\`hello\\``', '`hello`;']
  56. ['`\\`Escaping backticks in JS: \\\\\\`hello\\\\\\`\\``', '`Escaping backticks in JS: \\`hello\\``;']
  57. ['`Single backslash: \\ `', 'Single backslash: \\ ;']
  58. ['`Double backslash: \\\\ `', 'Double backslash: \\\\ ;']
  59. ['`Single backslash at EOS: \\\\`', 'Single backslash at EOS: \\;']
  60. ['`Double backslash at EOS: \\\\\\\\`', 'Double backslash at EOS: \\\\;']
  61. ]
  62. eqJS input, output