Skip to content

Commit

Permalink
Migrate string formatting for symbolic macro tests
Browse files Browse the repository at this point in the history
This is a follow-up to 25f0365, for symbolic macro tests in StarlarkRuleClassFunctionsTest.java.

Work toward #19922.

PiperOrigin-RevId: 619567559
Change-Id: Ifaab484fae81076324ff8b4bad8ca7538d7f66ad
  • Loading branch information
brandjon authored and copybara-github committed Mar 27, 2024
1 parent c576d5d commit 35c88de
Showing 1 changed file with 84 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -231,13 +231,17 @@ public void testSymbolicMacro_failsWithoutFlag() throws Exception {
setBuildLanguageOptions("--experimental_enable_first_class_macros=false");

scratch.file(
"pkg/foo.bzl", //
"def _impl(name):",
" pass",
"my_macro = macro(implementation=_impl)");
"pkg/foo.bzl",
"""
def _impl(name):
pass
my_macro = macro(implementation=_impl)
""");
scratch.file(
"pkg/BUILD", //
"load(':foo.bzl', 'my_macro')");
"pkg/BUILD",
"""
load(":foo.bzl", "my_macro")
""");

reporter.removeHandler(failFastHandler);
Package pkg = getPackage("pkg");
Expand All @@ -250,16 +254,20 @@ public void testSymbolicMacro_instantiationRegistersOnPackage() throws Exception
setBuildLanguageOptions("--experimental_enable_first_class_macros");

scratch.file(
"pkg/foo.bzl", //
"def _impl(name):",
" pass",
"my_macro = macro(implementation=_impl)");
scratch.file(
"pkg/BUILD", //
"load(':foo.bzl', 'my_macro')",
"my_macro(name='ghi')", // alphabetized when read back
"my_macro(name='abc')",
"my_macro(name='def')");
"pkg/foo.bzl",
"""
def _impl(name):
pass
my_macro = macro(implementation=_impl)
""");
scratch.file(
"pkg/BUILD",
"""
load(":foo.bzl", "my_macro")
my_macro(name="ghi") # alphabetized when read back
my_macro(name="abc")
my_macro(name="def")
""");

Package pkg = getPackage("pkg");
assertThat(pkg.getMacros().keySet()).containsExactly("abc", "def", "ghi").inOrder();
Expand All @@ -271,14 +279,18 @@ public void testSymbolicMacro_instantiationRequiresExport() throws Exception {
setBuildLanguageOptions("--experimental_enable_first_class_macros");

scratch.file(
"pkg/foo.bzl", //
"def _impl(name):",
" pass",
"s = struct(m = macro(implementation=_impl))");
scratch.file(
"pkg/BUILD", //
"load(':foo.bzl', 's')",
"s.m(name='abc')");
"pkg/foo.bzl",
"""
def _impl(name):
pass
s = struct(m = macro(implementation=_impl))
""");
scratch.file(
"pkg/BUILD",
"""
load(":foo.bzl", "s")
s.m(name="abc")
""");

reporter.removeHandler(failFastHandler);
Package pkg = getPackage("pkg");
Expand All @@ -293,19 +305,23 @@ public void testSymbolicMacro_cannotInstantiateInBzlThread() throws Exception {

scratch.file(
"pkg/foo.bzl",
"def _impl(name):",
" pass",
"my_macro = macro(implementation=_impl)",
"",
// Calling it from a function during .bzl load time is a little more interesting than
// calling it directly at the top level, since it forces us to check thread state rather
// than call stack state.
"def some_func():",
" my_macro(name='nope')",
"some_func()");
scratch.file(
"pkg/BUILD", //
"load(':foo.bzl', 'my_macro')");
"""
def _impl(name):
pass
my_macro = macro(implementation=_impl)
# Calling it from a function during .bzl load time is a little more interesting than
# calling it directly at the top level, since it forces us to check thread state rather
# than call stack state.
def some_func():
my_macro(name="nope")
some_func()
""");
scratch.file(
"pkg/BUILD",
"""
load(":foo.bzl", "my_macro")
""");

reporter.removeHandler(failFastHandler);
Package pkg = getPackage("pkg");
Expand All @@ -318,14 +334,18 @@ public void testSymbolicMacro_requiresNameAttribute() throws Exception {
setBuildLanguageOptions("--experimental_enable_first_class_macros");

scratch.file(
"pkg/foo.bzl", //
"def _impl(name):",
" pass",
"my_macro = macro(implementation=_impl)");
scratch.file(
"pkg/BUILD", //
"load(':foo.bzl', 'my_macro')",
"my_macro()");
"pkg/foo.bzl",
"""
def _impl(name):
pass
my_macro = macro(implementation=_impl)
""");
scratch.file(
"pkg/BUILD",
"""
load(":foo.bzl", "my_macro")
my_macro()
""");

reporter.removeHandler(failFastHandler);
Package pkg = getPackage("pkg");
Expand All @@ -339,14 +359,18 @@ public void testSymbolicMacro_prohibitsPositionalArgs() throws Exception {
setBuildLanguageOptions("--experimental_enable_first_class_macros");

scratch.file(
"pkg/foo.bzl", //
"def _impl(name):",
" pass",
"my_macro = macro(implementation=_impl)");
scratch.file(
"pkg/BUILD", //
"load(':foo.bzl', 'my_macro')",
"my_macro('a positional arg', name = 'abc')");
"pkg/foo.bzl",
"""
def _impl(name):
pass
my_macro = macro(implementation=_impl)
""");
scratch.file(
"pkg/BUILD",
"""
load(":foo.bzl", "my_macro")
my_macro("a positional arg", name = "abc")
""");

reporter.removeHandler(failFastHandler);
Package pkg = getPackage("pkg");
Expand All @@ -360,11 +384,13 @@ public void testSymbolicMacro_macroFunctionApi() throws Exception {
ev.setSemantics("--experimental_enable_first_class_macros");

evalAndExport(
ev, //
"def _impl(name):",
" pass",
"exported = macro(implementation=_impl)",
"s = struct(unexported = macro(implementation=_impl))");
ev,
"""
def _impl(name):
pass
exported = macro(implementation=_impl)
s = struct(unexported = macro(implementation=_impl))
""");

MacroFunction exported = (MacroFunction) ev.lookup("exported");
MacroFunction unexported = (MacroFunction) ev.eval("s.unexported");
Expand Down

0 comments on commit 35c88de

Please sign in to comment.