Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

multiline string literals and tests

Tags:

javascript

var multiply = function (a, b) {
  //An internal comment
  return a * b;
};
var stupid = "function (a, b) {
  return a * b;
  }"
expect(multiply.toString()).toBe(stupid);

The test passes, but firefox gives me this:

unterminated string literal
[Break On This Error] var stupid = "function (a, b) { 

Changing the code to use the \ line terminator stops the test from passing.

Expected 'function (a, b) { 
    return a * b; 
}' to be 'function (a, b) {return a * b;}'.

The test fails if you escape the multiline string literal.

With newlines, the test still does not pass:

var stupid = "function (a, b) {\n
    return a * b;\n
}"
expect(multiply.toString()).toBe(stupid);
like image 525
Chris G. Avatar asked Jun 06 '26 10:06

Chris G.


1 Answers

Without the backslashes, it's a syntax error. More about string literals in this HTML transcription of the spec.

With them, remember that all leading space on subsequent lines is included in the string.

Separately: Your expect call will not be reliable. First off, Function#toString has never been standardized anywhere (certainly not by the ECMAScript standard). Secondly, it works differently on different engines (some will include comments, others won't; some will reformat the code [so for instance, there may not be a space after function], others won't; etc.).

like image 129
T.J. Crowder Avatar answered Jun 08 '26 23:06

T.J. Crowder



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!