aboutsummaryrefslogtreecommitdiff
path: root/test/scopemeasure.cpp
blob: 24aa5c16f45cd747cc8a3d1d312325f14aa53276 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#define ENABLE_IMPLICIT_FILEPATH_CONVERSIONS

#include "scopemeasure.h"

#include "3rd-party/catch.hpp"

#include <fstream>

#include "logger.h"

#include "test_helpers/loggerresetter.h"
#include "test_helpers/tempfile.h"

using namespace newsboat;

unsigned int file_lines_count(const std::string& filepath)
{
	unsigned int line_count = 0;
	std::ifstream in(filepath);
	std::string line;
	while (std::getline(in, line)) {
		line_count++;
	}

	return line_count;
}

TEST_CASE("Destroying a ScopeMeasure object writes a line to the log",
	"[ScopeMeasure]")
{
	test_helpers::TempFile tmp;

	{
		test_helpers::LoggerResetter logReset;
		const auto filepath = Filepath::from_locale_string(tmp.get_path());
		logger::set_logfile(filepath);
		logger::set_loglevel(Level::DEBUG);

		ScopeMeasure sm("test");
	}

	REQUIRE(file_lines_count(tmp.get_path()) == 1);
}

TEST_CASE("stopover() adds an extra line to the log upon each call",
	"[ScopeMeasure]")
{
	test_helpers::TempFile tmp;

	// initialized to an impossible value to catch logical errors in the test
	// itself
	unsigned int expected_line_count = 100500;

	{
		test_helpers::LoggerResetter logReset;
		const auto filepath = Filepath::from_locale_string(tmp.get_path());
		logger::set_logfile(filepath);
		logger::set_loglevel(Level::DEBUG);

		ScopeMeasure sm("test");

		SECTION("one call") {
			sm.stopover("here");
			// One line from stopover(), one line from destructor
			expected_line_count = 2;
		}

		SECTION("two calls") {
			sm.stopover("here");
			sm.stopover("there");
			// Two lines from stopover(), one line from destructor
			expected_line_count = 3;
		}

		SECTION("five calls") {
			sm.stopover("here");
			sm.stopover("there");
			sm.stopover("and");
			sm.stopover("also");
			sm.stopover("here");
			// Five lines from stopover(), one line from destructor
			expected_line_count = 6;
		}
	}

	REQUIRE(file_lines_count(tmp.get_path()) == expected_line_count);
}