Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
log_test.cc
Go to the documentation of this file.
1// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
2// for details. All rights reserved. Use of this source code is governed by a
3// BSD-style license that can be found in the LICENSE file.
4
5#include "platform/globals.h"
6
8#include "platform/utils.h"
9#include "vm/dart_api_impl.h"
10#include "vm/dart_entry.h"
11#include "vm/debugger.h"
12#include "vm/globals.h"
13#include "vm/isolate.h"
14#include "vm/log.h"
15#include "vm/message_handler.h"
16#include "vm/unit_test.h"
17
18namespace dart {
19
20static const char* test_output_ = nullptr;
21
22static void TestPrinter(const char* buffer) {
23 if (test_output_ != nullptr) {
24 free(const_cast<char*>(test_output_));
25 test_output_ = nullptr;
26 }
28
29 // Also print to stdout to see the overall result.
31}
32
33class LogTestHelper : public AllStatic {
34 public:
35 static void SetPrinter(Log* log, LogPrinter printer) {
36 ASSERT(log != nullptr);
37 ASSERT(printer != nullptr);
38 log->printer_ = printer;
39 }
40
41 static void FreeTestOutput() {
42 if (test_output_ != nullptr) {
43 free(const_cast<char*>(test_output_));
44 test_output_ = nullptr;
45 }
46 }
47};
48
49TEST_CASE(Log_Macro) {
50 test_output_ = nullptr;
51 Log* log = Log::Current();
53
54 THR_Print("Hello %s", "World");
55 EXPECT_STREQ("Hello World", test_output_);
56 THR_Print("SingleArgument");
57 EXPECT_STREQ("SingleArgument", test_output_);
59}
60
61TEST_CASE(Log_Basic) {
62 test_output_ = nullptr;
63 Log* log = new Log(TestPrinter);
64
65 EXPECT_EQ(static_cast<const char*>(nullptr), test_output_);
66 log->Print("Hello %s", "World");
67 EXPECT_STREQ("Hello World", test_output_);
68
69 delete log;
71}
72
73TEST_CASE(Log_Block) {
74 test_output_ = nullptr;
75 Log* log = new Log(TestPrinter);
76
77 EXPECT_EQ(static_cast<const char*>(nullptr), test_output_);
78 {
79 LogBlock ba(thread, log);
80 log->Print("APPLE");
81 EXPECT_EQ(static_cast<const char*>(nullptr), test_output_);
82 {
83 LogBlock ba(thread, log);
84 log->Print("BANANA");
85 EXPECT_EQ(static_cast<const char*>(nullptr), test_output_);
86 }
87 EXPECT_EQ(static_cast<const char*>(nullptr), test_output_);
88 {
89 LogBlock ba(thread, log);
90 log->Print("PEAR");
91 EXPECT_EQ(static_cast<const char*>(nullptr), test_output_);
92 }
93 EXPECT_EQ(static_cast<const char*>(nullptr), test_output_);
94 }
95 EXPECT_STREQ("APPLEBANANAPEAR", test_output_);
96 delete log;
98}
99
100} // namespace dart
static void FreeTestOutput()
Definition log_test.cc:41
static void SetPrinter(Log *log, LogPrinter printer)
Definition log_test.cc:35
static Log * Current()
Definition log.cc:75
static void static void PrintErr(const char *format,...) PRINTF_ATTRIBUTE(1
static char * StrDup(const char *s)
#define THR_Print(format,...)
Definition log.h:20
#define ASSERT(E)
static const uint8_t buffer[]
static void TestPrinter(const char *buffer)
Definition log_test.cc:22
static const char * test_output_
Definition log_test.cc:20
void(* LogPrinter)(const char *data)
Definition log.h:25
#define TEST_CASE(name)
Definition unit_test.h:85