| 1 |
|
| 2 |
import py |
| 3 |
from rpython.annotator.argument import ArgumentsForTranslation, rawshape |
| 4 |
from rpython.flowspace.argument import Signature, CallSpec |
| 5 |
|
| 6 |
class MockArgs(ArgumentsForTranslation): |
| 7 |
def newtuple(self, items): |
| 8 |
return tuple(items) |
| 9 |
|
| 10 |
def unpackiterable(self, it): |
| 11 |
return list(it) |
| 12 |
|
| 13 |
|
| 14 |
class TestArgumentsForTranslation(object): |
| 15 |
|
| 16 |
def test_prepend(self): |
| 17 |
args = MockArgs(["0"]) |
| 18 |
args1 = args.prepend("thingy") |
| 19 |
assert args1 is not args |
| 20 |
assert args1.arguments_w == ["thingy", "0"] |
| 21 |
assert args1.keywords == args.keywords |
| 22 |
|
| 23 |
def test_fixedunpacked(self): |
| 24 |
args = MockArgs([], {"k": 1}) |
| 25 |
py.test.raises(ValueError, args.fixedunpack, 1) |
| 26 |
|
| 27 |
args = MockArgs(["a", "b"]) |
| 28 |
py.test.raises(ValueError, args.fixedunpack, 0) |
| 29 |
py.test.raises(ValueError, args.fixedunpack, 1) |
| 30 |
py.test.raises(ValueError, args.fixedunpack, 3) |
| 31 |
py.test.raises(ValueError, args.fixedunpack, 4) |
| 32 |
|
| 33 |
assert args.fixedunpack(2) == ['a', 'b'] |
| 34 |
|
| 35 |
def test_unmatch_signature(self): |
| 36 |
args = MockArgs([1, 2, 3]) |
| 37 |
sig = Signature(['a', 'b', 'c'], None, None) |
| 38 |
data = args.match_signature(sig, []) |
| 39 |
new_args = args.unmatch_signature(sig, data) |
| 40 |
assert args.unpack() == new_args.unpack() |
| 41 |
|
| 42 |
args = MockArgs([1]) |
| 43 |
sig = Signature(['a', 'b', 'c'], None, None) |
| 44 |
data = args.match_signature(sig, [2, 3]) |
| 45 |
new_args = args.unmatch_signature(sig, data) |
| 46 |
assert args.unpack() == new_args.unpack() |
| 47 |
|
| 48 |
args = MockArgs([1, 2, 3, 4, 5]) |
| 49 |
sig = Signature(['a', 'b', 'c'], 'r', None) |
| 50 |
data = args.match_signature(sig, []) |
| 51 |
new_args = args.unmatch_signature(sig, data) |
| 52 |
assert args.unpack() == new_args.unpack() |
| 53 |
|
| 54 |
args = MockArgs([1], {'c': 3, 'b': 2}) |
| 55 |
sig = Signature(['a', 'b', 'c'], None, None) |
| 56 |
data = args.match_signature(sig, []) |
| 57 |
new_args = args.unmatch_signature(sig, data) |
| 58 |
assert args.unpack() == new_args.unpack() |
| 59 |
|
| 60 |
args = MockArgs([1], {'c': 5}) |
| 61 |
sig = Signature(['a', 'b', 'c'], None, None) |
| 62 |
data = args.match_signature(sig, [2, 3]) |
| 63 |
new_args = args.unmatch_signature(sig, data) |
| 64 |
assert args.unpack() == new_args.unpack() |
| 65 |
|
| 66 |
def test_rawshape(self): |
| 67 |
args = MockArgs([1, 2, 3]) |
| 68 |
assert rawshape(args) == (3, (), False) |
| 69 |
|
| 70 |
args = MockArgs([1, 2, 3, 4, 5]) |
| 71 |
assert rawshape(args) == (5, (), False) |
| 72 |
|
| 73 |
args = MockArgs([1], {'c': 3, 'b': 2}) |
| 74 |
assert rawshape(args) == (1, ('b', 'c'), False) |
| 75 |
|
| 76 |
args = MockArgs([1], {'c': 5}) |
| 77 |
assert rawshape(args) == (1, ('c', ), False) |
| 78 |
|
| 79 |
args = MockArgs([1], {'c': 5, 'd': 7}) |
| 80 |
assert rawshape(args) == (1, ('c', 'd'), False) |
| 81 |
|
| 82 |
args = MockArgs([1, 2, 3, 4, 5], {'e': 5, 'd': 7}) |
| 83 |
assert rawshape(args) == (5, ('d', 'e'), False) |
| 84 |
|
| 85 |
def test_stararg_flowspace_variable(self): |
| 86 |
var = object() |
| 87 |
shape = ((2, ('g', ), True), [1, 2, 9, var]) |
| 88 |
args = MockArgs([1, 2], {'g': 9}, w_stararg=var) |
| 89 |
assert args.flatten() == shape |
| 90 |
|
| 91 |
args = MockArgs.fromshape(*shape) |
| 92 |
assert args.flatten() == shape |
| 93 |
|
| 94 |
def test_fromshape(self): |
| 95 |
shape = ((3, (), False), [1, 2, 3]) |
| 96 |
args = MockArgs.fromshape(*shape) |
| 97 |
assert args.flatten() == shape |
| 98 |
|
| 99 |
shape = ((1, (), False), [1]) |
| 100 |
args = MockArgs.fromshape(*shape) |
| 101 |
assert args.flatten() == shape |
| 102 |
|
| 103 |
shape = ((5, (), False), [1, 2, 3, 4, 5]) |
| 104 |
args = MockArgs.fromshape(*shape) |
| 105 |
assert args.flatten() == shape |
| 106 |
|
| 107 |
shape = ((1, ('b', 'c'), False), [1, 2, 3]) |
| 108 |
args = MockArgs.fromshape(*shape) |
| 109 |
assert args.flatten() == shape |
| 110 |
|
| 111 |
shape = ((1, ('c', ), False), [1, 5]) |
| 112 |
args = MockArgs.fromshape(*shape) |
| 113 |
assert args.flatten() == shape |
| 114 |
|
| 115 |
shape = ((1, ('c', 'd'), False), [1, 5, 7]) |
| 116 |
args = MockArgs.fromshape(*shape) |
| 117 |
assert args.flatten() == shape |
| 118 |
|
| 119 |
shape = ((5, ('d', 'e'), False), [1, 2, 3, 4, 5, 7, 5]) |
| 120 |
args = MockArgs.fromshape(*shape) |
| 121 |
assert args.flatten() == shape |