Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More object unit tests #44387

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 91 additions & 0 deletions tests/test_object.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,97 @@ TEST_CASE("[Object] Metadata") {
meta_list2.size() == 0,
"The metadata list should contain 0 items after removing all metadata items.");
}

TEST_CASE("[Object] IAPI methods") {
Object object;
bool *rvalid = memnew(bool(false));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this adds unnecessary complexity, I'd rewrite this to:

Suggested change
bool *rvalid = memnew(bool(false));
bool valid = false;

and then pass the pointer to methods which use this variable, such as below:

object.get_indexed(p_names, &valid);

Also, this way you don't have to dereference the pointer (*valid) whenever you need to reset the valid variable for other assertions.


object.set(StringName("script"), "script_source", rvalid);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not 100% sure but I think there's no need to explicitly convert the first argument to StringName:

Suggested change
object.set(StringName("script"), "script_source", rvalid);
object.set("script", "script_source", rvalid);

The same applies to other assertions.

CHECK_MESSAGE(
*rvalid == true,
"set() should update the passed bool to the expected value.");

*rvalid = false;
CHECK_MESSAGE(
object.get(StringName("script"), rvalid) == "script_source",
"get() should return the expected value.");
CHECK_MESSAGE(
*rvalid == true,
"get() should update the passed bool to the expected value.");

Vector<StringName> p_names;
object.set_indexed(p_names, "value", rvalid);
CHECK_MESSAGE(
*rvalid == false,
"set_indexed() should update the passed bool to the expected value for empty Vector<StringName>.");

*rvalid = true;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sanity check: should this be reset to false (at least for consistency)?

Suggested change
*rvalid = true;
*rvalid = false;

CHECK_MESSAGE(
object.get_indexed(p_names, rvalid) == Variant(),
"get_indexed() should return the expected value for empty Vector<StringName>.");
CHECK_MESSAGE(
*rvalid == false,
"get_indexed() should update the passed bool to the expected value for empty Vector<StringName>.");

p_names.push_back(StringName("script"));
object.set_indexed(p_names, "indexed_script_source", rvalid);
CHECK_MESSAGE(
*rvalid == true,
"set_indexed() should update the passed bool to the expected value for valid Vector<StringName> of size 1.");

*rvalid = false;
CHECK_MESSAGE(
object.get_indexed(p_names, rvalid) == "indexed_script_source",
"get_indexed() should return the expected value for valid Vector<StringName> of size 1.");
CHECK_MESSAGE(
*rvalid == true,
"get_indexed() should update the passed bool to the expected value for valid Vector<StringName> of size 1.");

CHECK_MESSAGE(
object.has_method(StringName("free")) == true,
"has_method() should return the expected value for StringName('free')");

CHECK_MESSAGE(
object.has_method(StringName("_to_string")) == false,
"has_method() should return the expected value for non-method");

CHECK_MESSAGE(
object.to_string() == "[Object:" + itos(object.get_instance_id()) + "]",
"to_string() should return the expected value.");

*rvalid = false;
CHECK_MESSAGE(
object.getvar(StringName("script"), rvalid) == "indexed_script_source",
"getvar() should return the expected value.");
CHECK_MESSAGE(
*rvalid == true,
"getvar() should update the passed bool to the expected value.");

*rvalid = false;
object.setvar(StringName("script"), "var_script", rvalid);
CHECK_MESSAGE(
*rvalid == true,
"setvar() should update the passed bool to the expected value.");
CHECK_MESSAGE(
object.getvar(StringName("script"), rvalid) == "var_script",
"getvar() should return the expected value.");

object.setvar(StringName("non-CoreStringName"), "nonvalid_script", rvalid);
CHECK_MESSAGE(
*rvalid == false,
"setvar() should update the passed bool to the expected value.");

memdelete(rvalid);
}

TEST_CASE("[Object] Script setter and getter") {
Object object;

object.set_script("script_source");
CHECK_MESSAGE(
object.get_script() == "script_source",
"get_script() should return the expected value.");
}
} // namespace TestObject

#endif // TEST_OBJECT_H