From 1c2b02767c05e3a53a7303c31f8183fbaec6d394 Mon Sep 17 00:00:00 2001 From: vladmos Date: Tue, 11 Jul 2017 15:38:40 +0200 Subject: Add a string representations test for glob values PiperOrigin-RevId: 161521237 --- .../skylark/SkylarkStringRepresentationsTest.java | 62 ++++++++++++++++++++++ 1 file changed, 62 insertions(+) (limited to 'src/test/java') diff --git a/src/test/java/com/google/devtools/build/lib/skylark/SkylarkStringRepresentationsTest.java b/src/test/java/com/google/devtools/build/lib/skylark/SkylarkStringRepresentationsTest.java index 3dbb2defb1..82e2dfed90 100644 --- a/src/test/java/com/google/devtools/build/lib/skylark/SkylarkStringRepresentationsTest.java +++ b/src/test/java/com/google/devtools/build/lib/skylark/SkylarkStringRepresentationsTest.java @@ -63,6 +63,55 @@ public class SkylarkStringRepresentationsTest extends SkylarkTestCase { return target.get("result"); } + /** + * Evaluates {@code code} in the loading phase in a BUILD file. {@code code} must return a string. + * + * @param code The code to execute + */ + private Object skylarkLoadingEvalInBuildFile(String code) throws Exception { + scratch.overwriteFile("eval/BUILD", + "load(':eval.bzl', 'eval')", + String.format("eval(name='eval', param = %s)", code)); + scratch.overwriteFile( + "eval/eval.bzl", + "def _impl(ctx):", + " return struct(result = ctx.attr.param)", + "eval = rule(implementation = _impl, attrs = {'param': attr.string()})"); + skyframeExecutor.invalidateFilesUnderPathForTesting( + reporter, + new ModifiedFileSet.Builder() + .modify(PathFragment.create("eval/BUILD")) + .modify(PathFragment.create("eval/eval.bzl")) + .build(), + rootDirectory); + + ConfiguredTarget target = getConfiguredTarget("//eval"); + return target.get("result"); + } + + /** + * Asserts that all 5 different ways to convert an object to a string of {@code expression} + * ({@code str}, {@code repr}, {@code '%s'}, {@code '%r'}, {@code '{}'.format} return the correct + * {@code representation}. Not applicable for objects that have different {@code str} and {@code + * repr} representations. + * + * @param expression the expression to evaluate a string representation of + * @param representation desired string representation + */ + private void assertStringRepresentationInBuildFile( + String expression, String representation) throws Exception { + assertThat(skylarkLoadingEvalInBuildFile(String.format("str(%s)", expression))) + .isEqualTo(representation); + assertThat(skylarkLoadingEvalInBuildFile(String.format("repr(%s)", expression))) + .isEqualTo(representation); + assertThat(skylarkLoadingEvalInBuildFile(String.format("'%%s' %% (%s,)", expression))) + .isEqualTo(representation); + assertThat(skylarkLoadingEvalInBuildFile(String.format("'%%r' %% (%s,)", expression))) + .isEqualTo(representation); + assertThat(skylarkLoadingEvalInBuildFile(String.format("'{}'.format(%s)", expression))) + .isEqualTo(representation); + } + /** * Asserts that all 5 different ways to convert an object to a string of {@code expression} * ({@code str}, {@code repr}, {@code '%s'}, {@code '%r'}, {@code '{}'.format} return the correct @@ -285,6 +334,19 @@ public class SkylarkStringRepresentationsTest extends SkylarkTestCase { } } + @Test + public void testStringRepresentations_Glob() throws Exception { + setSkylarkSemanticsOptions("--incompatible_descriptive_string_representations=true"); + + scratch.file("eval/one.txt"); + scratch.file("eval/two.txt"); + scratch.file("eval/three.txt"); + + assertStringRepresentationInBuildFile( + "glob(['*.txt'])", + "[\"one.txt\", \"three.txt\", \"two.txt\"]"); + } + @Test public void testStringRepresentations_Attr() throws Exception { setSkylarkSemanticsOptions("--incompatible_descriptive_string_representations=true"); -- cgit v1.2.3