1 // Licensed to the Apache Software Foundation(ASF) under one 2 // or more contributor license agreements.See the NOTICE file 3 // distributed with this work for additional information 4 // regarding copyright ownership.The ASF licenses this file 5 // to you under the Apache License, Version 2.0 (the 6 // "License"); you may not use this file except in compliance 7 // with the License. You may obtain a copy of the License at 8 // 9 // http://www.apache.org/licenses/LICENSE-2.0 10 // 11 // Unless required by applicable law or agreed to in writing, 12 // software distributed under the License is distributed on an 13 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 // KIND, either express or implied. See the License for the 15 // specific language governing permissions and limitations 16 // under the License. 17 18 #include "../catch/catch.hpp" 19 #include <thrift/parse/t_program.h> 20 #include <thrift/generate/t_netstd_generator.h> 21 #include "t_netstd_generator_functional_tests_helpers.h" 22 23 TEST_CASE( "t_netstd_generator should generate valid enum", "[functional]" ) 24 { 25 string path = "CassandraTest.thrift"; 26 string name = "netstd"; 27 map<string, string> parsed_options = { { "wcf", "wcf" } }; 28 string option_string = ""; 29 30 t_program* program = new t_program(path, name); 31 t_netstd_generator* gen = new t_netstd_generator(program, parsed_options, option_string); 32 33 std::pair<string, t_enum*> pair = TestDataGenerator::get_test_enum_data(program); 34 string expected_result = pair.first; 35 t_enum* test_enum = pair.second; 36 37 string file_path = test_enum->get_name() + ".cs"; 38 ofstream out; 39 out.open(file_path.c_str()); 40 41 REQUIRE_NOTHROW(gen->generate_enum(out, test_enum)); 42 43 out.close(); 44 45 std::ifstream ifs(file_path); 46 string actual_result((std::istreambuf_iterator<char>(ifs)), (std::istreambuf_iterator<char>())); 47 std::remove(file_path.c_str()); 48 49 REQUIRE(expected_result == actual_result); 50 51 delete test_enum; 52 delete gen; 53 delete program; 54 } 55 56 TEST_CASE("t_netstd_generator should generate valid void", "[functional]") 57 { 58 string path = "CassandraTest.thrift"; 59 string name = "netstd"; 60 map<string, string> parsed_options = { { "wcf", "wcf" } }; 61 string option_string = ""; 62 63 t_program* program = new t_program(path, name); 64 t_netstd_generator* gen = new t_netstd_generator(program, parsed_options, option_string); 65 66 std::pair<string, t_const*> pair = TestDataGenerator::get_test_void_const_data(gen); 67 string expected_result = pair.first; 68 t_const* const_ = pair.second; 69 vector<t_const*> consts_; 70 consts_.push_back(const_); 71 72 string file_path = const_->get_name() + ".cs"; 73 ofstream out; 74 out.open(file_path.c_str()); 75 76 REQUIRE_THROWS(gen->generate_consts(out, consts_)); 77 78 out.close(); 79 80 std::ifstream ifs(file_path); 81 string actual_result((std::istreambuf_iterator<char>(ifs)), (std::istreambuf_iterator<char>())); 82 std::remove(file_path.c_str()); 83 84 delete const_; 85 delete gen; 86 delete program; 87 } 88 89 TEST_CASE("t_netstd_generator should generate valid string with escaping keyword", "[functional]") 90 { 91 string path = "CassandraTest.thrift"; 92 string name = "netstd"; 93 map<string, string> parsed_options = { { "wcf", "wcf" } }; 94 string option_string = ""; 95 96 t_program* program = new t_program(path, name); 97 t_netstd_generator* gen = new t_netstd_generator(program, parsed_options, option_string); 98 gen->init_generator(); 99 100 std::pair<string, t_const*> pair = TestDataGenerator::get_test_string_const_data(gen); 101 string expected_result = pair.first; 102 t_const* const_ = pair.second; 103 vector<t_const*> consts_; 104 consts_.push_back(const_); 105 106 string file_path = const_->get_name() + ".cs"; 107 ofstream out; 108 out.open(file_path.c_str()); 109 110 REQUIRE_NOTHROW(gen->generate_consts(out, consts_)); 111 112 out.close(); 113 114 std::ifstream ifs(file_path); 115 string actual_result((std::istreambuf_iterator<char>(ifs)), (std::istreambuf_iterator<char>())); 116 std::remove(file_path.c_str()); 117 118 REQUIRE(expected_result == actual_result); 119 120 delete const_; 121 delete gen; 122 delete program; 123 } 124 125 TEST_CASE("t_netstd_generator should generate valid bool with escaping keyword", "[functional]") 126 { 127 string path = "CassandraTest.thrift"; 128 string name = "netstd"; 129 map<string, string> parsed_options = { { "wcf", "wcf" } }; 130 string option_string = ""; 131 132 t_program* program = new t_program(path, name); 133 t_netstd_generator* gen = new t_netstd_generator(program, parsed_options, option_string); 134 gen->init_generator(); 135 136 std::pair<string, t_const*> pair = TestDataGenerator::get_test_bool_const_data(gen); 137 string expected_result = pair.first; 138 t_const* const_ = pair.second; 139 vector<t_const*> consts_; 140 consts_.push_back(const_); 141 142 string file_path = const_->get_name() + ".cs"; 143 ofstream out; 144 out.open(file_path.c_str()); 145 146 REQUIRE_NOTHROW(gen->generate_consts(out, consts_)); 147 148 out.close(); 149 150 std::ifstream ifs(file_path); 151 string actual_result((std::istreambuf_iterator<char>(ifs)), (std::istreambuf_iterator<char>())); 152 std::remove(file_path.c_str()); 153 154 REQUIRE(expected_result == actual_result); 155 156 delete const_; 157 delete gen; 158 delete program; 159 } 160 161 TEST_CASE("t_netstd_generator should generate valid sbyte (i8) with escaping keyword", "[functional]") 162 { 163 string path = "CassandraTest.thrift"; 164 string name = "netstd"; 165 map<string, string> parsed_options = { { "wcf", "wcf" } }; 166 string option_string = ""; 167 168 t_program* program = new t_program(path, name); 169 t_netstd_generator* gen = new t_netstd_generator(program, parsed_options, option_string); 170 gen->init_generator(); 171 172 std::pair<string, t_const*> pair = TestDataGenerator::get_test_i8_const_data(gen); 173 string expected_result = pair.first; 174 t_const* const_ = pair.second; 175 vector<t_const*> consts_; 176 consts_.push_back(const_); 177 178 string file_path = const_->get_name() + ".cs"; 179 ofstream out; 180 out.open(file_path.c_str()); 181 182 REQUIRE_NOTHROW(gen->generate_consts(out, consts_)); 183 184 out.close(); 185 186 std::ifstream ifs(file_path); 187 string actual_result((std::istreambuf_iterator<char>(ifs)), (std::istreambuf_iterator<char>())); 188 std::remove(file_path.c_str()); 189 190 REQUIRE(expected_result == actual_result); 191 192 delete const_; 193 delete gen; 194 delete program; 195 } 196 197 TEST_CASE("t_netstd_generator should generate valid short (i16) with escaping keyword", "[functional]") 198 { 199 string path = "CassandraTest.thrift"; 200 string name = "netstd"; 201 map<string, string> parsed_options = { { "wcf", "wcf" } }; 202 string option_string = ""; 203 204 t_program* program = new t_program(path, name); 205 t_netstd_generator* gen = new t_netstd_generator(program, parsed_options, option_string); 206 gen->init_generator(); 207 208 std::pair<string, t_const*> pair = TestDataGenerator::get_test_i16_const_data(gen); 209 string expected_result = pair.first; 210 t_const* const_ = pair.second; 211 vector<t_const*> consts_; 212 consts_.push_back(const_); 213 214 string file_path = const_->get_name() + ".cs"; 215 ofstream out; 216 out.open(file_path.c_str()); 217 218 REQUIRE_NOTHROW(gen->generate_consts(out, consts_)); 219 220 out.close(); 221 222 std::ifstream ifs(file_path); 223 string actual_result((std::istreambuf_iterator<char>(ifs)), (std::istreambuf_iterator<char>())); 224 std::remove(file_path.c_str()); 225 226 REQUIRE(expected_result == actual_result); 227 228 delete const_; 229 delete gen; 230 delete program; 231 } 232 233 TEST_CASE("t_netstd_generator should generate valid integer (i32) with escaping keyword", "[functional]") 234 { 235 string path = "CassandraTest.thrift"; 236 string name = "netstd"; 237 map<string, string> parsed_options = { { "wcf", "wcf" } }; 238 string option_string = ""; 239 240 t_program* program = new t_program(path, name); 241 t_netstd_generator* gen = new t_netstd_generator(program, parsed_options, option_string); 242 gen->init_generator(); 243 244 std::pair<string, t_const*> pair = TestDataGenerator::get_test_i32_const_data(gen); 245 string expected_result = pair.first; 246 t_const* const_ = pair.second; 247 vector<t_const*> consts_; 248 consts_.push_back(const_); 249 250 string file_path = const_->get_name() + ".cs"; 251 ofstream out; 252 out.open(file_path.c_str()); 253 254 REQUIRE_NOTHROW(gen->generate_consts(out, consts_)); 255 256 out.close(); 257 258 std::ifstream ifs(file_path); 259 string actual_result((std::istreambuf_iterator<char>(ifs)), (std::istreambuf_iterator<char>())); 260 std::remove(file_path.c_str()); 261 262 REQUIRE(expected_result == actual_result); 263 264 delete const_; 265 delete gen; 266 delete program; 267 } 268 269 TEST_CASE("t_netstd_generator should generate valid long (i64) with escaping keyword", "[functional]") 270 { 271 string path = "CassandraTest.thrift"; 272 string name = "netstd"; 273 map<string, string> parsed_options = { { "wcf", "wcf" } }; 274 string option_string = ""; 275 276 t_program* program = new t_program(path, name); 277 t_netstd_generator* gen = new t_netstd_generator(program, parsed_options, option_string); 278 gen->init_generator(); 279 280 std::pair<string, t_const*> pair = TestDataGenerator::get_test_i64_const_data(gen); 281 string expected_result = pair.first; 282 t_const* const_ = pair.second; 283 vector<t_const*> consts_; 284 consts_.push_back(const_); 285 286 string file_path = const_->get_name() + ".cs"; 287 ofstream out; 288 out.open(file_path.c_str()); 289 290 REQUIRE_NOTHROW(gen->generate_consts(out, consts_)); 291 292 out.close(); 293 294 std::ifstream ifs(file_path); 295 string actual_result((std::istreambuf_iterator<char>(ifs)), (std::istreambuf_iterator<char>())); 296 std::remove(file_path.c_str()); 297 298 REQUIRE(expected_result == actual_result); 299 300 delete const_; 301 delete gen; 302 delete program; 303 } 304 305 TEST_CASE("t_netstd_generator should generate valid double with escaping keyword", "[functional]") 306 { 307 string path = "CassandraTest.thrift"; 308 string name = "netstd"; 309 map<string, string> parsed_options = { { "wcf", "wcf" } }; 310 string option_string = ""; 311 312 t_program* program = new t_program(path, name); 313 t_netstd_generator* gen = new t_netstd_generator(program, parsed_options, option_string); 314 gen->init_generator(); 315 316 std::pair<string, t_const*> pair = TestDataGenerator::get_test_double_const_data(gen); 317 string expected_result = pair.first; 318 t_const* const_ = pair.second; 319 vector<t_const*> consts_; 320 consts_.push_back(const_); 321 322 string file_path = const_->get_name() + ".cs"; 323 ofstream out; 324 out.open(file_path.c_str()); 325 326 REQUIRE_NOTHROW(gen->generate_consts(out, consts_)); 327 328 out.close(); 329 330 std::ifstream ifs(file_path); 331 string actual_result((std::istreambuf_iterator<char>(ifs)), (std::istreambuf_iterator<char>())); 332 std::remove(file_path.c_str()); 333 334 REQUIRE(expected_result == actual_result); 335 336 delete const_; 337 delete gen; 338 delete program; 339 } 340