|
| 1 | +// SPDX-License-Identifier: BSD-3-Clause |
| 2 | +// Copyright (c) 2024, The OpenROAD Authors |
| 3 | + |
| 4 | +#include <cstdio> |
| 5 | +#include <fstream> |
| 6 | +#include <iterator> |
| 7 | +#include <string> |
| 8 | + |
| 9 | +#include "gtest/gtest.h" |
| 10 | +#include "utl/Logger.h" |
| 11 | + |
| 12 | +namespace utl { |
| 13 | + |
| 14 | +TEST(Utl, WarningMetrics) |
| 15 | +{ |
| 16 | + // ARRANGE |
| 17 | + std::string metrics_filename = "test_metrics_warning_metrics.rpt"; |
| 18 | + // The logger must be scoped to trigger the destructor which writes the |
| 19 | + // metrics |
| 20 | + { |
| 21 | + utl::Logger logger; |
| 22 | + logger.addMetricsSink(metrics_filename.c_str()); |
| 23 | + |
| 24 | + // ACTION |
| 25 | + for (int i = 0; i < 10; i++) { |
| 26 | + logger.warn(utl::UTL, 20, "Test warning UTL-20."); |
| 27 | + } |
| 28 | + logger.warn(utl::UTL, 21, "Another test warning UTL-21."); |
| 29 | + } |
| 30 | + |
| 31 | + // ASSERT |
| 32 | + std::ifstream metrics_file(metrics_filename); |
| 33 | + ASSERT_TRUE(metrics_file.good()); |
| 34 | + std::string content((std::istreambuf_iterator<char>(metrics_file)), |
| 35 | + (std::istreambuf_iterator<char>())); |
| 36 | + |
| 37 | + // The output is a JSON string, which may be multi-line. |
| 38 | + // For simplicity, we'll just check for substrings. |
| 39 | + EXPECT_NE(content.find("\"flow__warnings__count\": 11"), std::string::npos); |
| 40 | + EXPECT_NE(content.find("\"flow__errors__count\": 0"), std::string::npos); |
| 41 | + EXPECT_NE(content.find("\"flow__warnings__count:UTL-20\": 10"), |
| 42 | + std::string::npos); |
| 43 | + EXPECT_NE(content.find("\"flow__warnings__count:UTL-21\": 1"), |
| 44 | + std::string::npos); |
| 45 | + EXPECT_NE(content.find("\"flow__warnings__type_count\": 2"), |
| 46 | + std::string::npos); |
| 47 | + |
| 48 | + // Clean up |
| 49 | + std::remove(metrics_filename.c_str()); |
| 50 | +} |
| 51 | + |
| 52 | +} // namespace utl |
0 commit comments