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 | |
7 | #include "include/dart_tools_api.h" |
8 | #include "vm/dart_api_impl.h" |
9 | #include "vm/dart_entry.h" |
10 | #include "vm/debugger.h" |
11 | #include "vm/globals.h" |
12 | #include "vm/isolate.h" |
13 | #include "vm/log.h" |
14 | #include "vm/message_handler.h" |
15 | #include "vm/unit_test.h" |
16 | |
17 | namespace dart { |
18 | |
19 | static const char* test_output_ = NULL; |
20 | |
21 | PRINTF_ATTRIBUTE(1, 2) |
22 | static void TestPrinter(const char* format, ...) { |
23 | // Measure. |
24 | va_list args; |
25 | va_start(args, format); |
26 | intptr_t len = Utils::VSNPrint(NULL, 0, format, args); |
27 | va_end(args); |
28 | |
29 | // Print string to buffer. |
30 | char* buffer = reinterpret_cast<char*>(malloc(len + 1)); |
31 | va_list args2; |
32 | va_start(args2, format); |
33 | Utils::VSNPrint(buffer, (len + 1), format, args2); |
34 | va_end(args2); |
35 | |
36 | if (test_output_ != NULL) { |
37 | free(const_cast<char*>(test_output_)); |
38 | test_output_ = NULL; |
39 | } |
40 | test_output_ = buffer; |
41 | |
42 | // Also print to stdout to see the overall result. |
43 | OS::PrintErr("%s" , test_output_); |
44 | } |
45 | |
46 | class LogTestHelper : public AllStatic { |
47 | public: |
48 | static void SetPrinter(Log* log, LogPrinter printer) { |
49 | ASSERT(log != NULL); |
50 | ASSERT(printer != NULL); |
51 | log->printer_ = printer; |
52 | } |
53 | |
54 | static void FreeTestOutput() { |
55 | if (test_output_ != NULL) { |
56 | free(const_cast<char*>(test_output_)); |
57 | test_output_ = NULL; |
58 | } |
59 | } |
60 | }; |
61 | |
62 | TEST_CASE(Log_Macro) { |
63 | test_output_ = NULL; |
64 | Log* log = Log::Current(); |
65 | LogTestHelper::SetPrinter(log, TestPrinter); |
66 | |
67 | THR_Print("Hello %s" , "World" ); |
68 | EXPECT_STREQ("Hello World" , test_output_); |
69 | THR_Print("SingleArgument" ); |
70 | EXPECT_STREQ("SingleArgument" , test_output_); |
71 | LogTestHelper::FreeTestOutput(); |
72 | } |
73 | |
74 | TEST_CASE(Log_Basic) { |
75 | test_output_ = NULL; |
76 | Log* log = new Log(TestPrinter); |
77 | |
78 | EXPECT_EQ(reinterpret_cast<const char*>(NULL), test_output_); |
79 | log->Print("Hello %s" , "World" ); |
80 | EXPECT_STREQ("Hello World" , test_output_); |
81 | |
82 | delete log; |
83 | LogTestHelper::FreeTestOutput(); |
84 | } |
85 | |
86 | TEST_CASE(Log_Block) { |
87 | test_output_ = NULL; |
88 | Log* log = new Log(TestPrinter); |
89 | |
90 | EXPECT_EQ(reinterpret_cast<const char*>(NULL), test_output_); |
91 | { |
92 | LogBlock ba(thread, log); |
93 | log->Print("APPLE" ); |
94 | EXPECT_EQ(reinterpret_cast<const char*>(NULL), test_output_); |
95 | { |
96 | LogBlock ba(thread, log); |
97 | log->Print("BANANA" ); |
98 | EXPECT_EQ(reinterpret_cast<const char*>(NULL), test_output_); |
99 | } |
100 | EXPECT_EQ(reinterpret_cast<const char*>(NULL), test_output_); |
101 | { |
102 | LogBlock ba(thread, log); |
103 | log->Print("PEAR" ); |
104 | EXPECT_EQ(reinterpret_cast<const char*>(NULL), test_output_); |
105 | } |
106 | EXPECT_EQ(reinterpret_cast<const char*>(NULL), test_output_); |
107 | } |
108 | EXPECT_STREQ("APPLEBANANAPEAR" , test_output_); |
109 | delete log; |
110 | LogTestHelper::FreeTestOutput(); |
111 | } |
112 | |
113 | } // namespace dart |
114 | |