rosavox/lib/alveolata/xml/test.spec.php
2025-05-23 07:33:29 +00:00

408 lines
9.7 KiB
PHP

<?php
require_once(DIR_ALVEOLATA . '/string/functions.php');
require_once(DIR_ALVEOLATA . '/xml/functions.php');
\alveolata\test\add(
[
'name' => 'alveolata',
'sections' => [
[
'name' => 'xml',
'setup' => function (&$environment) {
},
'sections' => [
[
'name' => 'to_raw',
'cases' => [
[
'name' => 'comment',
'procedure' => function ($assert, &$environment) {
$xmlnode = \alveolata\xml\make_comment('foo');
$output_actual = \alveolata\xml\to_raw($xmlnode);
$output_expected = [
'kind' => 'comment',
'data' => ['content' => 'foo'],
];
$assert->equal($output_expected, $output_actual);
},
],
[
'name' => 'text',
'procedure' => function ($assert, &$environment) {
$xmlnode = \alveolata\xml\make_text('foo');
$output_actual = \alveolata\xml\to_raw($xmlnode);
$output_expected = [
'kind' => 'text',
'data' => ['content' => 'foo'],
];
$assert->equal($output_expected, $output_actual);
},
],
[
'name' => 'complex',
'procedure' => function ($assert, &$environment) {
$xmlnode = \alveolata\xml\make_complex(
'foo',
[
'attributes' => [
'bar' => '0',
'baz' => '1',
],
'children' => [
\alveolata\xml\make_text('qux'),
]
]
);
$output_actual = \alveolata\xml\to_raw($xmlnode);
$output_expected = [
'kind' => 'complex',
'data' => [
'name' => 'foo',
'attributes' => [
'bar' => '0',
'baz' => '1',
],
'children' => [
[
'kind' => 'text',
'data' => [
'content' => 'qux',
]
],
]
]
];
$assert->equal($output_expected, $output_actual);
},
],
[
'name' => 'document',
'procedure' => function ($assert, &$environment) {
$xmlnode = \alveolata\xml\make_document(
[
\alveolata\xml\make_text('foo'),
\alveolata\xml\make_text('bar'),
]
);
$output_actual = \alveolata\xml\to_raw($xmlnode);
$output_expected = [
'kind' => 'document',
'data' => [
'children' => [
[
'kind' => 'text',
'data' => [
'content' => 'foo',
]
],
[
'kind' => 'text',
'data' => [
'content' => 'bar',
]
],
]
]
];
$assert->equal($output_expected, $output_actual);
},
],
],
],
[
'name' => 'to_string',
'cases' => [
[
'name' => 'comment',
'procedure' => function ($assert, &$environment) {
$xmlnode = \alveolata\xml\make_comment('foo');
$output_actual = \alveolata\xml\to_string($xmlnode);
$output_expected = '<!-- foo -->';
$assert->equal($output_expected, $output_actual);
},
],
[
'name' => 'text',
'procedure' => function ($assert, &$environment) {
$xmlnode = \alveolata\xml\make_text('foo');
$output_actual = \alveolata\xml\to_string($xmlnode);
$output_expected = 'foo';
$assert->equal($output_expected, $output_actual);
},
],
[
'name' => 'complex',
'procedure' => function ($assert, &$environment) {
$xmlnode = \alveolata\xml\make_complex(
'foo',
[
'attributes' => [
'bar' => '0',
'baz' => '1',
],
'children' => [
alveolata\xml\make_text('qux'),
]
]
);
$output_actual = \alveolata\xml\to_string($xmlnode);
$output_expected = '<foo bar="0" baz="1">qux</foo>';
$assert->equal($output_expected, $output_actual);
},
],
[
'name' => 'document',
'procedure' => function ($assert, &$environment) {
$xmlnode = \alveolata\xml\make_document(
[
\alveolata\xml\make_text('foo'),
\alveolata\xml\make_text('bar'),
]
);
$output_actual = \alveolata\xml\to_string($xmlnode);
$output_expected = 'foobar';
$assert->equal($output_expected, $output_actual);
},
],
],
],
[
'name' => 'find',
'cases' => [
[
'name' => 'oblique',
'procedure' => function ($assert) {
$node = \alveolata\xml\make_complex(
'foo',
[
'attributes' => [
'one' => '1',
'two' => '2',
],
'children' => [
\alveolata\xml\make_complex(
'bar',
[
'attributes' => [
'three' => 3,
'four' => 4,
],
'subnode' => null
]
),
\alveolata\xml\make_complex(
'baz',
[
'attributes' => [
'two' => 2,
'five' => 5,
],
'subnode' => null
]
),
]
]
);
// exec
$result = \alveolata\xml\find(
$node,
fn($x) => (
($x instanceof \alveolata\xml\struct_complex)
&&
\array_key_exists('two', $x->attributes)
)
);
// assertions
$assert->equal(
(
(\count($result) === 2)
&&
(
(
($result[0]->name === 'foo')
&&
($result[1]->name === 'baz')
)
||
(
($result[1]->name === 'foo')
&&
($result[0]->name === 'baz')
)
)
),
true
);
}
],
]
],
[
'name' => 'transform',
'cases' => [
[
'name' => 'oblique',
'procedure' => function ($assert) {
$node = \alveolata\xml\make_complex(
'foo',
[
'attributes' => [
'one' => '1',
'two' => '2',
],
'children' => [
\alveolata\xml\make_complex(
'bar',
[
'attributes' => [
'three' => 3,
'four' => 4,
],
'subnode' => \alveolata\xml\make_text('test')
]
),
]
]
);
// exec
$result_actual = \alveolata\xml\transform(
$node,
fn($x) => (
(
($x instanceof \alveolata\xml\struct_complex)
&&
($x->name === 'bar')
)
? \alveolata\xml\make_complex(
'baz',
[
'attributes' => $x->attributes,
'children' => $x->children,
]
)
: $x
)
);
// assertions
$result_expected = \alveolata\xml\make_complex(
'foo',
[
'attributes' => [
'one' => '1',
'two' => '2',
],
'children' => [
\alveolata\xml\make_complex(
'baz',
[
'attributes' => [
'three' => 3,
'four' => 4,
],
'subnode' => \alveolata\xml\make_text('test')
]
),
]
]
);
$assert->equal(\alveolata\xml\to_raw($result_actual), \alveolata\xml\to_raw($result_expected));
}
],
]
],
[
'name' => 'parse',
'cases' => \alveolata\list_\map(
[
[
'name' => 'comment',
'input' => '<!--foo-->',
'output' => [
'kind' => 'comment',
'data' => [
'content' => 'foo'
]
]
],
[
'name' => 'text',
'input' => 'foo',
'output' => [
'kind' => 'text',
'data' => [
'content' => 'foo'
]
]
],
[
'name' => 'complex',
'input' => '<foo bar="0" baz="1">bla</foo>',
'output' => [
'kind' => 'complex',
'data' => [
'name' => 'foo',
'attributes' => [
'bar' => '0',
'baz' => '1',
],
'children' => [
[
'kind' => 'text',
'data' => [
'content' => 'bla'
]
],
]
]
]
],
[
'name' => 'document',
'input' => '<!--foo-->bar',
'output' => [
'kind' => 'document',
'data' => [
'children' => [
[
'kind' => 'comment',
'data' => [
'content' => 'foo'
]
],
[
'kind' => 'text',
'data' => [
'content' => 'bar'
]
],
]
]
]
],
],
fn($case) => [
'name' => $case['name'],
'procedure' => function ($assert) use ($case) {
$assert->equal(
\alveolata\xml\parse_raw(
$case['input']
),
$case['output']
);
}
]
),
],
],
]
]
]
);
?>