forked from godotengine/godot
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds a new function Object.call_with_error_test Allows to tell if the call of a function fails and the reasons behind the failure. Should help better implement unit tets and other situations.
- Loading branch information
Showing
13 changed files
with
322 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/**************************************************************************/ | ||
/* call_error_info.cpp */ | ||
/**************************************************************************/ | ||
/* This file is part of: */ | ||
/* GODOT ENGINE */ | ||
/* https://godotengine.org */ | ||
/**************************************************************************/ | ||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ | ||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ | ||
/* */ | ||
/* Permission is hereby granted, free of charge, to any person obtaining */ | ||
/* a copy of this software and associated documentation files (the */ | ||
/* "Software"), to deal in the Software without restriction, including */ | ||
/* without limitation the rights to use, copy, modify, merge, publish, */ | ||
/* distribute, sublicense, and/or sell copies of the Software, and to */ | ||
/* permit persons to whom the Software is furnished to do so, subject to */ | ||
/* the following conditions: */ | ||
/* */ | ||
/* The above copyright notice and this permission notice shall be */ | ||
/* included in all copies or substantial portions of the Software. */ | ||
/* */ | ||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ | ||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ | ||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ | ||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ | ||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ | ||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ | ||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ | ||
/**************************************************************************/ | ||
|
||
#include "call_error_info.h" | ||
|
||
CallErrorInfo::CallError CallErrorInfo::get_call_error() { | ||
return call_error; | ||
} | ||
|
||
int CallErrorInfo::get_expected_arguments() { | ||
ERR_FAIL_COND_V_MSG(call_error != CALL_ERROR_TOO_MANY_ARGUMENTS && call_error != CALL_ERROR_TOO_FEW_ARGUMENTS, -1, "Error is not about expected argument count"); | ||
return expected; | ||
} | ||
|
||
Variant::Type CallErrorInfo::get_invalid_argument_type() { | ||
ERR_FAIL_COND_V_MSG(call_error != CALL_ERROR_INVALID_ARGUMENT, Variant::NIL, "Error is not about an invalid argument"); | ||
return Variant::Type(expected); | ||
} | ||
int CallErrorInfo::get_invalid_argument_index() { | ||
ERR_FAIL_COND_V_MSG(call_error != CALL_ERROR_INVALID_ARGUMENT, -1, "Error is not about an invalid argument"); | ||
return argument; | ||
} | ||
|
||
void CallErrorInfo::set_call_error(CallError p_error, int p_argument, int p_expected) { | ||
ERR_FAIL_COND_MSG(p_error == CALL_ERROR_INVALID_ARGUMENT && (p_expected < 0 || expected >= Variant::VARIANT_MAX), "Invalid value for expected argument, must be a valid Variant type"); | ||
call_error = p_error; | ||
argument = p_argument; | ||
expected = p_error; | ||
} | ||
|
||
void CallErrorInfo::_bind_methods() { | ||
ClassDB::bind_method(D_METHOD("get_call_error"), &CallErrorInfo::get_call_error); | ||
ClassDB::bind_method(D_METHOD("get_expected_arguments"), &CallErrorInfo::get_expected_arguments); | ||
ClassDB::bind_method(D_METHOD("get_invalid_argument_type"), &CallErrorInfo::get_invalid_argument_type); | ||
ClassDB::bind_method(D_METHOD("get_invalid_argument_index"), &CallErrorInfo::get_invalid_argument_index); | ||
|
||
ClassDB::bind_method(D_METHOD("set_call_error", "error", "argument", "expected"), &CallErrorInfo::set_call_error); | ||
|
||
BIND_ENUM_CONSTANT(CALL_OK); | ||
BIND_ENUM_CONSTANT(CALL_ERROR_INVALID_METHOD); | ||
BIND_ENUM_CONSTANT(CALL_ERROR_INVALID_ARGUMENT); | ||
BIND_ENUM_CONSTANT(CALL_ERROR_TOO_MANY_ARGUMENTS); | ||
BIND_ENUM_CONSTANT(CALL_ERROR_TOO_FEW_ARGUMENTS); | ||
BIND_ENUM_CONSTANT(CALL_ERROR_INSTANCE_IS_NULL); | ||
BIND_ENUM_CONSTANT(CALL_ERROR_METHOD_NOT_CONST); | ||
BIND_ENUM_CONSTANT(CALL_ERROR_SCRIPT_ERROR); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/**************************************************************************/ | ||
/* call_error_info.h */ | ||
/**************************************************************************/ | ||
/* This file is part of: */ | ||
/* GODOT ENGINE */ | ||
/* https://godotengine.org */ | ||
/**************************************************************************/ | ||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ | ||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ | ||
/* */ | ||
/* Permission is hereby granted, free of charge, to any person obtaining */ | ||
/* a copy of this software and associated documentation files (the */ | ||
/* "Software"), to deal in the Software without restriction, including */ | ||
/* without limitation the rights to use, copy, modify, merge, publish, */ | ||
/* distribute, sublicense, and/or sell copies of the Software, and to */ | ||
/* permit persons to whom the Software is furnished to do so, subject to */ | ||
/* the following conditions: */ | ||
/* */ | ||
/* The above copyright notice and this permission notice shall be */ | ||
/* included in all copies or substantial portions of the Software. */ | ||
/* */ | ||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ | ||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ | ||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ | ||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ | ||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ | ||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ | ||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ | ||
/**************************************************************************/ | ||
|
||
#ifndef CALL_ERROR_INFO_H | ||
#define CALL_ERROR_INFO_H | ||
|
||
#include "core/object/ref_counted.h" | ||
#include "core/variant/callable.h" | ||
|
||
class CallErrorInfo : public RefCounted { | ||
GDCLASS(CallErrorInfo, RefCounted) | ||
public: | ||
enum CallError { | ||
CALL_OK, | ||
CALL_ERROR_INVALID_METHOD, | ||
CALL_ERROR_INVALID_ARGUMENT, // expected is variant type | ||
CALL_ERROR_TOO_MANY_ARGUMENTS, // expected is number of arguments | ||
CALL_ERROR_TOO_FEW_ARGUMENTS, // expected is number of arguments | ||
CALL_ERROR_INSTANCE_IS_NULL, | ||
CALL_ERROR_METHOD_NOT_CONST, | ||
CALL_ERROR_SCRIPT_ERROR, | ||
}; | ||
|
||
private: | ||
CallError call_error = CALL_OK; | ||
int argument = 0; | ||
int expected = 0; | ||
|
||
protected: | ||
static void _bind_methods(); | ||
|
||
public: | ||
CallError get_call_error(); | ||
int get_expected_arguments(); | ||
Variant::Type get_invalid_argument_type(); | ||
int get_invalid_argument_index(); | ||
|
||
void set_call_error(CallError p_error, int p_argument, int p_expected); | ||
}; | ||
|
||
VARIANT_ENUM_CAST(CallErrorInfo::CallError); | ||
|
||
#endif // CALL_ERROR_INFO_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<class name="CallErrorInfo" inherits="RefCounted" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../class.xsd"> | ||
<brief_description> | ||
Information pertaining to error reporting on called functions. | ||
</brief_description> | ||
<description> | ||
Information pertaining to error reporting on called functions. This object is used in conjunction with [method Object.call_with_error_test]. | ||
</description> | ||
<tutorials> | ||
</tutorials> | ||
<methods> | ||
<method name="get_call_error"> | ||
<return type="int" enum="CallErrorInfo.CallError" /> | ||
<description> | ||
Return the error type. | ||
</description> | ||
</method> | ||
<method name="get_expected_arguments"> | ||
<return type="int" /> | ||
<description> | ||
Return the amount of arguments expected (when the error is too many or too few arguments). | ||
</description> | ||
</method> | ||
<method name="get_invalid_argument_index"> | ||
<return type="int" /> | ||
<description> | ||
Return the index of the invalid argument (when the error is an invalid argument). | ||
</description> | ||
</method> | ||
<method name="get_invalid_argument_type"> | ||
<return type="int" enum="Variant.Type" /> | ||
<description> | ||
Return the expected type for the invalid argument (when the error is an invalid argument). | ||
</description> | ||
</method> | ||
<method name="set_call_error"> | ||
<return type="void" /> | ||
<param index="0" name="error" type="int" enum="CallErrorInfo.CallError" /> | ||
<param index="1" name="argument" type="int" /> | ||
<param index="2" name="expected" type="int" /> | ||
<description> | ||
Setup the error. For invalid arguments [param argument] is the argument index and [param expected] is the expected variant type. For too few or too many arguments, [param argument] is the expected argument count. | ||
</description> | ||
</method> | ||
</methods> | ||
<constants> | ||
<constant name="CALL_OK" value="0" enum="CallError"> | ||
Call happened with no errors. | ||
</constant> | ||
<constant name="CALL_ERROR_INVALID_METHOD" value="1" enum="CallError"> | ||
The method was invalid (non existing). | ||
</constant> | ||
<constant name="CALL_ERROR_INVALID_ARGUMENT" value="2" enum="CallError"> | ||
At least one argument of the call was invalid (the type was not the right one). | ||
</constant> | ||
<constant name="CALL_ERROR_TOO_MANY_ARGUMENTS" value="3" enum="CallError"> | ||
More arguments were passed to the call than expected. | ||
</constant> | ||
<constant name="CALL_ERROR_TOO_FEW_ARGUMENTS" value="4" enum="CallError"> | ||
Fewer arguments were passed to the call than expected. | ||
</constant> | ||
<constant name="CALL_ERROR_INSTANCE_IS_NULL" value="5" enum="CallError"> | ||
A call happened on a null object instance. | ||
</constant> | ||
<constant name="CALL_ERROR_METHOD_NOT_CONST" value="6" enum="CallError"> | ||
A constant call happened in a non-const function. | ||
</constant> | ||
<constant name="CALL_ERROR_SCRIPT_ERROR" value="7" enum="CallError"> | ||
The function called was provided by a script, which failed during the call. | ||
</constant> | ||
</constants> | ||
</class> |
Oops, something went wrong.