mrdavidlaing / javascript-koans

Koans to learn Javascript
MIT License
2.41k stars 3.51k forks source link

function body as a string test #9

Closed chrisgillis closed 11 years ago

chrisgillis commented 13 years ago

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

why doesn't that work? if i remove the '\' lien terminators it works but Firefox throws an unterminated string literal error.

chrisgillis commented 13 years ago

It would seem you have to use \n characters and match the amount of spaces that toString() outputs exactly or the test will not pass. This seems poor.

EnzoMartin commented 13 years ago

Ah, didn't see this before opening my own issue on this.. yea, the script expects to receive:

'function (a, b) {\n return a * b;\n}'

It should accept:

'function (a, b) { return a * b; }'

william-s commented 12 years ago

In addition to these formating problems, it seems the latest version of chromium expects //An internal comment as well where as firefox does not.

victorb commented 12 years ago

I'm also having trouble with this one. This is the output I get:

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

This is the debug message:

Error: Expected 'function (a, b) {
at null.<anonymous> (http://localhost/javascript-koans/koans/AboutFunctions.js:112:33)

This is my code:

it("should use function body as a string", function () {
var add = new Function("a", "b", "return a + b;");
expect(add(1, 2)).toBe(3);

var multiply = function (a, b) {
  //An internal comment
  return a * b;
};
var functionMultiply = "function (a, b) {\n\
  //An internal comment\n\
  return a * b;\n\
}"

expect(multiply.toString()).toBe(functionMultiply);
});    

Could someone possibly help me with this one?

stefanscheidt commented 11 years ago

Here what works for me on Chrome 23:

it("should use function body as a string", function () {
  var add = new Function("a", "b", "return a + b;");
  expect(add(1, 2)).toBe(3);

  var multiply = function (a, b) {
    //An internal comment
    return a * b;
  };

  expect(multiply.toString()).toBe("function (a, b) {\n"
    + "      //An internal comment\n"
    + "      return a * b;\n"
    + "    }");
});    
stefanscheidt commented 11 years ago

See also #10.

adrianhorning08 commented 7 years ago

Wouldn't this work as well? "function(a, b) {" "// An internal comment" "return a * b;" "};"