test/test_Module.cpp in rice-1.1.0 vs test/test_Module.cpp in rice-1.2.0

- old
+ new

@@ -1,9 +1,11 @@ #include "unittest.hpp" #include "rice/Module.hpp" #include "rice/Exception.hpp" #include "rice/Array.hpp" +#include "rice/Arg.hpp" +#include "rice/global_function.hpp" using namespace Rice; TESTSUITE(Module); @@ -249,5 +251,178 @@ { Module m(anonymous_module()); ASSERT_EQUAL(String(""), m.name()); } +/** + * Tests for default arguments + */ +namespace +{ + int defaults_method_one_arg1; + int defaults_method_one_arg2; + bool defaults_method_one_arg3 = false; + + void defaults_method_one(int arg1, int arg2 = 3, bool arg3 = true) + { + defaults_method_one_arg1 = arg1; + defaults_method_one_arg2 = arg2; + defaults_method_one_arg3 = arg3; + } +} + +TESTCASE(define_method_default_arguments) +{ + Module m(anonymous_module()); + m.define_method("foo", &defaults_method_one, (Arg("arg1"), Arg("arg2") = 3, Arg("arg3") = true)); + + Object o = m.instance_eval("o = Object.new; o.extend(self); o"); + o.call("foo", 2); + + ASSERT_EQUAL(2, defaults_method_one_arg1); + ASSERT_EQUAL(3, defaults_method_one_arg2); + ASSERT(defaults_method_one_arg3); + + o.call("foo", 11, 10); + + ASSERT_EQUAL(11, defaults_method_one_arg1); + ASSERT_EQUAL(10, defaults_method_one_arg2); + ASSERT(defaults_method_one_arg3); + + o.call("foo", 22, 33, false); + + ASSERT_EQUAL(22, defaults_method_one_arg1); + ASSERT_EQUAL(33, defaults_method_one_arg2); + ASSERT(!defaults_method_one_arg3); +} + +TESTCASE(default_arguments_still_throws_argument_error) +{ + Module m(anonymous_module()); + m.define_method("foo", &defaults_method_one, (Arg("arg1"), Arg("arg2") = 3, Arg("arg3") = true)); + + ASSERT_EXCEPTION_CHECK( + Exception, + m.instance_eval("o = Object.new; o.extend(self); o.foo()"), + ASSERT_EQUAL( + Object(rb_eArgError), + Object(CLASS_OF(ex.value())) + ) + ); + + ASSERT_EXCEPTION_CHECK( + Exception, + m.instance_eval("o = Object.new; o.extend(self); o.foo(3, 4, false, 17)"), + ASSERT_EQUAL( + Object(rb_eArgError), + Object(CLASS_OF(ex.value())) + ) + ); +} + +namespace { + int the_one_default_arg = 0; + void method_with_one_default_arg(int num = 4) { + the_one_default_arg = num; + } +} + +TESTCASE(defining_methods_with_single_default_argument) +{ + // define_method + Module m(anonymous_module()); + m.define_method("foo", &method_with_one_default_arg, (Arg("num") = 4)); + m.instance_eval("o = Object.new; o.extend(self); o.foo()"); + ASSERT_EQUAL(4, the_one_default_arg); + + the_one_default_arg = 0; + + // define_singleton_method + Class c(anonymous_class()); + c.define_singleton_method("foo", &method_with_one_default_arg, (Arg("num") = 4)); + c.call("foo"); + ASSERT_EQUAL(4, the_one_default_arg); + + the_one_default_arg = 0; + + // define_module_function + m.define_module_function("foo2", &method_with_one_default_arg, (Arg("num") = 4)); + + m.call("foo2"); + ASSERT_EQUAL(4, the_one_default_arg); +} + +TESTCASE(default_arguments_for_define_singleton_method) +{ + Class c(anonymous_class()); + c.define_singleton_method("foo", &defaults_method_one, (Arg("arg1"), Arg("arg2") = 3, Arg("arg3") = true)); + + c.call("foo", 2); + + ASSERT_EQUAL(2, defaults_method_one_arg1); + ASSERT_EQUAL(3, defaults_method_one_arg2); + ASSERT(defaults_method_one_arg3); + + c.call("foo", 11, 10); + + ASSERT_EQUAL(11, defaults_method_one_arg1); + ASSERT_EQUAL(10, defaults_method_one_arg2); + ASSERT(defaults_method_one_arg3); + + c.call("foo", 22, 33, false); + + ASSERT_EQUAL(22, defaults_method_one_arg1); + ASSERT_EQUAL(33, defaults_method_one_arg2); + ASSERT(!defaults_method_one_arg3); +} + +TESTCASE(default_arguments_for_define_module_function) +{ + Module m(anonymous_module()); + m.define_module_function("foo", &defaults_method_one, (Arg("arg1"), Arg("arg2") = 3, Arg("arg3") = true)); + + m.call("foo", 2); + + ASSERT_EQUAL(2, defaults_method_one_arg1); + ASSERT_EQUAL(3, defaults_method_one_arg2); + ASSERT(defaults_method_one_arg3); + + m.call("foo", 11, 10); + + ASSERT_EQUAL(11, defaults_method_one_arg1); + ASSERT_EQUAL(10, defaults_method_one_arg2); + ASSERT(defaults_method_one_arg3); + + m.call("foo", 22, 33, false); + + ASSERT_EQUAL(22, defaults_method_one_arg1); + ASSERT_EQUAL(33, defaults_method_one_arg2); + ASSERT(!defaults_method_one_arg3); +} + +namespace { + std::string with_defaults_and_references_x; + bool with_defaults_and_references_doIt; + + void with_defaults_and_references(std::string const& x, bool doIt = false) + { + with_defaults_and_references_x = x; + with_defaults_and_references_doIt = doIt; + } +} + +template<> +std::string* from_ruby<std::string*>(Rice::Object x) { + return new std::string(from_ruby<char const*>(x)); +} + +TESTCASE(define_method_works_with_reference_arguments) +{ + Module m(anonymous_module()); + m.define_module_function("foo", &with_defaults_and_references, + (Arg("x"), Arg("doIt") = false)); + + m.call("foo", "test"); + + ASSERT_EQUAL("test", with_defaults_and_references_x); + ASSERT(!with_defaults_and_references_doIt); +}