| 1 |
# Minimalistic framework for automated testing in Nasal |
| 2 |
# |
| 3 |
# Copyright (C) 2014 Anton Gomez Alvedro |
| 4 |
# |
| 5 |
# This program is free software; you can redistribute it and/or |
| 6 |
# modify it under the terms of the GNU General Public License as |
| 7 |
# published by the Free Software Foundation; either version 2 of the |
| 8 |
# License, or (at your option) any later version. |
| 9 |
# |
| 10 |
# This program is distributed in the hope that it will be useful, but |
| 11 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
# General Public License for more details. |
| 14 |
# |
| 15 |
# You should have received a copy of the GNU General Public License |
| 16 |
# along with this program; if not, write to the Free Software |
| 17 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 18 |
|
| 19 |
|
| 20 |
# TestSuite |
| 21 |
# |
| 22 |
# Tests are organized in test suites. Each test suite contains an arbitrary |
| 23 |
# number of tests, and two special methods "setup" and "cleanup". Setup is |
| 24 |
# called before every test, and cleanup is called after every test. |
| 25 |
# |
| 26 |
# In order to define a test suite, you have to create an object parented to |
| 27 |
# TestSuite. The testing framework will identify any method in your object whose |
| 28 |
# name starts with "test_" as a test case to be run. |
| 29 |
# |
| 30 |
# Important: The order in which test cases and test suites are executed |
| 31 |
# is undefined! |
| 32 |
# |
| 33 |
# Example: |
| 34 |
# |
| 35 |
# var MyTestSuite = { |
| 36 |
# |
| 37 |
# parents: [TestSuite], |
| 38 |
# |
| 39 |
# setup: func { |
| 40 |
# Stuff to do before every test... |
| 41 |
# This is optional. You don't need to provide it if you don't use it. |
| 42 |
# }, |
| 43 |
# |
| 44 |
# cleanup: func { |
| 45 |
# Stuff to do after every test... |
| 46 |
# Also optional. |
| 47 |
# }, |
| 48 |
# |
| 49 |
# my_auxiliary_function: func { |
| 50 |
# Methods that do not start with "test_" will not be executed by the |
| 51 |
# test runner. You can define as many auxiliary functions in the test |
| 52 |
# suite as you wish. |
| 53 |
# }, |
| 54 |
# |
| 55 |
# test_trivial_test: func { |
| 56 |
# This is a real test (starts with "test_"), and it will be run by the |
| 57 |
# framework when test_run() is called.# |
| 58 |
# } |
| 59 |
# }; |
| 60 |
|
| 61 |
var TestSuite = { |
| 62 |
setup: func 0, |
| 63 |
cleanup: func 0 |
| 64 |
}; |
| 65 |
|
| 66 |
# run_tests([namespace]) |
| 67 |
# |
| 68 |
# Executes all test suites found in the given namespace. If no namespace is |
| 69 |
# specified, then the namespace where run_tests is defined is used by default. |
| 70 |
# |
| 71 |
# An effective way to work with the framework is to just include the framework |
| 72 |
# from your test files: |
| 73 |
# |
| 74 |
# io.include(".../test.nas"); |
| 75 |
# |
| 76 |
# and then execute a script like this in the Nasal Console: |
| 77 |
# |
| 78 |
# delete(globals, "test"); |
| 79 |
# io.load_nasal(".../my_test_suite.nas", "test"); |
| 80 |
# test.run_tests(); |
| 81 |
# |
| 82 |
# What this script does is: it empties the "test" namespace and then loads your |
| 83 |
# script into that namespace. The test framework will be loaded in there as |
| 84 |
# well if it was io.include'd in my_test_suite.nas. Finally, all test suites |
| 85 |
# in the "test" namespace are executed. |
| 86 |
|
| 87 |
var run_tests = func(namespace=nil) { |
| 88 |
|
| 89 |
var ns = namespace != nil ? namespace : closure(run_tests, 1); |
| 90 |
|
| 91 |
var passed = 0; |
| 92 |
var failed = 0; |
| 93 |
var err = []; |
| 94 |
|
| 95 |
foreach(var suite_name; keys(ns)) { |
| 96 |
var suite = ns[suite_name]; |
| 97 |
|
| 98 |
if (!isa(suite, TestSuite)) |
| 99 |
continue; |
| 100 |
|
| 101 |
print("Running test suite ", suite_name); |
| 102 |
|
| 103 |
foreach (var test_name; keys(suite)) { |
| 104 |
|
| 105 |
if (find("test_", test_name) != 0) |
| 106 |
continue; |
| 107 |
|
| 108 |
# Run the test case |
| 109 |
setsize(err, 0); |
| 110 |
contains(suite, "setup") and call(suite.setup, [], suite, err); |
| 111 |
size(err) == 0 and call(suite[test_name], [], suite, err); |
| 112 |
size(err) == 0 and contains(suite, "cleanup") and call(suite.cleanup, [], suite, err); |
| 113 |
|
| 114 |
if (size(err) == 0) { |
| 115 |
passed += 1; |
| 116 |
continue; |
| 117 |
} |
| 118 |
|
| 119 |
failed += 1; |
| 120 |
print("Test ", test_name, " FAILED\n"); |
| 121 |
debug.printerror(err); |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
print(sprintf("\n%d tests run. %d passed, %d failed", |
| 126 |
passed + failed, passed, failed)); |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
var assert_prop_exists = func (prop) { |
| 131 |
assert(props.globals.getNode(prop) != nil, |
| 132 |
sprintf("Property %s does not exist", prop)); |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
var fail_if_prop_exists = func (prop) { |
| 137 |
assert(props.globals.getNode(prop) == nil, |
| 138 |
sprintf("Property %s exists", prop)); |
| 139 |
} |