Skip to content

Commit

Permalink
Fix test for Function.prototype.toString/toSource
Browse files Browse the repository at this point in the history
  • Loading branch information
tuchida committed Aug 27, 2023
1 parent 1e9952b commit 07b7ca1
Show file tree
Hide file tree
Showing 43 changed files with 94 additions and 110 deletions.
10 changes: 4 additions & 6 deletions testsrc/doctests/433878.doctest
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,10 @@ js> function f(a,b,c) {
> return sum / 3;
> }
js> f.toString()

function f(a, b, c) {
let sum = a + b + c;
return sum / 3;
}

function f(a,b,c) {
let sum = a + b + c;
return sum / 3;
}
js> try {
> eval("function f() { for (;;) let a=3; return 3; }");
> } catch (e) {
Expand Down
2 changes: 1 addition & 1 deletion testsrc/doctests/773573.doctest
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ js> version(180)
js> try {
> (function({a}) { return a }).foo()
> } catch (e) {
> e.message.indexOf("function ({a}) {...}") != -1;
> e.message.indexOf("function({a}) {...}") != -1;
> }
true

24 changes: 8 additions & 16 deletions testsrc/doctests/expressionclosure.doctest
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,28 @@
js> version(180)
180
js> x = function(x) x;

function (x) x

function(x) x
js> x.toSource()
(function (x) x)
function(x) x
js> x(123) === 123
true
js> x = function([a, b]) a + b;

function ([a, b]) a + b

function([a, b]) a + b
js> x([1, 2])
3
js> x.toSource()
(function ([a, b]) a + b)
function([a, b]) a + b
js> function outer() {
> var k = function(a) a + 1;
> return function(b) k(b) * 2;
> }
js> outer

function outer() {
var k = function (a) a + 1;
return function (b) k(b) * 2;
}

var k = function(a) a + 1;
return function(b) k(b) * 2;
}
js> outer()

function (b) k(b) * 2

function(b) k(b) * 2
js> outer()(4)
10
js> outer()(5)
Expand Down
5 changes: 2 additions & 3 deletions testsrc/doctests/object.defineproperty.doctest
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ js> obj.a
js> // when define new property with accessor descriptor then those values are used for the descriptor
js> var obj = define({}, 'a', { get: function() { return 3; }, set: function(value) {} });
js> var {get:g, set:s} = describe(obj, 'a'); [g, s].toSource();
[(function () {return 3;}), (function (value) {})]
[function() { return 3; }, function(value) {}]
js> obj.a
3

Expand Down Expand Up @@ -128,8 +128,7 @@ js> var obj = define(obj, 'a', {get : function() { return 4 }});
js> obj.a
4
js> describe(obj, 'a').toSource()
({enumerable:false, configurable:true, get:(function () {return 4;})})

({enumerable:false, configurable:true, get:function() { return 4 }})
js> // can change from accessor property to data property when configurable is true
js> var obj = define({}, 'a', {get : function() { return 2 }, configurable:true});
js> var obj = define(obj, 'a', {value : 5});
Expand Down
4 changes: 2 additions & 2 deletions testsrc/doctests/object.getownpropertydescriptor.doctest
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ js> desc.value === undefined;
true
js> desc.writable
js> desc.get.toSource()
(function () {})
p() {}
js> desc.set.toSource()
(function () {})
p() {}
js> desc.enumerable
true
js> desc.configurable
Expand Down
6 changes: 3 additions & 3 deletions testsrc/jstests/arrowfn.jstest
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ function assertException(exception, fn) {

var f = a => a * a;
assertEq(f(2), 4);
assertEq(f.toString(), '\na => a * a\n');
assertEq(f.toString(), 'a => a * a');

var f = (a) => a * a;
assertEq(f(2), 4);
assertEq(f.toString(), '\n(a) => a * a\n');
assertEq(f.toString(), '(a) => a * a');

var f = (a, b) => a * b;
assertEq(f(2, 3), 6);
Expand Down Expand Up @@ -51,7 +51,7 @@ var f = a => {
return a * a;
};
assertEq(f(10), 100);
assertEq(f.toString(), '\na => {\n return a * a;\n}\n');
assertEq(f.toString(), 'a => {\n return a * a;\n}');

var f = a => {
a * a;
Expand Down
7 changes: 1 addition & 6 deletions testsrc/jstests/harmony/method-definition.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,7 @@ obj = {
assertEquals(123, obj.set());
// assertEquals("set", obj.set.name);

assertEquals("\n" +
"function () {\n" +
" +{f() {\n" +
" print(1);\n" +
" }};\n" +
"}\n", (function() { +{ f() { print(1); }}; }).toString());
assertEquals("function() { +{ f() { print(1); }}; }", (function() { +{ f() { print(1); }}; }).toString());

// Allow reserved word
assertEquals(123, {
Expand Down
4 changes: 2 additions & 2 deletions testsrc/org/mozilla/javascript/tests/Bug491621Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,9 @@ public void hexOctDecLiteralToSource() {

@Test
public void hexOctLiteralPropertyNameToSource() {
assertSource("({ 0xff: 1, 010: 2 });", "({\n ff: 1, \n 10: 2});\n");
assertSource("({ 0xff: 1, 010: 2 });", "({\n ff: 1,\n 10: 2\n});\n");
assertSource(
"({ 0xff: 1, 010: 2 });", "({\n 0xff: 1, \n 010: 2});\n", Context.VERSION_ES6);
"({ 0xff: 1, 010: 2 });", "({\n 0xff: 1,\n 010: 2\n});\n", Context.VERSION_ES6);
}

@Test
Expand Down
2 changes: 1 addition & 1 deletion testsrc/org/mozilla/javascript/tests/Bug708801Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ protected ScriptNode compile(CharSequence source) {
Codegen codegen = new Codegen();
codegen.setMainMethodClass(mainMethodClassName);
codegen.compileToClassFile(
compilerEnv, scriptClassName, tree, tree.getEncodedSource(), false);
compilerEnv, scriptClassName, tree, tree.getRawSource(), false);

return tree;
}
Expand Down
3 changes: 1 addition & 2 deletions testsrc/org/mozilla/javascript/tests/Bug782363Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ protected ScriptNode compile(CharSequence source) {

Codegen codegen = new Codegen();
codegen.setMainMethodClass(mainMethodClassName);
codegen.compileToClassFile(
compilerEnv, scriptClassName, tree, tree.getEncodedSource(), false);
codegen.compileToClassFile(compilerEnv, scriptClassName, tree, tree.getRawSource(), false);

return tree;
}
Expand Down
5 changes: 2 additions & 3 deletions testsrc/org/mozilla/javascript/tests/BugGetterSetterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,10 @@ public void setUp() throws Exception {
public void nodeReplacementInWhileLoopWithBrackets() throws IOException {
String script =
"var o = {\n"
+ " _x: 123, \n"
+ " _x: 123,\n"
+ " get x() {\n"
+ " return this._x;\n"
+ " }\n"
+ ", \n"
+ " },\n"
+ " set x(value) {\n"
+ " this._x = value;\n"
+ " }\n"
Expand Down
28 changes: 14 additions & 14 deletions testsrc/org/mozilla/javascript/tests/CodegenTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ public void largeMethod() {
.getName()
.startsWith("org.mozilla.javascript.InterpretedFunction"));
Assert.assertTrue(
"" + ((NativeFunction) script).getEncodedSource().length(),
((NativeFunction) script).getEncodedSource().length() > 1000);
"" + ((NativeFunction) script).getRawSource().length(),
((NativeFunction) script).getRawSource().length() > 1000);
return null;
} catch (IOException e) {
Assert.fail(e.getMessage());
Expand Down Expand Up @@ -144,8 +144,8 @@ public void largeVarList() {
.getName()
.startsWith("org.mozilla.javascript.InterpretedFunction"));
Assert.assertTrue(
"" + ((NativeFunction) script).getEncodedSource().length(),
((NativeFunction) script).getEncodedSource().length() > 1000);
"" + ((NativeFunction) script).getRawSource().length(),
((NativeFunction) script).getRawSource().length() > 1000);
return null;
});

Expand All @@ -164,8 +164,8 @@ public void largeVarList() {
.getName()
.startsWith("org.mozilla.javascript.InterpretedFunction"));
Assert.assertTrue(
"" + ((NativeFunction) script).getEncodedSource().length(),
((NativeFunction) script).getEncodedSource().length() > 1000);
"" + ((NativeFunction) script).getRawSource().length(),
((NativeFunction) script).getRawSource().length() > 1000);
return null;
} catch (IOException e) {
Assert.fail(e.getMessage());
Expand Down Expand Up @@ -216,8 +216,8 @@ public void largeLocalVarList() {
.getName()
.startsWith("org.mozilla.javascript.InterpretedFunction"));
Assert.assertTrue(
"" + ((NativeFunction) script).getEncodedSource().length(),
((NativeFunction) script).getEncodedSource().length() > 1000);
"" + ((NativeFunction) script).getRawSource().length(),
((NativeFunction) script).getRawSource().length() > 1000);
return null;
});

Expand All @@ -236,8 +236,8 @@ public void largeLocalVarList() {
.getName()
.startsWith("org.mozilla.javascript.InterpretedFunction"));
Assert.assertTrue(
"" + ((NativeFunction) script).getEncodedSource().length(),
((NativeFunction) script).getEncodedSource().length() > 1000);
"" + ((NativeFunction) script).getRawSource().length(),
((NativeFunction) script).getRawSource().length() > 1000);
return null;
} catch (IOException e) {
Assert.fail(e.getMessage());
Expand Down Expand Up @@ -284,8 +284,8 @@ public void tooManyMethods() {
.getName()
.startsWith("org.mozilla.javascript.InterpretedFunction"));
Assert.assertTrue(
"" + ((NativeFunction) script).getEncodedSource().length(),
((NativeFunction) script).getEncodedSource().length() > 1000);
"" + ((NativeFunction) script).getRawSource().length(),
((NativeFunction) script).getRawSource().length() > 1000);
return null;
});

Expand All @@ -304,8 +304,8 @@ public void tooManyMethods() {
.getName()
.startsWith("org.mozilla.javascript.InterpretedFunction"));
Assert.assertTrue(
"" + ((NativeFunction) script).getEncodedSource().length(),
((NativeFunction) script).getEncodedSource().length() > 1000);
"" + ((NativeFunction) script).getRawSource().length(),
((NativeFunction) script).getRawSource().length() > 1000);
return null;
} catch (IOException e) {
Assert.fail(e.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,13 @@ public void commentsInObjectLiteral() {
String outputStr =
"function f1() {\n"
+ " var timeRegistrationLines = {\n"
+ " \"orderId\": a.orderId, \n"
+ " \"registeredQuantity\": a.datavalue, \n"
+ " \"unit\": a.unit, \n"
+ " \"wageCompType\": a.wageCompType, \n"
+ " \"week\": a[a.timeLineFrom].weekNumber, \n"
+ " \"comments\": (a.datavalue || null)};\n"
+ " \"orderId\": a.orderId,\n"
+ " \"registeredQuantity\": a.datavalue,\n"
+ " \"unit\": a.unit,\n"
+ " \"wageCompType\": a.wageCompType,\n"
+ " \"week\": a[a.timeLineFrom].weekNumber,\n"
+ " \"comments\": (a.datavalue || null)\n"
+ "};\n"
+ "}\n";
AstNode scriptRoot =
CommentsTestUtils.getRhinoASTRootNode(inputStr, "tryCatch1", null, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ public void getOwnPropertyDescriptorSetPropertyIsAlwaysDefined() {
+ "+ ' ' + desc.get"
+ "+ ' ' + desc.set"
+ "+ ' [' + Object.getOwnPropertyNames(desc) + ']'",
"undefined \nfunction () {\n}\n undefined [get,set,enumerable,configurable]");
"undefined function() {} undefined [get,set,enumerable,configurable]");
}

@Test
Expand Down
15 changes: 7 additions & 8 deletions testsrc/tests/e4x/Regress/regress-301692.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,33 +14,32 @@ printBugNumber(BUGNUMBER);
START(summary);

actual = ToString((function () { return <xml/>; }));
expect = 'function () { return <xml/>;}';
expect = 'function () { return <xml/>; }';
TEST(1, expect, actual);

actual = ToString((function () { return <xml></xml>; }));
expect = 'function () { return <xml></xml>;}';
expect = 'function () { return <xml></xml>; }';
TEST(2, expect, actual);

actual = ToString((function () { return <xml><morexml/></xml>; }));
expect = 'function () { return <xml><morexml/></xml>;}';
expect = 'function () { return <xml><morexml/></xml>; }';
TEST(3, expect, actual);

actual = ToString((function (k) { return <xml>{k}</xml>; }));
expect = 'function (k) { return <xml>{k}</xml>;}';
expect = 'function (k) { return <xml>{k}</xml>; }';
TEST(4, expect, actual);

actual = ToString((function (k) { return <{k}/>; }));
expect = 'function (k) { return <{k}/>;}';
expect = 'function (k) { return <{k}/>; }';
TEST(5, expect, actual);

actual = ToString((function (k) { return <{k}>{k}</{k}>; }));
expect = 'function (k) { return <{k}>{k}</{k}>;}';
expect = 'function (k) { return <{k}>{k}</{k}>; }';
TEST(6, expect, actual);

actual = ToString((function (k) { return <{k}
{k}={k} {"k"}={k + "world"}><{k + "k"}/></{k}>; }));
expect = 'function (k) ' +
'{ return <{k} {k}={k} {"k"}={k + "world"}><{k + "k"}/></{k}>;}';
expect = 'function (k) { return <{k} {k}={k} {"k"}={k + "world"}><{k + "k"}/></{k}>; }';
TEST(7, expect, actual);

END();
Expand Down
5 changes: 4 additions & 1 deletion testsrc/tests/e4x/extensions/regress-321547.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ function a(){
}

actual = a.toSource();
expect = 'function a() {var x = <a><b><c>value c</c></b></a>;return x..c;}';
expect = 'function a(){\n\
var x=<a><b><c>value c</c></b></a>;\n\
return x..c;\n\
}';
actual = actual.replace(/[\n ]+/mg, ' ');
expect = expect.replace(/[\n ]+/mg, ' ');

Expand Down
2 changes: 1 addition & 1 deletion testsrc/tests/js1_5/Scope/regress-185485.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ with (x)
}
status = inSection(5);
actual = x.g.toString();
expect = (function () {}).toString();
expect = (function() {}).toString();
addThis();


Expand Down
2 changes: 1 addition & 1 deletion testsrc/tests/js1_5/decompilation/regress-344120.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function test()
printStatus (summary);

expect = 'function () { x = {1:1};}';
actual = ''+function (){x={1:1}}
actual = ''+function (){x={1:1};}

compareSource(expect, actual, summary);

Expand Down
2 changes: 1 addition & 1 deletion testsrc/tests/js1_5/decompilation/regress-352022.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function test()

var f = function (){a[b] = (c, d)}
actual = f + '';
expect = 'function () {\n a[b] = (c, d);\n}';
expect = 'function () {\n a[b] = (c, d)\n}';

compareSource(expect, actual, summary);

Expand Down
2 changes: 1 addition & 1 deletion testsrc/tests/js1_5/decompilation/regress-352360.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function test()
printStatus (summary);

var f = function() { return -0 };
expect = 'function() { return -0; }';
expect = 'function() { return -0 }';
actual = f + '';
compareSource(expect, actual, summary);

Expand Down
2 changes: 1 addition & 1 deletion testsrc/tests/js1_5/decompilation/regress-353120.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function test()

var f;
f = function() { return (new x)[y]++ }
expect = 'function() { return (new x)[y]++; }';
expect = 'function() { return (new x)[y]++ }';
actual = f + '';
compareSource(expect, actual, summary);

Expand Down
Loading

0 comments on commit 07b7ca1

Please sign in to comment.