PageRenderTime 24ms CodeModel.GetById 16ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

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