tests.bi

Summary
tests.bi
About this ModuleThis is the Extended Library’s built-in Unit Testing Framework.
Code LicenseContains code contributed and Copyright © 2006-2008 Luis Lavena, Multimedia systems
ext.tests
Types
test_func_tFunction prototype for testcases.
Enumerations
HookUsed with addSuiteHook to run a procedure on certain events.
Functions
customAssertionUsed by the helper macros below.
addSuiteAdd a test Suite.
addSuiteHookAssign a function to run at certain points in the suite.
addTestAdds a testcase to the testsuite.
runTestsRuns the testcases for all registered Suites.
Macros
ext_assert_true
ext_assert_true_error
ext_assert_false
ext_assert_false_error
ext_assert_equal
ext_assert_equal_error
ext_assert_not_equal
ext_assert_not_equal_error
ext_assert_string_equal
ext_assert_string_equal_error
ext_assert_string_not_equal
ext_assert_string_not_equal_error
ext_assert_pass
ext_assert_fail
ext_assert_error

About this Module

This is the Extended Library’s built-in Unit Testing Framework.  You can find more information about unit testing in general at http://en.wikipedia.org/wiki/Unit_testing

The best example of how to use this framework is to see how ext uses it.  All of the tests are located in directories at https://code.google.com/p/fb-extended-lib/source/browse/tests/.  The main.bas in the root directory is a just a simple main function that runs the tests.

Code License

Contains code contributed and Copyright © 2006-2008 Luis Lavena, Multimedia systems

Copyright © 2007-2014, FreeBASIC Extended Library Development Group

Distributed under the FreeBASIC Extended Library Group license.  See accompanying file LICENSE.txt or copy at http://code.google.com/p/fb-extended-lib/wiki/License

ext.tests

Summary
Types
test_func_tFunction prototype for testcases.
Enumerations
HookUsed with addSuiteHook to run a procedure on certain events.
Functions
customAssertionUsed by the helper macros below.
addSuiteAdd a test Suite.
addSuiteHookAssign a function to run at certain points in the suite.
addTestAdds a testcase to the testsuite.
runTestsRuns the testcases for all registered Suites.
Macros
ext_assert_true
ext_assert_true_error
ext_assert_false
ext_assert_false_error
ext_assert_equal
ext_assert_equal_error
ext_assert_not_equal
ext_assert_not_equal_error
ext_assert_string_equal
ext_assert_string_equal_error
ext_assert_string_not_equal
ext_assert_string_not_equal_error
ext_assert_pass
ext_assert_fail
ext_assert_error

Types

test_func_t

type test_func_t as sub()

Function prototype for testcases.

Enumerations

Hook

Used with addSuiteHook to run a procedure on certain events.

before_allruns this procedure before running any testcases.
before_eachruns this procedure before running each testcase.
after_allruns this procedure after running all testcases.
after_eachruns this procedure after each testcase.

Functions

customAssertion

declare function customAssertion(byval as ext.bool,  
byref as string,  
byval as uinteger,  
byref as string,  
byval as ext.bool =  false) as ext.bool

Used by the helper macros below.  Generally not needed to call directly.

Parameters

testboolean value indicating whether assertion was sucessful.
filestring containing the file the testcase is in.
lineuinteger containing the line number of the assertion.
msgstring containing the message to be passed to the user if assertion fails.
errorboolean value indicating true if there is an error and the suite should stop. defaults to false.

Returns

boolean value indicating false if there is a fatal error.  Usually ignored.

addSuite

declare function addSuite(byref as string) as ext.bool

Add a test Suite.

Parameters

namestring containing the name of the test suite to add, used in error reporting.

Returns

True if sucessful.

addSuiteHook

declare function addSuiteHook(byval as Hook,
byref as sub()) as ext.bool

Assign a function to run at certain points in the suite.

Parameters

hooktypesee Hook
procfunction to run at specified time.

Returns

True if procedure is added.

addTest

declare function addTest(byref as string,
byref as test_func_t) as ext.bool

Adds a testcase to the testsuite.

Parameters

namestring containing the name of the testcase.
procprocedure containing the testcase.

Returns

True if testcase is added.

runTests

declare function runTests() as ext.bool

Runs the testcases for all registered Suites.

Returns

True on success.

Macros

ext_assert_true

#define ext_assert_true(
   __value__
) ext.tests.customAssertion((__value__) = (true), __FILE__, __LINE__, ("{" #__value__ "} is not true."))

ext_assert_true_error

#define ext_assert_true_error(
   __value__
) if (ext.tests.customAssertion((__value__) = (true), __FILE__, __LINE__, ("{" #__value__ "} is not true."), true) = false) then exit sub

ext_assert_false

#define ext_assert_false(
   __value__
) ext.tests.customAssertion((__value__) = (false), __FILE__, __LINE__, ("{" #__value__ "} is not false."))

ext_assert_false_error

#define ext_assert_false_error(
   __value__
) if (ext.tests.customAssertion((__value__) = (false), __FILE__, __LINE__, ("{" #__value__ "} is not false."), true) = false) then exit sub

ext_assert_equal

#define ext_assert_equal(
   __expected__,
   __actual__
) ext.tests.customAssertion((__actual__) = (__expected__), __FILE__, __LINE__, ("expected {" #__expected__ "} but was {" #__actual__ "}"))

ext_assert_equal_error

#define ext_assert_equal_error(
   __expected__,
   __actual__
) if (ext.tests.customAssertion((__actual__) = (__expected__), __FILE__, __LINE__, ("expected {" #__expected__ "} but was {" #__actual__ "}"), true) = false) then exit sub

ext_assert_not_equal

#define ext_assert_not_equal(
   __expected__,
   __actual__
) ext.tests.customAssertion((__actual__) <> (__expected__), __FILE__, __LINE__, ("{" #__actual__ "} expected to be != to {" #__expected__ "}"))

ext_assert_not_equal_error

#define ext_assert_not_equal_error(
   __expected__,
   __actual__
) if (ext.tests.customAssertion((__actual__) <> (__expected__), __FILE__, __LINE__, ("{" #__actual__ "} expected to be != to {" #__expected__ "}"), true) = false) then exit sub

ext_assert_string_equal

#define ext_assert_string_equal(
   __expected__,
   __actual__
) ext.tests.customAssertion((str(__actual__) = str(__expected__)), __FILE__, __LINE__, ("expected {" #__expected__ "} but was {" #__actual__ "}"))

ext_assert_string_equal_error

#define ext_assert_string_equal_error(
   __expected__,
   __actual__
) if (ext.tests.customAssertion((str(__actual__) = str(__expected__)), __FILE__, __LINE__, ("expected {" #__expected__ "} but was {" #__actual__ "}"), true) = false) then exit sub

ext_assert_string_not_equal

#define ext_assert_string_not_equal(
   __expected__,
   __actual__
) ext.tests.customAssertion((str(__actual__) <> str(__expected__)), __FILE__, __LINE__, ("{" #__actual__ "} expected to be != to {" #__expected__ "}"))

ext_assert_string_not_equal_error

#define ext_assert_string_not_equal_error(
   __expected__,
   __actual__
) if (ext.tests.customAssertion((str(__actual__) <> str(__expected__)), __FILE__, __LINE__, ("{" #__actual__ "} expected to be != to {" #__expected__ "}"), true) = false) then exit sub

ext_assert_pass

#define ext_assert_pass(
   __message__
) ext.tests.customAssertion((true), __FILE__, __LINE__, __message__)

ext_assert_fail

#define ext_assert_fail(
   __message__
) ext.tests.customAssertion((false), __FILE__, __LINE__, __message__)

ext_assert_error

#define ext_assert_error(
   __message__
) if (ext.tests.customAssertion((false), __FILE__, __LINE__, __message__, true) = false) then exit sub
type test_func_t as sub()
Function prototype for testcases.
declare function customAssertion(byval as ext.bool,  
byref as string,  
byval as uinteger,  
byref as string,  
byval as ext.bool =  false) as ext.bool
Used by the helper macros below.
declare function addSuite(byref as string) as ext.bool
Add a test Suite.
declare function addSuiteHook(byval as Hook,
byref as sub()) as ext.bool
Assign a function to run at certain points in the suite.
declare function addTest(byref as string,
byref as test_func_t) as ext.bool
Adds a testcase to the testsuite.
declare function runTests() as ext.bool
Runs the testcases for all registered Suites.
#define ext_assert_true(
   __value__
) ext.tests.customAssertion((__value__) = (true), __FILE__, __LINE__, ("{" #__value__ "} is not true."))
#define ext_assert_true_error(
   __value__
) if (ext.tests.customAssertion((__value__) = (true), __FILE__, __LINE__, ("{" #__value__ "} is not true."), true) = false) then exit sub
#define ext_assert_false(
   __value__
) ext.tests.customAssertion((__value__) = (false), __FILE__, __LINE__, ("{" #__value__ "} is not false."))
#define ext_assert_false_error(
   __value__
) if (ext.tests.customAssertion((__value__) = (false), __FILE__, __LINE__, ("{" #__value__ "} is not false."), true) = false) then exit sub
#define ext_assert_equal(
   __expected__,
   __actual__
) ext.tests.customAssertion((__actual__) = (__expected__), __FILE__, __LINE__, ("expected {" #__expected__ "} but was {" #__actual__ "}"))
#define ext_assert_equal_error(
   __expected__,
   __actual__
) if (ext.tests.customAssertion((__actual__) = (__expected__), __FILE__, __LINE__, ("expected {" #__expected__ "} but was {" #__actual__ "}"), true) = false) then exit sub
#define ext_assert_not_equal(
   __expected__,
   __actual__
) ext.tests.customAssertion((__actual__) <> (__expected__), __FILE__, __LINE__, ("{" #__actual__ "} expected to be != to {" #__expected__ "}"))
#define ext_assert_not_equal_error(
   __expected__,
   __actual__
) if (ext.tests.customAssertion((__actual__) <> (__expected__), __FILE__, __LINE__, ("{" #__actual__ "} expected to be != to {" #__expected__ "}"), true) = false) then exit sub
#define ext_assert_string_equal(
   __expected__,
   __actual__
) ext.tests.customAssertion((str(__actual__) = str(__expected__)), __FILE__, __LINE__, ("expected {" #__expected__ "} but was {" #__actual__ "}"))
#define ext_assert_string_equal_error(
   __expected__,
   __actual__
) if (ext.tests.customAssertion((str(__actual__) = str(__expected__)), __FILE__, __LINE__, ("expected {" #__expected__ "} but was {" #__actual__ "}"), true) = false) then exit sub
#define ext_assert_string_not_equal(
   __expected__,
   __actual__
) ext.tests.customAssertion((str(__actual__) <> str(__expected__)), __FILE__, __LINE__, ("{" #__actual__ "} expected to be != to {" #__expected__ "}"))
#define ext_assert_string_not_equal_error(
   __expected__,
   __actual__
) if (ext.tests.customAssertion((str(__actual__) <> str(__expected__)), __FILE__, __LINE__, ("{" #__actual__ "} expected to be != to {" #__expected__ "}"), true) = false) then exit sub
#define ext_assert_pass(
   __message__
) ext.tests.customAssertion((true), __FILE__, __LINE__, __message__)
#define ext_assert_fail(
   __message__
) ext.tests.customAssertion((false), __FILE__, __LINE__, __message__)
#define ext_assert_error(
   __message__
) if (ext.tests.customAssertion((false), __FILE__, __LINE__, __message__, true) = false) then exit sub
Used with addSuiteHook to run a procedure on certain events.