Esempio n. 1
0
from libtest import assertRaises

doc = "float"
assert float("1.5") == 1.5
assert float(" 1.5") == 1.5
assert float(" 1.5 ") == 1.5
assert float("1.5 ") == 1.5
assert float("-1E9") == -1E9
assert float("1E400") == float("inf")
assert float(" -1E400") == float("-inf")
assertRaises(ValueError, float, "1 E200")

doc = "finished"
Esempio n. 2
0
# Copyright 2018 The go-python Authors.  All rights reserved.
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.

from libtest import assertRaises

doc = "open"
assertRaises(FileNotFoundError, open, "not-existent.file")

assertRaises(IsADirectoryError, open, ".")

f = open(__file__)
assert f is not None

doc = "read"
b = f.read(12)
assert b == '# Copyright '

b = f.read(4)
assert b == '2018'

b = f.read()
assert b != ''

b = f.read()
assert b == ''

doc = "write"
assertRaises(TypeError, f.write, 42)

# assertRaises(io.UnsupportedOperation, f.write, 'hello')
Esempio n. 3
0
    ok = True
assert ok, "ValueError not raised"

doc = "hex"
assert hex(0) == "0x0", "hex(0)"
assert hex(1) == "0x1", "hex(1)"
assert hex(42) == "0x2a", "hex(42)"
assert hex(-0) == "0x0", "hex(-0)"
assert hex(-1) == "-0x1", "hex(-1)"
assert hex(-42) == "-0x2a", "hex(-42)"
assert hex(1 << 64) == "0x10000000000000000", "hex(1<<64)"
assert hex(-1 << 64) == "-0x10000000000000000", "hex(-1<<64)"
assert hex(1 << 128) == "0x100000000000000000000000000000000", "hex(1<<128)"
assert hex(-1 << 128) == "-0x100000000000000000000000000000000", "hex(-1<<128)"
assertRaises(
    TypeError, hex,
    10.0)  ## TypeError: 'float' object cannot be interpreted as an integer
assertRaises(
    TypeError, hex,
    float(0))  ## TypeError: 'float' object cannot be interpreted as an integer

doc = "isinstance"


class A:
    pass


a = A()
assert isinstance(1, (str, tuple, int))
assert isinstance(a, (str, (tuple, (A, ))))
Esempio n. 4
0
doc = "enumerate"
a = [e for e in enumerate([3, 4, 5, 6, 7], 4)]
idxs = [4, 5, 6, 7, 8]
values = [3, 4, 5, 6, 7]
for idx, value in enumerate(values):
    assert idxs[idx] == a[idx][0]
    assert values[idx] == a[idx][1]

doc = "append"
a = [1, 2, 3]
a.append(4)
assert repr(a) == "[1, 2, 3, 4]"
a = ['a', 'b', 'c']
a.extend(['d', 'e', 'f'])
assert repr(a) == "['a', 'b', 'c', 'd', 'e', 'f']"
assertRaises(TypeError, lambda: [].append())

doc = "mul"
a = [1, 2, 3]
assert a * 2 == [1, 2, 3, 1, 2, 3]
assert a * 0 == []
assert a * -1 == []

doc = "sort"
# [].sort
a = [3, 1.1, 1, 2]
s1 = list(a)
s1.sort()
assert s1 == [1, 1.1, 2, 3]
s1.sort()  # sort a sorted list
assert s1 == [1, 1.1, 2, 3]
Esempio n. 5
0
assert repr(
    '\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\x7f\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f\xa0¡¢£¤¥¦§¨©ª«¬\xad®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ'
) == r"""'\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\x7f\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f\xa0¡¢£¤¥¦§¨©ª«¬\xad®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ'"""
assert repr('\u1000\uffff\U00010000\U0010ffff') == r"""'က\uffff𐀀\U0010ffff'"""

doc = "comparison"
assert "" < "hello"
assert "HELLO" < "hello"
assert "hello" > "HELLO"
assert "HELLO" != "hello"
assert "hello" == "hello"
assert "HELLO" <= "hello"
assert "hello" <= "hello"
assert "hello" >= "HELLO"
assert "hello" <= "hello"
assertRaises(TypeError, lambda: 1 > "potato")
assertRaises(TypeError, lambda: 1 >= "potato")
assertRaises(TypeError, lambda: 1 < "potato")
assertRaises(TypeError, lambda: 1 <= "potato")
assert not (1 == "potato")
assert 1 != "potato"

doc = "bool"
assert "true"
assert not ""

doc = "add"
a = "potato"
a = a + "sausage"
assert a == "potatosausage"
a = "potato"
Esempio n. 6
0
doc="str"
assert str([]) == "[]"
assert str([1,2,3]) == "[1, 2, 3]"
assert str([1,[2,3],4]) == "[1, [2, 3], 4]"
assert str(["1",[2.5,17,[]]]) == "['1', [2.5, 17, []]]"

doc="repr"
assert repr([]) == "[]"
assert repr([1,2,3]) == "[1, 2, 3]"
assert repr([1,[2,3],4]) == "[1, [2, 3], 4]"
assert repr(["1",[2.5,17,[]]]) == "['1', [2.5, 17, []]]"

doc="enumerate"
a = [e for e in enumerate([3,4,5,6,7], 4)]
idxs = [4, 5, 6, 7, 8]
values = [3, 4, 5, 6, 7]
for idx, value in enumerate(values):
    assert idxs[idx] == a[idx][0]
    assert values[idx] == a[idx][1]

doc="append"
a = [1,2,3]
a.append(4)
assert repr(a) == "[1, 2, 3, 4]"
a = ['a', 'b', 'c']
a.extend(['d', 'e', 'f'])
assert repr(a) == "['a', 'b', 'c', 'd', 'e', 'f']"
assertRaises(TypeError, lambda: [].append())

doc="finished"
Esempio n. 7
0
# Copyright 2018 The go-python Authors.  All rights reserved.
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.

from libtest import assertRaises


def fn(x):
    return x


doc = "check internal bound methods"
assert (1).__str__() == "1"
assert (1).__add__(2) == 3
assert fn.__call__(4) == 4
assert fn.__get__(fn, None)()(1) == 1
assertRaises(TypeError, fn.__get__, fn, None, None)
# These tests don't work on python3.4
# assert Exception().__getattr__("a") is not None # check doesn't explode only
# assertRaises(TypeError, Exception().__getattr__, "a", "b")
# assertRaises(ValueError, Exception().__getattr__, 42)

doc = "finished"
Esempio n. 8
0
doc = "whitespace"
assert int(" +100000", 0) == +tenE5
assert int("+100000 ", 0) == +tenE5
assert int("\t\t\t\t100000\t\t\t\t", 0) == tenE5
assert int("	100000	", 0) == tenE5

doc = "sigils"
assert int("7") == 7
assert int("07", 10) == 7

assert int("F", 16) == 15
assert int("0xF", 16) == 15
assert int("0XF", 0) == 15
assert int("0b", 16) == 11
assertRaises(ValueError, int, "0xF", 10)

assert int("77", 8) == 63
assert int("0o77", 0) == 63
assert int("0O77", 8) == 63
assertRaises(ValueError, int, "0o77", 10)

assert int("11", 2) == 3
assert int("0b11", 0) == 3
assert int("0B11", 2) == 3
assertRaises(ValueError, int, "0b11", 10)

doc = "errors"
assertRaises(ValueError, int, "07", 0)
assertRaises(ValueError, int, "", 0)
assertRaises(ValueError, int, "  ", 0)
Esempio n. 9
0
    assert False, "TypeError not raised"
    
doc="exec"
glob = {"a":100}
assert exec("b = a+100", glob) == None
assert glob["b"] == 200
loc = {"c":23}
assert exec("d = a+b+c", glob, loc) == None
assert loc["d"] == 323
co = compile("d = a+b+c+1", "s", "exec")
assert eval(co, glob, loc) == None
assert loc["d"] == 324

try:
    exec("if")
except SyntaxError as e:
    pass
else:
    assert False, "SyntaxError not raised"

doc="isinstance"
class A:
    pass
a = A()
assert True, isinstance(1, (str, tuple, int))
assert True, isinstance(a, (str, (tuple, (A, ))))
assertRaises(TypeError, isinstance, 1, (A, ), "foo")
assertRaises(TypeError, isinstance, 1, [A, "foo"])

doc="finished"
Esempio n. 10
0
a = {"a": 1}
assert a.get('a') == 1
assert a.get('a', 100) == 1
assert a.get('b') == None
assert a.get('b', 1) == 1
assert a.get('b', True) == True

doc = "check items"
a = {"a": "b", "c": 5.5}
for k, v in a.items():
    assert k in ["a", "c"]
    if k == "a":
        assert v == "b"
    if k == "c":
        assert v == 5.5
assertRaises(TypeError, a.items, 'a')

doc = "__contain__"
a = {'hello': 'world'}
assert a.__contains__('hello')
assert not a.__contains__('world')

doc = "__eq__, __ne__"
a = {'a': 'b'}
assert a.__eq__(3) != True
assert a.__ne__(3) != False
assert a.__ne__(3) != True
assert a.__ne__(3) != False

assert a.__ne__({}) == True
assert a.__eq__({'a': 'b'}) == True