Blender V5.3
rna_path_test.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2026 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include "BKE_gtest_base.hh"
6
7#include "RNA_path.hh"
8
9#include "testing/testing.h"
10
11namespace blender::tests {
12
13static void expect_member_item(const rna_path::Item &item, const StringRef expected)
14{
15 EXPECT_TRUE(std::holds_alternative<rna_path::Member>(item));
16 const auto &member = std::get<rna_path::Member>(item);
17 EXPECT_EQ(member.identifier.ref(), expected);
18}
19
20static void expect_lookup_index_item(const rna_path::Item &item, const int64_t expected)
21{
22 EXPECT_TRUE(std::holds_alternative<rna_path::LookupIndex>(item));
23 const auto &lookup_index = std::get<rna_path::LookupIndex>(item);
24 EXPECT_EQ(lookup_index.index, expected);
25}
26
27static void expect_lookup_key_item(const rna_path::Item &item, const StringRef expected)
28{
29 EXPECT_TRUE(std::holds_alternative<rna_path::LookupKey>(item));
30 const auto &lookup_key = std::get<rna_path::LookupKey>(item);
31 EXPECT_EQ(lookup_key.key.ref(), expected);
32}
33
34TEST(parse_rna_path, empty)
35{
36 const std::optional<ParsedRNAPath<>> path = ParsedRNAPath<>::from_string("");
37 EXPECT_FALSE(path.has_value());
38}
39
40TEST(parse_rna_path, just_member)
41{
42 const std::optional<ParsedRNAPath<>> path = ParsedRNAPath<>::from_string("foo");
43 EXPECT_EQ(path->to_string(), "foo");
44 EXPECT_EQ(path->items.size(), 1);
45 expect_member_item(path->items[0], "foo");
46}
47
48TEST(parse_rna_path, just_index)
49{
50 const std::optional<ParsedRNAPath<>> path = ParsedRNAPath<>::from_string("[42]");
51 EXPECT_EQ(path->to_string(), "[42]");
52 EXPECT_EQ(path->items.size(), 1);
53 expect_lookup_index_item(path->items[0], 42);
54}
55
56TEST(parse_rna_path, just_key)
57{
58 const std::optional<ParsedRNAPath<>> path = ParsedRNAPath<>::from_string("[\"foo\"]");
59 EXPECT_EQ(path->to_string(), "[\"foo\"]");
60 EXPECT_EQ(path->items.size(), 1);
61 expect_lookup_key_item(path->items[0], "foo");
62}
63
64TEST(parse_rna_path, multi)
65{
66 const std::optional<ParsedRNAPath<>> path = ParsedRNAPath<>::from_string(
67 "foo[42].bar.bar2[\"b\\\"az\"]");
68 EXPECT_EQ(path->to_string(), "foo[42].bar.bar2[\"b\\\"az\"]");
69 EXPECT_EQ(path->items.size(), 5);
70 expect_member_item(path->items[0], "foo");
71 expect_lookup_index_item(path->items[1], 42);
72 expect_member_item(path->items[2], "bar");
73 expect_member_item(path->items[3], "bar2");
74 expect_lookup_key_item(path->items[4], "b\"az");
75}
76
78
80{
81 { /* Simple infix replacement. Subscripts should be ignored. */
82 auto &&[old_key, new_key] = RNA_generate_keys_for_path_rename(
83 "old_name", "new_name", 1, 2, /*infix_is_name=*/false);
84 EXPECT_EQ("old_name", old_key);
85 EXPECT_EQ("new_name", new_key);
86 }
87
88 { /* Simple name replacement. Subscripts should be ignored. */
89 auto &&[old_key, new_key] = RNA_generate_keys_for_path_rename(
90 "old_name", "new_name", 1, 2, /*infix_is_name=*/true);
91 EXPECT_EQ("[\"old_name\"]", old_key);
92 EXPECT_EQ("[\"new_name\"]", new_key);
93 }
94
95 { /* Escaped name replacement. Subscripts should be ignored. */
96 auto &&[old_key, new_key] = RNA_generate_keys_for_path_rename(
97 "bone \"jaw\"", "\"jaw\" bone", 1, 2, /*infix_is_name=*/true);
98 EXPECT_EQ("[\"bone \\\"jaw\\\"\"]", old_key);
99 EXPECT_EQ("[\"\\\"jaw\\\" bone\"]", new_key);
100 }
101
102 {
103 /* Subscript replacement, marked as 'not a name'. */
104 auto &&[old_key,
105 new_key] = RNA_generate_keys_for_path_rename("", "", 327, 47, /*infix_is_name=*/false);
106 EXPECT_EQ("[327]", old_key);
107 EXPECT_EQ("[47]", new_key);
108 }
109
110 {
111 /* Subscript replacement, negative. */
112 auto &&[old_key,
113 new_key] = RNA_generate_keys_for_path_rename("", "", -1, 2, /*infix_is_name=*/false);
114 EXPECT_EQ("[-1]", old_key);
115 EXPECT_EQ("[2]", new_key);
116 }
117
118 {
119 /* Subscript replacement, and marked as name. */
120 auto &&[old_key,
121 new_key] = RNA_generate_keys_for_path_rename("", "", 327, 47, /*infix_is_name=*/true);
122 EXPECT_EQ("[327]", old_key);
123 EXPECT_EQ("[47]", new_key);
124 }
125}
126
128{
129 {
130 const std::string infix = RNA_path_name_to_infix("simple");
131 EXPECT_EQ("[\"simple\"]", infix);
132 }
133
134 {
135 const std::string infix = RNA_path_name_to_infix("\"quotes\"");
136 /* Expected result without double escaping: ["\"quotes\""] */
137 EXPECT_EQ("[\"\\\"quotes\\\"\"]", infix);
138 }
139}
140
142{
143 {
144 const std::string infix = RNA_path_number_to_infix(1);
145 EXPECT_EQ("[1]", infix);
146 }
147
148 {
149 /* Negative numbers also work. */
150 const std::string infix = RNA_path_number_to_infix(-1);
151 EXPECT_EQ("[-1]", infix);
152 }
153}
154
155} // namespace blender::tests
static std::optional< ParsedRNAPath > from_string(StringRefNull path)
Definition rna_path.cc:194
#define EXPECT_FALSE(result)
#define EXPECT_TRUE(result)
std::variant< Member, LookupIndex, LookupKey > Item
Definition RNA_path.hh:106
TEST_F(BlfLoadTest, load)
Definition BLF_tests.cc:37
static void expect_lookup_key_item(const rna_path::Item &item, const StringRef expected)
TEST(any_virtual, DefaultConstructor)
static void expect_lookup_index_item(const rna_path::Item &item, const int64_t expected)
static void expect_member_item(const rna_path::Item &item, const StringRef expected)
__int64 int64_t
Definition BKE_text.h:90
std::string RNA_path_number_to_infix(const int number)
Definition rna_path.cc:1425
std::string RNA_path_name_to_infix(const StringRefNull string)
Definition rna_path.cc:1419
std::pair< std::string, std::string > RNA_generate_keys_for_path_rename(const StringRefNull old_infix, const StringRefNull new_infix, const int old_subscript, const int new_subscript, const bool infix_is_name)
Definition rna_path.cc:1430
EXPECT_EQ(BLI_expr_pylike_eval(expr, nullptr, 0, &result), EXPR_PYLIKE_INVALID)