File: history.cpp

package info (click to toggle)
newsboat 2.38-0.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,540 kB
  • sloc: cpp: 90,216; xml: 606; sh: 429; makefile: 369; ruby: 258; python: 239; ansic: 211; php: 63; awk: 59; perl: 38
file content (98 lines) | stat: -rw-r--r-- 2,614 bytes parent folder | download | duplicates (2)
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
88
89
90
91
92
93
94
95
96
97
98
#include "history.h"

#include <unistd.h>

#include "3rd-party/catch.hpp"
#include "test_helpers/tempdir.h"

using namespace newsboat;

TEST_CASE("History can be iterated on in any direction", "[History]")
{
	History h;

	SECTION("Empty History returns nothing") {
		REQUIRE(h.previous_line() == "");
		REQUIRE(h.previous_line() == "");
		REQUIRE(h.next_line() == "");
		REQUIRE(h.next_line() == "");

		SECTION("One line in History") {
			h.add_line("testline");
			REQUIRE(h.previous_line() == "testline");
			REQUIRE(h.previous_line() == "testline");
			REQUIRE(h.next_line() == "testline");
			REQUIRE(h.next_line() == "");

			SECTION("Two lines in History") {
				h.add_line("foobar");
				REQUIRE(h.previous_line() == "foobar");
				REQUIRE(h.previous_line() == "testline");
				REQUIRE(h.next_line() == "testline");
				REQUIRE(h.previous_line() == "testline");
				REQUIRE(h.next_line() == "testline");
				REQUIRE(h.next_line() == "foobar");
				REQUIRE(h.next_line() == "");
				REQUIRE(h.next_line() == "");
			}
		}
	}
}

TEST_CASE("History can be saved and loaded from file", "[History]")
{
	test_helpers::TempDir tmp;
	const auto filepath = tmp.get_path() + "history.cmdline";

	History h;
	h.add_line("testline");
	h.add_line("foobar");

	SECTION("Nothing is saved to file if limit is zero") {
		h.save_to_file(filepath, 0);
		REQUIRE_FALSE(0 == ::access(filepath.c_str(), R_OK | W_OK));
	}

	SECTION("Save to file") {
		h.save_to_file(filepath, 10);
		REQUIRE(0 == ::access(filepath.c_str(), R_OK | W_OK));

		SECTION("Load from file") {
			History loaded_h;
			loaded_h.load_from_file(filepath);
			REQUIRE(loaded_h.previous_line() == "foobar");
			REQUIRE(loaded_h.previous_line() == "testline");
			REQUIRE(loaded_h.next_line() == "testline");
			REQUIRE(loaded_h.next_line() == "foobar");
			REQUIRE(loaded_h.next_line() == "");
		}
	}
}

TEST_CASE("Only the most recent lines are saved when limiting history",
	"[History]")
{
	test_helpers::TempDir tmp;
	const auto filepath = tmp.get_path() + "history.cmdline";
	const int max_lines = 3;

	History h;
	h.add_line("1");
	h.add_line("2");
	h.add_line("3");
	h.add_line("4");

	SECTION("file with history lines is created") {
		h.save_to_file(filepath, max_lines);
		REQUIRE(0 == ::access(filepath.c_str(), R_OK | W_OK));

		SECTION("when loading, only a limited number of lines are returned") {
			History loaded_h;
			loaded_h.load_from_file(filepath);
			REQUIRE(loaded_h.previous_line() == "4");
			REQUIRE(loaded_h.previous_line() == "3");
			REQUIRE(loaded_h.previous_line() == "2");
			REQUIRE(loaded_h.previous_line() == "2");
		}
	}
}