Skip to content

Commit

Permalink
fix(is_in_nav): assert invalid data
Browse files Browse the repository at this point in the history
When possible anyway. Directly modifying the array is unsupported.
  • Loading branch information
Lazerbeak12345 committed Apr 28, 2024
1 parent d55f8f3 commit 12f0507
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,15 @@ A `table` for page information.
| :-- | :--- | :---------- |
| `title` | `string` | **Required**. Human readable page name. |
| `get` | `function`, see below | **Required**. Return a flow form. |
| `is_in_nav` | `function`, see below | **Optional**. Return true to show in the navigation, which defaults to tabs. |
| `is_in_nav` | `function`, see below | **Optional**. Return true to show in the navigation, which defaults to tabs. If `is_in_nav` is absent, it will default to showing in the navigation. |
| `on_enter` | `function`, see below | **Optional**. Called when the player changes pages, usually using the tabs. |
| `on_leave` | `function`, see below | **Optional**. When leaving this page to go to another, called before other's `on_enter` |
| | | **Optional**. Anything else you'd like to add. These other values can, of course, be accessed through the `self` value in the above callbacks as you'd expect. |

##### Page definition function args

All page definition functions expect the same arguments.

```lua
function(self, player, context)
-- ...
Expand All @@ -121,7 +123,11 @@ end
| `player` | Player `ObjectRef` | The player that will be shown the page. |
| `context` | `table` | Context table. See `sway.get_or_create_context` |

Return a `flow` page. Sway makes use of `flow_extras.set_wrapped_context` internally. This ensures that `flow_extras.get_context` works if you need to share code between multiple forms.
`get` is expected to a `flow` page. Sway makes use of `flow_extras.set_wrapped_context` internally. This ensures that `flow_extras.get_context` works if you need to share code between multiple forms.

`is_in_nav` is expected to return a boolean (see above).

`on_enter` and `on_leave` are not expected to return anything.

#### Refresh form with new changes

Expand Down
8 changes: 8 additions & 0 deletions api.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ function sway.register_page(name, def)
assert(type(name) == "string", "[sway] register_page: requires name to be string")
assert(type(def) == "table", "[sway] register_page: requires definition table to be table")
assert(type(def.get) == "function", "[sway] register_page: requires get inside the definition table to be function")
assert(
def.is_in_nav == nil or type(def.is_in_nav) == "function",
"[sway] register_page: page '" .. name .. "' is_in_nav must be nil or a fn."
)
assert(not sway.pages[name], "[sway] register_page: page '" .. name .. "' must not already be registered")

sway.pages[name] = def
Expand All @@ -29,6 +33,10 @@ function sway.override_page(name, def)
if type(def.get) ~= "nil" then
assert(type(def.get) == "function", "[sway] override_page: When overriding get, it must be a function.")
end
assert(
def.is_in_nav == nil or type(def.is_in_nav) == "function",
"[sway] override_page: page '" .. name .. "' is_in_nav must be nil or a fn."
)
minetest.log("action", "[sway] override_page: '" .. name .. "' is becoming overriden")
for key, value in pairs(def) do
page[key] = value
Expand Down
13 changes: 11 additions & 2 deletions spec/sway_inv_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ describe("pages", function ()
sway.pages_ordered = {}
end)
describe("register_page", function ()
pending"assert when is_in_nav is not null or a function"
it("is a function on sway", function ()
assert.equal("function", type(sway.register_page))
end)
Expand Down Expand Up @@ -118,6 +117,11 @@ describe("pages", function ()
sway.register_page(testpagename, { get = function () end })
end, "[sway] register_page: page '" .. testpagename .. "' must not already be registered")
end)
it("assert when is_in_nav is not null or a function", function ()
assert.has_error(function ()
sway.register_page(testpagename, { get = function () end, is_in_nav = "A very sus string." })
end, "[sway] register_page: page '" .. testpagename .. "' is_in_nav must be nil or a fn.")
end)
it("inserts name into def, puts def into pages as key and pages_ordered", function ()
local def = { get = function () end }
sway.register_page(testpagename, def)
Expand Down Expand Up @@ -155,6 +159,12 @@ describe("pages", function ()
sway.override_page(testpagename, {})
end, "[sway] override_page: the page '" .. testpagename .. "' could not be found to override")
end)
it("assert when is_in_nav is not null or a function", function ()
sway.register_page(testpagename, { get = function () end })
assert.has_error(function ()
sway.override_page(testpagename, { get = function () end, is_in_nav = "A very sus string." })
end, "[sway] override_page: page '" .. testpagename .. "' is_in_nav must be nil or a fn.")
end)
it("logs that a page is getting overriden", function ()
sway.register_page(testpagename, { get = function () end })
fancy_stub(minetest, "log", function ()
Expand Down Expand Up @@ -1242,7 +1252,6 @@ describe("content functions", function ()
page = "nothing else"
})
end)
pending"sets the current_idx to the correct page"
end)
-- TODO: this should also be tested for pages that _do_ exist, but are not in the nav
pending"returns result of get for sway.page['404'] when page not found, and 404 is truthy"
Expand Down

0 comments on commit 12f0507

Please sign in to comment.