Backed out changeset 2450366cf7ca (bug 1891629) for causing win msix mochitest failures
[gecko.git] / js / src / jsapi-tests / testPrintError.cpp
blob31d2c2a95f7612acf7fbe6087f03a6d726ac1219
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:
3 */
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 {
16 char* buffer;
17 size_t size;
18 FILE* fp;
20 public:
21 AutoStreamBuffer() { fp = open_memstream(&buffer, &size); }
23 ~AutoStreamBuffer() {
24 fclose(fp);
25 free(buffer);
28 FILE* stream() { return fp; }
30 bool contains(const char* str) {
31 if (fflush(fp) != 0) {
32 fprintf(stderr, "Error flushing stream\n");
33 return false;
35 if (strcmp(buffer, str) != 0) {
36 fprintf(stderr, "Expected |%s|, got |%s|\n", str, buffer);
37 return false;
39 return true;
43 BEGIN_TEST(testPrintError_Works) {
44 AutoStreamBuffer buf;
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"));
57 return true;
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);
65 return true;
68 static bool warningSuccess;
70 static void warningReporter(JSContext* cx, JSErrorReport* report) {
71 AutoStreamBuffer buf;
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);
83 return true;
86 static bool warningSuccess;
88 static void warningReporter(JSContext* cx, JSErrorReport* report) {
89 AutoStreamBuffer buf;
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);
114 CHECK(
115 buf.contains("testPrintError_UTF16CodeUnits.js:3:6 SyntaxError: illegal "
116 "character U+1F32F:\n"
117 "testPrintError_UTF16CodeUnits.js:3:6 " BURRITO "`; " BURRITO
118 "; } f();\n"
119 "testPrintError_UTF16CodeUnits.js:3:6 .....^\n"));
121 return true;
123 END_TEST(testPrintError_UTF16CodeUnits)
125 #undef BURRITO