OXYGENHUB
◆ main openmsx / src / unittest / escape_newline_test.cc
1.0 KiB · C++ · last changed chore(): initial commit 1 month ago source blame history raw
1
#include "catch.hpp"
2
#include "escape_newline.hh"
3
4
TEST_CASE("escape_newline")
5
{
6
	SECTION("round trip") {
7
		auto test = [](std::string_view s) {
8
			auto e = escape_newline::encode(s);
9
			auto d = escape_newline::decode(e);
10
			CHECK(e.size() >= d.size());
11
			CHECK(d == s);
12
13
			escape_newline::decode_inplace(e);
14
			CHECK(e == s);
15
		};
16
		test("");
17
		test("bla");
18
		test("bla\nbla");
19
		test("bla\\bla");
20
		test("\n\n\\\\");
21
	}
22
	SECTION("errors") {
23
		// * All inputs are valid for encode().
24
		// * But some inputs should normally never be passed to decode()
25
		//   (as in: they can never have been the result of a prior
26
		//   encode()).
27
		//   Nevertheless we try to recover from such inputs.
28
29
		// Single backslash at the end -> gets dropped.
30
		CHECK(escape_newline::decode("bla\\") == "bla");
31
		std::string s = "bla\\";
32
		escape_newline::decode_inplace(s);
33
		CHECK(s == "bla");
34
35
		// Normally only "\\" or "\n" should occur, all other "\<x>" are
36
		// in principle invalid, but we decode them as 'x'
37
		CHECK(escape_newline::decode("bl\\a") == "bla");
38
	}
39
}