diff options
author | dmarting <dmarting@google.com> | 2017-09-12 22:02:04 +0200 |
---|---|---|
committer | Philipp Wollermann <philwo@google.com> | 2017-09-13 19:06:07 +0200 |
commit | 95e381bbc877ee367d72b8b42bf36c9b4be8711c (patch) | |
tree | d7fb0530ca3a990324e0e20542d32be4684da81e /src | |
parent | c9e6776d68a74408155dfb72b226a20c181bf88a (diff) |
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: 168427396
Diffstat (limited to 'src')
-rw-r--r-- | src/BUILD | 1 | ||||
-rw-r--r-- | src/test/skylark/BUILD | 60 | ||||
-rw-r--r-- | src/test/skylark/question.text | 1 | ||||
-rw-r--r-- | src/test/skylark/skylark_test.py | 34 | ||||
-rw-r--r-- | src/test/skylark/testdata/and_or_not.sky | 47 | ||||
-rw-r--r-- | src/test/skylark/testdata/equality.sky | 74 | ||||
-rw-r--r-- | src/test/skylark/testdata/int.sky | 69 |
7 files changed, 286 insertions, 0 deletions
@@ -403,6 +403,7 @@ filegroup( "//src/test/docker:srcs", "//src/test/gen:srcs", "//src/test/native:srcs", + "//src/test/skylark:srcs", "//src/test/java/com/google/devtools/build/android:srcs", "//src/test/java/com/google/devtools/build/docgen:srcs", "//src/test/java/com/google/devtools/build/lib:srcs", diff --git a/src/test/skylark/BUILD b/src/test/skylark/BUILD new file mode 100644 index 0000000000..c22bf079d0 --- /dev/null +++ b/src/test/skylark/BUILD @@ -0,0 +1,60 @@ +load( + "//tools/build_rules:test_rules.bzl", + "rule_test", + "file_test", +) + +package(default_visibility = ["//src:__subpackages__"]) + +filegroup( + name = "srcs", + srcs = glob(["**"]), +) + +py_test( + name = "skylark_test", + srcs = ["skylark_test.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..84a866f681 --- /dev/null +++ b/src/test/skylark/skylark_test.py @@ -0,0 +1,34 @@ +# 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 subprocess +import unittest + + +class SkylarkTest(unittest.TestCase): + + def testSuccess(self): + binary_path = ( + "third_party/bazel/src/main/java/com/google/devtools/skylark/Skylark") + test_dir = "third_party/bazel/src/test/skylark/testdata/" + + tests = ["int.sky", "equality.sky", "and_or_not.sky"] + for t in tests: + print subprocess.check_output([binary_path, test_dir + 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() |