blob: fc8f99a717f9481efd63eda782c9db2545a60095 [file] [log] [blame]
Alexander Afanasyev4ffcff22014-09-02 15:39:20 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento28229df2020-01-04 15:11:25 -05002/*
3 * Copyright (c) 2014-2020, Regents of the University of California,
Alexander Afanasyevfde570c2016-12-19 16:02:55 -08004 * Arizona Board of Regents,
5 * Colorado State University,
6 * University Pierre & Marie Curie, Sorbonne University,
7 * Washington University in St. Louis,
8 * Beijing Institute of Technology,
9 * The University of Memphis.
Alexander Afanasyev4ffcff22014-09-02 15:39:20 -070010 *
Alexander Afanasyevfde570c2016-12-19 16:02:55 -080011 * This file is part of NDNS (Named Data Networking Domain Name Service) and is
12 * based on the code written as part of NFD (Named Data Networking Daemon).
Alexander Afanasyev4ffcff22014-09-02 15:39:20 -070013 * See AUTHORS.md for complete list of NDNS authors and contributors.
14 *
15 * NDNS is free software: you can redistribute it and/or modify it under the terms
16 * of the GNU General Public License as published by the Free Software Foundation,
17 * either version 3 of the License, or (at your option) any later version.
18 *
19 * NDNS is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
20 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
21 * PURPOSE. See the GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License along with
24 * NDNS, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
25 */
26
Davide Pesavento28229df2020-01-04 15:11:25 -050027#define BOOST_TEST_MODULE NDNS Unit Tests
Alexander Afanasyevaa46c272016-03-09 12:54:12 -080028
Alexander Afanasyevc7c99002015-10-09 17:27:30 -070029#include <boost/version.hpp>
Alexander Afanasyevfde570c2016-12-19 16:02:55 -080030
31#if BOOST_VERSION >= 106200
32// Boost.Test v3.3 (Boost 1.62) natively supports multi-logger output
33#include "boost-test.hpp"
34#else
Davide Pesavento28229df2020-01-04 15:11:25 -050035#define BOOST_TEST_ALTERNATIVE_INIT_API
Alexander Afanasyevfde570c2016-12-19 16:02:55 -080036#define BOOST_TEST_NO_MAIN
37#include "boost-test.hpp"
Alexander Afanasyevfde570c2016-12-19 16:02:55 -080038#include "boost-multi-log-formatter.hpp"
39
40#include <boost/program_options/options_description.hpp>
41#include <boost/program_options/variables_map.hpp>
42#include <boost/program_options/parsers.hpp>
Alexander Afanasyev59a42ec2014-09-02 18:21:16 -070043
Alexander Afanasyevaa46c272016-03-09 12:54:12 -080044#include <fstream>
45#include <iostream>
Alexander Afanasyev59a42ec2014-09-02 18:21:16 -070046
Alexander Afanasyevfde570c2016-12-19 16:02:55 -080047static bool
48init_tests()
Alexander Afanasyev59a42ec2014-09-02 18:21:16 -070049{
Alexander Afanasyevfde570c2016-12-19 16:02:55 -080050 init_unit_test();
Alexander Afanasyevaa46c272016-03-09 12:54:12 -080051
Alexander Afanasyevfde570c2016-12-19 16:02:55 -080052 namespace po = boost::program_options;
53 namespace ut = boost::unit_test;
Alexander Afanasyevaa46c272016-03-09 12:54:12 -080054
Alexander Afanasyevfde570c2016-12-19 16:02:55 -080055 po::options_description extraOptions;
56 std::string logger;
57 std::string outputFile = "-";
58 extraOptions.add_options()
59 ("log_format2", po::value<std::string>(&logger), "Type of second log formatter: HRF or XML")
60 ("log_sink2", po::value<std::string>(&outputFile)->default_value(outputFile), "Second log sink, - for stdout")
61 ;
62 po::variables_map vm;
63 try {
64 po::store(po::command_line_parser(ut::framework::master_test_suite().argc,
65 ut::framework::master_test_suite().argv)
66 .options(extraOptions)
67 .run(),
68 vm);
69 po::notify(vm);
70 }
71 catch (const std::exception& e) {
72 std::cerr << "ERROR: " << e.what() << "\n"
73 << extraOptions << std::endl;
74 return false;
Alexander Afanasyev59a42ec2014-09-02 18:21:16 -070075 }
Alexander Afanasyevaa46c272016-03-09 12:54:12 -080076
Alexander Afanasyevfde570c2016-12-19 16:02:55 -080077 if (vm.count("log_format2") == 0) {
78 // second logger is not configured
79 return true;
Alexander Afanasyevaa46c272016-03-09 12:54:12 -080080 }
81
Alexander Afanasyevfde570c2016-12-19 16:02:55 -080082 std::shared_ptr<ut::unit_test_log_formatter> formatter;
83 if (logger == "XML") {
84 formatter = std::make_shared<ut::output::xml_log_formatter>();
85 }
86 else if (logger == "HRF") {
87 formatter = std::make_shared<ut::output::compiler_log_formatter>();
88 }
89 else {
90 std::cerr << "ERROR: only HRF or XML log formatter can be specified" << std::endl;
91 return false;
92 }
Alexander Afanasyev59a42ec2014-09-02 18:21:16 -070093
Alexander Afanasyevfde570c2016-12-19 16:02:55 -080094 std::shared_ptr<std::ostream> output;
95 if (outputFile == "-") {
96 output = std::shared_ptr<std::ostream>(&std::cout, std::bind([]{}));
97 }
98 else {
99 output = std::make_shared<std::ofstream>(outputFile.c_str());
100 }
Alexander Afanasyev59a42ec2014-09-02 18:21:16 -0700101
Alexander Afanasyevfde570c2016-12-19 16:02:55 -0800102 auto multiFormatter = new ut::output::multi_log_formatter;
103 multiFormatter->add(formatter, output);
104 ut::unit_test_log.set_formatter(multiFormatter);
105
106 return true;
107}
108
109int
110main(int argc, char* argv[])
111{
112 return ::boost::unit_test::unit_test_main(&init_tests, argc, argv);
113}
114
115#endif // BOOST_VERSION >= 106200