• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2020 The Tint Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "src/diagnostic/printer.h"
16 
17 #include "gtest/gtest.h"
18 
19 namespace tint {
20 namespace diag {
21 namespace {
22 
23 // Actually verifying that the expected colors are printed is exceptionally
24 // difficult as:
25 // a) The color emission varies by OS.
26 // b) The logic checks to see if the printer is writing to a terminal, making
27 //    mocking hard.
28 // c) Actually probing what gets written to a FILE* is notoriously tricky.
29 //
30 // The least we can do is to exersice the code - which is what we do here.
31 // The test will print each of the colors, and can be examined with human
32 // eyeballs.
33 // This can be enabled or disabled with ENABLE_PRINTER_TESTS
34 #define ENABLE_PRINTER_TESTS 0
35 #if ENABLE_PRINTER_TESTS
36 
37 using PrinterTest = testing::Test;
38 
TEST_F(PrinterTest,WithColors)39 TEST_F(PrinterTest, WithColors) {
40   auto printer = Printer::create(stdout, true);
41   printer->write("Default", Style{Color::kDefault, false});
42   printer->write("Black", Style{Color::kBlack, false});
43   printer->write("Red", Style{Color::kRed, false});
44   printer->write("Green", Style{Color::kGreen, false});
45   printer->write("Yellow", Style{Color::kYellow, false});
46   printer->write("Blue", Style{Color::kBlue, false});
47   printer->write("Magenta", Style{Color::kMagenta, false});
48   printer->write("Cyan", Style{Color::kCyan, false});
49   printer->write("White", Style{Color::kWhite, false});
50   printf("\n");
51 }
52 
TEST_F(PrinterTest,BoldWithColors)53 TEST_F(PrinterTest, BoldWithColors) {
54   auto printer = Printer::create(stdout, true);
55   printer->write("Default", Style{Color::kDefault, true});
56   printer->write("Black", Style{Color::kBlack, true});
57   printer->write("Red", Style{Color::kRed, true});
58   printer->write("Green", Style{Color::kGreen, true});
59   printer->write("Yellow", Style{Color::kYellow, true});
60   printer->write("Blue", Style{Color::kBlue, true});
61   printer->write("Magenta", Style{Color::kMagenta, true});
62   printer->write("Cyan", Style{Color::kCyan, true});
63   printer->write("White", Style{Color::kWhite, true});
64   printf("\n");
65 }
66 
TEST_F(PrinterTest,WithoutColors)67 TEST_F(PrinterTest, WithoutColors) {
68   auto printer = Printer::create(stdout, false);
69   printer->write("Default", Style{Color::kDefault, false});
70   printer->write("Black", Style{Color::kBlack, false});
71   printer->write("Red", Style{Color::kRed, false});
72   printer->write("Green", Style{Color::kGreen, false});
73   printer->write("Yellow", Style{Color::kYellow, false});
74   printer->write("Blue", Style{Color::kBlue, false});
75   printer->write("Magenta", Style{Color::kMagenta, false});
76   printer->write("Cyan", Style{Color::kCyan, false});
77   printer->write("White", Style{Color::kWhite, false});
78   printf("\n");
79 }
80 
TEST_F(PrinterTest,BoldWithoutColors)81 TEST_F(PrinterTest, BoldWithoutColors) {
82   auto printer = Printer::create(stdout, false);
83   printer->write("Default", Style{Color::kDefault, true});
84   printer->write("Black", Style{Color::kBlack, true});
85   printer->write("Red", Style{Color::kRed, true});
86   printer->write("Green", Style{Color::kGreen, true});
87   printer->write("Yellow", Style{Color::kYellow, true});
88   printer->write("Blue", Style{Color::kBlue, true});
89   printer->write("Magenta", Style{Color::kMagenta, true});
90   printer->write("Cyan", Style{Color::kCyan, true});
91   printer->write("White", Style{Color::kWhite, true});
92   printf("\n");
93 }
94 
95 #endif  // ENABLE_PRINTER_TESTS
96 }  // namespace
97 }  // namespace diag
98 }  // namespace tint
99