From bddd9c7d5977b235255af066b18ceade44904920 Mon Sep 17 00:00:00 2001 From: Rick Hanlon Date: Wed, 17 Apr 2019 07:21:26 -0700 Subject: [PATCH] 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 --- packages/react-native-codegen/DEFS.bzl | 30 +++++++++++++ .../src/generators/GenerateTests.js | 43 +++++++++++++++++++ .../src/generators/RNCodegen.js | 2 + 3 files changed, 75 insertions(+) create mode 100644 packages/react-native-codegen/src/generators/GenerateTests.js diff --git a/packages/react-native-codegen/DEFS.bzl b/packages/react-native-codegen/DEFS.bzl index 8f3ad2e3476c7b..edca699d92ae51 100644 --- a/packages/react-native-codegen/DEFS.bzl +++ b/packages/react-native-codegen/DEFS.bzl @@ -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", @@ -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) @@ -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), @@ -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), @@ -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", + ], + ) diff --git a/packages/react-native-codegen/src/generators/GenerateTests.js b/packages/react-native-codegen/src/generators/GenerateTests.js new file mode 100644 index 00000000000000..e6abf42d114d46 --- /dev/null +++ b/packages/react-native-codegen/src/generators/GenerateTests.js @@ -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; + +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 + +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]]); + }, +}; diff --git a/packages/react-native-codegen/src/generators/RNCodegen.js b/packages/react-native-codegen/src/generators/RNCodegen.js index 55a198a8b087d3..b1cd1953070bdf 100644 --- a/packages/react-native-codegen/src/generators/RNCodegen.js +++ b/packages/react-native-codegen/src/generators/RNCodegen.js @@ -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'); @@ -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),