~singpolyma/biboumi

66887c225b63cecea62d17bcfae40cddef38c9d1 — Florent Le Coz 9 years ago 73fd710
Add a few tests
5 files changed, 101 insertions(+), 1 deletions(-)

M tests/config.cpp
M tests/database.cpp
A tests/logger.cpp
M tests/utils.cpp
M tests/xmpp.cpp
M tests/config.cpp => tests/config.cpp +29 -0
@@ 23,3 23,32 @@ TEST_CASE("Config basic")
    }
  CHECK_FALSE(error);
}

TEST_CASE("Config callbacks")
{
  bool switched = false;
  Config::connect([&switched]()
                  {
                    switched = !switched;
                  });
  CHECK_FALSE(switched);
  Config::set("un", "deux", true);
  CHECK(switched);
  Config::set("un", "trois", true);
  CHECK_FALSE(switched);

  Config::set("un", "trois", false);
  CHECK_FALSE(switched);
}

TEST_CASE("Config get_int")
{
  auto res = Config::get_int("number", 0);
  CHECK(res == 0);
  Config::set("number", "88");
  res = Config::get_int("number", 0);
  CHECK(res == 88);
  Config::set("number", "pouet");
  res = Config::get_int("number", -1);
  CHECK(res == 0);
}

M tests/database.cpp => tests/database.cpp +2 -0
@@ 26,5 26,7 @@ TEST_CASE("Database")

  CHECK(b.pass == "");
  CHECK(b.pass.value() == "");

  Database::close();
#endif
}

A tests/logger.cpp => tests/logger.cpp +50 -0
@@ 0,0 1,50 @@
#include "catch.hpp"

#include <logger/logger.hpp>
#include <config/config.hpp>

#include "io_tester.hpp"
#include <iostream>

using namespace std::string_literals;

TEST_CASE("Basic logging")
{
  Logger::instance().reset();
  GIVEN("A logger with log_level 0")
    {
      Config::set("log_level", "0");
      WHEN("we log some debug text")
        {
          IoTester<std::ostream> out(std::cout);
          log_debug("debug");
          THEN("debug logs are written")
            CHECK(out.str() == "<7>tests/logger.cpp:" + std::to_string(__LINE__ - 2) + ":\tdebug\n");
        }
      WHEN("we log some errors")
        {
          IoTester<std::ostream> out(std::cout);
          log_error("error");
          THEN("error logs are written")
            CHECK(out.str() == "<3>tests/logger.cpp:" + std::to_string(__LINE__ - 2) + ":\terror\n");
        }
    }
  GIVEN("A logger with log_level 3")
    {
      Config::set("log_level", "3");
      WHEN("we log some debug text")
        {
          IoTester<std::ostream> out(std::cout);
          log_debug("debug");
          THEN("nothing is written")
            CHECK(out.str().empty());
        }
      WHEN("we log some errors")
        {
          IoTester<std::ostream> out(std::cout);
          log_error("error");
          THEN("error logs are still written")
            CHECK(out.str() == "<3>tests/logger.cpp:" + std::to_string(__LINE__ - 2) + ":\terror\n");
        }
    }
}

M tests/utils.cpp => tests/utils.cpp +18 -0
@@ 5,6 5,7 @@
#include <utils/string.hpp>
#include <utils/split.hpp>
#include <utils/xdg.hpp>
#include <utils/empty_if_fixed_server.hpp>

TEST_CASE("String split")
{


@@ 70,3 71,20 @@ TEST_CASE("xdg_*_path")
      CHECK(res == "/datadir/biboumi/bonjour.txt");
    }
}

TEST_CASE("empty if fixed irc server")
{
  GIVEN("A config with fixed_irc_server")
    {
      Config::set("fixed_irc_server", "irc.localhost");
      THEN("our string is made empty")
        CHECK(utils::empty_if_fixed_server("coucou coucou") == "");
    }
  GIVEN("A config with NO fixed_irc_server")
    {
      Config::set("fixed_irc_server", "");
      THEN("our string is returned untouched")
        CHECK(utils::empty_if_fixed_server("coucou coucou") == "coucou coucou");
    }

}

M tests/xmpp.cpp => tests/xmpp.cpp +2 -1
@@ 26,6 26,8 @@ TEST_CASE("Test basic XML parsing")
        // Do the same checks on a copy of that stanza.
        Stanza copy(stanza);
        check_stanza(copy);
        // And do the same checks on moved-constructed stanza
        Stanza moved(std::move(copy));
      });
  xml.feed(doc.data(), doc.size(), true);



@@ 44,4 46,3 @@ TEST_CASE("XML escape/unescape")
  CHECK(xml_escape(unescaped) == "&apos;coucou&apos;&lt;cc&gt;/&amp;&quot;gaga&quot;");
  CHECK(xml_unescape(xml_escape(unescaped)) == unescaped);
}