aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/test/skylark
diff options
context:
space:
mode:
authorGravatar laurentlb <laurentlb@google.com>2017-11-28 09:12:56 -0800
committerGravatar Copybara-Service <copybara-piper@google.com>2017-11-28 09:14:30 -0800
commitf8003cc399202c4b3fabade424235e643f99ea5c (patch)
treec378080a7354783cb0a1f4144debcd459bb3738f /src/test/skylark
parent059b52b71b509a0d94cbb5ad688c04af6b9e22b4 (diff)
Migrate some Skylark tests outside of Blaze.
RELNOTES: None PiperOrigin-RevId: 177169878
Diffstat (limited to 'src/test/skylark')
-rw-r--r--src/test/skylark/testdata/int_constructor.sky44
-rw-r--r--src/test/skylark/testdata/list_slices.sky57
-rw-r--r--src/test/skylark/testdata/string_format.sky93
3 files changed, 194 insertions, 0 deletions
diff --git a/src/test/skylark/testdata/int_constructor.sky b/src/test/skylark/testdata/int_constructor.sky
new file mode 100644
index 0000000000..03c43e4d45
--- /dev/null
+++ b/src/test/skylark/testdata/int_constructor.sky
@@ -0,0 +1,44 @@
+assert_eq(int('1'), 1)
+assert_eq(int('-1234'), -1234)
+assert_eq(int(42), 42)
+assert_eq(int(-1), -1)
+assert_eq(int(True), 1)
+assert_eq(int(False), 0)
+assert_eq(int('11', 2), 3)
+assert_eq(int('11', 9), 10)
+assert_eq(int('AF', 16), 175)
+assert_eq(int('11', 36), 37)
+assert_eq(int('az', 36), 395)
+assert_eq(int('11', 10), 11)
+assert_eq(int('11', 0), 11)
+assert_eq(int('0b11', 0), 3)
+assert_eq(int('0B11', 2), 3)
+assert_eq(int('0o11', 0), 9)
+assert_eq(int('0O11', 8), 9)
+assert_eq(int('0XFF', 0), 255)
+assert_eq(int('0xFF', 16), 255)
+
+---
+int('1.5') ### invalid literal for int\(\) with base 10
+---
+int('ab') ### invalid literal for int\(\) with base 10: "ab"
+---
+int(None) ### None is not of type string or int or bool
+---
+int('123', 3) ### invalid literal for int\(\) with base 3: "123"
+---
+int('FF', 15) ### invalid literal for int\(\) with base 15: "FF"
+---
+int('123', -1) ### int\(\) base must be >= 2 and <= 36
+---
+int('123', 1) ### int\(\) base must be >= 2 and <= 36
+---
+int('123', 37) ### int\(\) base must be >= 2 and <= 36
+---
+int('0xFF', 8) ### invalid literal for int\(\) with base 8: "0xFF"
+---
+int(True, 2) ### int\(\) can't convert non-string with explicit base
+---
+int(1, 2) ### int\(\) can't convert non-string with explicit base
+---
+int(True, 10) ### int\(\) can't convert non-string with explicit base
diff --git a/src/test/skylark/testdata/list_slices.sky b/src/test/skylark/testdata/list_slices.sky
new file mode 100644
index 0000000000..05d422621d
--- /dev/null
+++ b/src/test/skylark/testdata/list_slices.sky
@@ -0,0 +1,57 @@
+# Without step
+assert_eq([0, 1, 2, 3][0:-1], [0, 1, 2])
+assert_eq([0, 1, 2, 3, 4, 5][2:4], [2, 3])
+assert_eq([0, 1, 2, 3, 4, 5][-2:-1], [4])
+assert_eq([][1:2], [])
+assert_eq([0, 1, 2, 3][-10:10], [0, 1, 2, 3])
+
+# With step
+assert_eq([1, 2, 3, 4, 5][::1], [1, 2, 3, 4, 5])
+assert_eq([1, 2, 3, 4, 5][1::1], [2, 3, 4, 5])
+assert_eq([1, 2, 3, 4, 5][:2:1], [1, 2])
+assert_eq([1, 2, 3, 4, 5][1:3:1], [2, 3])
+assert_eq([1, 2, 3, 4, 5][-4:-2:1], [2, 3])
+assert_eq([1, 2, 3, 4, 5][-10:10:1], [1, 2, 3, 4, 5])
+assert_eq([1, 2, 3, 4, 5][::42], [1])
+assert_eq([][::1], [])
+assert_eq([][::-1], [])
+assert_eq([1, 2, 3, 4, 5, 6, 7][::3], [1, 4, 7])
+assert_eq([1, 2, 3, 4, 5, 6, 7, 8, 9][1:7:3], [2, 5])
+assert_eq([1, 2, 3][3:1:1], [])
+assert_eq([1, 2, 3][1:3:-1], [])
+
+# Negative step
+assert_eq([1, 2, 3, 4, 5][::-1], [5, 4, 3, 2, 1])
+assert_eq([1, 2, 3, 4, 5][4::-1], [5, 4, 3, 2, 1])
+assert_eq([1, 2, 3, 4, 5][:0:-1], [5, 4, 3, 2])
+assert_eq([1, 2, 3, 4, 5][3:1:-1], [4, 3])
+assert_eq([1, 2, 3, 4, 5][::-2], [5, 3, 1])
+assert_eq([1, 2, 3, 4, 5][::-10], [5])
+
+# Tuples
+assert_eq(()[1:2], ())
+assert_eq(()[::1], ())
+assert_eq((0, 1, 2, 3)[0:-1], (0, 1, 2))
+assert_eq((0, 1, 2, 3, 4, 5)[2:4], (2, 3))
+assert_eq((0, 1, 2, 3)[-10:10], (0, 1, 2, 3))
+assert_eq((1, 2, 3, 4, 5)[-10:10:1], (1, 2, 3, 4, 5))
+assert_eq((1, 2, 3, 4, 5, 6, 7, 8, 9)[1:7:3], (2, 5))
+assert_eq((1, 2, 3, 4, 5)[::-1], (5, 4, 3, 2, 1))
+assert_eq((1, 2, 3, 4, 5)[3:1:-1], (4, 3))
+assert_eq((1, 2, 3, 4, 5)[::-2], (5, 3, 1))
+assert_eq((1, 2, 3, 4, 5)[::-10], (5,))
+
+---
+'123'['a'::] ### slice start must be an integer, not 'a'
+---
+'123'[:'b':] ### slice end must be an integer, not 'b'
+---
+(1, 2, 3)[1::0] ### slice step cannot be zero
+---
+[1, 2, 3][::0] ### slice step cannot be zero
+---
+[1, 2, 3][1::0] ### slice step cannot be zero
+---
+[1, 2, 3][:3:0] ### slice step cannot be zero
+---
+[1, 2, 3][1:3:0] ### slice step cannot be zero
diff --git a/src/test/skylark/testdata/string_format.sky b/src/test/skylark/testdata/string_format.sky
new file mode 100644
index 0000000000..58c428821b
--- /dev/null
+++ b/src/test/skylark/testdata/string_format.sky
@@ -0,0 +1,93 @@
+assert_eq('abc'.format(), "abc")
+
+# named arguments
+assert_eq('x{key}x'.format(key = 2), "x2x")
+assert_eq('x{key}x'.format(key = 'abc'), "xabcx")
+assert_eq('{a}{b}{a}{b}'.format(a = 3, b = True), "3True3True")
+assert_eq('{a}{b}{a}{b}'.format(a = 3, b = True), "3True3True")
+assert_eq('{s1}{s2}'.format(s1 = ['a'], s2 = 'a'), "[\"a\"]a")
+assert_eq('{a}'.format(a = '$'), "$")
+assert_eq('{a}'.format(a = '$a'), "$a")
+assert_eq('{a}$'.format(a = '$a'), "$a$")
+assert_eq('{(}'.format(**{'(': 2}), "2")
+
+# curly brace escaping
+assert_eq('{{}}'.format(), "{}")
+assert_eq('{{}}'.format(42), "{}")
+assert_eq('{{ }}'.format(), "{ }")
+assert_eq('{{ }}'.format(42), "{ }")
+assert_eq('{{{{}}}}'.format(), "{{}}")
+assert_eq('{{{{}}}}'.format(42), "{{}}")
+assert_eq('{{0}}'.format(42), "{0}")
+assert_eq('{{}}'.format(42), "{}")
+assert_eq('{{{}}}'.format(42), "{42}")
+assert_eq('{{ '.format(42), "{ " )
+assert_eq(' }}'.format(42), " }")
+assert_eq('{{ {}'.format(42), "{ 42")
+assert_eq('{} }}'.format(42), "42 }")
+assert_eq('{{0}}'.format(42), "{0}")
+assert_eq('{{{0}}}'.format(42), "{42}")
+assert_eq('{{ 0'.format(42), "{ 0")
+assert_eq('0 }}'.format(42), "0 }")
+assert_eq('{{ {0}'.format(42), "{ 42")
+assert_eq('{0} }}'.format(42), "42 }")
+assert_eq('{{test}}'.format(test = 42), "{test}")
+assert_eq('{{{test}}}'.format(test = 42), "{42}")
+assert_eq('{{ test'.format(test = 42), "{ test")
+assert_eq('test }}'.format(test = 42), "test }")
+assert_eq('{{ {test}'.format(test = 42), "{ 42")
+assert_eq('{test} }}'.format(test = 42), "42 }")
+
+
+# Automatic positionals
+assert_eq('{}, {} {} {} test'.format('hi', 'this', 'is', 'a'), "hi, this is a test")
+assert_eq('skip some {}'.format('arguments', 'obsolete', 'deprecated'), "skip some arguments")
+
+# with numbered positions
+assert_eq('{0}, {1} {2} {3} test'.format('hi', 'this', 'is', 'a'), "hi, this is a test")
+assert_eq('{3}, {2} {1} {0} test'.format('a', 'is', 'this', 'hi'), "hi, this is a test")
+assert_eq('skip some {0}'.format('arguments', 'obsolete', 'deprecated'), "skip some arguments")
+assert_eq('{0} can be reused: {0}'.format('this', 'obsolete'), "this can be reused: this")
+
+# Mixed fields
+assert_eq('{test} and {}'.format(2, test = 1), "1 and 2")
+assert_eq('{test} and {0}'.format(2, test = 1), "1 and 2")
+
+---
+'{{}'.format(1) ### Found '}' without matching '{'
+---
+'{}}'.format(1) ### Found '}' without matching '{'
+---
+'{0}'.format() ### No replacement found for index 0
+---
+'{0} and {1}'.format('this') ### No replacement found for index 1
+---
+'{0} and {2}'.format('this', 'that') ### No replacement found for index 2
+---
+'{-0} and {-1}'.format('this', 'that') ### No replacement found for index -1
+---
+'{0,1} and {1}'.format('this', 'that') ### Invalid character ',' inside replacement field
+---
+'{0.1} and {1}'.format('this', 'that') ### Invalid character '.' inside replacement field
+---
+'{}'.format() ### No replacement found for index 0
+---
+'{} and {}'.format('this') ### No replacement found for index 1
+---
+'{test} and {}'.format(test = 1, 2) ### non-keyword arg after keyword arg
+---
+'{test} and {0}'.format(test = 1, 2) ### non-keyword arg after keyword arg
+---
+'{} and {1}'.format(1, 2) ### Cannot mix manual and automatic numbering of positional fields
+---
+'{1} and {}'.format(1, 2) ### Cannot mix manual and automatic numbering of positional fields
+---
+'{test.}'.format(test = 1) ### Invalid character '.' inside replacement field
+---
+'{test[}'.format(test = 1) ### Invalid character '\[' inside replacement field
+---
+'{test,}'.format(test = 1) ### Invalid character ',' inside replacement field
+---
+'{ {} }'.format(42) ### Nested replacement fields are not supported
+---
+'{a}{b}'.format(a = 5) ### Missing argument 'b'