aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/test
diff options
context:
space:
mode:
authorGravatar dmarting <dmarting@google.com>2017-09-13 14:28:48 +0200
committerGravatar Philipp Wollermann <philwo@google.com>2017-09-13 19:06:46 +0200
commita92d37752105dcb870d881551b5fb29200f4dadd (patch)
tree1b4e9a89e0f15c118df64185edd40c8bfde64f3d /src/test
parent423d5b568de6973f7f116bddf82f83e597e0b040 (diff)
Automated rollback of commit 72de1fe6bafa28fddcbc8cdc1ccbb59556996bfb.
*** Reason for rollback *** Roll-forward with fix *** Original change description *** Automated rollback of commit f5b8281f1f7599c67476663887db909a4206710e. *** Reason for rollback *** Read the wrong log, it is not timing out, actual failure. *** Original change description *** Open-source src/test/skylark/... This was means to be open-sourced from the beginning but an error in our set-up hide it from the open-source tree. PiperOrigin-RevId: 168526758
Diffstat (limited to 'src/test')
-rw-r--r--src/test/skylark/BUILD66
-rw-r--r--src/test/skylark/question.text1
-rw-r--r--src/test/skylark/skylark_test.py35
-rw-r--r--src/test/skylark/testdata/and_or_not.sky47
-rw-r--r--src/test/skylark/testdata/equality.sky74
-rw-r--r--src/test/skylark/testdata/int.sky69
-rw-r--r--src/test/skylark/testenv.py17
7 files changed, 309 insertions, 0 deletions
diff --git a/src/test/skylark/BUILD b/src/test/skylark/BUILD
new file mode 100644
index 0000000000..fede367e88
--- /dev/null
+++ b/src/test/skylark/BUILD
@@ -0,0 +1,66 @@
+load(
+ "//tools/build_rules:test_rules.bzl",
+ "rule_test",
+ "file_test",
+)
+
+package(default_visibility = ["//src:__subpackages__"])
+
+filegroup(
+ name = "srcs",
+ srcs = glob(
+ ["**"],
+ exclude = ["testenv.py"],
+ ),
+)
+
+py_test(
+ name = "skylark_test",
+ srcs = [
+ "skylark_test.py",
+ "testenv.py",
+ ],
+ data = ["//src/main/java/com/google/devtools/skylark:Skylark"] + glob([
+ "testdata/*",
+ ]),
+)
+
+## Rest of the file should be moved somewhere else (under bazel/tools/).
+
+genrule(
+ name = "tone_below",
+ outs = [
+ "g1",
+ "g2",
+ ],
+ cmd = "echo 48.9994 > $(location :g1) ; echo 97.9989 > $(location :g2)",
+)
+
+rule_test(
+ name = "assert_tone_below_1",
+ generates = [
+ "g1",
+ "g2",
+ ],
+ rule = "tone_below",
+)
+
+file_test(
+ name = "question_content",
+ content = ("If you could ask a unique question to a computer during a Turing test, " +
+ "what would you ask?\n"),
+ file = "question.text",
+)
+
+file_test(
+ name = "question_regexp",
+ file = "question.text",
+ regexp = "[eu]n[uchs questi]\\+on",
+)
+
+file_test(
+ name = "question_regexp_1",
+ file = "question.text",
+ matches = 1,
+ regexp = "what would you ask",
+)
diff --git a/src/test/skylark/question.text b/src/test/skylark/question.text
new file mode 100644
index 0000000000..238bebb86e
--- /dev/null
+++ b/src/test/skylark/question.text
@@ -0,0 +1 @@
+If you could ask a unique question to a computer during a Turing test, what would you ask?
diff --git a/src/test/skylark/skylark_test.py b/src/test/skylark/skylark_test.py
new file mode 100644
index 0000000000..73e033727c
--- /dev/null
+++ b/src/test/skylark/skylark_test.py
@@ -0,0 +1,35 @@
+# Copyright 2017 The Bazel Authors. All rights reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+import os.path
+import subprocess
+import unittest
+
+from src.test.skylark import testenv
+
+
+class SkylarkTest(unittest.TestCase):
+
+ def testSuccess(self):
+ tests = ["int.sky", "equality.sky", "and_or_not.sky"]
+ for t in tests:
+ print subprocess.check_output(
+ [testenv.SKYLARK_BINARY_PATH,
+ os.path.join(testenv.SKYLARK_TESTDATA_PATH, t)])
+
+ # TODO(laurentlb): Add support for negative tests (e.g. syntax errors).
+
+
+if __name__ == "__main__":
+ unittest.main()
diff --git a/src/test/skylark/testdata/and_or_not.sky b/src/test/skylark/testdata/and_or_not.sky
new file mode 100644
index 0000000000..53cfb98f68
--- /dev/null
+++ b/src/test/skylark/testdata/and_or_not.sky
@@ -0,0 +1,47 @@
+def assert_eq(x, y):
+ if x != y:
+ fail("%r != %r" % (x, y))
+
+
+assert_eq(8 or 9, 8)
+assert_eq(0 or 9, 9)
+assert_eq(8 and 9, 9)
+assert_eq(0 and 9, 0)
+
+assert_eq(1 and 2 or 3, 2)
+assert_eq(0 and 2 or 3, 3)
+assert_eq(1 and 0 or 3, 3)
+
+assert_eq(1 or 2 and 3, 1)
+assert_eq(0 or 2 and 3, 3)
+assert_eq(0 or 0 and 3, 0)
+assert_eq(1 or 0 and 3, 1)
+
+assert_eq(None and 1, None)
+assert_eq("" or 9, 9)
+assert_eq("abc" or 9, "abc")
+
+# check that fail() is not evaluated
+assert_eq(8 or fail("do not execute"), 8)
+assert_eq(0 and fail("do not execute"), 0)
+
+assert_eq(not 1, False)
+assert_eq(not "", True)
+
+assert_eq(not 0 + 0, True)
+assert_eq(not 2 - 1, False)
+
+assert_eq(not (0 and 0), True)
+assert_eq(not (1 or 0), False)
+
+assert_eq(0 and not 0, 0)
+assert_eq(not 0 and 0, 0)
+
+assert_eq(1 and not 0, True)
+assert_eq(not 0 or 0, True)
+
+assert_eq(not 1 or 0, 0)
+assert_eq(not 1 or 1, 1)
+
+assert_eq(not [], True)
+assert_eq(not {"a": 1}, False)
diff --git a/src/test/skylark/testdata/equality.sky b/src/test/skylark/testdata/equality.sky
new file mode 100644
index 0000000000..0f72f61a29
--- /dev/null
+++ b/src/test/skylark/testdata/equality.sky
@@ -0,0 +1,74 @@
+def assert_eq(x, y):
+ if x != y:
+ fail("%r != %r" % (x, y))
+
+
+# == operator
+assert_eq(1 == 1, True)
+assert_eq(1 == 2, False)
+assert_eq('hello' == 'hel' + 'lo', True)
+assert_eq('hello' == 'bye', False)
+assert_eq(None == None, True)
+assert_eq([1, 2] == [1, 2], True)
+assert_eq([1, 2] == [2, 1], False)
+assert_eq({'a': 1, 'b': 2} == {'b': 2, 'a': 1}, True)
+assert_eq({'a': 1, 'b': 2} == {'a': 1}, False)
+assert_eq({'a': 1, 'b': 2} == {'a': 1, 'b': 2, 'c': 3}, False)
+assert_eq({'a': 1, 'b': 2} == {'a': 1, 'b': 3}, False)
+
+# != operator
+assert_eq(1 != 1, False)
+assert_eq(1 != 2, True)
+assert_eq('hello' != 'hel' + 'lo', False)
+assert_eq('hello' != 'bye', True)
+assert_eq([1, 2] != [1, 2], False)
+assert_eq([1, 2] != [2, 1], True)
+assert_eq({'a': 1, 'b': 2} != {'b': 2, 'a': 1}, False)
+assert_eq({'a': 1, 'b': 2} != {'a': 1}, True)
+assert_eq({'a': 1, 'b': 2} != {'a': 1, 'b': 2, 'c': 3}, True)
+assert_eq({'a': 1, 'b': 2} != {'a': 1, 'b': 3}, True);
+
+# equality precedence
+assert_eq(1 + 3 == 2 + 2, True)
+assert_eq(not 1 == 2, True)
+assert_eq(not 1 != 2, False)
+assert_eq(2 and 3 == 3 or 1, True)
+assert_eq(2 or 3 == 3 and 1, 2);
+
+# < operator
+assert_eq(1 <= 1, True)
+assert_eq(1 < 1, False)
+assert_eq('a' <= 'b', True)
+assert_eq('c' < 'a', False);
+
+# <= and < operators
+assert_eq(1 <= 1, True)
+assert_eq(1 < 1, False)
+assert_eq('a' <= 'b', True)
+assert_eq('c' < 'a', False);
+
+# >= and > operators
+assert_eq(1 >= 1, True)
+assert_eq(1 > 1, False)
+assert_eq('a' >= 'b', False)
+assert_eq('c' > 'a', True);
+
+# list/tuple comparison
+assert_eq([] < [1], True)
+assert_eq([1] < [1, 1], True)
+assert_eq([1, 1] < [1, 2], True)
+assert_eq([1, 2] < [1, 2, 3], True)
+assert_eq([1, 2, 3] <= [1, 2, 3], True)
+
+assert_eq(['a', 'b'] > ['a'], True)
+assert_eq(['a', 'b'] >= ['a'], True)
+assert_eq(['a', 'b'] < ['a'], False)
+assert_eq(['a', 'b'] <= ['a'], False)
+
+assert_eq(('a', 'b') > ('a', 'b'), False)
+assert_eq(('a', 'b') >= ('a', 'b'), True)
+assert_eq(('a', 'b') < ('a', 'b'), False)
+assert_eq(('a', 'b') <= ('a', 'b'), True)
+
+assert_eq([[1, 1]] > [[1, 1], []], False)
+assert_eq([[1, 1]] < [[1, 1], []], True)
diff --git a/src/test/skylark/testdata/int.sky b/src/test/skylark/testdata/int.sky
new file mode 100644
index 0000000000..cd19f17669
--- /dev/null
+++ b/src/test/skylark/testdata/int.sky
@@ -0,0 +1,69 @@
+# Tests of Skylark 'int'
+
+def assert_eq(x, y):
+ if x != y:
+ fail("%r != %r" % (x, y))
+
+def assert_(cond, msg="assertion failed"):
+ if not cond:
+ fail(msg)
+
+# basic arithmetic
+assert_eq(0 - 1, -1)
+assert_eq(1 + 1, 2)
+assert_eq(5 + 7, 12)
+assert_eq(5 * 7, 35)
+assert_eq(5 - 7, -2)
+
+# truth
+assert_(123)
+assert_(-1)
+assert_(not 0)
+
+# comparisons
+assert_(5 > 2)
+assert_(2 + 1 == 3)
+assert_(2 + 1 >= 3)
+assert_(not (2 + 1 > 3))
+assert_(2 + 2 <= 5)
+assert_(not (2 + 1 < 3))
+
+# division
+assert_eq(100 // 7, 14)
+assert_eq(100 // -7, -15)
+assert_eq(-100 // 7, -15) # NB: different from Go / Java
+assert_eq(-100 // -7, 14) # NB: different from Go / Java
+assert_eq(98 // 7, 14)
+assert_eq(98 // -7, -14)
+assert_eq(-98 // 7, -14)
+assert_eq(-98 // -7, 14)
+
+# remainder
+assert_eq(100 % 7, 2)
+assert_eq(100 % -7, -5) # NB: different from Go / Java
+assert_eq(-100 % 7, 5) # NB: different from Go / Java
+assert_eq(-100 % -7, -2)
+assert_eq(98 % 7, 0)
+assert_eq(98 % -7, 0)
+assert_eq(-98 % 7, 0)
+assert_eq(-98 % -7, 0)
+
+# precedence
+assert_eq(5 - 7 * 2 + 3, -6)
+assert_eq(4 * 5 / 2 + 5 / 2 * 4, 18)
+
+# compound assignment
+def compound():
+ x = 1
+ x += 1
+ assert_eq(x, 2)
+ x -= 3
+ assert_eq(x, -1)
+ x *= 10
+ assert_eq(x, -10)
+ x /= -2
+ assert_eq(x, 5)
+ x %= 3
+ assert_eq(x, 2)
+
+compound()
diff --git a/src/test/skylark/testenv.py b/src/test/skylark/testenv.py
new file mode 100644
index 0000000000..5ac73f534e
--- /dev/null
+++ b/src/test/skylark/testenv.py
@@ -0,0 +1,17 @@
+# Copyright 2017 The Bazel Authors. All rights reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+"""Test constants for src/test/skylark."""
+
+SKYLARK_BINARY_PATH = "src/main/java/com/google/devtools/skylark/Skylark"
+SKYLARK_TESTDATA_PATH = "src/test/skylark/testdata/"