Improve Class test coverage
Project: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/repo Commit: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/commit/201441cd Tree: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/tree/201441cd Diff: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/diff/201441cd Branch: refs/heads/master Commit: 201441cde54308fcbe86e443f24c823a8c8f3228 Parents: 5646ae7 Author: Nick Wellnhofer <[email protected]> Authored: Wed May 11 12:31:00 2016 +0200 Committer: Nick Wellnhofer <[email protected]> Committed: Sun May 15 17:57:34 2016 +0200 ---------------------------------------------------------------------- runtime/core/Clownfish/Test/TestClass.c | 67 +++++++++++++++++++++++++++- 1 file changed, 66 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/201441cd/runtime/core/Clownfish/Test/TestClass.c ---------------------------------------------------------------------- diff --git a/runtime/core/Clownfish/Test/TestClass.c b/runtime/core/Clownfish/Test/TestClass.c index fc16a90..a6646f8 100644 --- a/runtime/core/Clownfish/Test/TestClass.c +++ b/runtime/core/Clownfish/Test/TestClass.c @@ -29,8 +29,11 @@ #include "Clownfish/Boolean.h" #include "Clownfish/Class.h" +#include "Clownfish/Method.h" +#include "Clownfish/String.h" #include "Clownfish/TestHarness/TestBatchRunner.h" #include "Clownfish/Util/Memory.h" +#include "Clownfish/Vector.h" TestClass* TestClass_new() { @@ -85,9 +88,71 @@ test_bootstrap_idempotence(TestBatchRunner *runner) { FREEMEM(bool_class_contents); } +static String* +MyObj_To_String_IMP(Obj *self) { + UNUSED_VAR(self); + return Str_newf("delta"); +} + +static void +test_simple_subclass(TestBatchRunner *runner) { + String *class_name = SSTR_WRAP_C("Clownfish::Test::MyObj"); + Class *subclass = Class_singleton(class_name, OBJ); + + TEST_TRUE(runner, Str_Equals(Class_Get_Name(subclass), (Obj*)class_name), + "Get_Name"); + TEST_TRUE(runner, Class_Get_Parent(subclass) == OBJ, "Get_Parent"); + + Obj *obj = Class_Make_Obj(subclass); + TEST_TRUE(runner, Obj_is_a(obj, subclass), "Make_Obj"); + + Class_Override(subclass, (cfish_method_t)MyObj_To_String_IMP, + CFISH_Obj_To_String_OFFSET); + String *str = Obj_To_String(obj); + TEST_TRUE(runner, Str_Equals_Utf8(str, "delta", 5), "Override"); + DECREF(str); + + DECREF(obj); +} + +static void +test_add_alias_to_registry(TestBatchRunner *runner) { + static const char alias[] = "Clownfish::Test::ObjAlias"; + bool added; + + added = Class_add_alias_to_registry(OBJ, alias, sizeof(alias) - 1); + TEST_TRUE(runner, added, "add_alias_to_registry returns true"); + Class *klass = Class_fetch_class(SSTR_WRAP_C(alias)); + TEST_TRUE(runner, klass == OBJ, "add_alias_to_registry works"); + + added = Class_add_alias_to_registry(CLASS, alias, sizeof(alias) - 1); + TEST_FALSE(runner, added, "add_alias_to_registry returns false"); +} + +static void +test_Get_Methods(TestBatchRunner *runner) { + Vector *methods = Class_Get_Methods(OBJ); + Method *destroy = NULL; + + for (size_t i = 0, size = Vec_Get_Size(methods); i < size; i++) { + Method *method = (Method*)Vec_Fetch(methods, i); + + if (Str_Equals_Utf8(Method_Get_Name(method), "Destroy", 7)) { + destroy = method; + } + } + + TEST_TRUE(runner, destroy != NULL, "Destroy method found"); + + DECREF(methods); +} + void TestClass_Run_IMP(TestClass *self, TestBatchRunner *runner) { - TestBatchRunner_Plan(runner, (TestBatch*)self, 4); + TestBatchRunner_Plan(runner, (TestBatch*)self, 12); test_bootstrap_idempotence(runner); + test_simple_subclass(runner); + test_add_alias_to_registry(runner); + test_Get_Methods(runner); }
