Skip to content

Commit 2543a31

Browse files
committed
[TASK] Add tests and make them work
1 parent bdb2a94 commit 2543a31

File tree

6 files changed

+161
-6
lines changed

6 files changed

+161
-6
lines changed

packages/guides-restructured-text/src/RestructuredText/Directives/ConfvalDirective.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,18 +80,16 @@ protected function processSub(
8080
}
8181

8282
if ($directive->hasOption('type')) {
83-
$type = $this->inlineParser->parse($directive->getOption('type')->toString(), $blockContext);
83+
$type = $this->inlineParser->parse($directive->getOptionString('type'), $blockContext);
8484
}
8585

86-
if ($directive->hasOption('required')) {
87-
$required = $directive->getOption('required')->getValue() === null || (bool) $directive->getOption('required')->toString();
88-
}
86+
$required = $directive->getOptionBool('required');
8987

9088
if ($directive->hasOption('default')) {
91-
$default = $this->inlineParser->parse($directive->getOption('default')->toString(), $blockContext);
89+
$default = $this->inlineParser->parse($directive->getOptionString('default'), $blockContext);
9290
}
9391

94-
$noindex = $directive->hasOption('noindex');
92+
$noindex = $directive->getOptionBool('noindex');
9593

9694
foreach ($directive->getOptions() as $option) {
9795
if (in_array($option->getName(), ['type', 'required', 'default', 'noindex', 'name'], true)) {

packages/guides-restructured-text/src/RestructuredText/Parser/Directive.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,37 @@ public function getOption(string $name): DirectiveOption
7171
return $this->options[$name] ?? new DirectiveOption($name, null);
7272
}
7373

74+
public function getOptionString(string $name, string $default = ''): string
75+
{
76+
if (!isset($this->options[$name])) {
77+
return $default;
78+
}
79+
80+
return $this->options[$name]->toString();
81+
}
82+
83+
public function getOptionBool(string $name, bool $default = false, bool $nullDefault = true): bool
84+
{
85+
if (!isset($this->options[$name])) {
86+
return $default;
87+
}
88+
89+
if ($this->options[$name]->getValue() === null) {
90+
return $nullDefault;
91+
}
92+
93+
return $this->options[$name]->toBool();
94+
}
95+
96+
public function getOptionInt(string $name, int $default = 0): int
97+
{
98+
if (!isset($this->options[$name])) {
99+
return $default;
100+
}
101+
102+
return (int) $this->options[$name]->getValue();
103+
}
104+
74105
public function getDataNode(): InlineCompoundNode|null
75106
{
76107
return $this->dataNode;

packages/guides-restructured-text/src/RestructuredText/Parser/DirectiveOption.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@
1313

1414
namespace phpDocumentor\Guides\RestructuredText\Parser;
1515

16+
use function is_bool;
17+
use function is_string;
18+
use function strtolower;
1619
use function strval;
20+
use function trim;
1721

1822
final class DirectiveOption
1923
{
@@ -36,6 +40,23 @@ public function toString(): string
3640
return strval($this->value);
3741
}
3842

43+
public function toBool(): bool
44+
{
45+
if (is_bool($this->value)) {
46+
return $this->value;
47+
}
48+
49+
if (is_string($this->value)) {
50+
return strtolower(trim($this->value)) !== 'false' && strtolower(trim($this->value)) !== '0' && strtolower(trim($this->value)) !== '';
51+
}
52+
53+
if ($this->value === null) {
54+
return false;
55+
}
56+
57+
return $this->value === 0 || $this->value === 0.0;
58+
}
59+
3960
public function appendValue(string $append): void
4061
{
4162
$this->value = ((string) $this->value) . $append;

tests/Integration/tests/confval/confval-name/expected/index.html

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,63 @@ <h1>Confval directive</h1>
1818

1919
<p>Another paragraph.</p>
2020

21+
</div>
22+
</dd>
23+
</dl>
24+
<dl class="confval">
25+
<dt id="confval-demo2">
26+
<code class="sig-name descname"><span class="pre">demo2</span></code></dt>
27+
<dd>
28+
<div class="line-block">
29+
<div class="line"><strong>Type:</strong> <code class="php">string</code></div>
30+
</div>
31+
<div class="confval-description">
32+
33+
<p>This is not required.</p>
34+
35+
</div>
36+
</dd>
37+
</dl>
38+
<dl class="confval">
39+
<dt id="confval-demo3">
40+
<code class="sig-name descname"><span class="pre">demo3</span></code></dt>
41+
<dd>
42+
<div class="line-block">
43+
<div class="line"><strong>Type:</strong> <code class="php">string</code></div>
44+
</div>
45+
<div class="confval-description">
46+
47+
<p>This is also not required.</p>
48+
49+
</div>
50+
</dd>
51+
</dl>
52+
<dl class="confval">
53+
<dt id="confval-demo4">
54+
<code class="sig-name descname"><span class="pre">demo4</span></code></dt>
55+
<dd>
56+
<div class="line-block">
57+
<div class="line"><strong>Type:</strong> <code class="php">string</code></div>
58+
</div>
59+
<div class="confval-description">
60+
61+
<p>And this is not required</p>
62+
63+
</div>
64+
</dd>
65+
</dl>
66+
<dl class="confval">
67+
<dt id="confval-demo5">
68+
<code class="sig-name descname"><span class="pre">demo5</span></code></dt>
69+
<dd>
70+
<div class="line-block">
71+
<div class="line"><strong>Type:</strong> <code class="php">string</code></div>
72+
<div class="line"><strong>Required:</strong> true</div>
73+
</div>
74+
<div class="confval-description">
75+
76+
<p>But this is required</p>
77+
2178
</div>
2279
</dd>
2380
</dl>

tests/Integration/tests/confval/confval-name/expected/objects.inv.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,30 @@
3434
"-",
3535
"index.html#demo",
3636
"demo"
37+
],
38+
"demo2": [
39+
"-",
40+
"-",
41+
"index.html#demo2",
42+
"demo2"
43+
],
44+
"demo3": [
45+
"-",
46+
"-",
47+
"index.html#demo3",
48+
"demo3"
49+
],
50+
"demo4": [
51+
"-",
52+
"-",
53+
"index.html#demo4",
54+
"demo4"
55+
],
56+
"demo5": [
57+
"-",
58+
"-",
59+
"index.html#demo5",
60+
"demo5"
3761
]
3862
}
3963
}

tests/Integration/tests/confval/confval-name/input/index.rst

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,30 @@ Confval directive
1111

1212
Another paragraph.
1313

14+
.. confval:: demo2
15+
:type: :php:`string`
16+
:required: false
17+
18+
This is not required.
19+
20+
.. confval:: demo3
21+
:type: :php:`string`
22+
:required: False
23+
24+
This is also not required.
25+
26+
.. confval:: demo4
27+
:type: :php:`string`
28+
:required: 0
29+
30+
And this is not required
31+
32+
.. confval:: demo5
33+
:type: :php:`string`
34+
:required:
35+
36+
But this is required
37+
1438
See option :confval:`demo`.
1539

1640
.. toctree::

0 commit comments

Comments
 (0)