File: at_path.cpp

package info (click to toggle)
tomlplusplus 3.3.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 3,184 kB
  • sloc: cpp: 35,145; ansic: 2,220; python: 983; makefile: 25; sh: 17
file content (119 lines) | stat: -rw-r--r-- 3,139 bytes parent folder | download
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// This file is a part of toml++ and is subject to the the terms of the MIT license.
// Copyright (c) Mark Gillard <mark.gillard@outlook.com.au>
// See https://github.com/marzer/tomlplusplus/blob/master/LICENSE for the full license text.
// SPDX-License-Identifier: MIT

#include "tests.h"
TOML_DISABLE_SPAM_WARNINGS;

TEST_CASE("at_path")
{
	// clang-format off

	const auto tbl = table
	{
		{ ""sv, 0 }, // blank key
		{ "a"sv, 1 },
		{
			"b"sv,
			array
			{
				2,
				array{ 3 },
				table { { "c", 4 } }
			},
		},
		{ "d", table{ {"e", 5, }, {""sv, -1 } } }
	};

	// clang-format on

	/*

	# equivalent to the following TOML:

	"" = 0
	a = 1
	b = [
			2,
			[ 3 ],
			{ "c" = 4 }
	]
	d = { "e" = 5, "" = -1 }

	*/

	SECTION("table")
	{
		// this section uses the free function version of at_path

		CHECK(tbl[""]);
		CHECK(tbl[""] == at_path(tbl, ""));

		CHECK(tbl["a"]);
		CHECK(tbl["a"] == at_path(tbl, "a"));
		CHECK(tbl["a"] != at_path(tbl, ".a")); // equivalent to ""."a"
		CHECK(!at_path(tbl, ".a"));

		CHECK(tbl["b"]);
		CHECK(tbl["b"] == at_path(tbl, "b"));

		CHECK(tbl["b"][0]);
		CHECK(tbl["b"][0] == at_path(tbl, "b[0]"));
		CHECK(tbl["b"][0] == at_path(tbl, "b[0]     "));
		CHECK(tbl["b"][0] == at_path(tbl, "b[ 0\t]")); // whitespace is allowed inside indexer

		CHECK(tbl["b"][1]);
		CHECK(tbl["b"][1] != tbl["b"][0]);
		CHECK(tbl["b"][1] == at_path(tbl, "b[1]"));

		CHECK(tbl["b"][1][0]);
		CHECK(tbl["b"][1][0] == at_path(tbl, "b[1][0]"));
		CHECK(tbl["b"][1][0] == at_path(tbl, "b[1]    \t   [0]")); // whitespace is allowed after indexers

		CHECK(tbl["b"][2]["c"]);
		CHECK(tbl["b"][2]["c"] == at_path(tbl, "b[2].c"));
		CHECK(tbl["b"][2]["c"] == at_path(tbl, "b[2]   \t.c")); // whitespace is allowed after indexers

		// permissivity checks for missing trailing ']'
		// (this permissivity is undocumented but serves to reduce error paths in user code)
		CHECK(tbl["b"][1][0] == at_path(tbl, "b[1[0]"));
		CHECK(tbl["b"][1][0] == at_path(tbl, "b[1[0"));
		CHECK(tbl["b"][2]["c"] == at_path(tbl, "b[2.c"));

		CHECK(tbl["d"]);
		CHECK(tbl["d"] == at_path(tbl, "d"));

		CHECK(tbl["d"]["e"]);
		CHECK(tbl["d"]["e"] == at_path(tbl, "d.e"));
		CHECK(tbl["d"]["e"] != at_path(tbl, "d. e")); // equivalent to "d"." e"
		CHECK(!at_path(tbl, "d. e"));

		CHECK(tbl["d"][""]);
		CHECK(tbl["d"][""] == at_path(tbl, "d."));
	}

	SECTION("array")
	{
		// this section uses the node_view member function version of at_path

		auto arr = tbl["b"];

		CHECK(tbl["b"][0]);
		CHECK(tbl["b"][0] == arr.at_path("[0]"));
		CHECK(tbl["b"][0] == arr.at_path("[0]     "));
		CHECK(tbl["b"][0] == arr.at_path("[ 0\t]")); // whitespace is allowed inside indexer

		CHECK(tbl["b"][1]);
		CHECK(tbl["b"][1].node() != arr[0].node());
		CHECK(tbl["b"][1] == arr.at_path("[1]"));

		CHECK(tbl["b"][1][0]);
		CHECK(tbl["b"][1][0] == arr.at_path("[1][0]"));
		CHECK(tbl["b"][1][0] == arr.at_path("[1]    \t   [0]")); // whitespace is allowed after indexers

		CHECK(tbl["b"][2]["c"]);
		CHECK(tbl["b"][2]["c"] == arr.at_path("[2].c"));
		CHECK(tbl["b"][2]["c"] == arr.at_path("[2]   \t.c")); // whitespace is allowed after indexers
	}
}