awesomewm.d.tl/spec/scraper/module_doc_spec.tl

627 lines
21 KiB
Plaintext

local assert = require("luassert")
local type Node = require("types.Node")
local scraper = require("scraper.module_doc")
local get_doc_from_page = scraper.get_doc_from_page
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 = {
children = {
{
children = {},
name = "Signal",
token = "enum",
}
},
name = "Empty",
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 = {
children = {
{
children = {
{
name = "property::value",
token = "identifier",
},
},
name = "Signal",
token = "enum",
},
{
name = "value",
types = { "number" },
token = "variable",
}
},
name = "Property_signal",
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([[
<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>
</dl>
]], "property_enum")
local expected <const>: Node = {
children = {
{
children = {
{
name = "property::horizontal_fit_policy",
token = "identifier",
},
},
name = "Signal",
token = "enum",
},
{
children = {
{
name = "auto",
token = "identifier",
},
{
name = "none",
token = "identifier",
},
{
name = "fit",
token = "identifier",
},
},
name = "Horizontal_fit_policy",
token = "enum",
},
{
name = "horizontal_fit_policy",
types = { "Horizontal_fit_policy" },
token = "variable",
},
},
name = "Property_enum",
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 = {
children = {
{
children = {
{
name = "property::markup",
token = "identifier",
},
},
name = "Signal",
token = "enum",
},
{
name = "markup",
types = { "string" },
token = "variable",
}
},
name = "Property_string",
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 = {
children = {
{
children = {},
name = "Signal",
token = "enum",
},
{
parameters = {
{
types = { "Tag" },
name = "self",
token = "variable",
},
{
types = { "tag" }, -- This needs to be fixed : tag -> Tag
name = "tag2",
token = "variable",
}
},
return_types = {},
name = "swap",
token = "function",
},
},
name = "Tag",
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">
<dt>
<a
class="copy-link js-copy-link"
name="widget::layout_changed"
href="#widget::layout_changed"
>🔗</a
>
<strong>widget::layout_changed</strong>
<span class="baseclass"> ·&nbsp;Inherited from wibox.widget.base </span>
</dt>
<dd></dd>
<dt>
<a
class="copy-link js-copy-link"
name="widget::redraw_needed"
href="#widget::redraw_needed"
>🔗</a
>
<strong>widget::redraw_needed</strong>
<span class="baseclass"> ·&nbsp;Inherited from wibox.widget.base </span>
</dt>
<dd></dd>
</dl>
]], "signal")
local expected <const>: Node = {
children = {
{
children = {
{
name = "widget::layout_changed",
token = "identifier",
},
{
name = "widget::redraw_needed",
token = "identifier",
},
},
name = "Signal",
token = "enum",
},
},
name = "Signal",
token = "module",
}
assert.same(expected, ast)
end)
it("should produce Function nodes", function()
local ast <const> = get_doc_from_page([[
<h2 class="section-header">
<a name="Static_module_functions"></a>Static module functions
</h2>
<dl class="function">
<dt>
<a class="copy-link js-copy-link" name="kill" href="#kill">🔗</a>
<strong
><span class="function_modname">awesome.</span>kill
<span class="function_args"> <b>(</b>pid, sig<b>)</b></span></strong
>
<span class="proptype"
><span class="summary_type"> -&gt;&nbsp;boolean</span></span
>
<span class="baseclass"> </span>
</dt>
<dd>
Send a signal to a process.
<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">pid</span></td>
<td></td>
<td>
<span class="types"><span class="type">integer</span></span>
</td>
<td class="see_also_description">
Process identifier. 0 and negative values have special meaning. See
<code>man 3 kill</code>.
</td>
</tr>
<tr>
<td><span class="parameter">sig</span></td>
<td></td>
<td>
<span class="types"><span class="type">integer</span></span>
</td>
<td class="see_also_description">
Signal number. See
<a href="../core_components/awesome.html#unix_signal"
>awesome.unix_signal</a
>
for a list of signals.
</td>
</tr>
</tbody>
</table>
<h3>Returns:</h3>
<ol>
<span class="types"><span class="type">boolean</span></span>
true if the signal was successfully sent, else false
</ol>
</dd>
</dl>
]], "awesome") -- The module name must be the same as the module name in the doc
local expected <const>: Node = {
children = {
{
children = {},
name = "Signal",
token = "enum",
},
{
parameters = {
{
types = { "integer" },
name = "pid",
token = "variable",
},
{
types = { "integer" },
name = "sig",
token = "variable",
},
},
return_types = { "boolean" },
name = "kill",
token = "function",
}
},
name = "Awesome",
token = "module",
}
assert.same(expected, ast)
end)
-- TODO : Fix the code then come back to this test, the current implementation is incomplete
-- it("should produce a Record node when the function parameter is a table", function()
-- local ast <const> = get_doc_from_page([[
-- <h2 class="section-header">
-- <a name="Static_module_functions"></a>Static module functions
-- </h2>
-- <dl class="function">
-- <dt>
-- <a
-- class="copy-link js-copy-link"
-- name="awful.screen.focused"
-- href="#awful.screen.focused"
-- >🔗</a
-- >
-- <strong
-- >awful.screen.focused
-- <span class="function_named_args"><b>{</b>[args]<b>}</b></span></strong
-- >
-- <span class="proptype"
-- ><span class="summary_type"> -&gt;&nbsp;nil <i>or</i> screen</span></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>
-- <th>Default value</th>
-- </tr>
-- <tr>
-- <td><span class="parameter">args</span></td>
-- <td><span class="chips">Optional</span></td>
-- <td>
-- <span class="types"><span class="type">table</span></span>
-- </td>
-- <td class="see_also_description"></td>
-- <td><span class="not_applicable">Undefined</span></td>
-- </tr>
-- <tr class="see_also_sublist">
-- <td><span class="parameter">client</span></td>
-- <td><span class="chips">Optional</span></td>
-- <td>
-- <span class="types"><span class="type">boolean</span></span>
-- </td>
-- <td class="see_also_description">
-- Use the client screen instead of the mouse screen.
-- </td>
-- <td>
-- <span class="default_value"><code>false</code></span>
-- </td>
-- </tr>
-- <tr class="see_also_sublist">
-- <td><span class="parameter">mouse</span></td>
-- <td><span class="chips">Optional</span></td>
-- <td>
-- <span class="types"><span class="type">boolean</span></span>
-- </td>
-- <td class="see_also_description">Use the mouse screen</td>
-- <td>
-- <span class="default_value"><code>true</code></span>
-- </td>
-- </tr>
-- </tbody>
-- </table>
-- <h3>Returns:</h3>
-- <ol>
-- <span class="types"
-- >optional
-- <a class="type" href="../core_components/screen.html#screen"
-- >screen</a
-- ></span
-- >
-- The focused screen object, or
-- <code>nil</code>
-- in case no screen is present currently.
-- </ol>
-- </dd>
-- </dl>
-- ]], "awful.screen")
-- print(require("inspect")(ast))
-- assert.same(ast, {
-- children = {
-- {
-- children = {},
-- name = "Signal",
-- token = "enum",
-- },
-- {
-- parameters = {
-- {
-- children = {
-- {
-- types = { "boolean" },
-- name = "client",
-- token = "variable",
-- },
-- {
-- types = { "boolean" },
-- name = "mouse",
-- token = "variable",
-- },
-- },
-- name = "Args",
-- token = "record",
-- },
-- {
-- types = { "Focused_Args" },
-- name = "args",
-- token = "variable",
-- },
-- },
-- return_types = { "screen" },
-- name = "focused",
-- token = "function",
-- },
-- },
-- name = "Screen",
-- token = "module",
-- })
-- 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([[
<h2 class="section-header">
<a name="Static_module_functions"></a>Static module functions
</h2>
<dl class="function">
<dt>
<a class="copy-link js-copy-link" name="instances" href="#instances">🔗</a>
<strong
><span class="function_modname">client.</span>instances
<span class="function_args"> <b>(</b><b>)</b></span></strong
>
<span class="proptype"
><span class="summary_type"> -&gt;&nbsp;integer</span></span
>
<span class="baseclass"> </span>
</dt>
<dd>
Get the number of instances.
<h3>Returns:</h3>
<ol>
<span class="types"><span class="type">integer</span></span>
The number of client objects alive.
</ol>
<span id="item826" class="hide_extra"> </span>
</dd>
</dl>
]], "awful.client")
local expected_ast <const>: Node = {
children = {
{
children = {},
name = "Signal",
token = "enum",
},
},
name = "Client",
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)