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
This commit is contained in:
Rick Hanlon
2019-04-17 07:21:26 -07:00
committed by Facebook Github Bot
parent 44fe9904ac
commit bddd9c7d59
3 changed files with 75 additions and 0 deletions

View File

@@ -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",
],
)

View File

@@ -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]]);
},
};

View File

@@ -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),