Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
213 changes: 211 additions & 2 deletions compiler/back_end/cpp/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@

load("@rules_python//python:py_binary.bzl", "py_binary")
load("@rules_python//python:py_library.bzl", "py_library")
load("@rules_python//python:py_test.bzl", "py_test")
load(":build_defs.bzl", "emboss_cc_test")
load(":build_defs.bzl", "cpp_golden_test", "emboss_cc_test")

package(
default_visibility = [
Expand Down Expand Up @@ -388,3 +387,213 @@ emboss_cc_test(
"@com_google_googletest//:gtest_main",
],
)

# New golden test infrastructure
py_library(
name = "one_golden_test_lib",
srcs = ["one_golden_test.py"],
)

py_binary(
name = "run_one_golden_test",
srcs = ["run_one_golden_test.py"],
deps = [":one_golden_test_lib"],
)

cpp_golden_test(
name = "anonymous_bits_golden_test",
emb_file = "//testdata:anonymous_bits.emb",
golden_file = "//testdata/golden_cpp:anonymous_bits.emb.h",
)

cpp_golden_test(
name = "bits_golden_test",
emb_file = "//testdata:bits.emb",
golden_file = "//testdata/golden_cpp:bits.emb.h",
)

cpp_golden_test(
name = "absolute_cpp_namespace_golden_test",
emb_file = "//testdata:absolute_cpp_namespace.emb",
golden_file = "//testdata/golden_cpp:absolute_cpp_namespace.emb.h",
)

cpp_golden_test(
name = "alignments_golden_test",
emb_file = "//testdata:alignments.emb",
golden_file = "//testdata/golden_cpp:alignments.emb.h",
)

cpp_golden_test(
name = "auto_array_size_golden_test",
emb_file = "//testdata:auto_array_size.emb",
golden_file = "//testdata/golden_cpp:auto_array_size.emb.h",
)

cpp_golden_test(
name = "bcd_golden_test",
emb_file = "//testdata:bcd.emb",
golden_file = "//testdata/golden_cpp:bcd.emb.h",
)

cpp_golden_test(
name = "complex_offset_golden_test",
emb_file = "//testdata:complex_offset.emb",
golden_file = "//testdata/golden_cpp:complex_offset.emb.h",
)

cpp_golden_test(
name = "complex_structure_golden_test",
emb_file = "//testdata:complex_structure.emb",
golden_file = "//testdata/golden_cpp:complex_structure.emb.h",
)

cpp_golden_test(
name = "condition_golden_test",
emb_file = "//testdata:condition.emb",
golden_file = "//testdata/golden_cpp:condition.emb.h",
)

cpp_golden_test(
name = "cpp_namespace_golden_test",
emb_file = "//testdata:cpp_namespace.emb",
golden_file = "//testdata/golden_cpp:cpp_namespace.emb.h",
)

cpp_golden_test(
name = "dynamic_size_golden_test",
emb_file = "//testdata:dynamic_size.emb",
golden_file = "//testdata/golden_cpp:dynamic_size.emb.h",
)

cpp_golden_test(
name = "enum_case_golden_test",
emb_file = "//testdata:enum_case.emb",
golden_file = "//testdata/golden_cpp:enum_case.emb.h",
)

cpp_golden_test(
name = "enum_golden_test",
emb_file = "//testdata:enum.emb",
golden_file = "//testdata/golden_cpp:enum.emb.h",
)

cpp_golden_test(
name = "explicit_sizes_golden_test",
emb_file = "//testdata:explicit_sizes.emb",
golden_file = "//testdata/golden_cpp:explicit_sizes.emb.h",
)

cpp_golden_test(
name = "float_golden_test",
emb_file = "//testdata:float.emb",
golden_file = "//testdata/golden_cpp:float.emb.h",
)

cpp_golden_test(
name = "imported_golden_test",
emb_file = "//testdata:imported.emb",
golden_file = "//testdata/golden_cpp:imported.emb.h",
)

cpp_golden_test(
name = "inline_type_golden_test",
emb_file = "//testdata:inline_type.emb",
golden_file = "//testdata/golden_cpp:inline_type.emb.h",
)

cpp_golden_test(
name = "int_sizes_golden_test",
emb_file = "//testdata:int_sizes.emb",
golden_file = "//testdata/golden_cpp:int_sizes.emb.h",
)

cpp_golden_test(
name = "large_array_golden_test",
emb_file = "//testdata:large_array.emb",
golden_file = "//testdata/golden_cpp:large_array.emb.h",
)

cpp_golden_test(
name = "nested_structure_golden_test",
emb_file = "//testdata:nested_structure.emb",
golden_file = "//testdata/golden_cpp:nested_structure.emb.h",
)

cpp_golden_test(
name = "next_keyword_golden_test",
emb_file = "//testdata:next_keyword.emb",
golden_file = "//testdata/golden_cpp:next_keyword.emb.h",
)

cpp_golden_test(
name = "no_cpp_namespace_golden_test",
emb_file = "//testdata:no_cpp_namespace.emb",
golden_file = "//testdata/golden_cpp:no_cpp_namespace.emb.h",
)

cpp_golden_test(
name = "no_enum_traits_golden_test",
emb_file = "//testdata:no_enum_traits.emb",
golden_file = "//testdata/golden_cpp:no_enum_traits.emb.h",
)

cpp_golden_test(
name = "parameters_golden_test",
emb_file = "//testdata:parameters.emb",
golden_file = "//testdata/golden_cpp:parameters.emb.h",
)

cpp_golden_test(
name = "requires_golden_test",
emb_file = "//testdata:requires.emb",
golden_file = "//testdata/golden_cpp:requires.emb.h",
)

cpp_golden_test(
name = "start_size_range_golden_test",
emb_file = "//testdata:start_size_range.emb",
golden_file = "//testdata/golden_cpp:start_size_range.emb.h",
)

cpp_golden_test(
name = "subtypes_golden_test",
emb_file = "//testdata:subtypes.emb",
golden_file = "//testdata/golden_cpp:subtypes.emb.h",
)

cpp_golden_test(
name = "text_format_golden_test",
emb_file = "//testdata:text_format.emb",
golden_file = "//testdata/golden_cpp:text_format.emb.h",
)

cpp_golden_test(
name = "uint_sizes_golden_test",
emb_file = "//testdata:uint_sizes.emb",
golden_file = "//testdata/golden_cpp:uint_sizes.emb.h",
)

cpp_golden_test(
name = "virtual_field_golden_test",
emb_file = "//testdata:virtual_field.emb",
golden_file = "//testdata/golden_cpp:virtual_field.emb.h",
)

cpp_golden_test(
name = "importer_golden_test",
emb_file = "//testdata:importer.emb",
golden_file = "//testdata/golden_cpp:importer.emb.h",
)

cpp_golden_test(
name = "importer2_golden_test",
emb_file = "//testdata:importer2.emb",
golden_file = "//testdata/golden_cpp:importer2.emb.h",
)

cpp_golden_test(
name = "imported_genfiles_golden_test",
emb_file = "//testdata:imported_genfiles.emb",
golden_file = "//testdata/golden_cpp:imported_genfiles.emb.h",
)
32 changes: 32 additions & 0 deletions compiler/back_end/cpp/build_defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
# vim:set ft=blazebuild:
"""Rule to generate cc_tests with and without system-specific optimizations."""

load("@rules_python//python:py_test.bzl", "py_test")

def emboss_cc_test(name, copts = None, no_w_sign_compare = False, **kwargs):
"""Generates cc_test rules with and without -DEMBOSS_NO_OPTIMIZATIONS."""
native.cc_test(
Expand Down Expand Up @@ -43,3 +45,33 @@ def emboss_cc_test(name, copts = None, no_w_sign_compare = False, **kwargs):
] + ([] if no_w_sign_compare else ["-Wsign-compare"]) + (copts or []),
**kwargs
)

def cpp_golden_test(name, emb_file, golden_file, import_dirs = []):
"""Defines a C++ golden file test.

Args:
name: The name of the test.
emb_file: The .emb file to test.
golden_file: The golden .h file.
import_dirs: A list of import directories.
"""
py_test(
name = name,
main = ":run_one_golden_test.py",
srcs = [":run_one_golden_test.py", ":one_golden_test.py"],
tags = ["golden"],
args = [
"$(location //compiler/front_end:emboss_front_end)",
"$(location :emboss_codegen_cpp)",
"$(location %s)" % emb_file,
"$(location %s)" % golden_file,
] + ["--import-dir=" + d for d in import_dirs],
data = [
"//compiler/front_end:emboss_front_end",
":emboss_codegen_cpp",
emb_file,
golden_file,
"//testdata:test_embs",
] + import_dirs,
deps = [":one_golden_test_lib"],
)
95 changes: 95 additions & 0 deletions compiler/back_end/cpp/one_golden_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
# Copyright 2025 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import difflib
import os
import subprocess
import sys
import unittest


class OneGoldenTest(unittest.TestCase):
def __init__(
self,
emboss_front_end,
emboss_compiler,
emb_file,
golden_file,
include_dirs=None,
):
super(OneGoldenTest, self).__init__("test_golden_file")
self.emboss_front_end = emboss_front_end
self.emboss_compiler = emboss_compiler
self.emb_file = emb_file
self.golden_file = golden_file
self.include_dirs = include_dirs if include_dirs is not None else []

def test_golden_file(self):
temp_dir = os.environ.get("TEST_TMPDIR", "")
ir_path = os.path.join(temp_dir, "ir.json")
output_path = os.path.join(temp_dir, os.path.basename(self.golden_file))

front_end_args = [
self.emboss_front_end,
self.emb_file,
"--output-file",
ir_path,
]
for include_dir in self.include_dirs:
front_end_args.extend(["--import-dir", include_dir])

process = subprocess.run(front_end_args, capture_output=True, text=True)
self.assertEqual(
process.returncode, 0, f"Front end failed with error:\n{process.stderr}"
)

compiler_args = [
self.emboss_compiler,
"--input-file",
ir_path,
"--output-file",
output_path,
]

process = subprocess.run(compiler_args, capture_output=True, text=True)

self.assertEqual(
process.returncode, 0, f"Compiler failed with error:\n{process.stderr}"
)

with open(output_path, "r") as f:
generated_contents = f.read()

with open(self.golden_file, "r") as f:
golden_contents = f.read()

self.assertMultiLineEqual(
golden_contents,
generated_contents,
msg="Generated file does not match golden file. Diff:\n"
+ "".join(
difflib.unified_diff(
golden_contents.splitlines(keepends=True),
generated_contents.splitlines(keepends=True),
fromfile=self.golden_file,
tofile="Generated C++ Header",
)
),
)


if __name__ == "__main__":
# This script is not intended to be run directly.
# It should be invoked by a test runner.
pass
Loading
Loading