aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/test/java
diff options
context:
space:
mode:
authorGravatar Googler <noreply@google.com>2018-05-22 13:21:04 -0700
committerGravatar Copybara-Service <copybara-piper@google.com>2018-05-22 13:22:15 -0700
commit055d6c619ab572debddb3518616c75f64462c145 (patch)
tree44b316758b6718a9b4fa6b099b352a489784a773 /src/test/java
parent724bdbfa47576c67eeec5c74d594203fe05188c7 (diff)
Add typo detection when lookups on SkylarkModules fail.
Also consolidate code with getattr so getattr now also gets typo detection. PiperOrigin-RevId: 197612666
Diffstat (limited to 'src/test/java')
-rw-r--r--src/test/java/com/google/devtools/build/lib/skylark/SkylarkRuleClassFunctionsTest.java4
-rw-r--r--src/test/java/com/google/devtools/build/lib/syntax/MethodLibraryTest.java39
-rw-r--r--src/test/java/com/google/devtools/build/lib/syntax/SkylarkEvaluationTest.java13
3 files changed, 52 insertions, 4 deletions
diff --git a/src/test/java/com/google/devtools/build/lib/skylark/SkylarkRuleClassFunctionsTest.java b/src/test/java/com/google/devtools/build/lib/skylark/SkylarkRuleClassFunctionsTest.java
index 06963f5450..debf9f5ab2 100644
--- a/src/test/java/com/google/devtools/build/lib/skylark/SkylarkRuleClassFunctionsTest.java
+++ b/src/test/java/com/google/devtools/build/lib/skylark/SkylarkRuleClassFunctionsTest.java
@@ -1098,7 +1098,9 @@ public class SkylarkRuleClassFunctionsTest extends SkylarkTestCase {
@Test
public void testGetattrNoAttr() throws Exception {
checkErrorContains(
- "object of type 'struct' has no attribute \"b\"", "s = struct(a='val')", "getattr(s, 'b')");
+ "'struct' object has no attribute 'b'\nAvailable attributes: a",
+ "s = struct(a='val')",
+ "getattr(s, 'b')");
}
@Test
diff --git a/src/test/java/com/google/devtools/build/lib/syntax/MethodLibraryTest.java b/src/test/java/com/google/devtools/build/lib/syntax/MethodLibraryTest.java
index d487eff0d2..76b643259f 100644
--- a/src/test/java/com/google/devtools/build/lib/syntax/MethodLibraryTest.java
+++ b/src/test/java/com/google/devtools/build/lib/syntax/MethodLibraryTest.java
@@ -16,7 +16,10 @@ package com.google.devtools.build.lib.syntax;
import static com.google.common.truth.Truth.assertThat;
+import com.google.common.collect.ImmutableCollection;
+import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
+import com.google.devtools.build.lib.skylarkinterface.SkylarkModule;
import com.google.devtools.build.lib.syntax.util.EvaluationTestCase;
import org.junit.Before;
import org.junit.Test;
@@ -168,16 +171,48 @@ public class MethodLibraryTest extends EvaluationTestCase {
public void testGetAttrMissingField() throws Exception {
new SkylarkTest()
.testIfExactError(
- "object of type 'string' has no attribute \"not_there\"",
+ "object of type 'string' has no attribute 'not_there'",
"getattr('a string', 'not_there')")
.testStatement("getattr('a string', 'not_there', 'use this')", "use this")
.testStatement("getattr('a string', 'not there', None)", Runtime.NONE);
}
+ @SkylarkModule(name = "AStruct", documented = false, doc = "")
+ static final class AStruct implements ClassObject {
+ @Override
+ public Object getValue(String name) {
+ switch (name) {
+ case "field":
+ return "a";
+ default:
+ return null;
+ }
+ }
+
+ @Override
+ public ImmutableCollection<String> getFieldNames() {
+ return ImmutableList.of("field");
+ }
+
+ @Override
+ public String getErrorMessageForUnknownField(String name) {
+ return null;
+ }
+ }
+
+ @Test
+ public void testGetAttrMissingField_typoDetection() throws Exception {
+ new SkylarkTest()
+ .update("s", new AStruct())
+ .testIfExactError(
+ "object of type 'AStruct' has no attribute 'feild' (did you mean 'field'?)",
+ "getattr(s, 'feild')");
+ }
+
@Test
public void testGetAttrWithMethods() throws Exception {
String msg =
- "object of type 'string' has no attribute \"count\", however, "
+ "object of type 'string' has no attribute 'count', however, "
+ "a method of that name exists";
new SkylarkTest()
.testIfExactError(msg, "getattr('a string', 'count')")
diff --git a/src/test/java/com/google/devtools/build/lib/syntax/SkylarkEvaluationTest.java b/src/test/java/com/google/devtools/build/lib/syntax/SkylarkEvaluationTest.java
index 36100727bf..3e0db3dd03 100644
--- a/src/test/java/com/google/devtools/build/lib/syntax/SkylarkEvaluationTest.java
+++ b/src/test/java/com/google/devtools/build/lib/syntax/SkylarkEvaluationTest.java
@@ -1347,7 +1347,9 @@ public class SkylarkEvaluationTest extends EvaluationTest {
public void testStructAccessOfMethod() throws Exception {
new SkylarkTest()
.update("mock", new Mock())
- .testIfExactError("object of type 'Mock' has no field 'function'", "v = mock.function");
+ .testIfExactError(
+ "object of type 'Mock' has no field 'function', however, a method of that name exists",
+ "v = mock.function");
}
@Test
@@ -1360,6 +1362,15 @@ public class SkylarkEvaluationTest extends EvaluationTest {
}
@Test
+ public void testStructAccessType_nonClassObject() throws Exception {
+ new SkylarkTest()
+ .update("mock", new Mock())
+ .testIfExactError(
+ "object of type 'Mock' has no field 'sturct_field' (did you mean 'struct_field'?)",
+ "v = mock.sturct_field");
+ }
+
+ @Test
public void testJavaFunctionReturnsMutableObject() throws Exception {
new SkylarkTest()
.update("mock", new Mock())