Path: blob/master/tests/core/object/test_method_bind.h
10278 views
/**************************************************************************/1/* test_method_bind.h */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#pragma once3132#include "core/object/class_db.h"3334#include "tests/test_macros.h"3536namespace TestMethodBind {3738class MethodBindTester : public Object {39GDCLASS(MethodBindTester, Object);4041public:42enum Test {43TEST_METHOD,44TEST_METHOD_ARGS,45TEST_METHODC,46TEST_METHODC_ARGS,47TEST_METHODR,48TEST_METHODR_ARGS,49TEST_METHODRC,50TEST_METHODRC_ARGS,51TEST_METHOD_DEFARGS,52TEST_METHOD_OBJECT_CAST,53TEST_MAX54};5556class ObjectSubclass : public Object {57GDSOFTCLASS(ObjectSubclass, Object);5859public:60int value = 1;61};6263int test_num = 0;6465bool test_valid[TEST_MAX];6667void test_method() {68test_valid[TEST_METHOD] = true;69}7071void test_method_args(int p_arg) {72test_valid[TEST_METHOD_ARGS] = p_arg == test_num;73}7475void test_methodc() {76test_valid[TEST_METHODC] = true;77}7879void test_methodc_args(int p_arg) {80test_valid[TEST_METHODC_ARGS] = p_arg == test_num;81}8283int test_methodr() {84test_valid[TEST_METHODR] = true; //temporary85return test_num;86}8788int test_methodr_args(int p_arg) {89test_valid[TEST_METHODR_ARGS] = true; //temporary90return p_arg;91}9293int test_methodrc() {94test_valid[TEST_METHODRC] = true; //temporary95return test_num;96}9798int test_methodrc_args(int p_arg) {99test_valid[TEST_METHODRC_ARGS] = true; //temporary100return p_arg;101}102103void test_method_default_args(int p_arg1, int p_arg2, int p_arg3, int p_arg4, int p_arg5) {104test_valid[TEST_METHOD_DEFARGS] = p_arg1 == 1 && p_arg2 == 2 && p_arg3 == 3 && p_arg4 == 4 && p_arg5 == 5; //temporary105}106107void test_method_object_cast(ObjectSubclass *p_object) {108test_valid[TEST_METHOD_OBJECT_CAST] = p_object->value == 1;109}110111static void _bind_methods() {112ClassDB::bind_method(D_METHOD("test_method"), &MethodBindTester::test_method);113ClassDB::bind_method(D_METHOD("test_method_args"), &MethodBindTester::test_method_args);114ClassDB::bind_method(D_METHOD("test_methodc"), &MethodBindTester::test_methodc);115ClassDB::bind_method(D_METHOD("test_methodc_args"), &MethodBindTester::test_methodc_args);116ClassDB::bind_method(D_METHOD("test_methodr"), &MethodBindTester::test_methodr);117ClassDB::bind_method(D_METHOD("test_methodr_args"), &MethodBindTester::test_methodr_args);118ClassDB::bind_method(D_METHOD("test_methodrc"), &MethodBindTester::test_methodrc);119ClassDB::bind_method(D_METHOD("test_methodrc_args"), &MethodBindTester::test_methodrc_args);120ClassDB::bind_method(D_METHOD("test_method_default_args"), &MethodBindTester::test_method_default_args, DEFVAL(9) /* wrong on purpose */, DEFVAL(4), DEFVAL(5));121ClassDB::bind_method(D_METHOD("test_method_object_cast", "object"), &MethodBindTester::test_method_object_cast);122}123124virtual void run_tests() {125for (int i = 0; i < TEST_MAX; i++) {126test_valid[i] = false;127}128//regular129test_num = Math::rand();130call("test_method");131test_num = Math::rand();132call("test_method_args", test_num);133test_num = Math::rand();134call("test_methodc");135test_num = Math::rand();136call("test_methodc_args", test_num);137//return138test_num = Math::rand();139test_valid[TEST_METHODR] = int(call("test_methodr")) == test_num && test_valid[TEST_METHODR];140test_num = Math::rand();141test_valid[TEST_METHODR_ARGS] = int(call("test_methodr_args", test_num)) == test_num && test_valid[TEST_METHODR_ARGS];142test_num = Math::rand();143test_valid[TEST_METHODRC] = int(call("test_methodrc")) == test_num && test_valid[TEST_METHODRC];144test_num = Math::rand();145test_valid[TEST_METHODRC_ARGS] = int(call("test_methodrc_args", test_num)) == test_num && test_valid[TEST_METHODRC_ARGS];146147call("test_method_default_args", 1, 2, 3, 4);148149ObjectSubclass *obj = memnew(ObjectSubclass);150call("test_method_object_cast", obj);151memdelete(obj);152}153};154155TEST_CASE("[MethodBind] check all method binds") {156MethodBindTester *mbt = memnew(MethodBindTester);157158mbt->run_tests();159160CHECK(mbt->test_valid[MethodBindTester::TEST_METHOD]);161CHECK(mbt->test_valid[MethodBindTester::TEST_METHOD_ARGS]);162CHECK(mbt->test_valid[MethodBindTester::TEST_METHODC]);163CHECK(mbt->test_valid[MethodBindTester::TEST_METHODC_ARGS]);164CHECK(mbt->test_valid[MethodBindTester::TEST_METHODR]);165CHECK(mbt->test_valid[MethodBindTester::TEST_METHODR_ARGS]);166CHECK(mbt->test_valid[MethodBindTester::TEST_METHODRC]);167CHECK(mbt->test_valid[MethodBindTester::TEST_METHODRC_ARGS]);168CHECK(mbt->test_valid[MethodBindTester::TEST_METHOD_DEFARGS]);169CHECK(mbt->test_valid[MethodBindTester::TEST_METHOD_OBJECT_CAST]);170171memdelete(mbt);172}173} // namespace TestMethodBind174175176