1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: set ts=8 sts=2 et sw=2 tw=80:
4 /* This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
8 #include <cstdio> // fclose, fflush, open_memstream
10 #include "js/ErrorReport.h" // JS::PrintError
11 #include "js/Warnings.h" // JS::SetWarningReporter, JS::WarnUTF8
13 #include "jsapi-tests/tests.h"
15 class AutoStreamBuffer
{
21 AutoStreamBuffer() { fp
= open_memstream(&buffer
, &size
); }
28 FILE* stream() { return fp
; }
30 bool contains(const char* str
) {
31 if (fflush(fp
) != 0) {
32 fprintf(stderr
, "Error flushing stream\n");
35 if (strcmp(buffer
, str
) != 0) {
36 fprintf(stderr
, "Expected |%s|, got |%s|\n", str
, buffer
);
43 BEGIN_TEST(testPrintError_Works
) {
46 CHECK(!execDontReport("throw null;", "testPrintError_Works.js", 3));
48 JS::ExceptionStack
exnStack(cx
);
49 CHECK(JS::StealPendingExceptionStack(cx
, &exnStack
));
51 JS::ErrorReportBuilder
builder(cx
);
52 CHECK(builder
.init(cx
, exnStack
, JS::ErrorReportBuilder::NoSideEffects
));
53 JS::PrintError(buf
.stream(), builder
, false);
55 CHECK(buf
.contains("testPrintError_Works.js:3:1 uncaught exception: null\n"));
59 END_TEST(testPrintError_Works
)
61 BEGIN_TEST(testPrintError_SkipWarning
) {
62 JS::SetWarningReporter(cx
, warningReporter
);
63 CHECK(JS::WarnUTF8(cx
, "warning message"));
64 CHECK(warningSuccess
);
68 static bool warningSuccess
;
70 static void warningReporter(JSContext
* cx
, JSErrorReport
* report
) {
72 JS::PrintError(buf
.stream(), report
, false);
73 warningSuccess
= buf
.contains("");
75 END_TEST(testPrintError_SkipWarning
)
77 bool cls_testPrintError_SkipWarning::warningSuccess
= false;
79 BEGIN_TEST(testPrintError_PrintWarning
) {
80 JS::SetWarningReporter(cx
, warningReporter
);
81 CHECK(JS::WarnUTF8(cx
, "warning message"));
82 CHECK(warningSuccess
);
86 static bool warningSuccess
;
88 static void warningReporter(JSContext
* cx
, JSErrorReport
* report
) {
90 JS::PrintError(buf
.stream(), report
, true);
91 warningSuccess
= buf
.contains("warning: warning message\n");
93 END_TEST(testPrintError_PrintWarning
)
95 bool cls_testPrintError_PrintWarning::warningSuccess
= false;
97 #define BURRITO "\xF0\x9F\x8C\xAF"
99 BEGIN_TEST(testPrintError_UTF16CodeUnits
) {
100 AutoStreamBuffer buf
;
102 static const char utf8code
[] =
103 "function f() {\n var x = `\n" BURRITO
"`; " BURRITO
"; } f();";
105 CHECK(!execDontReport(utf8code
, "testPrintError_UTF16CodeUnits.js", 1));
107 JS::ExceptionStack
exnStack(cx
);
108 CHECK(JS::StealPendingExceptionStack(cx
, &exnStack
));
110 JS::ErrorReportBuilder
builder(cx
);
111 CHECK(builder
.init(cx
, exnStack
, JS::ErrorReportBuilder::NoSideEffects
));
112 JS::PrintError(buf
.stream(), builder
, false);
115 buf
.contains("testPrintError_UTF16CodeUnits.js:3:6 SyntaxError: illegal "
116 "character U+1F32F:\n"
117 "testPrintError_UTF16CodeUnits.js:3:6 " BURRITO
"`; " BURRITO
119 "testPrintError_UTF16CodeUnits.js:3:6 .....^\n"));
123 END_TEST(testPrintError_UTF16CodeUnits
)