Path: blob/main/contrib/kyua/utils/logging/macros_test.cpp
48178 views
// Copyright 2011 The Kyua Authors.1// All rights reserved.2//3// Redistribution and use in source and binary forms, with or without4// modification, are permitted provided that the following conditions are5// met:6//7// * Redistributions of source code must retain the above copyright8// notice, this list of conditions and the following disclaimer.9// * Redistributions in binary form must reproduce the above copyright10// notice, this list of conditions and the following disclaimer in the11// documentation and/or other materials provided with the distribution.12// * Neither the name of Google Inc. nor the names of its contributors13// may be used to endorse or promote products derived from this software14// without specific prior written permission.15//16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.2728#include "utils/logging/macros.hpp"2930#include <fstream>31#include <string>3233#include <atf-c++.hpp>3435#include "utils/datetime.hpp"36#include "utils/fs/path.hpp"37#include "utils/logging/operations.hpp"3839namespace datetime = utils::datetime;40namespace fs = utils::fs;41namespace logging = utils::logging;424344ATF_TEST_CASE_WITHOUT_HEAD(ld);45ATF_TEST_CASE_BODY(ld)46{47logging::set_persistency("debug", fs::path("test.log"));48datetime::set_mock_now(2011, 2, 21, 18, 30, 0, 0);49LD("Debug message");5051std::ifstream input("test.log");52ATF_REQUIRE(input);5354std::string line;55ATF_REQUIRE(std::getline(input, line).good());56ATF_REQUIRE_MATCH("20110221-183000 D .*: Debug message", line);57}585960ATF_TEST_CASE_WITHOUT_HEAD(le);61ATF_TEST_CASE_BODY(le)62{63logging::set_persistency("debug", fs::path("test.log"));64datetime::set_mock_now(2011, 2, 21, 18, 30, 0, 0);65LE("Error message");6667std::ifstream input("test.log");68ATF_REQUIRE(input);6970std::string line;71ATF_REQUIRE(std::getline(input, line).good());72ATF_REQUIRE_MATCH("20110221-183000 E .*: Error message", line);73}747576ATF_TEST_CASE_WITHOUT_HEAD(li);77ATF_TEST_CASE_BODY(li)78{79logging::set_persistency("debug", fs::path("test.log"));80datetime::set_mock_now(2011, 2, 21, 18, 30, 0, 0);81LI("Info message");8283std::ifstream input("test.log");84ATF_REQUIRE(input);8586std::string line;87ATF_REQUIRE(std::getline(input, line).good());88ATF_REQUIRE_MATCH("20110221-183000 I .*: Info message", line);89}909192ATF_TEST_CASE_WITHOUT_HEAD(lw);93ATF_TEST_CASE_BODY(lw)94{95logging::set_persistency("debug", fs::path("test.log"));96datetime::set_mock_now(2011, 2, 21, 18, 30, 0, 0);97LW("Warning message");9899std::ifstream input("test.log");100ATF_REQUIRE(input);101102std::string line;103ATF_REQUIRE(std::getline(input, line).good());104ATF_REQUIRE_MATCH("20110221-183000 W .*: Warning message", line);105}106107108ATF_INIT_TEST_CASES(tcs)109{110ATF_ADD_TEST_CASE(tcs, ld);111ATF_ADD_TEST_CASE(tcs, le);112ATF_ADD_TEST_CASE(tcs, li);113ATF_ADD_TEST_CASE(tcs, lw);114}115116117