1 /* Funky: a light-weight embeddable programming language
2 * Copyright (c) 2007, Ronald Landheer-Cieslak
5 * This is free software. You may distribute it and/or modify it and
6 * distribute modified forms provided that the following terms are met:
8 * * Redistributions of the source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer;
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the distribution;
13 * * None of the names of the authors of this software may be used to endorse
14 * or promote this software, derived software or any distribution of this
15 * software or any distribution of which this software is part, without
16 * prior written permission from the authors involved;
17 * * Unless you have received a written statement from Ronald Landheer-Cieslak
18 * that says otherwise, the terms of the GNU General Public License, as
19 * published by the Free Software Foundation, version 2 or (at your option)
20 * any later version, also apply.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
35 #include "Stubs/Functor.h"
36 #include <Funky/Exceptions/ParseError.h>
38 CPPUNIT_TEST_SUITE_REGISTRATION( GroovyTest
);
40 void GroovyTest::setUp()
47 //! Clean up after the test run.
48 void GroovyTest::tearDown()
52 void GroovyTest::tryScript01(){CPPUNIT_ASSERT(groovy_
.eval("(test, 1)"));}
53 void GroovyTest::tryScript02(){CPPUNIT_ASSERT(groovy_
.eval("(add, 1, (neg, 1))") == 0);}
54 void GroovyTest::tryScript03(){CPPUNIT_ASSERT(groovy_
.eval("(add, 1, (neg, @0))", std::vector
< int >(parms_
, parms_
+ 1)) == 1);}
55 void GroovyTest::tryScript04(){CPPUNIT_ASSERT(groovy_
.eval("(add, (neg, (add, 6, 6)), 12)") == 0);}
56 void GroovyTest::tryScript05(){CPPUNIT_ASSERT(groovy_
.eval("(add, (add, 6, 6), -12)") == 0);}
57 void GroovyTest::tryScript06(){CPPUNIT_ASSERT(groovy_
.eval("(!sub-2, (add, @0, (neg, @1)))(sub, 1, 1)") == 0);}
58 void GroovyTest::tryScript07(){CPPUNIT_ASSERT(groovy_
.eval("(!sub, (add, @0, (neg, @1)))(sub, 1, 1)") == 0);}
59 void GroovyTest::tryScript08(){CPPUNIT_ASSERT(groovy_
.eval("(if, 0, 1, 2)") == 2);}
60 void GroovyTest::tryScript09(){CPPUNIT_ASSERT(groovy_
.eval("(if, 1, 1, 2)") == 1);}
61 void GroovyTest::tryScript10(){CPPUNIT_ASSERT(groovy_
.eval("(add, (shift), (add, (shift), (shift)))", std::vector
< int >(parms_
, parms_
+ 3)) == 3);}
62 void GroovyTest::tryScript11(){CPPUNIT_ASSERT(groovy_
.eval("(shift)", std::vector
< int >(parms_
, parms_
+ 3)) == 0);}
63 void GroovyTest::tryScript12(){CPPUNIT_ASSERT(groovy_
.eval("(defined, @3)", std::vector
< int >(parms_
, parms_
+ 3)) == 0);}
64 void GroovyTest::tryScript13(){CPPUNIT_ASSERT(groovy_
.eval("(defined, @2)", std::vector
< int >(parms_
, parms_
+ 3)) == 1);}
65 void GroovyTest::tryScript14(){CPPUNIT_ASSERT(groovy_
.eval("(defined, @1)", std::vector
< int >(parms_
, parms_
+ 3)) == 1);}
66 void GroovyTest::tryScript15(){CPPUNIT_ASSERT(groovy_
.eval("(defined, @0)", std::vector
< int >(parms_
, parms_
+ 3)) == 1);}
67 void GroovyTest::tryScript16(){CPPUNIT_ASSERT(groovy_
.eval("(!sub, (add, @0, (if, (defined, @1), @1, 0)))(sub, 1)") == 1);}
68 void GroovyTest::tryScript17(){CPPUNIT_ASSERT(groovy_
.eval("(shift)", std::vector
< int >(parms_
+ 1, parms_
+ 3)) == 1);}
69 void GroovyTest::tryScript18(){CPPUNIT_ASSERT(groovy_
.eval("(add, @@)", std::vector
< int >(parms_
+ 1, parms_
+ 3)) == 3);}
70 void GroovyTest::tryScript19()
72 CPPUNIT_ASSERT(groovy_
.eval(
74 " (if, (defined, @0),"
77 " (if, (defined, @0), (sum, @@), 0)),"
82 std::vector
< int >(parms_
, parms_
+ 3)) == 3);
85 void GroovyTest::tryScript20()
87 groovy_
.installFunction("S", Stubs::Functor
< std::vector
< int > >(), 2, 2);
88 CPPUNIT_ASSERT(groovy_
.eval("(add, (S, @@))", std::vector
< int >(parms_
+ 1, parms_
+ 3)) == 3);
91 void GroovyTest::tryParseError01()
93 CPPUNIT_ASSERT_THROW(groovy_
.eval("("), Funky::Exceptions::ParseError
);
96 void GroovyTest::tryParseError02()
103 catch (const Funky::Exceptions::ParseError
& e
)
106 CPPUNIT_ASSERT(e
.reason_
== Funky::Exceptions::ParseError::function_name_expected__
);
108 CPPUNIT_ASSERT(caught
);
111 void GroovyTest::tryParseError03()
116 groovy_
.eval("(!,0)");
118 catch (const Funky::Exceptions::ParseError
& e
)
121 CPPUNIT_ASSERT(e
.reason_
== Funky::Exceptions::ParseError::function_name_expected__
);
123 CPPUNIT_ASSERT(caught
);
126 void GroovyTest::tryParseError04()
131 groovy_
.eval("!,0)");
133 catch (const Funky::Exceptions::ParseError
& e
)
136 CPPUNIT_ASSERT(e
.reason_
== Funky::Exceptions::ParseError::left_parenthesis_expected__
);
138 CPPUNIT_ASSERT(caught
);
141 void GroovyTest::tryParseError05()
146 groovy_
.eval("(foo,)");
148 catch (const Funky::Exceptions::ParseError
& e
)
151 CPPUNIT_ASSERT(e
.reason_
== Funky::Exceptions::ParseError::literal_parameter_or_statement_expected__
);
153 CPPUNIT_ASSERT(caught
);
156 void GroovyTest::tryParseError06()
161 groovy_
.eval("(foo,0");
163 catch (const Funky::Exceptions::ParseError
& e
)
166 CPPUNIT_ASSERT(e
.reason_
== Funky::Exceptions::ParseError::right_parenthesis_expected__
);
168 CPPUNIT_ASSERT(caught
);
171 void GroovyTest::tryParseError07()
176 groovy_
.eval("(!sub-2, (add, @0, (neg, @1)))");
178 catch (const Funky::Exceptions::ParseError
& e
)
181 CPPUNIT_ASSERT(e
.reason_
== Funky::Exceptions::ParseError::left_parenthesis_expected__
);
183 CPPUNIT_ASSERT(caught
);
186 void GroovyTest::tryParseError08()
191 groovy_
.eval("(test, 1.0)");
193 catch (const Funky::Exceptions::ParseError
& e
)
196 CPPUNIT_ASSERT(e
.reason_
== Funky::Exceptions::ParseError::right_parenthesis_expected__
);
198 CPPUNIT_ASSERT(caught
);