blob: ff9e8ef0bcb5860016605551a86e0b06d068486b [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#"
Mårten Kongstad403658f2023-06-14 09:51:56 +0200139#ifndef com_android_aconfig_test_HEADER_H
140#define com_android_aconfig_test_HEADER_H
Dennis Shen1dc9ad42023-05-12 00:21:55 +0000141
Dennis Shen8d544f72023-06-29 00:45:42 +0000142#include <string>
143#include <memory>
Dennis Shen1dc9ad42023-05-12 00:21:55 +0000144
Mårten Kongstad403658f2023-06-14 09:51:56 +0200145namespace com::android::aconfig::test {
Dennis Shen8d544f72023-06-29 00:45:42 +0000146class flag_provider_interface {
147public:
148
149 virtual ~flag_provider_interface() = default;
150
151 virtual bool disabled_ro() = 0;
152
153 virtual bool disabled_rw() = 0;
154
155 virtual bool enabled_ro() = 0;
156
157 virtual bool enabled_rw() = 0;
Dennis Shen8d544f72023-06-29 00:45:42 +0000158};
159
160extern std::unique_ptr<flag_provider_interface> provider_;
161
Dennis Shen8d544f72023-06-29 00:45:42 +0000162inline bool disabled_ro() {
163 return false;
164}
165
166inline bool disabled_rw() {
167 return provider_->disabled_rw();
168}
169
170inline bool enabled_ro() {
171 return true;
172}
173
174inline bool enabled_rw() {
175 return provider_->enabled_rw();
176}
177
Dennis Shen8d544f72023-06-29 00:45:42 +0000178}
179#endif
180"#;
181
182 const EXPORTED_TEST_HEADER_EXPECTED: &str = r#"
183#ifndef com_android_aconfig_test_HEADER_H
184#define com_android_aconfig_test_HEADER_H
185
186#include <string>
187#include <memory>
Dennis Shen8d544f72023-06-29 00:45:42 +0000188
189namespace com::android::aconfig::test {
190class flag_provider_interface {
191public:
192
193 virtual ~flag_provider_interface() = default;
194
195 virtual bool disabled_ro() = 0;
196
Dennis Shen17a08eec2023-07-14 01:32:50 +0000197 virtual void disabled_ro(bool val) = 0;
198
Dennis Shen8d544f72023-06-29 00:45:42 +0000199 virtual bool disabled_rw() = 0;
200
Dennis Shen17a08eec2023-07-14 01:32:50 +0000201 virtual void disabled_rw(bool val) = 0;
202
Dennis Shen8d544f72023-06-29 00:45:42 +0000203 virtual bool enabled_ro() = 0;
204
Dennis Shen17a08eec2023-07-14 01:32:50 +0000205 virtual void enabled_ro(bool val) = 0;
206
Dennis Shen8d544f72023-06-29 00:45:42 +0000207 virtual bool enabled_rw() = 0;
208
Dennis Shen17a08eec2023-07-14 01:32:50 +0000209 virtual void enabled_rw(bool val) = 0;
Dennis Shen8d544f72023-06-29 00:45:42 +0000210
Dennis Shen17a08eec2023-07-14 01:32:50 +0000211 virtual void reset_flags() {}
Dennis Shen8d544f72023-06-29 00:45:42 +0000212};
213
214extern std::unique_ptr<flag_provider_interface> provider_;
215
Dennis Shen8d544f72023-06-29 00:45:42 +0000216inline bool disabled_ro() {
217 return provider_->disabled_ro();
218}
219
Dennis Shen17a08eec2023-07-14 01:32:50 +0000220inline void disabled_ro(bool val) {
221 provider_->disabled_ro(val);
222}
223
Dennis Shen8d544f72023-06-29 00:45:42 +0000224inline bool disabled_rw() {
225 return provider_->disabled_rw();
226}
227
Dennis Shen17a08eec2023-07-14 01:32:50 +0000228inline void disabled_rw(bool val) {
229 provider_->disabled_rw(val);
230}
231
Dennis Shen8d544f72023-06-29 00:45:42 +0000232inline bool enabled_ro() {
233 return provider_->enabled_ro();
234}
235
Dennis Shen17a08eec2023-07-14 01:32:50 +0000236inline void enabled_ro(bool val) {
237 provider_->enabled_ro(val);
238}
239
Dennis Shen8d544f72023-06-29 00:45:42 +0000240inline bool enabled_rw() {
241 return provider_->enabled_rw();
242}
243
Dennis Shen17a08eec2023-07-14 01:32:50 +0000244inline void enabled_rw(bool val) {
245 provider_->enabled_rw(val);
Dennis Shen8d544f72023-06-29 00:45:42 +0000246}
247
Dennis Shen17a08eec2023-07-14 01:32:50 +0000248inline void reset_flags() {
249 return provider_->reset_flags();
Dennis Shen8d544f72023-06-29 00:45:42 +0000250}
251
252}
253#endif
254"#;
255
256 const PROD_FLAG_PROVIDER_HEADER_EXPECTED: &str = r#"
257#ifndef com_android_aconfig_test_flag_provider_HEADER_H
258#define com_android_aconfig_test_flag_provider_HEADER_H
259
260#include "com_android_aconfig_test.h"
Dennis Shene3981442023-07-10 18:15:42 +0000261#include <server_configurable_flags/get_flags.h>
262using namespace server_configurable_flags;
Dennis Shen8d544f72023-06-29 00:45:42 +0000263
264namespace com::android::aconfig::test {
265class flag_provider : public flag_provider_interface {
266public:
267
268 virtual bool disabled_ro() override {
Mårten Kongstad403658f2023-06-14 09:51:56 +0200269 return false;
Dennis Shen1dc9ad42023-05-12 00:21:55 +0000270 }
271
Dennis Shen8d544f72023-06-29 00:45:42 +0000272 virtual bool disabled_rw() override {
Mårten Kongstad403658f2023-06-14 09:51:56 +0200273 return GetServerConfigurableFlag(
274 "aconfig_test",
275 "com.android.aconfig.test.disabled_rw",
276 "false") == "true";
277 }
Dennis Shen1dc9ad42023-05-12 00:21:55 +0000278
Dennis Shen8d544f72023-06-29 00:45:42 +0000279 virtual bool enabled_ro() override {
Mårten Kongstad403658f2023-06-14 09:51:56 +0200280 return true;
281 }
Dennis Shen1dc9ad42023-05-12 00:21:55 +0000282
Dennis Shen8d544f72023-06-29 00:45:42 +0000283 virtual bool enabled_rw() override {
Mårten Kongstad403658f2023-06-14 09:51:56 +0200284 return GetServerConfigurableFlag(
285 "aconfig_test",
286 "com.android.aconfig.test.enabled_rw",
287 "true") == "true";
288 }
Dennis Shen8d544f72023-06-29 00:45:42 +0000289};
Mårten Kongstad403658f2023-06-14 09:51:56 +0200290}
291#endif
292"#;
Dennis Shen8d544f72023-06-29 00:45:42 +0000293
294 const TEST_FLAG_PROVIDER_HEADER_EXPECTED: &str = r#"
295#ifndef com_android_aconfig_test_flag_provider_HEADER_H
296#define com_android_aconfig_test_flag_provider_HEADER_H
297
298#include "com_android_aconfig_test.h"
Dennis Shene3981442023-07-10 18:15:42 +0000299#include <server_configurable_flags/get_flags.h>
300using namespace server_configurable_flags;
Dennis Shen8d544f72023-06-29 00:45:42 +0000301
302#include <unordered_map>
Dennis Shen8d544f72023-06-29 00:45:42 +0000303
304namespace com::android::aconfig::test {
305class flag_provider : public flag_provider_interface {
306private:
307 std::unordered_map<std::string, bool> overrides_;
Dennis Shen8d544f72023-06-29 00:45:42 +0000308
309public:
310
311 flag_provider()
Dennis Shen17a08eec2023-07-14 01:32:50 +0000312 : overrides_()
313 {}
Dennis Shen8d544f72023-06-29 00:45:42 +0000314
315 virtual bool disabled_ro() override {
Dennis Shen17a08eec2023-07-14 01:32:50 +0000316 auto it = overrides_.find("disabled_ro");
Dennis Shen8d544f72023-06-29 00:45:42 +0000317 if (it != overrides_.end()) {
318 return it->second;
319 } else {
320 return false;
321 }
322 }
323
Dennis Shen17a08eec2023-07-14 01:32:50 +0000324 virtual void disabled_ro(bool val) override {
325 overrides_["disabled_ro"] = val;
326 }
327
Dennis Shen8d544f72023-06-29 00:45:42 +0000328 virtual bool disabled_rw() override {
Dennis Shen17a08eec2023-07-14 01:32:50 +0000329 auto it = overrides_.find("disabled_rw");
Dennis Shen8d544f72023-06-29 00:45:42 +0000330 if (it != overrides_.end()) {
331 return it->second;
332 } else {
333 return GetServerConfigurableFlag(
334 "aconfig_test",
335 "com.android.aconfig.test.disabled_rw",
336 "false") == "true";
337 }
338 }
339
Dennis Shen17a08eec2023-07-14 01:32:50 +0000340 virtual void disabled_rw(bool val) override {
341 overrides_["disabled_rw"] = val;
342 }
343
Dennis Shen8d544f72023-06-29 00:45:42 +0000344 virtual bool enabled_ro() override {
Dennis Shen17a08eec2023-07-14 01:32:50 +0000345 auto it = overrides_.find("enabled_ro");
Dennis Shen8d544f72023-06-29 00:45:42 +0000346 if (it != overrides_.end()) {
347 return it->second;
348 } else {
349 return true;
350 }
351 }
352
Dennis Shen17a08eec2023-07-14 01:32:50 +0000353 virtual void enabled_ro(bool val) override {
354 overrides_["enabled_ro"] = val;
355 }
356
Dennis Shen8d544f72023-06-29 00:45:42 +0000357 virtual bool enabled_rw() override {
Dennis Shen17a08eec2023-07-14 01:32:50 +0000358 auto it = overrides_.find("enabled_rw");
Dennis Shen8d544f72023-06-29 00:45:42 +0000359 if (it != overrides_.end()) {
360 return it->second;
361 } else {
362 return GetServerConfigurableFlag(
363 "aconfig_test",
364 "com.android.aconfig.test.enabled_rw",
365 "true") == "true";
366 }
367 }
368
Dennis Shen17a08eec2023-07-14 01:32:50 +0000369 virtual void enabled_rw(bool val) override {
370 overrides_["enabled_rw"] = val;
Dennis Shen8d544f72023-06-29 00:45:42 +0000371 }
372
Dennis Shen17a08eec2023-07-14 01:32:50 +0000373 virtual void reset_flags() override {
Dennis Shen8d544f72023-06-29 00:45:42 +0000374 overrides_.clear();
375 }
376};
377}
378#endif
379"#;
380
381 const SOURCE_FILE_EXPECTED: &str = r#"
382#include "com_android_aconfig_test.h"
383#include "com_android_aconfig_test_flag_provider.h"
384
385namespace com::android::aconfig::test {
Dennis Shen8d544f72023-06-29 00:45:42 +0000386 std::unique_ptr<flag_provider_interface> provider_ =
387 std::make_unique<flag_provider>();
388}
389"#;
390
Dennis Shen17a08eec2023-07-14 01:32:50 +0000391 const C_EXPORTED_PROD_HEADER_EXPECTED: &str = r#"
Dennis Shen7321f4f2023-07-11 15:45:00 +0000392#ifndef com_android_aconfig_test_c_HEADER_H
393#define com_android_aconfig_test_c_HEADER_H
394
395#ifdef __cplusplus
396extern "C" {
397#endif
398
Dennis Shen7321f4f2023-07-11 15:45:00 +0000399bool com_android_aconfig_test_disabled_ro();
400
401bool com_android_aconfig_test_disabled_rw();
402
403bool com_android_aconfig_test_enabled_ro();
404
405bool com_android_aconfig_test_enabled_rw();
406
Dennis Shen17a08eec2023-07-14 01:32:50 +0000407#ifdef __cplusplus
408}
409#endif
410#endif
411"#;
Dennis Shen7321f4f2023-07-11 15:45:00 +0000412
Dennis Shen17a08eec2023-07-14 01:32:50 +0000413 const C_EXPORTED_TEST_HEADER_EXPECTED: &str = r#"
414#ifndef com_android_aconfig_test_c_HEADER_H
415#define com_android_aconfig_test_c_HEADER_H
416
417#ifdef __cplusplus
418extern "C" {
419#endif
420
421bool com_android_aconfig_test_disabled_ro();
422
423void set_com_android_aconfig_test_disabled_ro(bool val);
424
425bool com_android_aconfig_test_disabled_rw();
426
427void set_com_android_aconfig_test_disabled_rw(bool val);
428
429bool com_android_aconfig_test_enabled_ro();
430
431void set_com_android_aconfig_test_enabled_ro(bool val);
432
433bool com_android_aconfig_test_enabled_rw();
434
435void set_com_android_aconfig_test_enabled_rw(bool val);
436
437void com_android_aconfig_test_reset_flags();
Dennis Shen7321f4f2023-07-11 15:45:00 +0000438
439#ifdef __cplusplus
440}
441#endif
442#endif
443"#;
444
Dennis Shen17a08eec2023-07-14 01:32:50 +0000445 const C_PROD_SOURCE_FILE_EXPECTED: &str = r#"
Dennis Shen7321f4f2023-07-11 15:45:00 +0000446#include "com_android_aconfig_test_c.h"
447#include "com_android_aconfig_test.h"
Dennis Shen7321f4f2023-07-11 15:45:00 +0000448
449bool com_android_aconfig_test_disabled_ro() {
450 return com::android::aconfig::test::disabled_ro();
451}
452
453bool com_android_aconfig_test_disabled_rw() {
454 return com::android::aconfig::test::disabled_rw();
455}
456
457bool com_android_aconfig_test_enabled_ro() {
458 return com::android::aconfig::test::enabled_ro();
459}
460
461bool com_android_aconfig_test_enabled_rw() {
462 return com::android::aconfig::test::enabled_rw();
463}
Dennis Shen17a08eec2023-07-14 01:32:50 +0000464"#;
Dennis Shen7321f4f2023-07-11 15:45:00 +0000465
Dennis Shen17a08eec2023-07-14 01:32:50 +0000466 const C_TEST_SOURCE_FILE_EXPECTED: &str = r#"
467#include "com_android_aconfig_test_c.h"
468#include "com_android_aconfig_test.h"
469
470bool com_android_aconfig_test_disabled_ro() {
471 return com::android::aconfig::test::disabled_ro();
Dennis Shen7321f4f2023-07-11 15:45:00 +0000472}
473
Dennis Shen17a08eec2023-07-14 01:32:50 +0000474void set_com_android_aconfig_test_disabled_ro(bool val) {
475 com::android::aconfig::test::disabled_ro(val);
476}
477
478bool com_android_aconfig_test_disabled_rw() {
479 return com::android::aconfig::test::disabled_rw();
480}
481
482void set_com_android_aconfig_test_disabled_rw(bool val) {
483 com::android::aconfig::test::disabled_rw(val);
484}
485
486bool com_android_aconfig_test_enabled_ro() {
487 return com::android::aconfig::test::enabled_ro();
488}
489
490void set_com_android_aconfig_test_enabled_ro(bool val) {
491 com::android::aconfig::test::enabled_ro(val);
492}
493
494bool com_android_aconfig_test_enabled_rw() {
495 return com::android::aconfig::test::enabled_rw();
496}
497
498void set_com_android_aconfig_test_enabled_rw(bool val) {
499 com::android::aconfig::test::enabled_rw(val);
500}
501
502void com_android_aconfig_test_reset_flags() {
503 com::android::aconfig::test::reset_flags();
Dennis Shen7321f4f2023-07-11 15:45:00 +0000504}
505"#;
Dennis Shen8d544f72023-06-29 00:45:42 +0000506 fn test_generate_cpp_code(mode: CodegenMode) {
507 let parsed_flags = crate::test::parse_test_flags();
508 let generated =
509 generate_cpp_code(crate::test::TEST_PACKAGE, parsed_flags.parsed_flag.iter(), mode)
510 .unwrap();
511 let mut generated_files_map = HashMap::new();
512 for file in generated {
513 generated_files_map.insert(
514 String::from(file.path.to_str().unwrap()),
515 String::from_utf8(file.contents.clone()).unwrap(),
516 );
517 }
518
519 let mut target_file_path = String::from("include/com_android_aconfig_test.h");
520 assert!(generated_files_map.contains_key(&target_file_path));
Dennis Shen1dc9ad42023-05-12 00:21:55 +0000521 assert_eq!(
Mårten Kongstadb0255072023-06-08 10:15:43 +0200522 None,
523 crate::test::first_significant_code_diff(
Dennis Shen8d544f72023-06-29 00:45:42 +0000524 match mode {
525 CodegenMode::Production => EXPORTED_PROD_HEADER_EXPECTED,
526 CodegenMode::Test => EXPORTED_TEST_HEADER_EXPECTED,
527 },
528 generated_files_map.get(&target_file_path).unwrap()
Mårten Kongstadb0255072023-06-08 10:15:43 +0200529 )
Dennis Shen1dc9ad42023-05-12 00:21:55 +0000530 );
Dennis Shen8d544f72023-06-29 00:45:42 +0000531
532 target_file_path = String::from("com_android_aconfig_test_flag_provider.h");
533 assert!(generated_files_map.contains_key(&target_file_path));
534 assert_eq!(
535 None,
536 crate::test::first_significant_code_diff(
537 match mode {
538 CodegenMode::Production => PROD_FLAG_PROVIDER_HEADER_EXPECTED,
539 CodegenMode::Test => TEST_FLAG_PROVIDER_HEADER_EXPECTED,
540 },
541 generated_files_map.get(&target_file_path).unwrap()
542 )
543 );
544
545 target_file_path = String::from("com_android_aconfig_test.cc");
546 assert!(generated_files_map.contains_key(&target_file_path));
547 assert_eq!(
548 None,
549 crate::test::first_significant_code_diff(
550 SOURCE_FILE_EXPECTED,
551 generated_files_map.get(&target_file_path).unwrap()
552 )
553 );
Dennis Shen7321f4f2023-07-11 15:45:00 +0000554
555 target_file_path = String::from("include/com_android_aconfig_test_c.h");
556 assert!(generated_files_map.contains_key(&target_file_path));
557 assert_eq!(
558 None,
559 crate::test::first_significant_code_diff(
Dennis Shen17a08eec2023-07-14 01:32:50 +0000560 match mode {
561 CodegenMode::Production => C_EXPORTED_PROD_HEADER_EXPECTED,
562 CodegenMode::Test => C_EXPORTED_TEST_HEADER_EXPECTED,
563 },
Dennis Shen7321f4f2023-07-11 15:45:00 +0000564 generated_files_map.get(&target_file_path).unwrap()
565 )
566 );
567
568 target_file_path = String::from("com_android_aconfig_test_c.cc");
569 assert!(generated_files_map.contains_key(&target_file_path));
570 assert_eq!(
571 None,
572 crate::test::first_significant_code_diff(
Dennis Shen17a08eec2023-07-14 01:32:50 +0000573 match mode {
574 CodegenMode::Production => C_PROD_SOURCE_FILE_EXPECTED,
575 CodegenMode::Test => C_TEST_SOURCE_FILE_EXPECTED,
576 },
Dennis Shen7321f4f2023-07-11 15:45:00 +0000577 generated_files_map.get(&target_file_path).unwrap()
578 )
579 );
Dennis Shen8d544f72023-06-29 00:45:42 +0000580 }
581
582 #[test]
583 fn test_generate_cpp_code_for_prod() {
584 test_generate_cpp_code(CodegenMode::Production);
585 }
586
587 #[test]
588 fn test_generate_cpp_code_for_test() {
589 test_generate_cpp_code(CodegenMode::Test);
Dennis Shen1dc9ad42023-05-12 00:21:55 +0000590 }
591}