blob: c17af1f402090b4629f25a7a11b13ddb5794ec1d [file] [log] [blame]
Dennis Shen1dc9ad42023-05-12 00:21:55 +00001/*
2 * Copyright (C) 2023 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Mårten Kongstadfbd71e22023-05-31 13:29:35 +020017use anyhow::{ensure, Result};
Dennis Shen1dc9ad42023-05-12 00:21:55 +000018use serde::Serialize;
Dennis Shen8d544f72023-06-29 00:45:42 +000019use std::path::PathBuf;
Dennis Shen1dc9ad42023-05-12 00:21:55 +000020use tinytemplate::TinyTemplate;
21
Mårten Kongstadfbd71e22023-05-31 13:29:35 +020022use crate::codegen;
Dennis Shen8d544f72023-06-29 00:45:42 +000023use crate::commands::{CodegenMode, OutputFile};
Mårten Kongstad403658f2023-06-14 09:51:56 +020024use crate::protos::{ProtoFlagPermission, ProtoFlagState, ProtoParsedFlag};
Dennis Shen1dc9ad42023-05-12 00:21:55 +000025
Dennis Shen8d544f72023-06-29 00:45:42 +000026pub fn generate_cpp_code<'a, I>(
27 package: &str,
28 parsed_flags_iter: I,
29 codegen_mode: CodegenMode,
30) -> Result<Vec<OutputFile>>
Mårten Kongstad403658f2023-06-14 09:51:56 +020031where
32 I: Iterator<Item = &'a ProtoParsedFlag>,
33{
Mårten Kongstad066575b2023-06-07 16:29:25 +020034 let class_elements: Vec<ClassElement> =
Mårten Kongstad403658f2023-06-14 09:51:56 +020035 parsed_flags_iter.map(|pf| create_class_element(package, pf)).collect();
Dennis Shen1dc9ad42023-05-12 00:21:55 +000036 let readwrite = class_elements.iter().any(|item| item.readwrite);
Mårten Kongstadfbd71e22023-05-31 13:29:35 +020037 let header = package.replace('.', "_");
38 let cpp_namespace = package.replace('.', "::");
39 ensure!(codegen::is_valid_name_ident(&header));
Mårten Kongstad066575b2023-06-07 16:29:25 +020040 let context = Context {
41 header: header.clone(),
42 cpp_namespace,
43 package: package.to_string(),
44 readwrite,
Dennis Shen17a08eec2023-07-14 01:32:50 +000045 for_test: codegen_mode == CodegenMode::Test,
Mårten Kongstad066575b2023-06-07 16:29:25 +020046 class_elements,
47 };
Dennis Shen8d544f72023-06-29 00:45:42 +000048
49 let files = [
50 FileSpec {
51 name: &format!("{}.h", header),
52 template: include_str!("../templates/cpp_exported_header.template"),
53 dir: "include",
54 },
55 FileSpec {
56 name: &format!("{}.cc", header),
57 template: include_str!("../templates/cpp_source_file.template"),
58 dir: "",
59 },
60 FileSpec {
61 name: &format!("{}_flag_provider.h", header),
62 template: match codegen_mode {
63 CodegenMode::Production => {
64 include_str!("../templates/cpp_prod_flag_provider.template")
65 }
66 CodegenMode::Test => include_str!("../templates/cpp_test_flag_provider.template"),
67 },
68 dir: "",
69 },
Dennis Shen7321f4f2023-07-11 15:45:00 +000070 FileSpec {
71 name: &format!("{}_c.h", header),
72 template: include_str!("../templates/c_exported_header.template"),
73 dir: "include",
74 },
75 FileSpec {
76 name: &format!("{}_c.cc", header),
77 template: include_str!("../templates/c_source_file.template"),
78 dir: "",
79 },
Dennis Shen8d544f72023-06-29 00:45:42 +000080 ];
81 files.iter().map(|file| generate_file(file, &context)).collect()
82}
83
84pub fn generate_file(file: &FileSpec, context: &Context) -> Result<OutputFile> {
Dennis Shen1dc9ad42023-05-12 00:21:55 +000085 let mut template = TinyTemplate::new();
Dennis Shen8d544f72023-06-29 00:45:42 +000086 template.add_template(file.name, file.template)?;
87 let contents = template.render(file.name, &context)?;
88 let path: PathBuf = [&file.dir, &file.name].iter().collect();
Dennis Shen1dc9ad42023-05-12 00:21:55 +000089 Ok(OutputFile { contents: contents.into(), path })
90}
91
92#[derive(Serialize)]
Dennis Shen8d544f72023-06-29 00:45:42 +000093pub struct FileSpec<'a> {
94 pub name: &'a str,
95 pub template: &'a str,
96 pub dir: &'a str,
97}
98
99#[derive(Serialize)]
100pub struct Context {
Mårten Kongstadfbd71e22023-05-31 13:29:35 +0200101 pub header: String,
102 pub cpp_namespace: String,
Mårten Kongstad9fb58962023-05-31 13:02:13 +0200103 pub package: String,
Dennis Shen1dc9ad42023-05-12 00:21:55 +0000104 pub readwrite: bool,
Dennis Shen17a08eec2023-07-14 01:32:50 +0000105 pub for_test: bool,
Dennis Shen1dc9ad42023-05-12 00:21:55 +0000106 pub class_elements: Vec<ClassElement>,
107}
108
109#[derive(Serialize)]
Dennis Shen8d544f72023-06-29 00:45:42 +0000110pub struct ClassElement {
Dennis Shen1dc9ad42023-05-12 00:21:55 +0000111 pub readwrite: bool,
112 pub default_value: String,
113 pub flag_name: String,
Mårten Kongstad066575b2023-06-07 16:29:25 +0200114 pub device_config_namespace: String,
115 pub device_config_flag: String,
Dennis Shen1dc9ad42023-05-12 00:21:55 +0000116}
117
Mårten Kongstad403658f2023-06-14 09:51:56 +0200118fn create_class_element(package: &str, pf: &ProtoParsedFlag) -> ClassElement {
Dennis Shen1dc9ad42023-05-12 00:21:55 +0000119 ClassElement {
Mårten Kongstad403658f2023-06-14 09:51:56 +0200120 readwrite: pf.permission() == ProtoFlagPermission::READ_WRITE,
121 default_value: if pf.state() == ProtoFlagState::ENABLED {
Dennis Shen1dc9ad42023-05-12 00:21:55 +0000122 "true".to_string()
123 } else {
124 "false".to_string()
125 },
Mårten Kongstad403658f2023-06-14 09:51:56 +0200126 flag_name: pf.name().to_string(),
127 device_config_namespace: pf.namespace().to_string(),
128 device_config_flag: codegen::create_device_config_ident(package, pf.name())
129 .expect("values checked at flag parse time"),
Dennis Shen1dc9ad42023-05-12 00:21:55 +0000130 }
131}
132
133#[cfg(test)]
134mod tests {
135 use super::*;
Dennis Shen8d544f72023-06-29 00:45:42 +0000136 use std::collections::HashMap;
Dennis Shen1dc9ad42023-05-12 00:21:55 +0000137
Dennis Shen8d544f72023-06-29 00:45:42 +0000138 const EXPORTED_PROD_HEADER_EXPECTED: &str = r#"
Dennis Shen5c242132023-07-14 14:57:08 +0000139#pragma once
Dennis Shen1dc9ad42023-05-12 00:21:55 +0000140
Dennis Shen8d544f72023-06-29 00:45:42 +0000141#include <memory>
Dennis Shen1dc9ad42023-05-12 00:21:55 +0000142
Mårten Kongstad403658f2023-06-14 09:51:56 +0200143namespace com::android::aconfig::test {
Dennis Shen8d544f72023-06-29 00:45:42 +0000144class flag_provider_interface {
145public:
146
147 virtual ~flag_provider_interface() = default;
148
149 virtual bool disabled_ro() = 0;
150
151 virtual bool disabled_rw() = 0;
152
153 virtual bool enabled_ro() = 0;
154
155 virtual bool enabled_rw() = 0;
Dennis Shen8d544f72023-06-29 00:45:42 +0000156};
157
158extern std::unique_ptr<flag_provider_interface> provider_;
159
Dennis Shen8d544f72023-06-29 00:45:42 +0000160inline bool disabled_ro() {
161 return false;
162}
163
164inline bool disabled_rw() {
165 return provider_->disabled_rw();
166}
167
168inline bool enabled_ro() {
169 return true;
170}
171
172inline bool enabled_rw() {
173 return provider_->enabled_rw();
174}
175
Dennis Shen8d544f72023-06-29 00:45:42 +0000176}
Dennis Shen8d544f72023-06-29 00:45:42 +0000177"#;
178
179 const EXPORTED_TEST_HEADER_EXPECTED: &str = r#"
Dennis Shen5c242132023-07-14 14:57:08 +0000180#pragma once
Dennis Shen8d544f72023-06-29 00:45:42 +0000181
Dennis Shen8d544f72023-06-29 00:45:42 +0000182#include <memory>
Dennis Shen8d544f72023-06-29 00:45:42 +0000183
184namespace com::android::aconfig::test {
185class flag_provider_interface {
186public:
187
188 virtual ~flag_provider_interface() = default;
189
190 virtual bool disabled_ro() = 0;
191
Dennis Shen17a08eec2023-07-14 01:32:50 +0000192 virtual void disabled_ro(bool val) = 0;
193
Dennis Shen8d544f72023-06-29 00:45:42 +0000194 virtual bool disabled_rw() = 0;
195
Dennis Shen17a08eec2023-07-14 01:32:50 +0000196 virtual void disabled_rw(bool val) = 0;
197
Dennis Shen8d544f72023-06-29 00:45:42 +0000198 virtual bool enabled_ro() = 0;
199
Dennis Shen17a08eec2023-07-14 01:32:50 +0000200 virtual void enabled_ro(bool val) = 0;
201
Dennis Shen8d544f72023-06-29 00:45:42 +0000202 virtual bool enabled_rw() = 0;
203
Dennis Shen17a08eec2023-07-14 01:32:50 +0000204 virtual void enabled_rw(bool val) = 0;
Dennis Shen8d544f72023-06-29 00:45:42 +0000205
Dennis Shen17a08eec2023-07-14 01:32:50 +0000206 virtual void reset_flags() {}
Dennis Shen8d544f72023-06-29 00:45:42 +0000207};
208
209extern std::unique_ptr<flag_provider_interface> provider_;
210
Dennis Shen8d544f72023-06-29 00:45:42 +0000211inline bool disabled_ro() {
212 return provider_->disabled_ro();
213}
214
Dennis Shen17a08eec2023-07-14 01:32:50 +0000215inline void disabled_ro(bool val) {
216 provider_->disabled_ro(val);
217}
218
Dennis Shen8d544f72023-06-29 00:45:42 +0000219inline bool disabled_rw() {
220 return provider_->disabled_rw();
221}
222
Dennis Shen17a08eec2023-07-14 01:32:50 +0000223inline void disabled_rw(bool val) {
224 provider_->disabled_rw(val);
225}
226
Dennis Shen8d544f72023-06-29 00:45:42 +0000227inline bool enabled_ro() {
228 return provider_->enabled_ro();
229}
230
Dennis Shen17a08eec2023-07-14 01:32:50 +0000231inline void enabled_ro(bool val) {
232 provider_->enabled_ro(val);
233}
234
Dennis Shen8d544f72023-06-29 00:45:42 +0000235inline bool enabled_rw() {
236 return provider_->enabled_rw();
237}
238
Dennis Shen17a08eec2023-07-14 01:32:50 +0000239inline void enabled_rw(bool val) {
240 provider_->enabled_rw(val);
Dennis Shen8d544f72023-06-29 00:45:42 +0000241}
242
Dennis Shen17a08eec2023-07-14 01:32:50 +0000243inline void reset_flags() {
244 return provider_->reset_flags();
Dennis Shen8d544f72023-06-29 00:45:42 +0000245}
246
247}
Dennis Shen8d544f72023-06-29 00:45:42 +0000248"#;
249
250 const PROD_FLAG_PROVIDER_HEADER_EXPECTED: &str = r#"
Dennis Shen5c242132023-07-14 14:57:08 +0000251#pragma once
Dennis Shen8d544f72023-06-29 00:45:42 +0000252
253#include "com_android_aconfig_test.h"
Dennis Shene3981442023-07-10 18:15:42 +0000254#include <server_configurable_flags/get_flags.h>
Dennis Shen8d544f72023-06-29 00:45:42 +0000255
256namespace com::android::aconfig::test {
257class flag_provider : public flag_provider_interface {
258public:
259
260 virtual bool disabled_ro() override {
Mårten Kongstad403658f2023-06-14 09:51:56 +0200261 return false;
Dennis Shen1dc9ad42023-05-12 00:21:55 +0000262 }
263
Dennis Shen8d544f72023-06-29 00:45:42 +0000264 virtual bool disabled_rw() override {
Dennis Shen5c242132023-07-14 14:57:08 +0000265 return server_configurable_flags::GetServerConfigurableFlag(
Mårten Kongstad403658f2023-06-14 09:51:56 +0200266 "aconfig_test",
267 "com.android.aconfig.test.disabled_rw",
268 "false") == "true";
269 }
Dennis Shen1dc9ad42023-05-12 00:21:55 +0000270
Dennis Shen8d544f72023-06-29 00:45:42 +0000271 virtual bool enabled_ro() override {
Mårten Kongstad403658f2023-06-14 09:51:56 +0200272 return true;
273 }
Dennis Shen1dc9ad42023-05-12 00:21:55 +0000274
Dennis Shen8d544f72023-06-29 00:45:42 +0000275 virtual bool enabled_rw() override {
Dennis Shen5c242132023-07-14 14:57:08 +0000276 return server_configurable_flags::GetServerConfigurableFlag(
Mårten Kongstad403658f2023-06-14 09:51:56 +0200277 "aconfig_test",
278 "com.android.aconfig.test.enabled_rw",
279 "true") == "true";
280 }
Dennis Shen8d544f72023-06-29 00:45:42 +0000281};
Mårten Kongstad403658f2023-06-14 09:51:56 +0200282}
Mårten Kongstad403658f2023-06-14 09:51:56 +0200283"#;
Dennis Shen8d544f72023-06-29 00:45:42 +0000284
285 const TEST_FLAG_PROVIDER_HEADER_EXPECTED: &str = r#"
Dennis Shen5c242132023-07-14 14:57:08 +0000286#pragma once
Dennis Shen8d544f72023-06-29 00:45:42 +0000287
288#include "com_android_aconfig_test.h"
Dennis Shene3981442023-07-10 18:15:42 +0000289#include <server_configurable_flags/get_flags.h>
Dennis Shen8d544f72023-06-29 00:45:42 +0000290
291#include <unordered_map>
Dennis Shen5c242132023-07-14 14:57:08 +0000292#include <string>
Dennis Shen8d544f72023-06-29 00:45:42 +0000293
294namespace com::android::aconfig::test {
295class flag_provider : public flag_provider_interface {
296private:
297 std::unordered_map<std::string, bool> overrides_;
Dennis Shen8d544f72023-06-29 00:45:42 +0000298
299public:
300
301 flag_provider()
Dennis Shen17a08eec2023-07-14 01:32:50 +0000302 : overrides_()
303 {}
Dennis Shen8d544f72023-06-29 00:45:42 +0000304
305 virtual bool disabled_ro() override {
Dennis Shen17a08eec2023-07-14 01:32:50 +0000306 auto it = overrides_.find("disabled_ro");
Dennis Shen8d544f72023-06-29 00:45:42 +0000307 if (it != overrides_.end()) {
308 return it->second;
309 } else {
310 return false;
311 }
312 }
313
Dennis Shen17a08eec2023-07-14 01:32:50 +0000314 virtual void disabled_ro(bool val) override {
315 overrides_["disabled_ro"] = val;
316 }
317
Dennis Shen8d544f72023-06-29 00:45:42 +0000318 virtual bool disabled_rw() override {
Dennis Shen17a08eec2023-07-14 01:32:50 +0000319 auto it = overrides_.find("disabled_rw");
Dennis Shen8d544f72023-06-29 00:45:42 +0000320 if (it != overrides_.end()) {
321 return it->second;
322 } else {
Dennis Shen5c242132023-07-14 14:57:08 +0000323 return server_configurable_flags::GetServerConfigurableFlag(
Dennis Shen8d544f72023-06-29 00:45:42 +0000324 "aconfig_test",
325 "com.android.aconfig.test.disabled_rw",
326 "false") == "true";
327 }
328 }
329
Dennis Shen17a08eec2023-07-14 01:32:50 +0000330 virtual void disabled_rw(bool val) override {
331 overrides_["disabled_rw"] = val;
332 }
333
Dennis Shen8d544f72023-06-29 00:45:42 +0000334 virtual bool enabled_ro() override {
Dennis Shen17a08eec2023-07-14 01:32:50 +0000335 auto it = overrides_.find("enabled_ro");
Dennis Shen8d544f72023-06-29 00:45:42 +0000336 if (it != overrides_.end()) {
337 return it->second;
338 } else {
339 return true;
340 }
341 }
342
Dennis Shen17a08eec2023-07-14 01:32:50 +0000343 virtual void enabled_ro(bool val) override {
344 overrides_["enabled_ro"] = val;
345 }
346
Dennis Shen8d544f72023-06-29 00:45:42 +0000347 virtual bool enabled_rw() override {
Dennis Shen17a08eec2023-07-14 01:32:50 +0000348 auto it = overrides_.find("enabled_rw");
Dennis Shen8d544f72023-06-29 00:45:42 +0000349 if (it != overrides_.end()) {
350 return it->second;
351 } else {
Dennis Shen5c242132023-07-14 14:57:08 +0000352 return server_configurable_flags::GetServerConfigurableFlag(
Dennis Shen8d544f72023-06-29 00:45:42 +0000353 "aconfig_test",
354 "com.android.aconfig.test.enabled_rw",
355 "true") == "true";
356 }
357 }
358
Dennis Shen17a08eec2023-07-14 01:32:50 +0000359 virtual void enabled_rw(bool val) override {
360 overrides_["enabled_rw"] = val;
Dennis Shen8d544f72023-06-29 00:45:42 +0000361 }
362
Dennis Shen17a08eec2023-07-14 01:32:50 +0000363 virtual void reset_flags() override {
Dennis Shen8d544f72023-06-29 00:45:42 +0000364 overrides_.clear();
365 }
366};
367}
Dennis Shen8d544f72023-06-29 00:45:42 +0000368"#;
369
370 const SOURCE_FILE_EXPECTED: &str = r#"
371#include "com_android_aconfig_test.h"
372#include "com_android_aconfig_test_flag_provider.h"
373
374namespace com::android::aconfig::test {
Dennis Shen8d544f72023-06-29 00:45:42 +0000375 std::unique_ptr<flag_provider_interface> provider_ =
376 std::make_unique<flag_provider>();
377}
378"#;
379
Dennis Shen17a08eec2023-07-14 01:32:50 +0000380 const C_EXPORTED_PROD_HEADER_EXPECTED: &str = r#"
Dennis Shen5c242132023-07-14 14:57:08 +0000381#pragma once
Dennis Shen7321f4f2023-07-11 15:45:00 +0000382
383#ifdef __cplusplus
384extern "C" {
385#endif
386
Dennis Shen7321f4f2023-07-11 15:45:00 +0000387bool com_android_aconfig_test_disabled_ro();
388
389bool com_android_aconfig_test_disabled_rw();
390
391bool com_android_aconfig_test_enabled_ro();
392
393bool com_android_aconfig_test_enabled_rw();
394
Dennis Shen17a08eec2023-07-14 01:32:50 +0000395#ifdef __cplusplus
396}
397#endif
Dennis Shen17a08eec2023-07-14 01:32:50 +0000398"#;
Dennis Shen7321f4f2023-07-11 15:45:00 +0000399
Dennis Shen17a08eec2023-07-14 01:32:50 +0000400 const C_EXPORTED_TEST_HEADER_EXPECTED: &str = r#"
Dennis Shen5c242132023-07-14 14:57:08 +0000401#pragma once
Dennis Shen17a08eec2023-07-14 01:32:50 +0000402
403#ifdef __cplusplus
404extern "C" {
405#endif
406
407bool com_android_aconfig_test_disabled_ro();
408
409void set_com_android_aconfig_test_disabled_ro(bool val);
410
411bool com_android_aconfig_test_disabled_rw();
412
413void set_com_android_aconfig_test_disabled_rw(bool val);
414
415bool com_android_aconfig_test_enabled_ro();
416
417void set_com_android_aconfig_test_enabled_ro(bool val);
418
419bool com_android_aconfig_test_enabled_rw();
420
421void set_com_android_aconfig_test_enabled_rw(bool val);
422
423void com_android_aconfig_test_reset_flags();
Dennis Shen7321f4f2023-07-11 15:45:00 +0000424
425#ifdef __cplusplus
426}
427#endif
Dennis Shen7321f4f2023-07-11 15:45:00 +0000428"#;
429
Dennis Shen17a08eec2023-07-14 01:32:50 +0000430 const C_PROD_SOURCE_FILE_EXPECTED: &str = r#"
Dennis Shen7321f4f2023-07-11 15:45:00 +0000431#include "com_android_aconfig_test_c.h"
432#include "com_android_aconfig_test.h"
Dennis Shen7321f4f2023-07-11 15:45:00 +0000433
434bool com_android_aconfig_test_disabled_ro() {
Dennis Shen5c242132023-07-14 14:57:08 +0000435 return false;
Dennis Shen7321f4f2023-07-11 15:45:00 +0000436}
437
438bool com_android_aconfig_test_disabled_rw() {
439 return com::android::aconfig::test::disabled_rw();
440}
441
442bool com_android_aconfig_test_enabled_ro() {
Dennis Shen5c242132023-07-14 14:57:08 +0000443 return true;
Dennis Shen7321f4f2023-07-11 15:45:00 +0000444}
445
446bool com_android_aconfig_test_enabled_rw() {
447 return com::android::aconfig::test::enabled_rw();
448}
Dennis Shen17a08eec2023-07-14 01:32:50 +0000449"#;
Dennis Shen7321f4f2023-07-11 15:45:00 +0000450
Dennis Shen17a08eec2023-07-14 01:32:50 +0000451 const C_TEST_SOURCE_FILE_EXPECTED: &str = r#"
452#include "com_android_aconfig_test_c.h"
453#include "com_android_aconfig_test.h"
454
455bool com_android_aconfig_test_disabled_ro() {
456 return com::android::aconfig::test::disabled_ro();
Dennis Shen7321f4f2023-07-11 15:45:00 +0000457}
458
Dennis Shen17a08eec2023-07-14 01:32:50 +0000459void set_com_android_aconfig_test_disabled_ro(bool val) {
460 com::android::aconfig::test::disabled_ro(val);
461}
462
463bool com_android_aconfig_test_disabled_rw() {
464 return com::android::aconfig::test::disabled_rw();
465}
466
467void set_com_android_aconfig_test_disabled_rw(bool val) {
468 com::android::aconfig::test::disabled_rw(val);
469}
470
471bool com_android_aconfig_test_enabled_ro() {
472 return com::android::aconfig::test::enabled_ro();
473}
474
475void set_com_android_aconfig_test_enabled_ro(bool val) {
476 com::android::aconfig::test::enabled_ro(val);
477}
478
479bool com_android_aconfig_test_enabled_rw() {
480 return com::android::aconfig::test::enabled_rw();
481}
482
483void set_com_android_aconfig_test_enabled_rw(bool val) {
484 com::android::aconfig::test::enabled_rw(val);
485}
486
487void com_android_aconfig_test_reset_flags() {
488 com::android::aconfig::test::reset_flags();
Dennis Shen7321f4f2023-07-11 15:45:00 +0000489}
490"#;
Dennis Shen8d544f72023-06-29 00:45:42 +0000491 fn test_generate_cpp_code(mode: CodegenMode) {
492 let parsed_flags = crate::test::parse_test_flags();
493 let generated =
494 generate_cpp_code(crate::test::TEST_PACKAGE, parsed_flags.parsed_flag.iter(), mode)
495 .unwrap();
496 let mut generated_files_map = HashMap::new();
497 for file in generated {
498 generated_files_map.insert(
499 String::from(file.path.to_str().unwrap()),
500 String::from_utf8(file.contents.clone()).unwrap(),
501 );
502 }
503
504 let mut target_file_path = String::from("include/com_android_aconfig_test.h");
505 assert!(generated_files_map.contains_key(&target_file_path));
Dennis Shen1dc9ad42023-05-12 00:21:55 +0000506 assert_eq!(
Mårten Kongstadb0255072023-06-08 10:15:43 +0200507 None,
508 crate::test::first_significant_code_diff(
Dennis Shen8d544f72023-06-29 00:45:42 +0000509 match mode {
510 CodegenMode::Production => EXPORTED_PROD_HEADER_EXPECTED,
511 CodegenMode::Test => EXPORTED_TEST_HEADER_EXPECTED,
512 },
513 generated_files_map.get(&target_file_path).unwrap()
Mårten Kongstadb0255072023-06-08 10:15:43 +0200514 )
Dennis Shen1dc9ad42023-05-12 00:21:55 +0000515 );
Dennis Shen8d544f72023-06-29 00:45:42 +0000516
517 target_file_path = String::from("com_android_aconfig_test_flag_provider.h");
518 assert!(generated_files_map.contains_key(&target_file_path));
519 assert_eq!(
520 None,
521 crate::test::first_significant_code_diff(
522 match mode {
523 CodegenMode::Production => PROD_FLAG_PROVIDER_HEADER_EXPECTED,
524 CodegenMode::Test => TEST_FLAG_PROVIDER_HEADER_EXPECTED,
525 },
526 generated_files_map.get(&target_file_path).unwrap()
527 )
528 );
529
530 target_file_path = String::from("com_android_aconfig_test.cc");
531 assert!(generated_files_map.contains_key(&target_file_path));
532 assert_eq!(
533 None,
534 crate::test::first_significant_code_diff(
535 SOURCE_FILE_EXPECTED,
536 generated_files_map.get(&target_file_path).unwrap()
537 )
538 );
Dennis Shen7321f4f2023-07-11 15:45:00 +0000539
540 target_file_path = String::from("include/com_android_aconfig_test_c.h");
541 assert!(generated_files_map.contains_key(&target_file_path));
542 assert_eq!(
543 None,
544 crate::test::first_significant_code_diff(
Dennis Shen17a08eec2023-07-14 01:32:50 +0000545 match mode {
546 CodegenMode::Production => C_EXPORTED_PROD_HEADER_EXPECTED,
547 CodegenMode::Test => C_EXPORTED_TEST_HEADER_EXPECTED,
548 },
Dennis Shen7321f4f2023-07-11 15:45:00 +0000549 generated_files_map.get(&target_file_path).unwrap()
550 )
551 );
552
553 target_file_path = String::from("com_android_aconfig_test_c.cc");
554 assert!(generated_files_map.contains_key(&target_file_path));
555 assert_eq!(
556 None,
557 crate::test::first_significant_code_diff(
Dennis Shen17a08eec2023-07-14 01:32:50 +0000558 match mode {
559 CodegenMode::Production => C_PROD_SOURCE_FILE_EXPECTED,
560 CodegenMode::Test => C_TEST_SOURCE_FILE_EXPECTED,
561 },
Dennis Shen7321f4f2023-07-11 15:45:00 +0000562 generated_files_map.get(&target_file_path).unwrap()
563 )
564 );
Dennis Shen8d544f72023-06-29 00:45:42 +0000565 }
566
567 #[test]
568 fn test_generate_cpp_code_for_prod() {
569 test_generate_cpp_code(CodegenMode::Production);
570 }
571
572 #[test]
573 fn test_generate_cpp_code_for_test() {
574 test_generate_cpp_code(CodegenMode::Test);
Dennis Shen1dc9ad42023-05-12 00:21:55 +0000575 }
576}