Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

more console fixes and tests #1208

Merged
merged 2 commits into from
Apr 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/org/mozilla/javascript/NativeConsole.java
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ private void jsAssert(Context cx, Scriptable scope, Object[] args) {
System.arraycopy(args, 1, newArgs, 0, newArgs.length);
args = newArgs;
} else {
args[0] = "Assertion failed: ";
args[0] = "Assertion failed:";
}

printer.print(cx, scope, Level.ERROR, args, null);
Expand Down
67 changes: 65 additions & 2 deletions testsrc/org/mozilla/javascript/tests/NativeConsoleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ public void assertEquals(PrinterCall expectedCall) {
}

private static class DummyConsolePrinter implements NativeConsole.ConsolePrinter {
public List<PrinterCall> calls = new ArrayList<>();
private List<PrinterCall> calls = new ArrayList<>();
private String msg;

@Override
public void print(
Expand All @@ -82,6 +83,14 @@ public void print(
Object[] args,
ScriptStackElement[] stack) {
calls.add(new PrinterCall(level, args, stack));

msg = NativeConsole.format(cx, scope, args);
if (stack != null) {
for (ScriptStackElement scriptStackElement : stack) {
msg += "\n";
msg += scriptStackElement;
}
}
}

public void assertCalls(List<PrinterCall> expectedCalls) {
Expand All @@ -90,6 +99,10 @@ public void assertCalls(List<PrinterCall> expectedCalls) {
calls.get(i).assertEquals(expectedCalls.get(i));
}
}

public void assertMsf(String expectedMsg) {
assertEquals(expectedMsg, msg);
}
}

@Test
Expand Down Expand Up @@ -314,6 +327,7 @@ public void print() {
assertPrintCalls(
"console.log('abc', 123)",
Collections.singletonList(new PrinterCall(Level.INFO, new Object[] {"abc", 123})));
assertPrintMsg("console.log('abc', 123)", "abc 123");

assertPrintCalls(
"console.trace('abc', 123)",
Expand All @@ -324,22 +338,50 @@ public void print() {
new ScriptStackElement[] {
new ScriptStackElement("source", null, 1)
})));
assertPrintMsg("console.trace('abc', 123)", "abc 123\n@source:1");

assertPrintCalls(
"console.debug('abc', 123)",
Collections.singletonList(new PrinterCall(Level.DEBUG, new Object[] {"abc", 123})));
assertPrintMsg("console.debug('abc', 123)", "abc 123");

assertPrintCalls(
"console.info('abc', 123)",
Collections.singletonList(new PrinterCall(Level.INFO, new Object[] {"abc", 123})));
assertPrintMsg("console.info('abc', 123)", "abc 123");

assertPrintCalls(
"console.warn('abc', 123)",
Collections.singletonList(new PrinterCall(Level.WARN, new Object[] {"abc", 123})));
assertPrintMsg("console.warn('abc', 123)", "abc 123");

assertPrintCalls(
"console.error('abc', 123)",
Collections.singletonList(new PrinterCall(Level.ERROR, new Object[] {"abc", 123})));
assertPrintMsg("console.error('abc', 123)", "abc 123");
}

@Test
public void trace() {
assertPrintMsg(
" function foo() {\n"
+ " function bar() {\n"
+ " console.trace();\n"
+ " }\n"
+ " bar();\n"
+ " }\n"
+ " foo();\n",
"\n" + "bar()@source:3\n" + "foo()@source:5\n" + "@source:7");

assertPrintMsg(
" function foo() {\n"
+ " function bar() {\n"
+ " console.trace('the word is %s', 'foo');\n"
+ " }\n"
+ " bar();\n"
+ " }\n"
+ " foo();\n",
"the word is foo\n" + "bar()@source:3\n" + "foo()@source:5\n" + "@source:7");
}

@Test
Expand All @@ -351,35 +393,45 @@ public void testAssert() {
Collections.singletonList(
new PrinterCall(
Level.ERROR, new String[] {"Assertion failed: console.assert"})));
assertPrintMsg("console.assert(false)", "Assertion failed: console.assert");

assertPrintCalls(
"console.assert()",
Collections.singletonList(
new PrinterCall(
Level.ERROR, new String[] {"Assertion failed: console.assert"})));
assertPrintMsg("console.assert()", "Assertion failed: console.assert");

assertPrintCalls(
"console.assert(false, 'Fail')",
Collections.singletonList(
new PrinterCall(Level.ERROR, new String[] {"Assertion failed: Fail"})));
assertPrintMsg("console.assert(false, 'Fail')", "Assertion failed: Fail");

assertPrintCalls(
"console.assert(false, 'Fail', 1)",
Collections.singletonList(
new PrinterCall(
Level.ERROR, new Object[] {"Assertion failed: Fail", 1.0})));
assertPrintMsg("console.assert(false, 'Fail', 1)", "Assertion failed: Fail 1");

assertPrintCalls(
"console.assert(false, 'the word is %s', 'foo')",
Collections.singletonList(
new PrinterCall(
Level.ERROR,
new String[] {"Assertion failed: the word is %s", "foo"})));
assertPrintMsg(
"console.assert(false, 'the word is %s', 'foo')",
"Assertion failed: the word is foo");

assertPrintCalls(
"console.assert(false, 42)",
Collections.singletonList(
new PrinterCall(Level.ERROR, new Object[] {"Assertion failed: ", 42})));
new PrinterCall(Level.ERROR, new Object[] {"Assertion failed:", 42})));
assertPrintMsg("console.assert(false, 42)", "Assertion failed: 42");

assertPrintMsg("console.assert(false, {a: 7})", "Assertion failed: {\"a\":7}");
}

@Test
Expand Down Expand Up @@ -449,4 +501,15 @@ private static void assertPrintCalls(String source, List<PrinterCall> expectedCa
printer.assertCalls(expectedCalls);
}
}

private static void assertPrintMsg(String source, String expectedMsg) {
DummyConsolePrinter printer = new DummyConsolePrinter();

try (Context cx = Context.enter()) {
Scriptable scope = cx.initStandardObjects();
NativeConsole.init(scope, false, printer);
cx.evaluateString(scope, source, "source", 1, null);
printer.assertMsf(expectedMsg);
}
}
}