|
| 1 | +This directory contains the LuaJIT test suite, or at least something which |
| 2 | +will evolve into the LuaJIT test suite. Large chunks of the suite can also |
| 3 | +be run with any other Lua 5.1 or 5.2 interpreter. |
| 4 | + |
| 5 | +## Running the test suite ## |
| 6 | + |
| 7 | +To run the default test suite, run `test.lua` using the Lua interpreter you |
| 8 | +wish to test, for example: |
| 9 | + |
| 10 | + $ ~/luajit-2.0/src/luajit test.lua |
| 11 | + |
| 12 | +If the test suite passes, the final line printed to stdout will be |
| 13 | +`NNN passed`, and the exit code of the process will be zero. If any tests |
| 14 | +fail, the exit code will be non-zero. If the failures caused catastrophic |
| 15 | +termination of the entire process (such as a segmentation fault or assertion |
| 16 | +failure), the last line of output will be number and name of the test which |
| 17 | +caused the catastrophe. If the failures weren't catastrophic, the penultimate |
| 18 | +line of output will be `NNN passed, MMM failed`, and the last line will say |
| 19 | +how to re-run just the failing tests. |
| 20 | + |
| 21 | +Various flags and options can be passed to `test.lua` to control which tests |
| 22 | +are run, and in which order. Run `lua test.lua --help` for details. |
| 23 | + |
| 24 | +## Structure of the test suite ## |
| 25 | + |
| 26 | +The test suite consists of a directory tree. Within said tree there are various |
| 27 | +`.lua` files, and within every `.lua` file there are one or more tests. Every |
| 28 | +directory in the tree contains a file called `index`, which enumerates the |
| 29 | +members of the directory which contribute to the test suite (this is done to |
| 30 | +avoid an external dependency for directory iteration, and to allow metadata to |
| 31 | +be specified at the file/directory level). Every `.lua` file is structured as: |
| 32 | + |
| 33 | + << local definitions >> |
| 34 | + << test 1 >> |
| 35 | + ... |
| 36 | + << test N >> |
| 37 | + |
| 38 | +Where `<< local definitions >>` consists of Lua code to act as a common prefix |
| 39 | +for every test in the file, and each `<< test >>` looks like: |
| 40 | + |
| 41 | + do --- <<name>> <<metadata>> |
| 42 | + << code >> |
| 43 | + end |
| 44 | + |
| 45 | +Where `<<name>>` is (almost) free-form, and `<< code >>` is Lua code which |
| 46 | +performs some actions and probably calls `assert` alot. The `<<metadata>>` |
| 47 | +fragment can be used to specify the conditions under which the test should |
| 48 | +or should not run, to adjust the environment in which the test is run, and to |
| 49 | +allow key/value pairs to be specified in a standard place/format. |
| 50 | + |
| 51 | +Some common pieces of metadata are: |
| 52 | + * `+luajit>=2.1` - The test requires LuaJIT 2.1 or later to run. |
| 53 | + * `+lua<5.2` - The test requires Lua 5.1 or earlier to run (all versions of |
| 54 | + LuaJIT report themselves as 5.1). |
| 55 | + * `+ffi` - The test requires the `ffi` library to be present. |
| 56 | + * `+bit` - The test requires the `bit` library to be present. |
| 57 | + * `+jit` - The test requires JIT compilation be available and turned on. |
| 58 | + * `+slow` - The test is too slow to run as part of the default suite, and |
| 59 | + thus requires `+slow` to be specified on the command line. |
| 60 | + * `!private_G` - The test modifies globals, and thus needs to be run with a |
| 61 | + private (shallow) copy of `_G`. |
| 62 | + |
| 63 | +Lua code which is common across all (or some) tests in a single file can be |
| 64 | +written at the top of the file as part of `<< local definitions >>`. Code |
| 65 | +which is common across multiple files lives in the `common` directory, and |
| 66 | +is pulled into applicable tests by means of `local x = require"common.x"`. |
| 67 | + |
| 68 | +It is intended that most `.lua` files in the test suite can be exercised |
| 69 | +without the test runner by just passing them to a Lua interpreter. In such |
| 70 | +cases, metadata is ignored, the tests are executed from top to bottom, and |
| 71 | +any failure results in later tests not running. Also note that the test |
| 72 | +runner converts every test into a separate function, which causes references |
| 73 | +to local definitions to become upvalue accesses rather than local variable |
| 74 | +accesses - in some cases this can cause differences in behaviour. |
| 75 | + |
| 76 | +## Extending the test suite ## |
| 77 | + |
| 78 | +First of all, decide where your new test(s) should live. This might be within |
| 79 | +an existing `.lua` file, or might involve creating new files and/or directories. |
| 80 | +If new files are created, remember to add them to the `index` file of the |
| 81 | +enclosing directory. If new directories are created, remember to create an |
| 82 | +`index` file in said directory, and add the new directory to the `index` file |
| 83 | +in the parent directory. |
| 84 | + |
| 85 | +Once you've decided in which file the test(s) should live, you're ready to add |
| 86 | +your test(s) to said file. Each test should be wrapped in a `do`/`end` block, |
| 87 | +and given some kind of name (after the `do` keyword, as in `do --- <<name>>`). |
| 88 | +The test should call `assert` to confirm whether the thing under test is |
| 89 | +behaving, or otherwise raise an error if the thing under test is misbehaving. |
| 90 | +Your test(s) should not write to stdout or stderr, nor should they mutate |
| 91 | +global state. After your test(s) are written, you should be able to determine |
| 92 | +which features they require, and put on metadata appropriately. |
| 93 | + |
| 94 | +## Completing the tidy-up of the test suite ## |
| 95 | + |
| 96 | +Some files/directories in this directory need some thought: |
| 97 | + |
| 98 | + * `common/ffi_util.inc` - Needs renaming and being made `require`-able. |
| 99 | + * `lib/ffi` - Tests need converting to structure described in this document. |
| 100 | + * `lib/table/misc.lua` - Tests need organising and converting to structure |
| 101 | + described in this document. |
| 102 | + * `misc` - Tests need organising and converting to structure described in |
| 103 | + this document. |
| 104 | + * `src` - C/C++ source which needs to be compiled into a dynamic library and |
| 105 | + loaded for certain tests. Need to figure out a good way of handling |
| 106 | + C/C++ source. |
| 107 | + * `sysdep` - Need to figure out a good way of handling these. |
| 108 | + * `unportable` - Need to figure out a good way of handling these. |
| 109 | + |
| 110 | +After that, consult the README file by Mike in the directory above this one. |
0 commit comments