plptools
Loading...
Searching...
No Matches
initests.cc
Go to the documentation of this file.
1/*
2 * This file is part of plptools.
3 *
4 * Copyright (c) 2026 Jason Morley <hello@jbmorley.co.uk>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * along with this program; if not, see <https://www.gnu.org/licenses/>.
18 *
19 */
20#include "config.h"
21
22#include <iostream>
23#include <ostream>
24#include <stdio.h>
25#include <stdlib.h>
26
27#include "doctest.h"
28#include "deviceconfiguration.h"
29#include "pathutils.h"
30#include "uuid.h"
31#include "ini.h"
32
33TEST_CASE("parse_ini") {
34
35 SUBCASE("empty string") {
36 auto contents = ini::deserialize("");
37 REQUIRE(contents != nullptr);
38 CHECK(contents->empty());
39 }
40
41 SUBCASE("multiple new lines") {
42 auto contents = ini::deserialize("\r\n\r\n\r\n");
43 REQUIRE(contents != nullptr);
44 CHECK(contents->empty());
45 }
46
47 SUBCASE("corrupt fails") {
48 REQUIRE(ini::deserialize("broken") == nullptr);
49 REQUIRE(ini::deserialize("a=b\nbroken") == nullptr);
50 }
51
52 SUBCASE("missing key fails") {
53 REQUIRE(ini::deserialize("=value\n") == nullptr);
54 }
55
56 SUBCASE("non-alpha key fails") {
57 REQUIRE(ini::deserialize("some2=value\n") == nullptr);
58 }
59
60 SUBCASE("single value") {
61 auto contents = ini::deserialize("key = bar\r\n");
62 REQUIRE(contents != nullptr);
63 CHECK(*contents == std::unordered_map<std::string, std::string>{
64 {"key", "bar"}
65 });
66 }
67
68 SUBCASE("empty value") {
69 auto contents = ini::deserialize("key=\r\n");
70 REQUIRE(contents != nullptr);
71 CHECK(*contents == std::unordered_map<std::string, std::string>{
72 {"key", ""}
73 });
74 }
75
76 SUBCASE("missing newline") {
77 auto contents = ini::deserialize("key = bar");
78 REQUIRE(contents != nullptr);
79 CHECK(*contents == std::unordered_map<std::string, std::string>{
80 {"key", "bar"}
81 });
82 }
83
84 SUBCASE("ignore trailing whitespace with missing newline") {
85 auto contents = ini::deserialize("key = bar ");
86 REQUIRE(contents != nullptr);
87 CHECK(*contents == std::unordered_map<std::string, std::string>{
88 {"key", "bar"}
89 });
90 }
91
92 SUBCASE("ignore trailing whitespace") {
93 auto contents = ini::deserialize("key = bar \n");
94 REQUIRE(contents != nullptr);
95 CHECK(*contents == std::unordered_map<std::string, std::string>{
96 {"key", "bar"}
97 });
98 }
99
100 SUBCASE("ignore trailing whitespace (windows line ending)") {
101 auto contents = ini::deserialize("key = bar \r\n");
102 REQUIRE(contents != nullptr);
103 CHECK(*contents == std::unordered_map<std::string, std::string>{
104 {"key", "bar"}
105 });
106 }
107}
#define SUBCASE(name)
Definition: doctest.h:2946
#define CHECK(...)
Definition: doctest.h:2970
#define TEST_CASE(name)
Definition: doctest.h:2937
#define REQUIRE(...)
Definition: doctest.h:2977
std::unique_ptr< std::unordered_map< std::string, std::string > > deserialize(const std::string contents)
Simple parser for flat ini data.
Definition: ini.cc:49