Skip to content

Commit

Permalink
Generate Tests
Browse files Browse the repository at this point in the history
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
  • Loading branch information
rickhanlonii authored and facebook-github-bot committed Apr 17, 2019
1 parent 44fe990 commit bddd9c7
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 0 deletions.
30 changes: 30 additions & 0 deletions packages/react-native-codegen/DEFS.bzl
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
load("@fbsource//tools/build_defs:default_platform_defs.bzl", "IOS", "MACOSX")
load("@fbsource//tools/build_defs:fb_native_wrapper.bzl", "fb_native")
load("@fbsource//tools/build_defs/apple:flag_defs.bzl", "get_debug_preprocessor_flags")
load(
"//tools/build_defs/oss:rn_defs.bzl",
"ANDROID",
"APPLE",
"fb_xplat_cxx_test",
"get_apple_compiler_flags",
"get_apple_inspector_flags",
"react_native_xplat_target",
Expand Down Expand Up @@ -31,6 +33,7 @@ def rn_codegen(
generate_event_emitter_cpp_name = "generate_event_emitter_cpp-{}".format(name)
generate_event_emitter_h_name = "generate_event_emitter_h-{}".format(name)
generate_props_cpp_name = "generate_props_cpp-{}".format(name)
generate_tests_cpp_name = "generate_tests_cpp-{}".format(name)
generate_props_h_name = "generated_props_h-{}".format(name)
generate_shadow_node_cpp_name = "generated_shadow_node_cpp-{}".format(name)
generate_shadow_node_h_name = "generated_shadow_node_h-{}".format(name)
Expand Down Expand Up @@ -66,6 +69,12 @@ def rn_codegen(
out = "Props.cpp",
)

fb_native.genrule(
name = generate_tests_cpp_name,
cmd = "cp $(location :{})/Tests.cpp $OUT".format(generate_fixtures_rule_name),
out = "Tests.cpp",
)

fb_native.genrule(
name = generate_props_h_name,
cmd = "cp $(location :{})/Props.h $OUT".format(generate_fixtures_rule_name),
Expand All @@ -87,6 +96,7 @@ def rn_codegen(
# libs
rn_xplat_cxx_library(
name = "generated_components-{}".format(name),
tests = [":generated_tests-{}".format(name)],
srcs = [
":{}".format(generate_event_emitter_cpp_name),
":{}".format(generate_props_cpp_name),
Expand Down Expand Up @@ -134,3 +144,23 @@ def rn_codegen(
react_native_xplat_target("fabric/components/view:view"),
],
)

# Tests
fb_xplat_cxx_test(
name = "generated_tests-{}".format(name),
srcs = [
":{}".format(generate_tests_cpp_name),
],
compiler_flags = [
"-fexceptions",
"-frtti",
"-std=c++14",
"-Wall",
],
contacts = ["oncall+react_native@xmail.facebook.com"],
apple_sdks = (IOS, MACOSX),
platforms = (ANDROID, APPLE),
deps = [
"fbsource//xplat/third-party/gmock:gtest",
],
)
43 changes: 43 additions & 0 deletions packages/react-native-codegen/src/generators/GenerateTests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict
* @format
*/

'use strict';

import type {SchemaType} from '../CodegenSchema';

// File path -> contents
type FilesOutput = Map<string, string>;

const template = `
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include <gtest/gtest.h>
TEST(::_COMPONENT_NAME_::, etc) {
ASSERT_EQ(true, true);
}
`;

module.exports = {
generate(libraryName: string, schema: SchemaType): FilesOutput {
const fileName = 'Tests.cpp';

const replacedTemplate = template
.replace('::_COMPONENT_NAME_::', libraryName)
.trim();
return new Map([[fileName, replacedTemplate]]);
},
};
2 changes: 2 additions & 0 deletions packages/react-native-codegen/src/generators/RNCodegen.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const generateEventEmitterCpp = require('./GenerateEventEmitterCpp.js');
const generateEventEmitterH = require('./GenerateEventEmitterH.js');
const generatePropsCpp = require('./GeneratePropsCpp.js');
const generatePropsH = require('./GeneratePropsH.js');
const generateTests = require('./GenerateTests.js');
const generateShadowNodeCpp = require('./GenerateShadowNodeCpp.js');
const generateShadowNodeH = require('./GenerateShadowNodeH.js');
const generateViewConfigJs = require('./GenerateViewConfigJs.js');
Expand Down Expand Up @@ -53,6 +54,7 @@ module.exports = {
...generateEventEmitterH.generate(libraryName, schema),
...generatePropsCpp.generate(libraryName, schema),
...generatePropsH.generate(libraryName, schema),
...generateTests.generate(libraryName, schema),
...generateShadowNodeCpp.generate(libraryName, schema),
...generateShadowNodeH.generate(libraryName, schema),
...generateViewConfigJs.generate(libraryName, schema),
Expand Down

0 comments on commit bddd9c7

Please sign in to comment.