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
|
#include "config-merge.hpp"
#include <cassert>
#include <iostream>
int main()
{
const std::string before = "[cube]\ndim = 0.25\nmouse_overview = false\n";
const std::string after = "[cube]\ndim = 0.25\nmouse_overview = true\n";
const std::string external = "# retained\r\n[cube]\r\ndim = 0.37\r\nmouse_overview = false # note\r\n[new]\r\nkeep = yes\r\n";
const auto merged = wcm_config::merge(external, before, after);
assert(merged == "# retained\r\n[cube]\r\ndim = 0.37\r\nmouse_overview = true # note\r\n[new]\r\nkeep = yes\r\n");
assert(wcm_config::merge(merged, after, after) == merged);
assert(wcm_config::merge("[command]\na = old; first\nb = external\n", "[command]\na = old; first\n",
"[command]\na = old; second\n") == "[command]\na = old; second\nb = external\n");
assert(wcm_config::merge("[command]\na = old\nb = external\n", "[command]\na = old\n",
"[command]\n") == "[command]\nb = external\n");
const std::string multiline = "[command]\na = first \\\n second\nb = old\n";
assert(wcm_config::merge(multiline, "[command]\nb = old\n", "[command]\nb = new\n") ==
"[command]\na = first \\\n second\nb = new\n");
assert(wcm_config::merge("[a]\nx=1", "[a]\nx=1\n", "[a]\nx=1\ny=2\n[b]\nz=3\n") ==
"[a]\nx=1\ny = 2\n\n[b]\nz = 3\n");
char directory[] = "/tmp/wcm-merge-XXXXXX";
assert(mkdtemp(directory));
const auto path = std::string(directory) + "/wayfire.ini";
{ std::ofstream f(path); f << external; }
assert(chmod(path.c_str(), 0640) == 0);
wcm_config::save(path, before, after);
{ std::ifstream f(path); assert(std::string(std::istreambuf_iterator<char>(f), {}) == merged); }
struct stat st{}; assert(stat(path.c_str(), &st) == 0 && (st.st_mode & 0777) == 0640);
unlink(path.c_str()); rmdir(directory);
std::cout << "PASS scoped edits, comments, CRLF, compound deletion, semicolons, multiline, atomic permissions\n";
}
|