File: parsing_spec_example.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 (92 lines) | stat: -rw-r--r-- 3,078 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
// 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("parsing - TOML spec example")
{
	static constexpr auto toml_text = R"(
		# This is a TOML document.

		title = "TOML Example"

		[owner]
		name = "Tom Preston-Werner"
		dob = 1979-05-27T07:32:00-08:00 # First class dates

		[database]
		server = "192.168.1.1"
		ports = [ 8001, 8001, 8002 ]
		connection_max = 5000
		enabled = true

		[servers]

		  # Indentation (tabs and/or spaces) is allowed but not required
		  [servers.alpha]
		  ip = "10.0.0.1"
		  dc = "eqdc10"

		  [servers.beta]
		  ip = "10.0.0.2"
		  dc = "eqdc10"

		[clients]
		data = [ ["gamma", "delta"], [1, 2] ]

		# Line breaks are OK when inside arrays
		hosts = [
		  "alpha",
		  "omega"
		]
	)"sv;

	parsing_should_succeed(FILE_LINE_ARGS,
						   toml_text,
						   [](table&& tbl)
						   {
							   CHECK(tbl.size() == 5);

							   CHECK(tbl["title"] == "TOML Example"sv);

							   CHECK(tbl["owner"]);
							   CHECK(tbl["owner"].as<table>());
							   CHECK(tbl["owner"]["name"] == "Tom Preston-Werner"sv);
							   const auto dob = date_time{ { 1979, 5, 27 }, { 7, 32 }, { -8, 0 } };
							   CHECK(tbl["owner"]["dob"] == dob);

							   CHECK(tbl["database"].as<table>());
							   CHECK(tbl["database"]["server"] == "192.168.1.1"sv);
							   const auto ports = { 8001, 8001, 8002 };
							   CHECK(tbl["database"]["ports"] == ports);
							   CHECK(tbl["database"]["connection_max"] == 5000);
							   CHECK(tbl["database"]["enabled"] == true);

							   CHECK(tbl["servers"].as<table>());
							   CHECK(tbl["servers"]["alpha"].as<table>());
							   CHECK(tbl["servers"]["alpha"]["ip"] == "10.0.0.1"sv);
							   CHECK(tbl["servers"]["alpha"]["dc"] == "eqdc10"sv);
							   CHECK(tbl["servers"]["beta"].as<table>());
							   CHECK(tbl["servers"]["beta"]["ip"] == "10.0.0.2"sv);
							   CHECK(tbl["servers"]["beta"]["dc"] == "eqdc10"sv);

							   CHECK(tbl["clients"].as<table>());
							   REQUIRE(tbl["clients"]["data"].as<array>());
							   CHECK(tbl["clients"]["data"].as<array>()->size() == 2);
							   REQUIRE(tbl["clients"]["data"][0].as<array>());
							   CHECK(tbl["clients"]["data"][0].as<array>()->size() == 2);
							   CHECK(tbl["clients"]["data"][0][0] == "gamma"sv);
							   CHECK(tbl["clients"]["data"][0][1] == "delta"sv);
							   REQUIRE(tbl["clients"]["data"][1].as<array>());
							   CHECK(tbl["clients"]["data"][1].as<array>()->size() == 2);
							   CHECK(tbl["clients"]["data"][1][0] == 1);
							   CHECK(tbl["clients"]["data"][1][1] == 2);
							   REQUIRE(tbl["clients"]["hosts"].as<array>());
							   CHECK(tbl["clients"]["hosts"].as<array>()->size() == 2);
							   CHECK(tbl["clients"]["hosts"][0] == "alpha"sv);
							   CHECK(tbl["clients"]["hosts"][1] == "omega"sv);
						   });
}