MethodLibraryTest: Implemented an alternative approach that reduces duplicated code and may lead to cleaner tests.
--
MOS_MIGRATED_REVID=97326780
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 0264681..88f4503 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
@@ -15,8 +15,6 @@
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import com.google.common.collect.ImmutableCollection;
@@ -29,8 +27,8 @@
import com.google.devtools.build.lib.analysis.RuleConfiguredTarget;
import com.google.devtools.build.lib.analysis.TransitiveInfoCollection;
import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder;
-import com.google.devtools.build.lib.rules.SkylarkModules;
import com.google.devtools.build.lib.syntax.ClassObject.SkylarkClassObject;
+import com.google.devtools.build.lib.testutil.TestMode;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -41,13 +39,19 @@
*/
@RunWith(JUnit4.class)
public class SkylarkEvaluationTest extends EvaluationTest {
-
- // Restoring defaults overridden by EvaluationTest
- @Override
- public EvaluationContext newEvaluationContext() {
- return SkylarkModules.newEvaluationContext(getEventHandler());
+ public SkylarkEvaluationTest() throws Exception {
+ setMode(TestMode.SKYLARK);
}
+ /**
+ * Creates an instance of {@code SkylarkTest} in order to run the tests from the base class in a
+ * Skylark context
+ */
+ @Override
+ protected ModalTestCase newTest() {
+ return new SkylarkTest();
+ }
+
@SkylarkModule(name = "Mock", doc = "")
static class Mock {
@SkylarkCallable(doc = "")
@@ -146,43 +150,34 @@
@Test
public void testSimpleIf() throws Exception {
- eval("def foo():",
+ new SkylarkTest().setUp("def foo():",
" a = 0",
" x = 0",
" if x: a = 5",
" return a",
- "a = foo()");
- assertEquals(0, lookup("a"));
+ "a = foo()").testLookup("a", 0);
}
@Test
public void testIfPass() throws Exception {
- eval("def foo():",
+ new SkylarkTest().setUp("def foo():",
" a = 1",
" x = True",
" if x: pass",
" return a",
- "a = foo()");
- assertEquals(1, lookup("a"));
+ "a = foo()").testLookup("a", 1);
}
@Test
public void testNestedIf() throws Exception {
- executeNestedIf(0, 0);
- assertEquals(0, lookup("x"));
-
- setUp();
- executeNestedIf(1, 0);
- assertEquals(3, lookup("x"));
-
- setUp();
- executeNestedIf(1, 1);
- assertEquals(5, lookup("x"));
+ executeNestedIf(0, 0, 0);
+ executeNestedIf(1, 0, 3);
+ executeNestedIf(1, 1, 5);
}
- private void executeNestedIf(int x, int y) throws Exception {
+ private void executeNestedIf(int x, int y, int expected) throws Exception {
String fun = String.format("foo%s%s", x, y);
- eval("def " + fun + "():",
+ new SkylarkTest().setUp("def " + fun + "():",
" x = " + x,
" y = " + y,
" a = 0",
@@ -192,7 +187,7 @@
" a = 2",
" b = 3",
" return a + b",
- "x = " + fun + "()");
+ "x = " + fun + "()").testLookup("x", expected);
}
@Test
@@ -202,14 +197,14 @@
}
private void executeIfElse(String fun, String y, int expected) throws Exception {
- eval("def " + fun + "():",
+ new SkylarkTest().setUp("def " + fun + "():",
" y = '" + y + "'",
" x = 5",
" if x:",
" if y: a = 2",
" else: a = 3",
- " return a\n");
- assertEquals(expected, eval(fun + "()"));
+ " return a",
+ "z = " + fun + "()").testLookup("z", expected);
}
@Test
@@ -228,7 +223,7 @@
}
private void execIfElifElse(int x, int y, int v) throws Exception {
- eval("def foo():",
+ new SkylarkTest().setUp("def foo():",
" x = " + x + "",
" y = " + y + "",
" if x:",
@@ -237,100 +232,89 @@
" return 2",
" else:",
" return 3",
- "v = foo()");
- assertEquals(v, lookup("v"));
+ "v = foo()").testLookup("v", v);
}
@Test
public void testForOnList() throws Exception {
- eval("def foo():",
+ new SkylarkTest().setUp("def foo():",
" s = ''",
" for i in ['hello', ' ', 'world']:",
" s = s + i",
" return s",
- "s = foo()");
- assertEquals("hello world", lookup("s"));
+ "s = foo()").testLookup("s", "hello world");
}
- @SuppressWarnings("unchecked")
@Test
public void testForOnString() throws Exception {
- eval("def foo():",
+ new SkylarkTest().setUp("def foo():",
" s = []",
" for i in 'abc':",
" s = s + [i]",
" return s",
- "s = foo()");
- assertThat((Iterable<Object>) lookup("s")).containsExactly("a", "b", "c").inOrder();
+ "s = foo()").testExactOrder("s", "a", "b", "c");
}
@Test
public void testForAssignmentList() throws Exception {
- eval("def foo():",
+ new SkylarkTest().setUp("def foo():",
" d = ['a', 'b', 'c']",
" s = ''",
" for i in d:",
" s = s + i",
- " d = ['d', 'e', 'f']", // check that we use the old list
+ " d = ['d', 'e', 'f']", // check that we use the old list
" return s",
- "s = foo()");
- assertEquals("abc", lookup("s"));
+ "s = foo()").testLookup("s", "abc");
}
@Test
public void testForAssignmentDict() throws Exception {
- eval("def func():",
+ new SkylarkTest().setUp("def func():",
" d = {'a' : 1, 'b' : 2, 'c' : 3}",
" s = ''",
" for i in d:",
" s = s + i",
" d = {'d' : 1, 'e' : 2, 'f' : 3}",
" return s",
- "s = func()");
- assertEquals("abc", lookup("s"));
+ "s = func()").testLookup("s", "abc");
}
@Test
public void testForNotIterable() throws Exception {
- update("mock", new Mock());
- checkEvalError("type 'int' is not iterable",
- "def func():",
- " for i in mock.value_of('1'): a = i",
- "func()");
-
+ new SkylarkTest()
+ .update("mock", new Mock())
+ .testIfExactError("type 'int' is not iterable", "def func():",
+ " for i in mock.value_of('1'): a = i", "func()\n");
}
@Test
public void testForOnDictionary() throws Exception {
- eval("def foo():",
+ new SkylarkTest().setUp("def foo():",
" d = {1: 'a', 2: 'b', 3: 'c'}",
" s = ''",
" for i in d: s = s + d[i]",
" return s",
- "s = foo()");
- assertEquals("abc", lookup("s"));
+ "s = foo()").testLookup("s", "abc");
}
@Test
public void testForLoopReuseVariable() throws Exception {
- eval("def foo():",
+ new SkylarkTest().setUp("def foo():",
" s = ''",
" for i in ['a', 'b']:",
" for i in ['c', 'd']: s = s + i",
" return s",
- "s = foo()");
- assertEquals("cdcd", lookup("s"));
+ "s = foo()").testLookup("s", "cdcd");
}
@Test
public void testForLoopMultipleVariables() throws Exception {
- eval("def foo():",
+ new SkylarkTest().setUp("def foo():",
" s = ''",
" for [i, j] in [[1, 2], [3, 4]]:",
" s = s + str(i) + str(j) + '.'",
" return s",
- "s = foo()");
- assertEquals("12.34.", lookup("s"));
+ "s = foo()").testLookup("s", "12.34.");
}
@Test
@@ -457,15 +441,12 @@
" " + statement + "",
"y = foo2()");
}
-
+
@Test
public void testNoneAssignment() throws Exception {
- eval("def foo(x=None):",
- " x = 1",
- " x = None",
- " return 2",
- "s = foo()");
- assertEquals(2, lookup("s"));
+ new SkylarkTest()
+ .setUp("def foo(x=None):", " x = 1", " x = None", " return 2", "s = foo()")
+ .testLookup("s", 2);
}
@Test
@@ -481,98 +462,111 @@
@Test
public void testJavaCalls() throws Exception {
- update("mock", new Mock());
- eval("b = mock.is_empty('a')");
- assertEquals(Boolean.FALSE, lookup("b"));
+ new SkylarkTest()
+ .update("mock", new Mock())
+ .setUp("b = mock.is_empty('a')")
+ .testLookup("b", Boolean.FALSE);
}
@Test
public void testJavaCallsOnSubClass() throws Exception {
- update("mock", new MockSubClass());
- eval("b = mock.is_empty('a')");
- assertEquals(Boolean.FALSE, lookup("b"));
+ new SkylarkTest()
+ .update("mock", new MockSubClass())
+ .setUp("b = mock.is_empty('a')")
+ .testLookup("b", Boolean.FALSE);
}
@Test
public void testJavaCallsOnInterface() throws Exception {
- update("mock", new MockSubClass());
- eval("b = mock.is_empty_interface('a')");
- assertEquals(Boolean.FALSE, lookup("b"));
+ new SkylarkTest()
+ .update("mock", new MockSubClass())
+ .setUp("b = mock.is_empty_interface('a')")
+ .testLookup("b", Boolean.FALSE);
}
@Test
public void testJavaCallsNotSkylarkCallable() throws Exception {
- update("mock", new Mock());
- checkEvalError("No matching method found for value() in Mock", "mock.value()");
+ new SkylarkTest()
+ .update("mock", new Mock())
+ .testIfExactError("No matching method found for value() in Mock", "mock.value()");
}
@Test
public void testJavaCallsNoMethod() throws Exception {
- checkEvalError("No matching method found for bad() in int", "s = 3.bad()");
+ new SkylarkTest().testIfExactError("No matching method found for bad() in int", "s = 3.bad()");
}
@Test
public void testJavaCallsNoMethodErrorMsg() throws Exception {
- checkEvalError("No matching method found for bad(string, string, string) in int",
+ new SkylarkTest().testIfExactError(
+ "No matching method found for bad(string, string, string) in int",
"s = 3.bad('a', 'b', 'c')");
}
@Test
public void testJavaCallsMultipleMethod() throws Exception {
- update("mock", new MockMultipleMethodClass());
- checkEvalError("Multiple matching methods for method(string) in MockMultipleMethodClass",
- "s = mock.method('string')");
+ new SkylarkTest()
+ .update("mock", new MockMultipleMethodClass())
+ .testIfExactError(
+ "Multiple matching methods for method(string) in MockMultipleMethodClass",
+ "s = mock.method('string')");
}
@Test
public void testJavaCallWithKwargs() throws Exception {
- checkEvalError("Keyword arguments are not allowed when calling a java method"
+ new SkylarkTest().testIfExactError(
+ "Keyword arguments are not allowed when calling a java method"
+ "\nwhile calling method 'compare_to' on object 3 of type int",
"comp = 3.compare_to(x = 4)");
}
@Test
public void testNoJavaCallsWithoutSkylark() throws Exception {
- checkEvalError("No matching method found for to_string() in int", "s = 3.to_string()\n");
+ new SkylarkTest().testIfExactError(
+ "No matching method found for to_string() in int", "s = 3.to_string()");
}
@Test
public void testNoJavaCallsIfClassNotAnnotated() throws Exception {
- update("mock", new MockSubClass());
- checkEvalError(
- "No matching method found for is_empty_class_not_annotated(string) in MockSubClass",
- "b = mock.is_empty_class_not_annotated('a')");
+ new SkylarkTest()
+ .update("mock", new MockSubClass())
+ .testIfExactError(
+ "No matching method found for is_empty_class_not_annotated(string) in MockSubClass",
+ "b = mock.is_empty_class_not_annotated('a')");
}
@Test
public void testStructAccess() throws Exception {
- update("mock", new Mock());
- eval("v = mock.struct_field");
- assertEquals("a", lookup("v"));
+ new SkylarkTest()
+ .update("mock", new Mock())
+ .setUp("v = mock.struct_field")
+ .testLookup("v", "a");
}
@Test
public void testStructAccessAsFuncall() throws Exception {
- update("mock", new Mock());
- checkEvalError("No matching method found for struct_field() in Mock",
- "v = mock.struct_field()");
+ new SkylarkTest()
+ .update("mock", new Mock())
+ .testIfExactError(
+ "No matching method found for struct_field() in Mock", "v = mock.struct_field()");
}
@Test
public void testStructAccessOfMethod() throws Exception {
- update("mock", new Mock());
- checkEvalError("Object of type 'Mock' has no field \"function\"",
- "v = mock.function");
+ new SkylarkTest()
+ .update("mock", new Mock())
+ .testIfExactError("Object of type 'Mock' has no field \"function\"", "v = mock.function");
}
@Test
public void testConditionalStructConcatenation() throws Exception {
+ // TODO(fwe): cannot be handled by current testing suite
eval("def func():",
" x = struct(a = 1, b = 2)",
" if True:",
" x += struct(c = 1, d = 2)",
" return x",
- "x = func()\n");
+ "x = func()");
SkylarkClassObject x = (SkylarkClassObject) lookup("x");
assertEquals(1, x.getValue("a"));
assertEquals(2, x.getValue("b"));
@@ -582,96 +576,103 @@
@Test
public void testJavaFunctionReturnsMutableObject() throws Exception {
- update("mock", new Mock());
- checkEvalError("Method 'return_mutable' returns a mutable object (type of Mock)",
- "mock.return_mutable()");
+ new SkylarkTest()
+ .update("mock", new Mock())
+ .testIfExactError(
+ "Method 'return_mutable' returns a mutable object (type of Mock)",
+ "mock.return_mutable()");
}
@Test
public void testJavaFunctionReturnsNullFails() throws Exception {
- update("mock", new Mock());
- checkEvalError("Method invocation returned None,"
- + " please contact Skylark developers: nullfunc_failing(\"abc\", 1)",
- "mock.nullfunc_failing('abc', 1)");
+ new SkylarkTest()
+ .update("mock", new Mock())
+ .testIfExactError(
+ "Method invocation returned None,"
+ + " please contact Skylark developers: nullfunc_failing(\"abc\", 1)",
+ "mock.nullfunc_failing('abc', 1)");
}
@Test
public void testClassObjectAccess() throws Exception {
- update("mock", new MockClassObject());
- eval("v = mock.field");
- assertEquals("a", lookup("v"));
+ new SkylarkTest()
+ .update("mock", new MockClassObject())
+ .setUp("v = mock.field")
+ .testLookup("v", "a");
}
@Test
public void testInSet() throws Exception {
- assertEquals(Boolean.TRUE, eval("'b' in set(['a', 'b'])"));
- assertEquals(Boolean.FALSE, eval("'c' in set(['a', 'b'])"));
- assertEquals(Boolean.FALSE, eval("1 in set(['a', 'b'])"));
+ new SkylarkTest().testStatement("'b' in set(['a', 'b'])", Boolean.TRUE)
+ .testStatement("'c' in set(['a', 'b'])", Boolean.FALSE)
+ .testStatement("1 in set(['a', 'b'])", Boolean.FALSE);
}
@Test
public void testClassObjectCannotAccessNestedSet() throws Exception {
- update("mock", new MockClassObject());
- checkEvalError("Type is not allowed in Skylark: EmptyNestedSet", "v = mock.nset");
+ new SkylarkTest()
+ .update("mock", new MockClassObject())
+ .testIfExactError("Type is not allowed in Skylark: EmptyNestedSet", "v = mock.nset");
}
@Test
public void testJavaFunctionReturnsNone() throws Exception {
- update("mock", new Mock());
- eval("v = mock.nullfunc_working()");
- assertSame(Environment.NONE, lookup("v"));
+ new SkylarkTest()
+ .update("mock", new Mock())
+ .setUp("v = mock.nullfunc_working()")
+ .testLookup("v", Environment.NONE);
}
@Test
public void testVoidJavaFunctionReturnsNone() throws Exception {
- update("mock", new Mock());
- eval("v = mock.voidfunc()");
- assertSame(Environment.NONE, lookup("v"));
+ new SkylarkTest()
+ .update("mock", new Mock())
+ .setUp("v = mock.voidfunc()")
+ .testLookup("v", Environment.NONE);
}
@Test
public void testAugmentedAssignment() throws Exception {
- eval("def f1(x):",
+ new SkylarkTest().setUp("def f1(x):",
" x += 1",
" return x",
"",
- "foo = f1(41)\n");
- assertEquals(42, lookup("foo"));
+ "foo = f1(41)").testLookup("foo", 42);
}
@Test
public void testStaticDirectJavaCall() throws Exception {
- update("Mock", Mock.class);
- eval("val = Mock.value_of('8')");
- assertEquals(8, lookup("val"));
+ new SkylarkTest().update("Mock", Mock.class).setUp("val = Mock.value_of('8')")
+ .testLookup("val", 8);
}
@Test
public void testStaticDirectJavaCallMethodIsNonStatic() throws Exception {
- update("Mock", Mock.class);
- checkEvalError("Method 'is_empty' is not static", "val = Mock.is_empty('a')");
+ new SkylarkTest().update("Mock", Mock.class).testIfExactError("Method 'is_empty' is not static",
+ "val = Mock.is_empty('a')");
}
@Test
public void testDictComprehensions_IterationOrder() throws Exception {
- eval("def foo():",
+ new SkylarkTest().setUp("def foo():",
" d = {x : x for x in ['c', 'a', 'b']}",
" s = ''",
" for a in d:",
" s += a",
" return s",
- "s = foo()");
- assertEquals("abc", lookup("s"));
+ "s = foo()").testLookup("s", "abc");
}
@Test
public void testStructCreation() throws Exception {
+ // TODO(fwe): cannot be handled by current testing suite
eval("x = struct(a = 1, b = 2)");
assertThat(lookup("x")).isInstanceOf(ClassObject.class);
}
@Test
public void testStructFields() throws Exception {
+ // TODO(fwe): cannot be handled by current testing suite
eval("x = struct(a = 1, b = 2)");
ClassObject x = (ClassObject) lookup("x");
assertEquals(1, x.getValue("a"));
@@ -680,35 +681,33 @@
@Test
public void testStructAccessingFieldsFromSkylark() throws Exception {
- eval("x = struct(a = 1, b = 2)",
- "x1 = x.a",
- "x2 = x.b\n");
- assertEquals(1, lookup("x1"));
- assertEquals(2, lookup("x2"));
+ new SkylarkTest()
+ .setUp("x = struct(a = 1, b = 2)", "x1 = x.a", "x2 = x.b")
+ .testLookup("x1", 1)
+ .testLookup("x2", 2);
}
@Test
public void testStructAccessingUnknownField() throws Exception {
- checkEvalError("Object of type 'struct' has no field \"c\"",
- "x = struct(a = 1, b = 2)",
- "y = x.c\n");
+ new SkylarkTest().testIfExactError(
+ "Object of type 'struct' has no field \"c\"", "x = struct(a = 1, b = 2)", "y = x.c");
}
@Test
public void testStructAccessingFieldsWithArgs() throws Exception {
- checkEvalError("No matching method found for a(int) in struct",
- "x = struct(a = 1, b = 2)",
- "x1 = x.a(1)\n");
+ new SkylarkTest().testIfExactError(
+ "No matching method found for a(int) in struct", "x = struct(a = 1, b = 2)", "x1 = x.a(1)");
}
@Test
public void testStructPosArgs() throws Exception {
- checkEvalError("struct(**kwargs) does not accept positional arguments, but got 1",
- "x = struct(1, b = 2)\n");
+ new SkylarkTest().testIfExactError(
+ "struct(**kwargs) does not accept positional arguments, but got 1", "x = struct(1, b = 2)");
}
@Test
public void testStructConcatenationFieldNames() throws Exception {
+ // TODO(fwe): cannot be handled by current testing suite
eval("x = struct(a = 1, b = 2)",
"y = struct(c = 1, d = 2)",
"z = x + y\n");
@@ -718,6 +717,7 @@
@Test
public void testStructConcatenationFieldValues() throws Exception {
+ // TODO(fwe): cannot be handled by current testing suite
eval("x = struct(a = 1, b = 2)",
"y = struct(c = 1, d = 2)",
"z = x + y\n");
@@ -730,49 +730,46 @@
@Test
public void testStructConcatenationCommonFields() throws Exception {
- checkEvalError("Cannot concat structs with common field(s): a",
- "x = struct(a = 1, b = 2)",
- "y = struct(c = 1, a = 2)",
- "z = x + y\n");
+ new SkylarkTest().testIfExactError("Cannot concat structs with common field(s): a",
+ "x = struct(a = 1, b = 2)", "y = struct(c = 1, a = 2)", "z = x + y\n");
}
@Test
public void testDotExpressionOnNonStructObject() throws Exception {
- checkEvalError("Object of type 'string' has no field \"field\"", "x = 'a'.field");
+ new SkylarkTest().testIfExactError("Object of type 'string' has no field \"field\"",
+ "x = 'a'.field");
}
@Test
public void testPlusEqualsOnDict() throws Exception {
- eval("def func():",
+ new SkylarkTest().setUp("def func():",
" d = {'a' : 1}",
" d += {'b' : 2}",
" return d",
- "d = func()");
- assertEquals(ImmutableMap.of("a", 1, "b", 2), lookup("d"));
+ "d = func()")
+ .testLookup("d", ImmutableMap.of("a", 1, "b", 2));
}
@Test
public void testDictAssignmentAsLValue() throws Exception {
- eval("def func():",
+ new SkylarkTest().setUp("def func():",
" d = {'a' : 1}",
" d['b'] = 2",
" return d",
- "d = func()");
- assertEquals(ImmutableMap.of("a", 1, "b", 2), lookup("d"));
+ "d = func()").testLookup("d", ImmutableMap.of("a", 1, "b", 2));
}
@Test
public void testDictAssignmentAsLValueNoSideEffects() throws Exception {
- eval("def func(d):",
+ new SkylarkTest().setUp("def func(d):",
" d['b'] = 2",
"d = {'a' : 1}",
- "func(d)");
- assertEquals(ImmutableMap.of("a", 1), lookup("d"));
+ "func(d)").testLookup("d", ImmutableMap.of("a", 1));
}
@Test
public void testListIndexAsLValueAsLValue() throws Exception {
- checkEvalError("unsupported operand type(s) for +: 'list' and 'dict'",
+ new SkylarkTest().testIfExactError("unsupported operand type(s) for +: 'list' and 'dict'",
"def id(l):",
" return l",
"def func():",
@@ -784,32 +781,30 @@
@Test
public void testTopLevelDict() throws Exception {
- eval("if 1:",
+ new SkylarkTest().setUp("if 1:",
" v = 'a'",
"else:",
- " v = 'b'");
- assertEquals("a", lookup("v"));
+ " v = 'b'").testLookup("v", "a");
}
@Test
public void testUserFunctionKeywordArgs() throws Exception {
- eval("def foo(a, b, c):",
- " return a + b + c",
- "s = foo(1, c=2, b=3)");
- assertEquals(6, lookup("s"));
+ new SkylarkTest().setUp("def foo(a, b, c):",
+ " return a + b + c", "s = foo(1, c=2, b=3)")
+ .testLookup("s", 6);
}
@Test
public void testFunctionCallOrdering() throws Exception {
- eval("def func(): return foo() * 2",
+ new SkylarkTest().setUp("def func(): return foo() * 2",
"def foo(): return 2",
- "x = func()");
- assertThat(lookup("x")).isEqualTo(4);
+ "x = func()")
+ .testLookup("x", 4);
}
@Test
public void testFunctionCallBadOrdering() throws Exception {
- checkEvalError("name 'foo' is not defined",
+ new SkylarkTest().testIfExactError("name 'foo' is not defined",
"def func(): return foo() * 2",
"x = func()",
"def foo(): return 2");
@@ -817,118 +812,111 @@
@Test
public void testNoneTrueFalseInSkylark() throws Exception {
- eval("a = None",
+ new SkylarkTest().setUp("a = None",
"b = True",
- "c = False");
- assertSame(Environment.NONE, lookup("a"));
- assertTrue((Boolean) lookup("b"));
- assertFalse((Boolean) lookup("c"));
+ "c = False")
+ .testLookup("a", Environment.NONE)
+ .testLookup("b", Boolean.TRUE)
+ .testLookup("c", Boolean.FALSE);
}
@Test
public void testHasattr() throws Exception {
- eval("s = struct(a=1)",
+ new SkylarkTest().setUp("s = struct(a=1)",
"x = hasattr(s, 'a')",
- "y = hasattr(s, 'b')\n");
- assertTrue((Boolean) lookup("x"));
- assertFalse((Boolean) lookup("y"));
+ "y = hasattr(s, 'b')\n")
+ .testLookup("x", Boolean.TRUE)
+ .testLookup("y", Boolean.FALSE);
}
@Test
public void testHasattrMethods() throws Exception {
- update("mock", new Mock());
- eval("a = hasattr(mock, 'struct_field')",
- "b = hasattr(mock, 'function')",
- "c = hasattr(mock, 'is_empty')",
- "d = hasattr('str', 'replace')",
- "e = hasattr(mock, 'other')\n");
- assertTrue((Boolean) lookup("a"));
- assertTrue((Boolean) lookup("b"));
- assertTrue((Boolean) lookup("c"));
- assertTrue((Boolean) lookup("d"));
- assertFalse((Boolean) lookup("e"));
+ new SkylarkTest()
+ .update("mock", new Mock())
+ .setUp("a = hasattr(mock, 'struct_field')", "b = hasattr(mock, 'function')",
+ "c = hasattr(mock, 'is_empty')", "d = hasattr('str', 'replace')",
+ "e = hasattr(mock, 'other')\n")
+ .testLookup("a", Boolean.TRUE)
+ .testLookup("b", Boolean.TRUE)
+ .testLookup("c", Boolean.TRUE)
+ .testLookup("d", Boolean.TRUE)
+ .testLookup("e", Boolean.FALSE);
}
@Test
public void testGetattr() throws Exception {
- eval("s = struct(a='val')",
- "x = getattr(s, 'a')",
- "y = getattr(s, 'b', 'def')",
- "z = getattr(s, 'b', default = 'def')",
- "w = getattr(s, 'a', default='ignored')");
- assertEquals("val", lookup("x"));
- assertEquals("def", lookup("y"));
- assertEquals("def", lookup("z"));
- assertEquals("val", lookup("w"));
+ new SkylarkTest()
+ .setUp("s = struct(a='val')", "x = getattr(s, 'a')", "y = getattr(s, 'b', 'def')",
+ "z = getattr(s, 'b', default = 'def')", "w = getattr(s, 'a', default='ignored')")
+ .testLookup("x", "val")
+ .testLookup("y", "def")
+ .testLookup("z", "def")
+ .testLookup("w", "val");
}
@Test
public void testGetattrNoAttr() throws Exception {
- checkEvalError("Object of type 'struct' has no field \"b\"",
- "s = struct(a='val')",
- "getattr(s, 'b')");
+ new SkylarkTest().testIfExactError("Object of type 'struct' has no field \"b\"",
+ "s = struct(a='val')", "getattr(s, 'b')");
}
- @SuppressWarnings("unchecked")
@Test
public void testListAnTupleConcatenationDoesNotWorkInSkylark() throws Exception {
- checkEvalError("cannot concatenate lists and tuples", "[1, 2] + (3, 4)");
+ new SkylarkTest().testIfExactError("cannot concatenate lists and tuples", "[1, 2] + (3, 4)");
}
@Test
public void testCannotCreateMixedListInSkylark() throws Exception {
- update("mock", new Mock());
- checkEvalError(
+ new SkylarkTest().update("mock", new Mock()).testIfExactError(
"Incompatible types in list: found a int but the previous elements were strings",
"[mock.string(), 1, 2]");
}
@Test
public void testCannotConcatListInSkylarkWithDifferentGenericTypes() throws Exception {
- update("mock", new Mock());
- checkEvalError("cannot concatenate list of string with list of int",
- "mock.string_list() + [1, 2]");
+ new SkylarkTest().update("mock", new Mock()).testIfExactError(
+ "cannot concatenate list of string with list of int", "mock.string_list() + [1, 2]");
}
- @SuppressWarnings("unchecked")
@Test
public void testConcatEmptyListWithNonEmptyWorks() throws Exception {
- eval("l = [] + ['a', 'b']");
- assertThat((Iterable<Object>) lookup("l")).containsExactly("a", "b").inOrder();
+ new SkylarkTest().testExactOrder("[] + ['a', 'b']", "a", "b");
}
@Test
public void testFormatStringWithTuple() throws Exception {
- eval("v = '%s%s' % ('a', 1)");
- assertEquals("a1", lookup("v"));
+ new SkylarkTest().setUp("v = '%s%s' % ('a', 1)").testLookup("v", "a1");
}
@Test
public void testSingletonTuple() throws Exception {
- eval("v = (1,)");
- assertEquals("(1,)", lookup("v").toString());
+ new SkylarkTest().testExactOrder("(1,)", 1);
}
- @SuppressWarnings("unchecked")
@Test
public void testDirFindsClassObjectFields() throws Exception {
- update("mock", new MockClassObject());
- eval("v = dir(mock)");
- assertThat((Iterable<String>) lookup("v")).containsExactly("field", "nset").inOrder();
+ new SkylarkTest().update("mock", new MockClassObject()).setUp()
+ .testExactOrder("dir(mock)", "field", "nset");
}
- @SuppressWarnings("unchecked")
@Test
public void testDirFindsJavaObjectStructFieldsAndMethods() throws Exception {
- update("mock", new Mock());
- eval("v = dir(mock)");
- assertThat((Iterable<String>) lookup("v")).containsExactly("function", "is_empty",
- "nullfunc_failing", "nullfunc_working", "return_mutable", "string", "string_list",
- "struct_field", "value_of", "voidfunc").inOrder();
+ new SkylarkTest().update("mock", new Mock()).testExactOrder("dir(mock)",
+ "function",
+ "is_empty",
+ "nullfunc_failing",
+ "nullfunc_working",
+ "return_mutable",
+ "string",
+ "string_list",
+ "struct_field",
+ "value_of",
+ "voidfunc");
}
@Test
public void testPrint() throws Exception {
+ // TODO(fwe): cannot be handled by current testing suite
setFailFast(false);
eval("print('hello')");
assertContainsEvent("hello");
@@ -940,7 +928,7 @@
@Test
public void testPrintBadKwargs() throws Exception {
- checkEvalError(
+ new SkylarkTest().testIfExactError(
"unexpected keywords 'end', 'other' in call to print(*args, sep: string = \" \")",
"print(end='x', other='y')");
}
@@ -960,6 +948,10 @@
@Override
@Test
public void testConcatLists() throws Exception {
+ new SkylarkTest().testExactOrder("[1,2] + [3,4]", 1, 2, 3, 4).testExactOrder("(1,2)", 1, 2)
+ .testExactOrder("(1,2) + (3,4)", 1, 2, 3, 4);
+
+ // TODO(fwe): cannot be handled by current testing suite
// list
Object x = eval("[1,2] + [3,4]");
assertThat((Iterable<Object>) x).containsExactly(1, 2, 3, 4).inOrder();
@@ -981,56 +973,56 @@
@Override
@Test
public void testInFail() throws Exception {
- checkEvalError("in operator only works on strings if the left operand is also a string",
- "1 in '123'");
- checkEvalError("in operator only works on lists, tuples, sets, dicts and strings",
- "'a' in 1");
+ new SkylarkTest().testIfExactError(
+ "in operator only works on strings if the left operand is also a string", "1 in '123'");
+ new SkylarkTest().testIfExactError(
+ "in operator only works on lists, tuples, sets, dicts and strings", "'a' in 1");
}
@Override
@Test
public void testCompareStringInt() throws Exception {
- checkEvalError("Cannot compare string with int", "'a' >= 1");
+ new SkylarkTest().testIfExactError("Cannot compare string with int", "'a' >= 1");
}
@Override
@Test
public void testListComprehensionsMultipleVariablesFail() throws Exception {
- checkEvalError("lvalue has length 3, but rvalue has has length 2",
+ new SkylarkTest().testIfExactError("lvalue has length 3, but rvalue has has length 2",
"def foo (): return [x + y for x, y, z in [(1, 2), (3, 4)]]",
"foo()");
- checkEvalError("type 'int' is not a collection",
+ new SkylarkTest().testIfExactError("type 'int' is not a collection",
"def bar (): return [x + y for x, y in (1, 2)]",
"bar()");
- checkEvalError("lvalue has length 3, but rvalue has has length 2",
+ new SkylarkTest().testIfExactError("lvalue has length 3, but rvalue has has length 2",
"[x + y for x, y, z in [(1, 2), (3, 4)]]");
// can't reuse the same local variable twice(!)
- checkEvalError("ERROR 1:1: Variable x is read only", "[x + y for x, y in (1, 2)]");
+ new SkylarkTest().testIfExactError("ERROR 2:1: Variable x is read only",
+ "[x + y for x, y in (1, 2)]", "[x + y for x, y in (1, 2)]");
- checkEvalError("type 'int' is not a collection", "[x2 + y2 for x2, y2 in (1, 2)]");
+ new SkylarkTest().testIfExactError("type 'int' is not a collection",
+ "[x2 + y2 for x2, y2 in (1, 2)]");
}
@Override
@Test
public void testNotCallInt() throws Exception {
- eval("sum = 123456");
- assertEquals(123456, lookup("sum"));
- checkEvalError("'int' object is not callable", "sum(1, 2, 3, 4, 5, 6)");
- assertEquals(123456, eval("sum"));
+ new SkylarkTest().setUp("sum = 123456").testLookup("sum", 123456)
+ .testIfExactError("'int' object is not callable", "sum(1, 2, 3, 4, 5, 6)")
+ .testStatement("sum", 123456);
}
@Test
public void testConditionalExpressionAtToplevel() throws Exception {
- eval("x = 1 if 2 else 3");
- assertEquals(1, lookup("x"));
+ new SkylarkTest().setUp("x = 1 if 2 else 3").testLookup("x", 1);
}
@Test
public void testConditionalExpressionInFunction() throws Exception {
- eval("def foo(a, b, c): return a+b if c else a-b\n");
- assertEquals(18, eval("foo(23, 5, 0)"));
+ new SkylarkTest().setUp("def foo(a, b, c): return a+b if c else a-b\n").testStatement(
+ "foo(23, 5, 0)", 18);
}
}