Path: blob/master/tests/core/variant/test_variant_utility.h
10278 views
/**************************************************************************/1/* test_variant_utility.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/variant/variant_utility.h"3334#include "tests/test_macros.h"3536namespace TestVariantUtility {3738TEST_CASE("[VariantUtility] Type conversion") {39Variant converted;40converted = VariantUtilityFunctions::type_convert("Hi!", Variant::Type::NIL);41CHECK(converted.get_type() == Variant::Type::NIL);42CHECK(converted == Variant());4344converted = VariantUtilityFunctions::type_convert("Hi!", Variant::Type::INT);45CHECK(converted.get_type() == Variant::Type::INT);46CHECK(converted == Variant(0));4748converted = VariantUtilityFunctions::type_convert("123", Variant::Type::INT);49CHECK(converted.get_type() == Variant::Type::INT);50CHECK(converted == Variant(123));5152converted = VariantUtilityFunctions::type_convert(123, Variant::Type::STRING);53CHECK(converted.get_type() == Variant::Type::STRING);54CHECK(converted == Variant("123"));5556converted = VariantUtilityFunctions::type_convert(123.4, Variant::Type::INT);57CHECK(converted.get_type() == Variant::Type::INT);58CHECK(converted == Variant(123));5960converted = VariantUtilityFunctions::type_convert(5, Variant::Type::VECTOR2);61CHECK(converted.get_type() == Variant::Type::VECTOR2);62CHECK(converted == Variant(Vector2(0, 0)));6364converted = VariantUtilityFunctions::type_convert(Vector3(1, 2, 3), Variant::Type::VECTOR2);65CHECK(converted.get_type() == Variant::Type::VECTOR2);66CHECK(converted == Variant(Vector2(1, 2)));6768converted = VariantUtilityFunctions::type_convert(Vector2(1, 2), Variant::Type::VECTOR4);69CHECK(converted.get_type() == Variant::Type::VECTOR4);70CHECK(converted == Variant(Vector4(1, 2, 0, 0)));7172converted = VariantUtilityFunctions::type_convert(Vector4(1.2, 3.4, 5.6, 7.8), Variant::Type::VECTOR3I);73CHECK(converted.get_type() == Variant::Type::VECTOR3I);74CHECK(converted == Variant(Vector3i(1, 3, 5)));7576{77Basis basis = Basis::from_scale(Vector3(1.2, 3.4, 5.6));78Transform3D transform = Transform3D(basis, Vector3());7980converted = VariantUtilityFunctions::type_convert(transform, Variant::Type::BASIS);81CHECK(converted.get_type() == Variant::Type::BASIS);82CHECK(converted == basis);8384converted = VariantUtilityFunctions::type_convert(basis, Variant::Type::TRANSFORM3D);85CHECK(converted.get_type() == Variant::Type::TRANSFORM3D);86CHECK(converted == transform);8788converted = VariantUtilityFunctions::type_convert(basis, Variant::Type::STRING);89CHECK(converted.get_type() == Variant::Type::STRING);90CHECK(converted == Variant("[X: (1.2, 0.0, 0.0), Y: (0.0, 3.4, 0.0), Z: (0.0, 0.0, 5.6)]"));91}9293{94Array arr = { 1.2, 3.4, 5.6 };95PackedFloat64Array packed = { 1.2, 3.4, 5.6 };9697converted = VariantUtilityFunctions::type_convert(arr, Variant::Type::PACKED_FLOAT64_ARRAY);98CHECK(converted.get_type() == Variant::Type::PACKED_FLOAT64_ARRAY);99CHECK(converted == packed);100101converted = VariantUtilityFunctions::type_convert(packed, Variant::Type::ARRAY);102CHECK(converted.get_type() == Variant::Type::ARRAY);103CHECK(converted == arr);104}105106{107// Check that using Variant::call_utility_function also works.108Vector<const Variant *> args;109Variant data_arg = "Hi!";110args.push_back(&data_arg);111Variant type_arg = Variant::Type::NIL;112args.push_back(&type_arg);113Callable::CallError call_error;114Variant::call_utility_function("type_convert", &converted, (const Variant **)args.ptr(), 2, call_error);115CHECK(converted.get_type() == Variant::Type::NIL);116CHECK(converted == Variant());117118type_arg = Variant::Type::INT;119Variant::call_utility_function("type_convert", &converted, (const Variant **)args.ptr(), 2, call_error);120CHECK(converted.get_type() == Variant::Type::INT);121CHECK(converted == Variant(0));122123data_arg = "123";124Variant::call_utility_function("type_convert", &converted, (const Variant **)args.ptr(), 2, call_error);125CHECK(converted.get_type() == Variant::Type::INT);126CHECK(converted == Variant(123));127}128}129130} // namespace TestVariantUtility131132133