| Copilot | 1e09a04 | 2026-01-29 10:36:28 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | |
| 20 | /** |
| 21 | * Test file to verify that private_optional generated code compiles and works correctly. |
| 22 | * This exercises the private_optional option using ThriftTest types. |
| 23 | */ |
| 24 | |
| 25 | #include <iostream> |
| 26 | #include <string> |
| 27 | #include <cassert> |
| 28 | #include <type_traits> |
| 29 | |
| 30 | // Include generated thrift types with private_optional option |
| 31 | #include "ThriftTest_types.h" |
| 32 | |
| 33 | using namespace thrift::test; |
| 34 | |
| 35 | // SFINAE test to check if a field is directly accessible |
| 36 | template<typename T, typename = void> |
| 37 | struct has_public_string_thing : std::false_type {}; |
| 38 | |
| 39 | template<typename T> |
| 40 | struct has_public_string_thing<T, decltype(void(std::declval<T>().string_thing))> : std::true_type {}; |
| 41 | |
| 42 | // SFINAE test to check if optional field 'aa' is directly accessible |
| 43 | template<typename T, typename = void> |
| 44 | struct has_public_aa : std::false_type {}; |
| 45 | |
| 46 | template<typename T> |
| 47 | struct has_public_aa<T, decltype(void(std::declval<T>().aa))> : std::true_type {}; |
| 48 | |
| 49 | // SFINAE test to check if required field 'ab' is directly accessible |
| 50 | template<typename T, typename = void> |
| 51 | struct has_public_ab : std::false_type {}; |
| 52 | |
| 53 | template<typename T> |
| 54 | struct has_public_ab<T, decltype(void(std::declval<T>().ab))> : std::true_type {}; |
| 55 | |
| 56 | int main() { |
| 57 | std::cout << "Testing private_optional with ThriftTest types..." << std::endl; |
| 58 | |
| 59 | // Compile-time verification: required fields should still be publicly accessible |
| 60 | static_assert(has_public_string_thing<Xtruct>::value, |
| 61 | "Required fields (like string_thing in Xtruct) should remain public"); |
| 62 | std::cout << " ✓ Compile-time verification: Required fields are public (Xtruct)" << std::endl; |
| 63 | |
| 64 | // Compile-time verification for StructB: optional field 'aa' should be private |
| 65 | static_assert(!has_public_aa<StructB>::value, |
| 66 | "Optional field 'aa' in StructB should be private with private_optional"); |
| 67 | std::cout << " ✓ Compile-time verification: Optional field 'aa' is private (StructB)" << std::endl; |
| 68 | |
| 69 | // Compile-time verification for StructB: required field 'ab' should be public |
| 70 | static_assert(has_public_ab<StructB>::value, |
| 71 | "Required field 'ab' in StructB should remain public"); |
| 72 | std::cout << " ✓ Compile-time verification: Required field 'ab' is public (StructB)" << std::endl; |
| 73 | |
| 74 | // Test 1: Verify getters work for accessing fields |
| 75 | { |
| 76 | Xtruct x; |
| 77 | x.__set_string_thing("test"); |
| 78 | const std::string& str = x.__get_string_thing(); |
| 79 | assert(str == "test"); |
| 80 | std::cout << " ✓ Getter for string field works" << std::endl; |
| 81 | } |
| 82 | |
| 83 | // Test 2: Verify setters work |
| 84 | { |
| 85 | Xtruct x; |
| 86 | x.__set_i32_thing(42); |
| 87 | x.__set_i64_thing(1234567890); |
| 88 | assert(x.__get_i32_thing() == 42); |
| 89 | assert(x.__get_i64_thing() == 1234567890); |
| 90 | std::cout << " ✓ Setters for primitive fields work" << std::endl; |
| 91 | } |
| 92 | |
| 93 | // Test 3: Verify getters/setters for complex types |
| 94 | { |
| 95 | Xtruct2 x2; |
| 96 | Xtruct x; |
| 97 | x.__set_string_thing("nested"); |
| 98 | x.__set_i32_thing(99); |
| 99 | x2.__set_struct_thing(x); |
| 100 | // With private_optional, use getters to access fields |
| 101 | assert(x2.__get_struct_thing().__get_string_thing() == "nested"); |
| 102 | assert(x2.__get_struct_thing().__get_i32_thing() == 99); |
| 103 | std::cout << " ✓ Getters/setters for struct fields work" << std::endl; |
| 104 | } |
| 105 | |
| 106 | // Test 4: Verify direct access to required fields still works |
| 107 | { |
| 108 | Xtruct x; |
| 109 | x.string_thing = "direct access"; |
| 110 | x.i32_thing = 123; |
| 111 | assert(x.string_thing == "direct access"); |
| 112 | assert(x.i32_thing == 123); |
| 113 | std::cout << " ✓ Direct access to required fields works" << std::endl; |
| 114 | } |
| 115 | |
| 116 | // Test 5: Test StructB with optional and required fields |
| 117 | { |
| 118 | StructB sb; |
| 119 | StructA sa; |
| 120 | sa.__set_s("test struct"); |
| 121 | |
| 122 | // Set optional field 'aa' using setter (cannot access directly) |
| 123 | sb.__set_aa(sa); |
| 124 | |
| 125 | // Set and access required field 'ab' directly (it's public) |
| 126 | sb.ab = sa; |
| 127 | |
| 128 | // Verify using getters |
| 129 | assert(sb.__get_aa().__get_s() == "test struct"); |
| 130 | assert(sb.ab.__get_s() == "test struct"); |
| 131 | std::cout << " ✓ StructB: Optional field private, required field public" << std::endl; |
| 132 | } |
| 133 | |
| 134 | std::cout << "\n✅ All private_optional tests passed!" << std::endl; |
| 135 | std::cout << " Verified at compile-time: Optional fields are private, required fields public" << std::endl; |
| 136 | return 0; |
| 137 | } |