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:
parent
78d50c7541
commit
c5430c59b7
|
@ -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,14 +25,13 @@ 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>
|
||||||
|
@ -58,8 +66,9 @@ describe("Scrap documentation", function()
|
||||||
</span>
|
</span>
|
||||||
</dd>
|
</dd>
|
||||||
</dl>
|
</dl>
|
||||||
]], "property_signal")
|
]],
|
||||||
local expected <const>: Node = {
|
"property_signal",
|
||||||
|
{
|
||||||
children = {
|
children = {
|
||||||
{
|
{
|
||||||
children = {
|
children = {
|
||||||
|
@ -78,13 +87,13 @@ 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>
|
||||||
|
@ -152,8 +161,9 @@ describe("Scrap documentation", function()
|
||||||
</span>
|
</span>
|
||||||
</dd>
|
</dd>
|
||||||
</dl>
|
</dl>
|
||||||
]], "property_enum")
|
]],
|
||||||
local expected <const>: Node = {
|
"property_enum",
|
||||||
|
{
|
||||||
children = {
|
children = {
|
||||||
{
|
{
|
||||||
children = {
|
children = {
|
||||||
|
@ -190,13 +200,13 @@ 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>
|
||||||
|
@ -211,8 +221,9 @@ describe("Scrap documentation", function()
|
||||||
<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,13 +242,13 @@ 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>
|
||||||
|
@ -270,8 +281,9 @@ describe("Scrap documentation", function()
|
||||||
</table>
|
</table>
|
||||||
</dd>
|
</dd>
|
||||||
</dl>
|
</dl>
|
||||||
]], "awful.tag")
|
]],
|
||||||
local expected <const>: Node = {
|
"awful.tag",
|
||||||
|
{
|
||||||
children = {
|
children = {
|
||||||
{
|
{
|
||||||
children = {},
|
children = {},
|
||||||
|
@ -297,13 +309,13 @@ 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>
|
||||||
|
@ -329,8 +341,9 @@ describe("Scrap documentation", function()
|
||||||
</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)
|
||||||
|
|
Loading…
Reference in New Issue