Move to a more standardized unit testing design which relies less on OS support and can soon be moved to a ctest implementation

This commit is contained in:
Jason Turner
2010-03-14 05:19:24 +00:00
parent 5c98a5d6e7
commit 567f911093
229 changed files with 336 additions and 416 deletions

View File

@@ -252,8 +252,8 @@ namespace chaiscript
push_back_name = "push_back";
}
typedef void (ContainerType::*pushback)(const typename ContainerType::value_type &);
m->add(fun(static_cast<pushback>(&ContainerType::push_back)), push_back_name);
typedef void (ContainerType::*pushback)(const typename ContainerType::value_type &);
m->add(fun(static_cast<pushback>(&ContainerType::push_back)), push_back_name);
m->add(fun(&ContainerType::pop_back), "pop_back");
return m;
}
@@ -267,7 +267,6 @@ namespace chaiscript
ModulePtr front_insertion_sequence_type(const std::string &type, ModulePtr m = ModulePtr(new Module()))
{
typedef typename ContainerType::reference (ContainerType::*frontptr)();
m->add(fun(static_cast<frontptr>(&ContainerType::front)), "front");
std::string push_front_name;
@@ -334,7 +333,7 @@ namespace chaiscript
{
m->add(user_type<MapType>(), type);
typedef typename MapType::mapped_type &(MapType::*elemaccess)(const typename MapType::key_type &);
typedef typename MapType::mapped_type &(MapType::*elemaccess)(const typename MapType::key_type &);
m->add(fun(static_cast<elemaccess>(&MapType::operator[])), "[]");
container_type<MapType>(type, m);
@@ -375,6 +374,10 @@ namespace chaiscript
{
m->add(user_type<VectorType>(), type);
typedef typename VectorType::reference (VectorType::*frontptr)();
m->add(fun(static_cast<frontptr>(&VectorType::front)), "front");
back_insertion_sequence_type<VectorType>(type, m);
sequence_type<VectorType>(type, m);
random_access_container_type<VectorType>(type, m);
@@ -383,6 +386,27 @@ namespace chaiscript
assignable_type<VectorType>(type, m);
input_range_type<VectorType>(type, m);
m->eval("def Vector::`==`(rhs) : type_match(rhs, this) { \
if ( rhs.size() != this.size() ) { \
return false; \
} else { \
var r1 = range(this); \
var r2 = range(rhs); \
while (!r1.empty()) \
{ \
if (!eq(r1.front(), r2.front())) \
{ \
return false; \
} \
r1.pop_front(); \
r2.pop_front(); \
} \
return true; \
} \
}");
return m;
}

View File

@@ -8,15 +8,13 @@ echo -n "Running unit tests"
for file in unittests/*.chai
do
tstname=${file%.*}
tst="$tstname.txt"
LD_LIBRARY_PATH=. ./chaiscript_eval $file > /tmp/tstout.txt
diff $tst /tmp/tstout.txt
OUTPUT=`LD_LIBRARY_PATH=. ./chaiscript_eval unittests/unit_test.inc $file`
if [ "$?" -eq "0" ]
then
echo -n "."
successes=$((successes+1))
else
echo "[from failed test $file]"
echo "[from failed test $file] $OUTPUT"
failures=$((failures+1))
fi
done

View File

@@ -15,6 +15,7 @@
#include <chaiscript/chaiscript.hpp>
void print_help() {
std::cout << "ChaiScript evaluator. To evaluate an expression, type it and press <enter>." << std::endl;
std::cout << "Additionally, you can inspect the runtime system using:" << std::endl;
@@ -22,6 +23,19 @@ void print_help() {
std::cout << " dump_object(x) - dumps information about the given symbol" << std::endl;
}
bool throws_exception(const chaiscript::Proxy_Function &f)
{
try {
chaiscript::functor<void ()>(f)();
} catch (...) {
return true;
}
return false;
}
std::string get_next_command() {
#ifdef READLINE_AVAILABLE
char *input_raw;
@@ -40,10 +54,10 @@ int main(int argc, char *argv[]) {
std::string input;
chaiscript::ChaiScript chai;
chai.add(chaiscript::fun(&exit), "exit");
chai.add(chaiscript::fun(&throws_exception), "throws_exception");
if (argc < 2) {
//std::cout << "eval> ";
//std::getline(std::cin, input);
#ifdef READLINE_AVAILABLE
using_history();
#endif
@@ -80,14 +94,16 @@ int main(int argc, char *argv[]) {
}
else {
for (int i = 1; i < argc; ++i) {
std::string filename(argv[i]);
try {
chaiscript::Boxed_Value val = chai.eval_file(argv[i]);
}
catch (std::exception &e) {
std::cout << e.what() << std::endl;
return EXIT_FAILURE;
}
}
}
return EXIT_SUCCESS;
}

View File

@@ -1,2 +1,2 @@
var prod = bind(foldl, _, `*`, 1.0)
print(prod([3, 4, 5]))
assert_equal(60, prod([3, 4, 5]))

View File

@@ -1 +0,0 @@
60

View File

@@ -1 +0,0 @@
hello

View File

@@ -1 +1 @@
print(!true)
assert_equal(false, !true)

View File

@@ -1 +0,0 @@
false

View File

@@ -4,4 +4,4 @@ while (i < 10) {
break
}
}
print(i)
assert_equal(5, i);

View File

@@ -1 +0,0 @@
5

View File

@@ -1 +1 @@
print('b')
assert_equal("b", to_string('b'))

View File

@@ -1 +0,0 @@
b

View File

@@ -1,7 +1,7 @@
print(1.is_var_const());
print(1.is_var_reference());
print(1.is_var_pointer());
print(1.is_var_null());
print(1.is_var_undef());
assert_equal(true, 1.is_var_const());
assert_equal(false, 1.is_var_reference());
assert_equal(true, 1.is_var_pointer());
assert_equal(false, 1.is_var_null());
assert_equal(false, 1.is_var_undef());
var i;
print(i.is_var_undef());
assert_equal(true, i.is_var_undef());

View File

@@ -1,6 +0,0 @@
true
false
true
false
false
true

View File

@@ -1 +1,3 @@
print(collate(1, 2))
var v = collate(1, 2)
assert_equal(1, v[0])
assert_equal(2, v[1])

View File

@@ -1 +0,0 @@
[1, 2]

View File

@@ -1 +1 @@
print(1 > 2)
assert_equal(false, 1 > 2);

View File

@@ -1 +0,0 @@
false

View File

@@ -1 +1 @@
print(1 < 2)
assert_equal(true, 1 < 2)

View File

@@ -1 +0,0 @@
true

View File

@@ -1 +1,5 @@
print(concat([1, 2], [3, 4]))
var v = concat([1, 2], [3, 4]);
assert_equal(4, v.size());
assert_equal(1, v[0]);
assert_equal(4, v[3]);

View File

@@ -1 +0,0 @@
[1, 2, 3, 4]

View File

@@ -1 +1 @@
print(3.5.to_string() + "bob")
assert_equal("3.5bob", 3.5.to_string() + "bob");

View File

@@ -1 +0,0 @@
3.5bob

View File

@@ -1 +1 @@
print(3.to_string + "bob")
assert_equal("3bob", 3.to_string + "bob")

View File

@@ -1 +0,0 @@
3bob

View File

@@ -1 +1 @@
print("3.5".to_double() + 3.3)
assert_equal(6.8, "3.5".to_double() + 3.3)

View File

@@ -1 +0,0 @@
6.7999999999999998

View File

@@ -1 +1 @@
print("4".to_int() + 4)
assert_equal(8, "4".to_int() + 4)

View File

@@ -1 +0,0 @@
8

View File

@@ -1 +1 @@
print(drop([1, 2, 3, 4], 2))
assert_equal([3,4], drop([1, 2, 3, 4], 2))

View File

@@ -1 +0,0 @@
[3, 4]

View File

@@ -1 +1 @@
print(drop_while([1, 2, 3], odd))
assert_equal([2, 3], drop_while([1, 2, 3], odd))

View File

@@ -1 +0,0 @@
[2, 3]

View File

@@ -1,4 +1,4 @@
var x=.5
print(x)
assert_equal(.5, x)
var y=-.5
print(y)
assert_equal(-.5, y)

View File

@@ -1,2 +0,0 @@
0.5
-0.5

View File

@@ -1 +1 @@
print(eval("3 + 4"))
assert_equal(7, eval("3 + 4"))

View File

@@ -1 +0,0 @@
7

View File

@@ -1 +1 @@
print(even(4))
assert_equal(true, even(4))

View File

@@ -1 +0,0 @@
true

View File

@@ -6,4 +6,4 @@ try {
catch(e) {
x = e + 3
}
print(x)
assert_equal(4, x);

View File

@@ -1 +0,0 @@
4

View File

@@ -1,19 +1,32 @@
var finallyone = false;
try {
throw(3)
}
catch(x) {
print(x)
assert_equal(3, x)
}
finally {
print("Finally #1")
finallyone = true;
}
assert_equal(true, finallyone);
var try2 = false;
var catch2 = false;
var finally2 = false;
try {
print("Safe")
try2 = true;
}
catch {
print("Caught")
catch2 = true;
}
finally {
print("Finally #2")
finally2 = true;
}
assert_equal(true, try2);
assert_equal(false, catch2);
assert_equal(true, finally2);

View File

@@ -1,4 +0,0 @@
3
Finally #1
Safe
Finally #2

View File

@@ -1,18 +1,21 @@
var results = [];
for (var i = 2; i < 6; ++i) {
try {
throw(i)
}
catch(e) : e < 2 {
print("Catch 1: " + e.to_string())
results.push_back("c1: " + e.to_string());
}
catch(e) : e < 4 {
print("Catch 2: " + e.to_string())
results.push_back("c2: " + e.to_string());
}
catch(e) {
print("Catch 3: " + e.to_string())
results.push_back("c3: " + e.to_string());
}
catch {
print("This is never called")
// Should never get called
assert_equal(false, true)
}
}
@@ -21,8 +24,11 @@ try {
}
catch(e) : e < 3
{
print("Caught less than 3")
// Should never get called
assert_equal(false, true);
}
catch {
print("Backup catch")
results.push_back("defaultcatch");
}
assert_equal(["c2: 2", "c2: 3", "c3: 4", "c3: 5", "defaultcatch"], results);

View File

@@ -1,5 +0,0 @@
Catch 2: 2
Catch 2: 3
Catch 3: 4
Catch 3: 5
Backup catch

View File

@@ -1 +1 @@
print(filter([1, 2, 3, 4], odd))
assert_equal([1,3], filter([1, 2, 3, 4], odd))

View File

@@ -1 +0,0 @@
[1, 3]

View File

@@ -1,2 +1,7 @@
print(1.2)
print(.5)
assert_equal(true, 1.2 < 2)
assert_equal(true, 1.2 > 1)
assert_equal(1.2, 1.2)
assert_equal(true, .5 > 0)
assert_equal(true, .5 < 1)
assert_equal(0.5, .5)

View File

@@ -1,2 +0,0 @@
1.2
0.5

View File

@@ -1 +1 @@
print(foldl([1, 2, 3, 4], `+`, 0))
assert_equal(10, foldl([1, 2, 3, 4], `+`, 0))

View File

@@ -1 +0,0 @@
10

View File

@@ -1,3 +1,7 @@
var ret = []
for (var i = 0; i < 5; ++i) {
print(i)
ret.push_back(i);
}
assert_equal([0,1,2,3,4], ret);

View File

@@ -1,5 +0,0 @@
0
1
2
3
4

View File

@@ -1,3 +0,0 @@
1
2
3

View File

@@ -1,3 +1,3 @@
var v = [1,2,3];
var r = range(v);
for_each(r, print)
for_each(r, fun(x) { assert_equal(true, x>0); } )

View File

@@ -1,3 +0,0 @@
1
2
3

View File

@@ -1,3 +1,4 @@
// Don't bother checking the output from this one, just makes sure it executes
var v = [1,2,3];
var r = retro(range(v));
for_each(r, print)

View File

@@ -1,3 +0,0 @@
3
2
1

View File

@@ -1,3 +1,3 @@
var x = `+`
x = `-`
print(x(5,4))
assert_equal(1, x(5,4))

View File

@@ -1 +0,0 @@
1

View File

@@ -1 +1 @@
print(generate_range(1, 10))
assert_equal([1,2,3,4,5,6,7,8,9,10], generate_range(1, 10))

View File

@@ -1 +0,0 @@
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

View File

@@ -1,4 +1,7 @@
var t = false;
if (true) {
print("true")
t = true;
}
print("done")
assert_equal(true, t);

View File

@@ -1,2 +0,0 @@
true
done

View File

@@ -1,8 +1,13 @@
var i = 3
var b1 = false;
var b2 = false;
if (i == 2) {
print("2")
b1 = true;
}
else {
print("other")
b2 = true;
}
print("done")
assert_equal(false, b1);
assert_equal(true, b2);

View File

@@ -1,2 +0,0 @@
other
done

View File

@@ -1,12 +1,18 @@
var b1 = false;
var b2 = false;
var b3 = false;
var i = 3
if (i == 2) {
print("2")
b1 = true;
}
else if (i == 4) {
print("4")
b2 = true;
}
else if (i == 3) {
print("3")
b3 = true;
}
print("done")
assert_equal(false, b1);
assert_equal(false, b2);
assert_equal(true, b3);

View File

@@ -1,2 +0,0 @@
3
done

View File

@@ -1,12 +1,14 @@
var i = 3
var b = false
if (i == 2) {
print("2")
assert_equal(false, true)
}
else if (i == 4) {
print("4")
assert_equal(false, true)
}
else {
print("3")
assert_equal(true, true)
b = true
}
print("done")
assert_equal(true, b)

View File

@@ -1,2 +0,0 @@
3
done

View File

@@ -1,3 +1,3 @@
var bob = 5.5
print("${bob}")
print("val: ${5.5 + 2.5} and ${bob + 2.5}")
assert_equal("5.5", "${bob}")
assert_equal("val: 8 and 8", "val: ${5.5 + 2.5} and ${bob + 2.5}")

View File

@@ -1,2 +0,0 @@
5.5
val: 8 and 8

View File

@@ -1,4 +1,4 @@
print("$ {4 + 5}")
print("$${4+5}")
print("Value: \${4 + 5}")
print("Value: \$${4 + 5}")
assert_equal("\$ {4 + 5}", "$ {4 + 5}")
assert_equal("\$9", "$${4+5}")
assert_equal("Value: \${4 + 5}", "Value: \${4 + 5}")
assert_equal("Value: \$9", "Value: \$${4 + 5}")

View File

@@ -1,4 +0,0 @@
$ {4 + 5}
$9
Value: ${4 + 5}
Value: $9

View File

@@ -1 +1 @@
clone = `-`
assert_throws("Illegal const function assignment", fun() { clone = `-` } );

View File

@@ -1 +0,0 @@
Error: "Mismatched types in equation, lhs is const." in 'unittests/invalid_function_assignment.chai' at (1, 7)

View File

@@ -1,2 +1 @@
var x = 5
x = `-`
assert_throws("Invalid function reassignment", fun() { var x = 5; x = `-`; } );

View File

@@ -1 +0,0 @@
Error: "Mismatched types in equation." in 'unittests/invalid_function_reassignment.chai' at (2, 3)

View File

@@ -1,4 +1,4 @@
var i;
print(i.is_var_undef());
assert_equal(true, i.is_var_undef());
i = 5;
print(i.is_var_undef());
assert_equal(false, i.is_var_undef());

View File

@@ -1,2 +0,0 @@
true
false

View File

@@ -1 +1 @@
print(join([1, 2, 3], "*"))
assert_equal("1*2*3", join([1, 2, 3], "*"))

View File

@@ -1 +0,0 @@
1*2*3

View File

@@ -1,2 +1,2 @@
var bob = fun(x) { x + 1 }
print(bob(3))
assert_equal(4, bob(3));

View File

@@ -1 +0,0 @@
4

View File

@@ -3,4 +3,6 @@ load_module("stl_extra")
var x = List()
x.push_back(3)
x.push_back("A")
print(x)
assert_equal(3, x.front());
assert_equal("A", x.back());

View File

@@ -1 +0,0 @@
[3, A]

View File

@@ -3,4 +3,6 @@ load_module("stl_extra")
var x = List()
x.push_front(3)
x.push_front("A")
print(x)
assert_equal("A", x.front());
assert_equal(3, x.back());

View File

@@ -1 +0,0 @@
[A, 3]

View File

@@ -1,2 +1,2 @@
load_module("test")
print(hello_world())
assert_equal("Hello World", hello_world());

View File

@@ -1 +0,0 @@
Hello World

View File

@@ -6,5 +6,4 @@ for (var i = 0; i < 10; ++i) {
}
}
print(total)
assert_equal(100, total);

View File

@@ -1 +0,0 @@
100

View File

@@ -1 +1 @@
print(map([1, 2, 3], odd))
assert_equal([true, false, true], map([1,2,3], odd))

View File

@@ -1 +0,0 @@
[true, false, true]

View File

@@ -1,2 +1,2 @@
var x = ["bob":2, "fred":3]
print(x["fred"])
assert_equal(3, x["fred"])

View File

@@ -1 +0,0 @@
3

View File

@@ -1,2 +1,3 @@
var x = ["bob":1, "fred":2]
print(x)
assert_equal(2, x.size());

View File

@@ -1 +0,0 @@
[<bob, 1>, <fred, 2>]

View File

@@ -1 +1 @@
print(1 + 2)
assert_equal(3, (1 + 2))

Some files were not shown because too many files have changed in this diff Show More