~mna/tcheck

Simple Lua module of sanity-checks for types.

clone

read-only
https://git.sr.ht/~mna/tcheck
read/write
git@git.sr.ht:~mna/tcheck

You can also use your local clone with git send-email.

#tcheck

A pure Lua module with no external dependency that provides simple sanity-checks of types for values. It doesn't aim to turn Lua into a typed language or anything fancy, it's just basically the equivalent of the luaL_check... functions in the Lua/C API that you will typically want to have at least on your public functions.

#Install

Via Luarocks:

$ luarocks install tcheck

Or simply copy the single tcheck.lua file in your project or your LUA_PATH.

#API

Assuming local tcheck = require 'tcheck'. You can check out the tests for actual examples of using the API.

#tcheck(types, ...) or tcheck.check(types, ...)

Checks that the values conform to the types provided as first argument. The types argument can be a single string for a single value, or an array of strings.

The type string can be any of the type built-in function values, any of the math.type or io.type values, or a value to match with the __name or __type field of the value's metadata.

To support multiple types for a value, the pipe | character is used to separate different type names. As a special case, * can be specified to mean any non-nil value, it cannot be combined with any other type.

Returns the array of type names that did match for each value, or raises an error if a value did not match.

#Development

Clone the project and install the required development dependencies:

  • luaunit (unit test runner)
  • luacov (recommended, test coverage)

If like me you prefer to keep your dependencies locally, per-project, then I recommend using my llrocks wrapper of the luarocks cli, which by default uses a local lua_modules/ tree.

$ llrocks install ...

To run tests:

$ llrocks run test/main.lua

To view code coverage:

$ llrocks cover test/main.lua

#License

The BSD 3-clause license.