| 1 | ; docformat = 'rst' |
|---|
| 2 | |
|---|
| 3 | ;+ |
|---|
| 4 | ; To create a test case just inherit from MGtestCase and create method with |
|---|
| 5 | ; names that start with "test". This test can be run with the command:: |
|---|
| 6 | ; |
|---|
| 7 | ; mgunit, cases='findgentest' ;- |
|---|
| 8 | ;- |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | ;+ |
|---|
| 12 | ; This test should pass the assertion and return 1 (i.e. success). Tests can |
|---|
| 13 | ; also return 0 or generate an error to indicate failure. |
|---|
| 14 | ;- |
|---|
| 15 | function findgen_ut::test_basic |
|---|
| 16 | compile_opt strictarr |
|---|
| 17 | |
|---|
| 18 | a = findgen(5) |
|---|
| 19 | assert, array_equal(a, [0.0, 1.0, 2.0, 3.0, 4.0]), 'Correct elements' |
|---|
| 20 | |
|---|
| 21 | return, 1 |
|---|
| 22 | end |
|---|
| 23 | |
|---|
| 24 | |
|---|
| 25 | ;+ |
|---|
| 26 | ; This test fails because the assertion is wrong. |
|---|
| 27 | ;- |
|---|
| 28 | function findgen_ut::test_fail_example |
|---|
| 29 | compile_opt strictarr |
|---|
| 30 | |
|---|
| 31 | a = findgen(5) |
|---|
| 32 | assert, n_elements(a) eq 6, 'Wrong number of elements' |
|---|
| 33 | |
|---|
| 34 | return, 1 |
|---|
| 35 | end |
|---|
| 36 | |
|---|
| 37 | |
|---|
| 38 | ;+ |
|---|
| 39 | ; This is a test that will pass because the code of the test is supposed to |
|---|
| 40 | ; cause an error. To do this kind of test, use the "error_is_pass" batch file. |
|---|
| 41 | ;- |
|---|
| 42 | function findgen_ut::test_error |
|---|
| 43 | compile_opt strictarr |
|---|
| 44 | @error_is_pass |
|---|
| 45 | |
|---|
| 46 | a = findgen('string') |
|---|
| 47 | |
|---|
| 48 | return, 0 |
|---|
| 49 | end |
|---|
| 50 | |
|---|
| 51 | |
|---|
| 52 | ;+ |
|---|
| 53 | ; This is a test that will fail on an io error because of the use of the |
|---|
| 54 | ; "error_is_fail" batch file. IO errors don't normally cause a test to fail. |
|---|
| 55 | ;- |
|---|
| 56 | function findgen_ut::test_baderror |
|---|
| 57 | compile_opt strictarr |
|---|
| 58 | @error_is_fail |
|---|
| 59 | |
|---|
| 60 | a = findgen('another_string') |
|---|
| 61 | |
|---|
| 62 | return, 1 |
|---|
| 63 | end |
|---|
| 64 | |
|---|
| 65 | |
|---|
| 66 | ;+ |
|---|
| 67 | ; Inherit from MGtestCase. |
|---|
| 68 | ;- |
|---|
| 69 | pro findgen_ut__define |
|---|
| 70 | compile_opt strictarr |
|---|
| 71 | |
|---|
| 72 | define = { findgen_ut, inherits MGutTestCase } |
|---|
| 73 | end |
|---|