Skip to content

Commit bddd9c7

Browse files
rickhanloniifacebook-github-bot
authored andcommitted
Generate Tests
Summary: To ensure greater type safety, we want to generate some cpp tests for the fromRawValue conversions This diff adds support to generate Tests.cpp along with the `buck test` targets itegrated into the rn_codegen rule automatically. The tests just `assert(true, true)` as a starting point Reviewed By: fkgozali Differential Revision: D14739493 fbshipit-source-id: fc9dea64ea31e6af7d997aebc54cfd459d48bf4f
1 parent 44fe990 commit bddd9c7

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-0
lines changed

Diff for: packages/react-native-codegen/DEFS.bzl

+30
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
load("@fbsource//tools/build_defs:default_platform_defs.bzl", "IOS", "MACOSX")
12
load("@fbsource//tools/build_defs:fb_native_wrapper.bzl", "fb_native")
23
load("@fbsource//tools/build_defs/apple:flag_defs.bzl", "get_debug_preprocessor_flags")
34
load(
45
"//tools/build_defs/oss:rn_defs.bzl",
56
"ANDROID",
67
"APPLE",
8+
"fb_xplat_cxx_test",
79
"get_apple_compiler_flags",
810
"get_apple_inspector_flags",
911
"react_native_xplat_target",
@@ -31,6 +33,7 @@ def rn_codegen(
3133
generate_event_emitter_cpp_name = "generate_event_emitter_cpp-{}".format(name)
3234
generate_event_emitter_h_name = "generate_event_emitter_h-{}".format(name)
3335
generate_props_cpp_name = "generate_props_cpp-{}".format(name)
36+
generate_tests_cpp_name = "generate_tests_cpp-{}".format(name)
3437
generate_props_h_name = "generated_props_h-{}".format(name)
3538
generate_shadow_node_cpp_name = "generated_shadow_node_cpp-{}".format(name)
3639
generate_shadow_node_h_name = "generated_shadow_node_h-{}".format(name)
@@ -66,6 +69,12 @@ def rn_codegen(
6669
out = "Props.cpp",
6770
)
6871

72+
fb_native.genrule(
73+
name = generate_tests_cpp_name,
74+
cmd = "cp $(location :{})/Tests.cpp $OUT".format(generate_fixtures_rule_name),
75+
out = "Tests.cpp",
76+
)
77+
6978
fb_native.genrule(
7079
name = generate_props_h_name,
7180
cmd = "cp $(location :{})/Props.h $OUT".format(generate_fixtures_rule_name),
@@ -87,6 +96,7 @@ def rn_codegen(
8796
# libs
8897
rn_xplat_cxx_library(
8998
name = "generated_components-{}".format(name),
99+
tests = [":generated_tests-{}".format(name)],
90100
srcs = [
91101
":{}".format(generate_event_emitter_cpp_name),
92102
":{}".format(generate_props_cpp_name),
@@ -134,3 +144,23 @@ def rn_codegen(
134144
react_native_xplat_target("fabric/components/view:view"),
135145
],
136146
)
147+
148+
# Tests
149+
fb_xplat_cxx_test(
150+
name = "generated_tests-{}".format(name),
151+
srcs = [
152+
":{}".format(generate_tests_cpp_name),
153+
],
154+
compiler_flags = [
155+
"-fexceptions",
156+
"-frtti",
157+
"-std=c++14",
158+
"-Wall",
159+
],
160+
contacts = ["[email protected]"],
161+
apple_sdks = (IOS, MACOSX),
162+
platforms = (ANDROID, APPLE),
163+
deps = [
164+
"fbsource//xplat/third-party/gmock:gtest",
165+
],
166+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow strict
8+
* @format
9+
*/
10+
11+
'use strict';
12+
13+
import type {SchemaType} from '../CodegenSchema';
14+
15+
// File path -> contents
16+
type FilesOutput = Map<string, string>;
17+
18+
const template = `
19+
/**
20+
* Copyright (c) Facebook, Inc. and its affiliates.
21+
*
22+
* This source code is licensed under the MIT license found in the
23+
* LICENSE file in the root directory of this source tree.
24+
*/
25+
26+
#include <gtest/gtest.h>
27+
28+
TEST(::_COMPONENT_NAME_::, etc) {
29+
30+
ASSERT_EQ(true, true);
31+
}
32+
`;
33+
34+
module.exports = {
35+
generate(libraryName: string, schema: SchemaType): FilesOutput {
36+
const fileName = 'Tests.cpp';
37+
38+
const replacedTemplate = template
39+
.replace('::_COMPONENT_NAME_::', libraryName)
40+
.trim();
41+
return new Map([[fileName, replacedTemplate]]);
42+
},
43+
};

Diff for: packages/react-native-codegen/src/generators/RNCodegen.js

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ const generateEventEmitterCpp = require('./GenerateEventEmitterCpp.js');
2222
const generateEventEmitterH = require('./GenerateEventEmitterH.js');
2323
const generatePropsCpp = require('./GeneratePropsCpp.js');
2424
const generatePropsH = require('./GeneratePropsH.js');
25+
const generateTests = require('./GenerateTests.js');
2526
const generateShadowNodeCpp = require('./GenerateShadowNodeCpp.js');
2627
const generateShadowNodeH = require('./GenerateShadowNodeH.js');
2728
const generateViewConfigJs = require('./GenerateViewConfigJs.js');
@@ -53,6 +54,7 @@ module.exports = {
5354
...generateEventEmitterH.generate(libraryName, schema),
5455
...generatePropsCpp.generate(libraryName, schema),
5556
...generatePropsH.generate(libraryName, schema),
57+
...generateTests.generate(libraryName, schema),
5658
...generateShadowNodeCpp.generate(libraryName, schema),
5759
...generateShadowNodeH.generate(libraryName, schema),
5860
...generateViewConfigJs.generate(libraryName, schema),

0 commit comments

Comments
 (0)