Path: blob/master/tests/core/string/test_node_path.h
10278 views
/**************************************************************************/1/* test_node_path.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/string/node_path.h"3334#include "tests/test_macros.h"3536namespace TestNodePath {3738TEST_CASE("[NodePath] Relative path") {39const NodePath node_path_relative = NodePath("Path2D/PathFollow2D/Sprite2D:position:x");4041CHECK_MESSAGE(42node_path_relative.get_as_property_path() == NodePath(":Path2D/PathFollow2D/Sprite2D:position:x"),43"The returned property path should match the expected value.");44CHECK_MESSAGE(45node_path_relative.get_concatenated_subnames() == "position:x",46"The returned concatenated subnames should match the expected value.");4748CHECK_MESSAGE(49node_path_relative.get_name(0) == "Path2D",50"The returned name at index 0 should match the expected value.");51CHECK_MESSAGE(52node_path_relative.get_name(1) == "PathFollow2D",53"The returned name at index 1 should match the expected value.");54CHECK_MESSAGE(55node_path_relative.get_name(2) == "Sprite2D",56"The returned name at index 2 should match the expected value.");57ERR_PRINT_OFF;58CHECK_MESSAGE(59node_path_relative.get_name(3) == "",60"The returned name at invalid index 3 should match the expected value.");61CHECK_MESSAGE(62node_path_relative.get_name(-1) == "",63"The returned name at invalid index -1 should match the expected value.");64ERR_PRINT_ON;6566CHECK_MESSAGE(67node_path_relative.get_name_count() == 3,68"The returned number of names should match the expected value.");6970CHECK_MESSAGE(71node_path_relative.get_subname(0) == "position",72"The returned subname at index 0 should match the expected value.");73CHECK_MESSAGE(74node_path_relative.get_subname(1) == "x",75"The returned subname at index 1 should match the expected value.");76ERR_PRINT_OFF;77CHECK_MESSAGE(78node_path_relative.get_subname(2) == "",79"The returned subname at invalid index 2 should match the expected value.");80CHECK_MESSAGE(81node_path_relative.get_subname(-1) == "",82"The returned subname at invalid index -1 should match the expected value.");83ERR_PRINT_ON;8485CHECK_MESSAGE(86node_path_relative.get_subname_count() == 2,87"The returned number of subnames should match the expected value.");8889CHECK_MESSAGE(90!node_path_relative.is_absolute(),91"The node path should be considered relative.");9293CHECK_MESSAGE(94!node_path_relative.is_empty(),95"The node path shouldn't be considered empty.");96}9798TEST_CASE("[NodePath] Absolute path") {99const NodePath node_path_absolute = NodePath("/root/Sprite2D");100101CHECK_MESSAGE(102node_path_absolute.get_as_property_path() == NodePath(":root/Sprite2D"),103"The returned property path should match the expected value.");104CHECK_MESSAGE(105node_path_absolute.get_concatenated_subnames() == "",106"The returned concatenated subnames should match the expected value.");107108CHECK_MESSAGE(109node_path_absolute.get_name(0) == "root",110"The returned name at index 0 should match the expected value.");111CHECK_MESSAGE(112node_path_absolute.get_name(1) == "Sprite2D",113"The returned name at index 1 should match the expected value.");114ERR_PRINT_OFF;115CHECK_MESSAGE(116node_path_absolute.get_name(2) == "",117"The returned name at invalid index 2 should match the expected value.");118CHECK_MESSAGE(119node_path_absolute.get_name(-1) == "",120"The returned name at invalid index -1 should match the expected value.");121ERR_PRINT_ON;122123CHECK_MESSAGE(124node_path_absolute.get_name_count() == 2,125"The returned number of names should match the expected value.");126127CHECK_MESSAGE(128node_path_absolute.get_subname_count() == 0,129"The returned number of subnames should match the expected value.");130131CHECK_MESSAGE(132node_path_absolute.is_absolute(),133"The node path should be considered absolute.");134135CHECK_MESSAGE(136!node_path_absolute.is_empty(),137"The node path shouldn't be considered empty.");138}139140TEST_CASE("[NodePath] Empty path") {141const NodePath node_path_empty = NodePath();142143CHECK_MESSAGE(144node_path_empty.get_as_property_path() == NodePath(),145"The returned property path should match the expected value.");146ERR_PRINT_OFF;147CHECK_MESSAGE(148node_path_empty.get_concatenated_subnames() == "",149"The returned concatenated subnames should match the expected value.");150ERR_PRINT_ON;151152CHECK_MESSAGE(153node_path_empty.get_name_count() == 0,154"The returned number of names should match the expected value.");155156CHECK_MESSAGE(157node_path_empty.get_subname_count() == 0,158"The returned number of subnames should match the expected value.");159160CHECK_MESSAGE(161!node_path_empty.is_absolute(),162"The node path shouldn't be considered absolute.");163164CHECK_MESSAGE(165node_path_empty.is_empty(),166"The node path should be considered empty.");167}168169TEST_CASE("[NodePath] Slice") {170const NodePath node_path_relative = NodePath("Parent/Child:prop:subprop");171const NodePath node_path_absolute = NodePath("/root/Parent/Child:prop");172CHECK_MESSAGE(173node_path_relative.slice(0, 2) == NodePath("Parent/Child"),174"The slice lower bound should be inclusive and the slice upper bound should be exclusive.");175CHECK_MESSAGE(176node_path_relative.slice(3) == NodePath(":subprop"),177"Slicing on the last index (length - 1) should return the last entry.");178CHECK_MESSAGE(179node_path_relative.slice(1) == NodePath("Child:prop:subprop"),180"Slicing without upper bound should return remaining entries after index.");181CHECK_MESSAGE(182node_path_relative.slice(1, 3) == NodePath("Child:prop"),183"Slicing should include names and subnames.");184CHECK_MESSAGE(185node_path_relative.slice(-1) == NodePath(":subprop"),186"Slicing on -1 should return the last entry.");187CHECK_MESSAGE(188node_path_relative.slice(0, -1) == NodePath("Parent/Child:prop"),189"Slicing up to -1 should include the second-to-last entry.");190CHECK_MESSAGE(191node_path_relative.slice(-2, -1) == NodePath(":prop"),192"Slicing from negative to negative should treat lower bound as inclusive and upper bound as exclusive.");193CHECK_MESSAGE(194node_path_relative.slice(0, 10) == NodePath("Parent/Child:prop:subprop"),195"Slicing past the length of the path should work like slicing up to the last entry.");196CHECK_MESSAGE(197node_path_relative.slice(-10, 2) == NodePath("Parent/Child"),198"Slicing negatively past the length of the path should work like slicing from the first entry.");199CHECK_MESSAGE(200node_path_relative.slice(1, 1) == NodePath(""),201"Slicing with a lower bound equal to upper bound should return empty path.");202203CHECK_MESSAGE(204node_path_absolute.slice(0, 2) == NodePath("/root/Parent"),205"Slice from beginning of an absolute path should be an absolute path.");206CHECK_MESSAGE(207node_path_absolute.slice(1, 4) == NodePath("Parent/Child:prop"),208"Slice of an absolute path that does not start at the beginning should be a relative path.");209CHECK_MESSAGE(210node_path_absolute.slice(3, 4) == NodePath(":prop"),211"Slice of an absolute path that does not start at the beginning should be a relative path.");212213CHECK_MESSAGE(214NodePath("").slice(0, 1) == NodePath(""),215"Slice of an empty path should be an empty path.");216CHECK_MESSAGE(217NodePath("").slice(-1, 2) == NodePath(""),218"Slice of an empty path should be an empty path.");219CHECK_MESSAGE(220NodePath("/").slice(-1, 2) == NodePath("/"),221"Slice of an empty absolute path should be an empty absolute path.");222}223224} // namespace TestNodePath225226227