~mna/tcheck

8eed7129524f1808ae588ba0b01e7dd714c21e52 — Martin Angers 3 years ago 28bc9bb
doc
2 files changed, 81 insertions(+), 0 deletions(-)

A LICENSE
A README.md
A LICENSE => LICENSE +12 -0
@@ 0,0 1,12 @@
Copyright (c) 2020, Martin Angers

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


A README.md => README.md +69 -0
@@ 0,0 1,69 @@
# tcheck

A pure Lua module with no external dependency that provides simple sanity-checks of types for values.

* Canonical repository: https://git.sr.ht/~mna/tcheck
* Issue tracker: https://todo.sr.ht/~mna/tcheck

## 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][bsd] license.

[bsd]: http://opensource.org/licenses/BSD-3-Clause
[llrocks]: https://git.sr.ht/~mna/llrocks