spec(module_doc): fix and refactor
ci/woodpecker/pr/docker-build Pipeline was successful Details
ci/woodpecker/pr/lint Pipeline was successful Details
ci/woodpecker/pr/build Pipeline was successful Details
ci/woodpecker/pr/test Pipeline failed Details

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