Writing Tests
Preview
Testing and the related agent skills are a preview feature. Details may change as we refine them.
Tests live in "spec" files named <name>.spec.pro. Each one is a main-level program that uses Luna.
Structure
idl
compile_opt idl2
l = luna()
s = l.suite('toBeA')
it = s.test('matches all of the specified types')
(it.expects(fltarr(5))).toBeA, ['array', 'float']
(it.expects(42))._not_.toBeA, 'string'
it = s.test('can run a routine')
(it.expects('my_routine')).toRunProcedure, 5
l.generateTestSummary
endl = luna()must be in a main-level program. It finds the closestidltest.jsonautomatically- A suite groups related tests, a test (
it) is one behavior, and a test has one or more expectations compile_opt idl2is required. Always end withl.generateTestSummaryandend- Name tests for what they check, like "throws on bad input". Test names are how the results read
- Define any helper routines before the main-level code
Expectations
Call a matcher on an expectation: (it.expects(value)).matcher, args. Negate any matcher with _not_:
idl
(it.expects(42))._not_.toBeNull| To check | Use |
|---|---|
| Equality | toEqual (/fold_case for strings), toStrictEqual |
| Truth | toBeTrue, toBeFalse, toBeTruthy, toBeFalsy, toBeNull |
| Numbers | toBeGreaterThan, toBeGreaterThanOrEqual, toBeLessThan, toBeLessThanOrEqual, toBeBetween, toBeCloseTo, toBeNaN, toBeFinite, toBeInfinite |
| Types | toBeA, toBeTypename, toBeInstanceOf, toBeAnArrayOf, toBeScalar, toBeArray, toBeAValidObject, toBeSameObject |
| Strings | toContainString, toMatchRegex, toStartWith, toEndWith |
| Arrays and lists | toContain, toContainEqual, toContainAll, toContainAny, toFindInArray, toBeSorted, toHaveUniqueValues, toBeEmpty |
| Size | toHaveLength, toHaveDimensions, toHaveSameDimensions |
| Hashes, structures, objects | toHaveKey, toHaveKeyValue, toHaveTag, toHaveMethod |
| Files | toBeFile, toBeDirectory, toExist |
| Running code | toRunProcedure, toRunFunction, toRunProcedureMethod, toRunFunctionMethod |
| Errors, speed, output | toThrow, toThrowMessage, toThrowMatching, toRunWithin, toPrint |
| ENVI and IDL tasks | toBeAValidENVITask, toBeAValidIDLTask, toExecuteENVITask, toExecuteIDLTask |
Testing routines
idl
(it.expects('my_routine')).toThrow, bad_input ; procedure by default
(it.expects('my_function')).toThrow, bad_input, /is_function ; use /IS_FUNCTION for functions
(it.expects('my_routine')).toThrowMessage, 'must be positive', -1
(it.expects('my_routine')).toRunWithin, 2.0, big_input
(it.expects('my_routine')).toPrint, 'Hello', /fold_case
(it.expects(obj)).toThrow, 'method', arg1 ; objects take the method name firstHelpful hints
- Create objects with
obj_new, because the routine is passed by name:(it.expects('obj_new')).toRunFunction, 'my_class', arg - If your code uses
scope_level(), call it directly in the test. Routines run from inside the expectation code, which has a different scope - If your code handles an error and moves on, call
message, /resetin its catch block. Otherwise the error bubbles up through the IDL-IDLBridge - Only start ENVI (headless) in specs that test ENVI tasks
- Sol and Luna can't test widgets directly
Pro Tip
Ask IDL Agent to write tests for you. The idl-write-tests skill knows every matcher and how to read the results.
Reading the output
Test file: src/example.spec.pro
toBeA
can be negated
expected not to be types byte, and string (0.00 s, line 16)
%FAILED% expected to be type string (0.01 s)
Expected: to be type string
Received: long
3 | it = s.test('variable is a string')
> 4 | (it.expects(42)).toBeA, 'string'
5 | (it.expects(myVar)).toEqual, 'I love tests!'
at C:\repo\src\example.spec.pro:5
FAIL
Tests: 1 failed, 1 passed, 2 total
Time: 0.2 s- A failure shows
%FAILED%, the expected and received values, the code around the failure (>marks the line), and the location Tests:counts one peritblock. A test fails if any of its expectations fail- Each spec also writes
<name>.spec.pro.lognext to it with the same output plus a code coverage analysis