aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar vladmos <vladmos@google.com>2017-07-11 15:38:40 +0200
committerGravatar László Csomor <laszlocsomor@google.com>2017-07-11 18:25:56 +0200
commit1c2b02767c05e3a53a7303c31f8183fbaec6d394 (patch)
treeab69c0a233933a064523003aa4b9c10a0ff01fb2
parenta3ead77d7641dbcb8414db0fc837a2c70721439b (diff)
Add a string representations test for glob values
PiperOrigin-RevId: 161521237
-rw-r--r--src/test/java/com/google/devtools/build/lib/skylark/SkylarkStringRepresentationsTest.java62
1 files changed, 62 insertions, 0 deletions
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
@@ -64,6 +64,55 @@ public class SkylarkStringRepresentationsTest extends SkylarkTestCase {
}
/**
+ * 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
* {@code representation}. Not applicable for objects that have different {@code str} and {@code
@@ -286,6 +335,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");