spec(module_doc): fix and refactor

Fix the tests by adding the new keys from `"module"` tokens.

We now use a `test` function to make the `it` easier to write/read.
This commit is contained in:
Aire-One 2023-05-21 20:42:32 +02:00
parent 4d81aaef56
commit bea2f0fc54
1 changed files with 242 additions and 224 deletions

View File

@ -4,10 +4,19 @@ local scraper = require("scraper.module_doc")
local get_doc_from_page = scraper.get_doc_from_page
local function test(html: string, module_path: string, expected_ast: Node, expected_other_nodes: { Node } | nil): function()
return function()
local ast <const>, other_nodes <const> = get_doc_from_page(html, module_path)
assert.same(expected_ast, ast)
assert.same(expected_other_nodes or {}, other_nodes)
end
end
describe("Scrap documentation", function()
it("should return a valid AST for an empty module", function()
local ast <const>, nodes <const> = get_doc_from_page("", "empty")
local expected <const>: Node = {
it("should return a valid AST for an empty module", test(
"",
"empty",
{
children = {
{
children = {},
@ -16,50 +25,50 @@ describe("Scrap documentation", function()
}
},
name = "Empty",
module_path = "empty",
dependencies = {},
token = "module",
}
assert.same(expected, ast)
assert.same({}, nodes)
end)
}))
it("should produce Variable and `property::` Signal nodes", function()
local ast <const> = get_doc_from_page([[
<h2 class="section-header">
<a name="Object_properties"></a>Object properties
</h2>
<dl class="function">
<dt>
<a class="copy-link js-copy-link" name="value" href="#value">🔗</a>
<strong>value</strong>
<span class="proptype"><span class="summary_type">number</span></span>
<span class="baseclass"> · 1 signal </span>
</dt>
<dd>
<h3>Constraints:</h3>
<span class="property_type">
<table class="see_also">
<tbody><tr class="">
<td style="padding-left:0px;">
<i>
Default value
</i>
</td>
<td>: <code>0</code></td>
</tr><tr>
</tr><tr class="">
<td style="padding-left:0px;">
<i>
Negative allowed
</i>
</td>
<td>: true</td>
</tr><tr>
</tr></tbody></table>
</span>
</dd>
</dl>
]], "property_signal")
local expected <const>: Node = {
it("should produce Variable and `property::` Signal nodes", test(
[[
<h2 class="section-header">
<a name="Object_properties"></a>Object properties
</h2>
<dl class="function">
<dt>
<a class="copy-link js-copy-link" name="value" href="#value">🔗</a>
<strong>value</strong>
<span class="proptype"><span class="summary_type">number</span></span>
<span class="baseclass"> · 1 signal </span>
</dt>
<dd>
<h3>Constraints:</h3>
<span class="property_type">
<table class="see_also">
<tbody><tr class="">
<td style="padding-left:0px;">
<i>
Default value
</i>
</td>
<td>: <code>0</code></td>
</tr><tr>
</tr><tr class="">
<td style="padding-left:0px;">
<i>
Negative allowed
</i>
</td>
<td>: true</td>
</tr><tr>
</tr></tbody></table>
</span>
</dd>
</dl>
]],
"property_signal",
{
children = {
{
children = {
@ -78,82 +87,83 @@ describe("Scrap documentation", function()
}
},
name = "Property_signal",
module_path = "property_signal",
dependencies = {},
token = "module",
}
assert.same(expected, ast)
end)
}))
it("should produce Enum nodes when an Object Property type is a String with constraints", function()
local ast <const> = get_doc_from_page([[
it("should produce Enum nodes when an Object Property type is a String with constraints", test(
[[
<h2 class="section-header">
<a name="Object_properties"></a>Object properties
</h2>
<dl class="function">
<dt>
<a
class="copy-link js-copy-link"
name="horizontal_fit_policy"
href="#horizontal_fit_policy"
>🔗</a
>
<strong>horizontal_fit_policy</strong>
<span class="proptype"><span class="summary_type">string</span></span>
<span class="baseclass"> · 1 signal </span>
</dt>
<dd>
<span class="property_type">
<table class="see_also">
<tbody>
<tr class="">
<td style="padding-left: 0px">
<i> Default value </i>
</td>
<td>: <code>"auto"</code></td>
</tr>
<tr></tr>
<tr class="">
<td style="padding-left: 0px">
<i> Valid values: </i>
</td>
</tr>
<tr></tr>
<tr class="see_also_sublist">
<td style="padding-left: 15px">
<i>
<code>"auto"</code>
</i>
</td>
<td>
: Honor the <code>resize</code> variable and preserve the aspect
ratio.
</td>
</tr>
<tr></tr>
<tr class="see_also_sublist">
<td style="padding-left: 15px">
<i>
<code>"none"</code>
</i>
</td>
<td>: Do not resize at all.</td>
</tr>
<tr></tr>
<tr class="see_also_sublist">
<td style="padding-left: 15px">
<i>
<code>"fit"</code>
</i>
</td>
<td>: Resize to the widget width.</td>
</tr>
<tr></tr>
</tbody>
</table>
</span>
</dd>
<dt>
<a
class="copy-link js-copy-link"
name="horizontal_fit_policy"
href="#horizontal_fit_policy"
>🔗</a
>
<strong>horizontal_fit_policy</strong>
<span class="proptype"><span class="summary_type">string</span></span>
<span class="baseclass"> · 1 signal </span>
</dt>
<dd>
<span class="property_type">
<table class="see_also">
<tbody>
<tr class="">
<td style="padding-left: 0px">
<i> Default value </i>
</td>
<td>: <code>"auto"</code></td>
</tr>
<tr></tr>
<tr class="">
<td style="padding-left: 0px">
<i> Valid values: </i>
</td>
</tr>
<tr></tr>
<tr class="see_also_sublist">
<td style="padding-left: 15px">
<i>
<code>"auto"</code>
</i>
</td>
<td>
: Honor the <code>resize</code> variable and preserve the aspect
ratio.
</td>
</tr>
<tr></tr>
<tr class="see_also_sublist">
<td style="padding-left: 15px">
<i>
<code>"none"</code>
</i>
</td>
<td>: Do not resize at all.</td>
</tr>
<tr></tr>
<tr class="see_also_sublist">
<td style="padding-left: 15px">
<i>
<code>"fit"</code>
</i>
</td>
<td>: Resize to the widget width.</td>
</tr>
<tr></tr>
</tbody>
</table>
</span>
</dd>
</dl>
]], "property_enum")
local expected <const>: Node = {
]],
"property_enum",
{
children = {
{
children = {
@ -190,29 +200,30 @@ describe("Scrap documentation", function()
},
},
name = "Property_enum",
module_path = "property_enum",
dependencies = {},
token = "module",
}
assert.same(expected, ast)
end)
}))
it("should produce a `string` typed Variable node when a String Property has no constraint", function()
local ast <const> = get_doc_from_page([[
<h2 class="section-header">
<a name="Object_properties"></a>Object properties
</h2>
<dl class="function">
<dt>
<a class="copy-link js-copy-link" name="markup" href="#markup">🔗</a>
<strong>markup</strong>
<span class="proptype"><span class="summary_type">string</span></span>
<span class="baseclass"> · 1 signal </span>
</dt>
<dd>
<span class="property_type">string</span>
</dd>
</dl>
]], "property_string")
local expected <const>: Node = {
it("should produce a `string` typed Variable node when a String Property has no constraint", test(
[[
<h2 class="section-header">
<a name="Object_properties"></a>Object properties
</h2>
<dl class="function">
<dt>
<a class="copy-link js-copy-link" name="markup" href="#markup">🔗</a>
<strong>markup</strong>
<span class="proptype"><span class="summary_type">string</span></span>
<span class="baseclass"> · 1 signal </span>
</dt>
<dd>
<span class="property_type">string</span>
</dd>
</dl>
]],
"property_string",
{
children = {
{
children = {
@ -231,47 +242,48 @@ describe("Scrap documentation", function()
}
},
name = "Property_string",
module_path = "property_string",
dependencies = {},
token = "module",
}
assert.same(expected, ast)
end)
}))
it("should provide a Function node with the `self` as the first positional parameter", function()
local ast <const> = get_doc_from_page([[
<h2 class="section-header"><a name="Object_methods"></a>Object methods</h2>
<dl class="function">
<dt>
<a class="copy-link js-copy-link" name="swap" href="#swap">🔗</a>
<strong
>:swap <span class="function_args"> <b>(</b>tag2<b>)</b></span></strong
>
<span class="proptype"></span>
<span class="baseclass"> </span>
</dt>
<dd>
<h3>Parameters:</h3>
<table class="see_also">
<tbody>
<tr class="param_header">
<th>Name</th>
<th></th>
<th>Type(s)</th>
<th>Description</th>
</tr>
<tr>
<td><span class="parameter">tag2</span></td>
<td></td>
<td>
<span class="types"><span class="type">tag</span></span>
</td>
<td class="see_also_description">The second tag</td>
</tr>
</tbody>
</table>
</dd>
</dl>
]], "awful.tag")
local expected <const>: Node = {
it("should provide a Function node with the `self` as the first positional parameter", test(
[[
<h2 class="section-header"><a name="Object_methods"></a>Object methods</h2>
<dl class="function">
<dt>
<a class="copy-link js-copy-link" name="swap" href="#swap">🔗</a>
<strong
>:swap <span class="function_args"> <b>(</b>tag2<b>)</b></span></strong
>
<span class="proptype"></span>
<span class="baseclass"> </span>
</dt>
<dd>
<h3>Parameters:</h3>
<table class="see_also">
<tbody>
<tr class="param_header">
<th>Name</th>
<th></th>
<th>Type(s)</th>
<th>Description</th>
</tr>
<tr>
<td><span class="parameter">tag2</span></td>
<td></td>
<td>
<span class="types"><span class="type">tag</span></span>
</td>
<td class="see_also_description">The second tag</td>
</tr>
</tbody>
</table>
</dd>
</dl>
]],
"awful.tag",
{
children = {
{
children = {},
@ -297,15 +309,15 @@ describe("Scrap documentation", function()
},
},
name = "Tag",
module_path = "awful.tag",
dependencies = {},
token = "module",
}
assert.same(expected, ast)
end)
}))
it("should produce Signal nodes", function()
local ast <const> = get_doc_from_page([[
<h2 class="section-header"><a name="Signals"></a>Signals</h2>
<dl class="function">
it("should produce Signal nodes", test(
[[
<h2 class="section-header"><a name="Signals"></a>Signals</h2>
<dl class="function">
<dt>
<a
class="copy-link js-copy-link"
@ -328,9 +340,10 @@ describe("Scrap documentation", function()
<span class="baseclass"> ·&nbsp;Inherited from wibox.widget.base </span>
</dt>
<dd></dd>
</dl>
]], "signal")
local expected <const>: Node = {
</dl>
]],
"signal",
{
children = {
{
children = {
@ -348,13 +361,13 @@ describe("Scrap documentation", function()
},
},
name = "Signal",
module_path = "signal",
dependencies = {},
token = "module",
}
assert.same(expected, ast)
end)
}))
it("should produce Function nodes", function()
local ast <const> = get_doc_from_page([[
it("should produce Function nodes", test(
[[
<h2 class="section-header">
<a name="Static_module_functions"></a>Static module functions
</h2>
@ -415,8 +428,9 @@ describe("Scrap documentation", function()
</ol>
</dd>
</dl>
]], "awesome") -- The module name must be the same as the module name in the doc
local expected <const>: Node = {
]],
"awesome", -- The module name must be the same as the module name in the doc
{
children = {
{
children = {},
@ -442,13 +456,13 @@ describe("Scrap documentation", function()
}
},
name = "Awesome",
module_path = "awesome",
dependencies = {},
token = "module",
}
assert.same(expected, ast)
end)
}))
it("should produce a Record node when a function parameter is a named-parameter-table", function()
local ast <const> = get_doc_from_page([[
it("should produce a Record node when a function parameter is a named-parameter-table", test(
[[
<h2 class="section-header">
<a name="Static_module_functions"></a>Static module functions
</h2>
@ -529,8 +543,9 @@ describe("Scrap documentation", function()
</ol>
</dd>
</dl>
]], "awful.screen")
assert.same(ast, {
]],
"awful.screen",
{
children = {
{
children = {},
@ -567,12 +582,13 @@ describe("Scrap documentation", function()
},
},
name = "Screen",
module_path = "awful.screen",
dependencies = {},
token = "module",
})
end)
}))
it("should go back to a table typed parameter when the record is empty", function()
local ast <const> = get_doc_from_page([[
it("should go back to a table typed parameter when the record is empty", test(
[[
<h2 class="section-header "><a name="Static_module_functions"></a>Static module functions</h2>
<dl class="function">
<dt>
@ -624,8 +640,9 @@ describe("Scrap documentation", function()
</ol>
</dd>
</dl>
]], "gears.table")
local expected <const>: Node = {
]],
"gears.table",
{
children = {
{
children = {},
@ -656,13 +673,13 @@ describe("Scrap documentation", function()
}
},
name = "Table",
module_path = "gears.table",
dependencies = {},
token = "module",
}
assert.same(expected, ast)
end)
}))
it("should go back to a table typed parameter when the record is empty and it's the last parameter", function()
local ast <const> = get_doc_from_page([[
it("should go back to a table typed parameter when the record is empty and it's the last parameter", test(
[[
<h2 class="section-header"><a name="Object_methods"></a>Object methods</h2>
<dl class="function">
<dt>
@ -720,8 +737,9 @@ describe("Scrap documentation", function()
</ol>
</dd>
</dl>
]], "awful.client")
local expected <const>: Node = {
]],
"awful.client",
{
children = {
{
children = {},
@ -747,13 +765,13 @@ describe("Scrap documentation", function()
},
},
name = "Client",
module_path = "awful.client",
dependencies = {},
token = "module",
}
assert.same(expected, ast)
end)
}))
it("should return Function nodes with the `other_nodes` list when the function module name doesn't match the module name", function()
local ast <const>, other_nodes <const> = get_doc_from_page([[
it("should return Function nodes with the `other_nodes` list when the function module name doesn't match the module name", test(
[[
<h2 class="section-header">
<a name="Static_module_functions"></a>Static module functions
</h2>
@ -779,8 +797,9 @@ describe("Scrap documentation", function()
<span id="item826" class="hide_extra"> </span>
</dd>
</dl>
]], "awful.client")
local expected_ast <const>: Node = {
]],
"awful.client",
{
children = {
{
children = {},
@ -789,17 +808,16 @@ describe("Scrap documentation", function()
},
},
name = "Client",
module_path = "awful.client",
dependencies = {},
token = "module",
}
assert.same(expected_ast, ast)
local expected_other_nodes <const>: { Node } = {
},
{
{
parameters = {},
return_types = { "integer" },
name = "client.instances",
token = "function",
}
}
assert.same(expected_other_nodes, other_nodes)
end)
}))
end)