Skip to content

Commit

Permalink
Migrate SymbolicMacroTest.java to use text blocks
Browse files Browse the repository at this point in the history
The test cases in this file are updated to use the text block feature available now that we've upgraded to JDK 21.

- Removed the empty "//" comments that were used as hints to the autoformatter to not mush all string arguments onto the same line.
- Replaced single quotes with double, since that's the preferred style in actual Starlark code. Single quoting was only used for readability to avoid escaping, which is no longer necessary.

In general, we shouldn't blindly migrate all tests to text blocks, since that obscures blame history. But in this case, the history of this file is relatively short and we're actively working on adding new test cases, so it seemed like a good tradeoff.

Work toward #19922.

PiperOrigin-RevId: 617153701
Change-Id: I1ecc70ecc47deb2386b2faf0ffd6d02621087e2f
  • Loading branch information
brandjon authored and copybara-github committed Mar 19, 2024
1 parent 488b630 commit 25f0365
Showing 1 changed file with 86 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,18 @@ private void assertPackageNotInError(@Nullable Package pkg) {
@Test
public void implementationIsInvokedWithNameParam() throws Exception {
scratch.file(
"pkg/foo.bzl", //
"def _impl(name):",
" print('my_macro called with name = %s' % name)",
"my_macro = macro(implementation=_impl)");
"pkg/foo.bzl",
"""
def _impl(name):
print("my_macro called with name = %s" % name)
my_macro = macro(implementation=_impl)
""");
scratch.file(
"pkg/BUILD", //
"load(':foo.bzl', 'my_macro')",
"my_macro(name='abc')");
"pkg/BUILD",
"""
load(":foo.bzl", "my_macro")
my_macro(name="abc")
""");

Package pkg = getPackage("pkg");
assertPackageNotInError(pkg);
Expand All @@ -73,14 +77,18 @@ public void implementationIsInvokedWithNameParam() throws Exception {
@Test
public void implementationFailsDueToBadSignature() throws Exception {
scratch.file(
"pkg/foo.bzl", //
"def _impl():",
" pass",
"my_macro = macro(implementation=_impl)");
"pkg/foo.bzl",
"""
def _impl():
pass
my_macro = macro(implementation=_impl)
""");
scratch.file(
"pkg/BUILD", //
"load(':foo.bzl', 'my_macro')",
"my_macro(name='abc')");
"pkg/BUILD",
"""
load(":foo.bzl", "my_macro")
my_macro(name="abc")
""");

reporter.removeHandler(failFastHandler);
Package pkg = getPackage("pkg");
Expand All @@ -92,14 +100,18 @@ public void implementationFailsDueToBadSignature() throws Exception {
@Test
public void macroCanDeclareTargets() throws Exception {
scratch.file(
"pkg/foo.bzl", //
"def _impl(name):",
" native.cc_library(name = name + '$lib')",
"my_macro = macro(implementation=_impl)");
"pkg/foo.bzl",
"""
def _impl(name):
native.cc_library(name = name + "$lib")
my_macro = macro(implementation=_impl)
""");
scratch.file(
"pkg/BUILD", //
"load(':foo.bzl', 'my_macro')",
"my_macro(name='abc')");
"pkg/BUILD",
"""
load(":foo.bzl", "my_macro")
my_macro(name="abc")
""");

Package pkg = getPackage("pkg");
assertPackageNotInError(pkg);
Expand All @@ -109,17 +121,21 @@ public void macroCanDeclareTargets() throws Exception {
@Test
public void macroCanDeclareSubmacros() throws Exception {
scratch.file(
"pkg/foo.bzl", //
"def _inner_impl(name):",
" native.cc_library(name = name + '$lib')",
"inner_macro = macro(implementation=_inner_impl)",
"def _impl(name):",
" inner_macro(name = name + '$inner')",
"my_macro = macro(implementation=_impl)");
"pkg/foo.bzl",
"""
def _inner_impl(name):
native.cc_library(name = name + "$lib")
inner_macro = macro(implementation=_inner_impl)
def _impl(name):
inner_macro(name = name + "$inner")
my_macro = macro(implementation=_impl)
""");
scratch.file(
"pkg/BUILD", //
"load(':foo.bzl', 'my_macro')",
"my_macro(name='abc')");
"pkg/BUILD",
"""
load(":foo.bzl", "my_macro")
my_macro(name="abc")
""");

Package pkg = getPackage("pkg");
assertPackageNotInError(pkg);
Expand All @@ -131,14 +147,18 @@ public void macroCanDeclareSubmacros() throws Exception {
public void macroCanCallGlob() throws Exception {
scratch.file("pkg/foo.txt");
scratch.file(
"pkg/foo.bzl", //
"def _impl(name):",
" print('Glob result: %s' % native.glob(['foo*']))",
"my_macro = macro(implementation=_impl)");
"pkg/foo.bzl",
"""
def _impl(name):
print("Glob result: %s" % native.glob(["foo*"]))
my_macro = macro(implementation=_impl)
""");
scratch.file(
"pkg/BUILD", //
"load(':foo.bzl', 'my_macro')",
"my_macro(name='abc')");
"pkg/BUILD",
"""
load(":foo.bzl", "my_macro")
my_macro(name="abc")
""");

Package pkg = getPackage("pkg");
assertPackageNotInError(pkg);
Expand All @@ -149,16 +169,20 @@ public void macroCanCallGlob() throws Exception {
@Test
public void macroCanCallExistingRules() throws Exception {
scratch.file(
"pkg/foo.bzl", //
"def _impl(name):",
" native.cc_binary(name = name + '$lib')",
" print('existing_rules() keys: %s' % native.existing_rules().keys())",
"my_macro = macro(implementation=_impl)");
"pkg/foo.bzl",
"""
def _impl(name):
native.cc_binary(name = name + "$lib")
print("existing_rules() keys: %s" % native.existing_rules().keys())
my_macro = macro(implementation=_impl)
""");
scratch.file(
"pkg/BUILD", //
"load(':foo.bzl', 'my_macro')",
"cc_library(name = 'outer_target')",
"my_macro(name='abc')");
"pkg/BUILD",
"""
load(":foo.bzl", "my_macro")
cc_library(name = "outer_target")
my_macro(name="abc")
""");

Package pkg = getPackage("pkg");
assertPackageNotInError(pkg);
Expand All @@ -170,18 +194,22 @@ public void macroCanCallExistingRules() throws Exception {
@Test
public void macroDeclaredTargetsAreVisibleToExistingRules() throws Exception {
scratch.file(
"pkg/foo.bzl", //
"def _impl(name):",
" native.cc_binary(name = name + '$lib')",
"my_macro = macro(implementation=_impl)",
"def query():",
" print('existing_rules() keys: %s' % native.existing_rules().keys())");
"pkg/foo.bzl",
"""
def _impl(name):
native.cc_binary(name = name + "$lib")
my_macro = macro(implementation=_impl)
def query():
print("existing_rules() keys: %s" % native.existing_rules().keys())
""");
scratch.file(
"pkg/BUILD", //
"load(':foo.bzl', 'my_macro', 'query')",
"cc_library(name = 'outer_target')",
"my_macro(name='abc')",
"query()");
"pkg/BUILD",
"""
load(":foo.bzl", "my_macro", "query")
cc_library(name = "outer_target")
my_macro(name="abc")
query()
""");

Package pkg = getPackage("pkg");
assertPackageNotInError(pkg);
Expand Down

0 comments on commit 25f0365

Please sign in to comment.