File: xml.c

package info (click to toggle)
labwc 0.9.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 2,980 kB
  • sloc: ansic: 34,416; perl: 5,836; xml: 875; sh: 162; python: 131; makefile: 12
file content (128 lines) | stat: -rw-r--r-- 2,520 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
120
121
122
123
124
125
126
127
128
// SPDX-License-Identifier: GPL-2.0-only
#define _POSIX_C_SOURCE 200809L
#include <setjmp.h>
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include <cmocka.h>
#include "common/macros.h"
#include "common/xml.h"

struct test_case {
	const char *before, *after;
} test_cases[] = {{
	"<keybind name.action='ShowMenu' menu.action='root-menu' "
		"x.position.action='1' y.position.action='2'/>",

	"<keybind>"
		"<action>"
			"<name>ShowMenu</name>"
			"<menu>root-menu</menu>"
			"<position>"
				"<x>1</x>"
				"<y>2</y>"
			"</position>"
		"</action>"
	"</keybind>"
}, {
	"<AAA aaa='111' bbb='222'/>",

	"<AAA>"
		"<aaa>111</aaa>"
		"<bbb>222</bbb>"
	"</AAA>"
}, {
	"<AAA aaa.bbb.ccc='111' ddd.ccc='222' eee.bbb.ccc='333'/>",

	"<AAA><ccc>"
		"<bbb><aaa>111</aaa></bbb>"
		"<ddd>222</ddd>"
		"<bbb><eee>333</eee></bbb>"
	"</ccc></AAA>"
}, {
	"<AAA aaa.bbb.ccc='111' bbb.ccc='222' ddd.bbb.ccc='333'/>",

	"<AAA><ccc><bbb>"
		"<aaa>111</aaa>"
		"222"
		"<ddd>333</ddd>"
	"</bbb></ccc></AAA>"
}, {
	"<AAA aaa.bbb='111' aaa.ddd='222'/>",

	"<AAA>"
		"<bbb><aaa>111</aaa></bbb>"
		"<ddd><aaa>222</aaa></ddd>"
	"</AAA>"
}, {
	"<AAA aaa.bbb='111' bbb='222' ccc.bbb='333'/>",

	"<AAA><bbb>"
		"<aaa>111</aaa>"
		"222"
		"<ccc>333</ccc>"
	"</bbb></AAA>",
}, {
	"<AAA>"
		"<BBB aaa.bbb='111'/>"
		"<BBB aaa.bbb='111'/>"
	"</AAA>",

	"<AAA>"
		"<BBB><bbb><aaa>111</aaa></bbb></BBB>"
		"<BBB><bbb><aaa>111</aaa></bbb></BBB>"
	"</AAA>",
}, {
	"<AAA bbb.ccc='111'>"
		"<BBB>222</BBB>"
	"</AAA>",

	"<AAA>"
		"<ccc><bbb>111</bbb></ccc>"
		"<BBB>222</BBB>"
	"</AAA>",
}, {
	"<AAA>"
		"<BBB><CCC>111</CCC></BBB>"
		"<BBB><CCC>111</CCC></BBB>"
	"</AAA>",

	"<AAA>"
		"<BBB><CCC>111</CCC></BBB>"
		"<BBB><CCC>111</CCC></BBB>"
	"</AAA>",
}, {
	"<AAA aaa..bbb.ccc.='111' />",

	"<AAA><ccc><bbb><aaa>111</aaa></bbb></ccc></AAA>"
}};

static void
test_lab_xml_expand_dotted_attributes(void **state)
{
	(void)state;

	for (size_t i = 0; i < ARRAY_SIZE(test_cases); i++) {
		xmlDoc *doc = xmlReadDoc((xmlChar *)test_cases[i].before,
					NULL, NULL, 0);
		xmlNode *root = xmlDocGetRootElement(doc);

		lab_xml_expand_dotted_attributes(root);

		xmlBuffer *buf = xmlBufferCreate();
		xmlNodeDump(buf, root->doc, root, 0, 0);
		assert_string_equal(test_cases[i].after, (char *)xmlBufferContent(buf));
		xmlBufferFree(buf);

		xmlFreeDoc(doc);
	}
}

int main(int argc, char **argv)
{
	const struct CMUnitTest tests[] = {
		cmocka_unit_test(test_lab_xml_expand_dotted_attributes),
	};

	return cmocka_run_group_tests(tests, NULL, NULL);
}