github.com/grumpyhome/grumpy@v0.3.1-0.20201208125205-7b775405bdf1/grumpy-runtime-src/runtime/builtin_types_test.go (about)

     1  // Copyright 2016 Google Inc. All Rights Reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package grumpy
    16  
    17  import (
    18  	"bytes"
    19  	"fmt"
    20  	"io"
    21  	"math/big"
    22  	"os"
    23  	"testing"
    24  )
    25  
    26  func TestBuiltinDelAttr(t *testing.T) {
    27  	f := NewRootFrame()
    28  	delattr := mustNotRaise(Builtins.GetItemString(f, "delattr"))
    29  	fooType := newTestClass("Foo", []*Type{ObjectType}, NewDict())
    30  	fooForDelAttr := newObject(fooType)
    31  	fooValue := newObject(ObjectType)
    32  	mustNotRaise(nil, SetAttr(f, fooForDelAttr, NewStr("bar"), fooValue))
    33  	fun := wrapFuncForTest(func(f *Frame, args ...*Object) (*Object, *BaseException) {
    34  		result, raised := delattr.Call(f, args, nil)
    35  		if raised != nil {
    36  			return nil, raised
    37  		}
    38  		val, raised := GetAttr(f, args[0], toStrUnsafe(args[1]), None)
    39  		return newTestTuple(result, val == fooValue).ToObject(), nil
    40  	})
    41  	cases := []invokeTestCase{
    42  		{args: wrapArgs(fooForDelAttr, "bar"), want: newTestTuple(None, False.ToObject()).ToObject()},
    43  		{args: wrapArgs(fooForDelAttr, "baz"), wantExc: mustCreateException(AttributeErrorType, "'Foo' object has no attribute 'baz'")},
    44  		{args: wrapArgs(fooForDelAttr), wantExc: mustCreateException(TypeErrorType, "'delattr' requires 2 arguments")},
    45  		{args: wrapArgs(fooForDelAttr, "foo", "bar"), wantExc: mustCreateException(TypeErrorType, "'delattr' requires 2 arguments")},
    46  	}
    47  	for _, cas := range cases {
    48  		if err := runInvokeTestCase(fun, &cas); err != "" {
    49  			t.Error(err)
    50  		}
    51  	}
    52  }
    53  
    54  func TestBuiltinFuncs(t *testing.T) {
    55  	f := NewRootFrame()
    56  	objectDir := ObjectType.Dict().Keys(f)
    57  	objectDir.Sort(f)
    58  	fooType := newTestClass("Foo", []*Type{ObjectType}, newStringDict(map[string]*Object{"bar": None}))
    59  	fooTypeDir := NewList(objectDir.elems...)
    60  	fooTypeDir.Append(NewStr("bar").ToObject())
    61  	fooTypeDir.Sort(f)
    62  	foo := newObject(fooType)
    63  	SetAttr(f, foo, NewStr("baz"), None)
    64  	fooDir := NewList(fooTypeDir.elems...)
    65  	fooDir.Append(NewStr("baz").ToObject())
    66  	fooDir.Sort(f)
    67  	dirModule := newTestModule("foo", "foo.py")
    68  	if raised := dirModule.Dict().SetItemString(NewRootFrame(), "bar", newObject(ObjectType)); raised != nil {
    69  		panic(raised)
    70  	}
    71  	dirModuleDir := dirModule.Dict().Keys(NewRootFrame())
    72  	if raised := dirModuleDir.Sort(NewRootFrame()); raised != nil {
    73  		panic(raised)
    74  	}
    75  	iter := mustNotRaise(Iter(f, mustNotRaise(xrangeType.Call(f, wrapArgs(5), nil))))
    76  	neg := wrapFuncForTest(func(f *Frame, i int) int { return -i })
    77  	raiseKey := wrapFuncForTest(func(f *Frame, o *Object) *BaseException { return f.RaiseType(RuntimeErrorType, "foo") })
    78  	hexOctType := newTestClass("HexOct", []*Type{ObjectType}, newStringDict(map[string]*Object{
    79  		"__hex__": newBuiltinFunction("__hex__", func(f *Frame, _ Args, _ KWArgs) (*Object, *BaseException) {
    80  			return NewStr("0xhexadecimal").ToObject(), nil
    81  		}).ToObject(),
    82  		"__oct__": newBuiltinFunction("__hex__", func(f *Frame, _ Args, _ KWArgs) (*Object, *BaseException) {
    83  			return NewStr("0octal").ToObject(), nil
    84  		}).ToObject(),
    85  	}))
    86  	badNonZeroType := newTestClass("BadNonZeroType", []*Type{ObjectType}, newStringDict(map[string]*Object{
    87  		"__nonzero__": newBuiltinFunction("__nonzero__", func(f *Frame, args Args, kwargs KWArgs) (*Object, *BaseException) {
    88  			return nil, f.RaiseType(RuntimeErrorType, "foo")
    89  		}).ToObject(),
    90  	}))
    91  	badNextType := newTestClass("BadNextType", []*Type{ObjectType}, newStringDict(map[string]*Object{
    92  		"next": newBuiltinFunction("next", func(f *Frame, args Args, kwargs KWArgs) (*Object, *BaseException) {
    93  			return nil, f.RaiseType(RuntimeErrorType, "foo")
    94  		}).ToObject(),
    95  	}))
    96  	badIterType := newTestClass("BadIterType", []*Type{ObjectType}, newStringDict(map[string]*Object{
    97  		"__iter__": newBuiltinFunction("__iter__", func(f *Frame, args Args, kwargs KWArgs) (*Object, *BaseException) {
    98  			return newObject(badNextType), nil
    99  		}).ToObject(),
   100  	}))
   101  	createNextType := func(n int) *Type {
   102  		i := -1
   103  		nextType := newTestClass("FooNextType", []*Type{ObjectType}, newStringDict(map[string]*Object{
   104  			"next": newBuiltinFunction("next", func(f *Frame, args Args, kwargs KWArgs) (*Object, *BaseException) {
   105  				if i >= n {
   106  					return nil, f.RaiseType(StopIterationType, "foo")
   107  				}
   108  				i++
   109  				return NewInt(i).ToObject(), nil
   110  			}).ToObject(),
   111  		}))
   112  		return nextType
   113  	}
   114  	fooIterType := newTestClass("FooIterType", []*Type{ObjectType}, newStringDict(map[string]*Object{
   115  		"__iter__": newBuiltinFunction("__iter__", func(f *Frame, args Args, kwargs KWArgs) (*Object, *BaseException) {
   116  			return newObject(createNextType(3)), nil
   117  		}).ToObject(),
   118  	}))
   119  	customIterStrType := newTestClass("CustomIterStrType", []*Type{StrType}, newStringDict(map[string]*Object{
   120  		"__iter__": newBuiltinFunction("__iter__", func(f *Frame, args Args, kwargs KWArgs) (*Object, *BaseException) {
   121  			return newObject(createNextType(3)), nil
   122  		}).ToObject(),
   123  	}))
   124  	customIterTupleType := newTestClass("CustomIterTupleType", []*Type{TupleType}, newStringDict(map[string]*Object{
   125  		"__iter__": newBuiltinFunction("__iter__", func(f *Frame, args Args, kwargs KWArgs) (*Object, *BaseException) {
   126  			return newObject(createNextType(3)), nil
   127  		}).ToObject(),
   128  	}))
   129  	addType := newTestClass("Add", []*Type{ObjectType}, newStringDict(map[string]*Object{
   130  		"__add__": newBuiltinFunction("__add__", func(f *Frame, _ Args, _ KWArgs) (*Object, *BaseException) {
   131  			return NewInt(1).ToObject(), nil
   132  		}).ToObject(),
   133  	}))
   134  	fooBuiltinFunc := newBuiltinFunction("foo", func(f *Frame, args Args, kwargs KWArgs) (*Object, *BaseException) {
   135  		return newTestTuple(NewTuple(args.makeCopy()...), kwargs.makeDict()).ToObject(), nil
   136  	}).ToObject()
   137  	fooFunc := NewFunction(NewCode("foo", "foo.py", nil, CodeFlagVarArg, func(f *Frame, args []*Object) (*Object, *BaseException) {
   138  		return args[0], nil
   139  	}), nil)
   140  	cases := []struct {
   141  		f       string
   142  		args    Args
   143  		kwargs  KWArgs
   144  		want    *Object
   145  		wantExc *BaseException
   146  	}{
   147  		{f: "abs", args: wrapArgs(1, 2, 3), wantExc: mustCreateException(TypeErrorType, "'abs' requires 1 arguments")},
   148  		{f: "abs", args: wrapArgs(1), want: NewInt(1).ToObject()},
   149  		{f: "abs", args: wrapArgs(-1), want: NewInt(1).ToObject()},
   150  		{f: "abs", args: wrapArgs(big.NewInt(2)), want: NewLong(big.NewInt(2)).ToObject()},
   151  		{f: "abs", args: wrapArgs(big.NewInt(-2)), want: NewLong(big.NewInt(2)).ToObject()},
   152  		{f: "abs", args: wrapArgs(NewFloat(3.4)), want: NewFloat(3.4).ToObject()},
   153  		{f: "abs", args: wrapArgs(NewFloat(-3.4)), want: NewFloat(3.4).ToObject()},
   154  		{f: "abs", args: wrapArgs(MinInt), want: NewLong(big.NewInt(MinInt).Neg(minIntBig)).ToObject()},
   155  		{f: "abs", args: wrapArgs(NewStr("a")), wantExc: mustCreateException(TypeErrorType, "bad operand type for abs(): 'str'")},
   156  		{f: "all", args: wrapArgs(newTestList()), want: True.ToObject()},
   157  		{f: "all", args: wrapArgs(newTestList(1, 2, 3)), want: True.ToObject()},
   158  		{f: "all", args: wrapArgs(newTestList(1, 0, 1)), want: False.ToObject()},
   159  		{f: "all", args: wrapArgs(13), wantExc: mustCreateException(TypeErrorType, "'int' object is not iterable")},
   160  		{f: "all", args: wrapArgs(newTestList(newObject(badNonZeroType))), wantExc: mustCreateException(RuntimeErrorType, "foo")},
   161  		{f: "all", args: wrapArgs(newObject(badIterType)), wantExc: mustCreateException(RuntimeErrorType, "foo")},
   162  		{f: "any", args: wrapArgs(newTestList()), want: False.ToObject()},
   163  		{f: "any", args: wrapArgs(newTestList(1, 2, 3)), want: True.ToObject()},
   164  		{f: "any", args: wrapArgs(newTestList(1, 0, 1)), want: True.ToObject()},
   165  		{f: "any", args: wrapArgs(newTestList(0, 0, 0)), want: False.ToObject()},
   166  		{f: "any", args: wrapArgs(newTestList(False.ToObject(), False.ToObject())), want: False.ToObject()},
   167  		{f: "any", args: wrapArgs(13), wantExc: mustCreateException(TypeErrorType, "'int' object is not iterable")},
   168  		{f: "any", args: wrapArgs(newTestList(newObject(badNonZeroType))), wantExc: mustCreateException(RuntimeErrorType, "foo")},
   169  		{f: "any", args: wrapArgs(newObject(badIterType)), wantExc: mustCreateException(RuntimeErrorType, "foo")},
   170  		{f: "bin", args: wrapArgs(64 + 8 + 1), want: NewStr("0b1001001").ToObject()},
   171  		{f: "bin", args: wrapArgs(MinInt), want: NewStr(fmt.Sprintf("-0b%b0", -(MinInt >> 1))).ToObject()},
   172  		{f: "bin", args: wrapArgs(0), want: NewStr("0b0").ToObject()},
   173  		{f: "bin", args: wrapArgs(1), want: NewStr("0b1").ToObject()},
   174  		{f: "bin", args: wrapArgs(-1), want: NewStr("-0b1").ToObject()},
   175  		{f: "bin", args: wrapArgs(big.NewInt(-1)), want: NewStr("-0b1").ToObject()},
   176  		{f: "bin", args: wrapArgs("foo"), wantExc: mustCreateException(TypeErrorType, "str object cannot be interpreted as an index")},
   177  		{f: "bin", args: wrapArgs(0.1), wantExc: mustCreateException(TypeErrorType, "float object cannot be interpreted as an index")},
   178  		{f: "bin", args: wrapArgs(1, 2, 3), wantExc: mustCreateException(TypeErrorType, "'bin' requires 1 arguments")},
   179  		{f: "bin", args: wrapArgs(newTestIndexObject(123)), want: NewStr("0b1111011").ToObject()},
   180  		{f: "callable", args: wrapArgs(fooBuiltinFunc), want: True.ToObject()},
   181  		{f: "callable", args: wrapArgs(fooFunc), want: True.ToObject()},
   182  		{f: "callable", args: wrapArgs(0), want: False.ToObject()},
   183  		{f: "callable", args: wrapArgs(0.1), want: False.ToObject()},
   184  		{f: "callable", args: wrapArgs("foo"), want: False.ToObject()},
   185  		{f: "callable", args: wrapArgs(newTestDict("foo", 1, "bar", 2)), want: False.ToObject()},
   186  		{f: "callable", args: wrapArgs(newTestList(1, 2, 3)), want: False.ToObject()},
   187  		{f: "callable", args: wrapArgs(iter), want: False.ToObject()},
   188  		{f: "callable", args: wrapArgs(1, 2), wantExc: mustCreateException(TypeErrorType, "'callable' requires 1 arguments")},
   189  		{f: "chr", args: wrapArgs(0), want: NewStr("\x00").ToObject()},
   190  		{f: "chr", args: wrapArgs(65), want: NewStr("A").ToObject()},
   191  		{f: "chr", args: wrapArgs(300), wantExc: mustCreateException(ValueErrorType, "chr() arg not in range(256)")},
   192  		{f: "chr", args: wrapArgs(-1), wantExc: mustCreateException(ValueErrorType, "chr() arg not in range(256)")},
   193  		{f: "chr", args: wrapArgs(), wantExc: mustCreateException(TypeErrorType, "'chr' requires 1 arguments")},
   194  		{f: "dir", args: wrapArgs(newObject(ObjectType)), want: objectDir.ToObject()},
   195  		{f: "dir", args: wrapArgs(newObject(fooType)), want: fooTypeDir.ToObject()},
   196  		{f: "dir", args: wrapArgs(fooType), want: fooTypeDir.ToObject()},
   197  		{f: "dir", args: wrapArgs(foo), want: fooDir.ToObject()},
   198  		{f: "dir", args: wrapArgs(dirModule), want: dirModuleDir.ToObject()},
   199  		{f: "dir", args: wrapArgs(), wantExc: mustCreateException(TypeErrorType, "'dir' requires 1 arguments")},
   200  		{f: "divmod", args: wrapArgs(12, 7), want: NewTuple2(NewInt(1).ToObject(), NewInt(5).ToObject()).ToObject()},
   201  		{f: "divmod", args: wrapArgs(-12, 7), want: NewTuple2(NewInt(-2).ToObject(), NewInt(2).ToObject()).ToObject()},
   202  		{f: "divmod", args: wrapArgs(12, -7), want: NewTuple2(NewInt(-2).ToObject(), NewInt(-2).ToObject()).ToObject()},
   203  		{f: "divmod", args: wrapArgs(-12, -7), want: NewTuple2(NewInt(1).ToObject(), NewInt(-5).ToObject()).ToObject()},
   204  		{f: "divmod", args: wrapArgs(MaxInt, MinInt), want: NewTuple2(NewInt(-1).ToObject(), NewInt(-1).ToObject()).ToObject()},
   205  		{f: "divmod", args: wrapArgs(MinInt, MaxInt), want: NewTuple2(NewInt(-2).ToObject(), NewInt(MaxInt-1).ToObject()).ToObject()},
   206  		{f: "divmod", args: wrapArgs(MinInt, -1), want: NewTuple2(NewLong(new(big.Int).Neg(minIntBig)).ToObject(), NewLong(big.NewInt(0)).ToObject()).ToObject()},
   207  		{f: "divmod", args: wrapArgs(big.NewInt(12), big.NewInt(7)), want: NewTuple2(NewLong(big.NewInt(1)).ToObject(), NewLong(big.NewInt(5)).ToObject()).ToObject()},
   208  		{f: "divmod", args: wrapArgs(big.NewInt(-12), big.NewInt(7)), want: NewTuple2(NewLong(big.NewInt(-2)).ToObject(), NewLong(big.NewInt(2)).ToObject()).ToObject()},
   209  		{f: "divmod", args: wrapArgs(big.NewInt(12), big.NewInt(-7)), want: NewTuple2(NewLong(big.NewInt(-2)).ToObject(), NewLong(big.NewInt(-2)).ToObject()).ToObject()},
   210  		{f: "divmod", args: wrapArgs(big.NewInt(-12), big.NewInt(-7)), want: NewTuple2(NewLong(big.NewInt(1)).ToObject(), NewLong(big.NewInt(-5)).ToObject()).ToObject()},
   211  		{f: "divmod", args: wrapArgs(3.25, 1.0), want: NewTuple2(NewFloat(3.0).ToObject(), NewFloat(0.25).ToObject()).ToObject()},
   212  		{f: "divmod", args: wrapArgs(-3.25, 1.0), want: NewTuple2(NewFloat(-4.0).ToObject(), NewFloat(0.75).ToObject()).ToObject()},
   213  		{f: "divmod", args: wrapArgs(3.25, -1.0), want: NewTuple2(NewFloat(-4.0).ToObject(), NewFloat(-0.75).ToObject()).ToObject()},
   214  		{f: "divmod", args: wrapArgs(-3.25, -1.0), want: NewTuple2(NewFloat(3.0).ToObject(), NewFloat(-0.25).ToObject()).ToObject()},
   215  		{f: "divmod", args: wrapArgs(NewStr("a"), NewStr("b")), wantExc: mustCreateException(TypeErrorType, "unsupported operand type(s) for divmod(): 'str' and 'str'")},
   216  		{f: "divmod", args: wrapArgs(), wantExc: mustCreateException(TypeErrorType, "'divmod' requires 2 arguments")},
   217  		{f: "filter", args: wrapArgs(None, NewTuple2(NewInt(0).ToObject(), NewInt(1).ToObject())), want: NewTuple1(NewInt(1).ToObject()).ToObject()},
   218  		{f: "filter", args: wrapArgs(BoolType, NewTuple2(NewInt(0).ToObject(), NewInt(1).ToObject())), want: NewTuple1(NewInt(1).ToObject()).ToObject()},
   219  		{f: "filter", args: wrapArgs(None, "012"), want: NewStr("012").ToObject()},
   220  		{f: "filter", args: wrapArgs(IntType, "012"), want: NewStr("12").ToObject()},
   221  		{f: "filter", args: wrapArgs(None, NewUnicode("012")), want: NewUnicode("012").ToObject()},
   222  		{f: "filter", args: wrapArgs(None, newTestList(1, 0, 3)), want: newTestList(1, 3).ToObject()},
   223  		{f: "filter", args: wrapArgs(IntType, newTestList("1", "0", "3")), want: newTestList("1", "3").ToObject()},
   224  		{f: "filter", args: wrapArgs(BoolType, newObject(fooIterType)), want: newTestList(1, 2, 3).ToObject()},
   225  		{f: "filter", args: wrapArgs(BoolType, &Str{Object: Object{typ: customIterStrType}, value: "foo"}), want: NewStr("foo").ToObject()},
   226  		{f: "filter", args: wrapArgs(BoolType, &Tuple{Object: Object{typ: customIterTupleType}, elems: []*Object{NewInt(5).ToObject()}}), want: NewTuple1(NewInt(5).ToObject()).ToObject()},
   227  		{f: "getattr", args: wrapArgs(None, NewStr("foo").ToObject(), NewStr("bar").ToObject()), want: NewStr("bar").ToObject()},
   228  		{f: "getattr", args: wrapArgs(None, NewStr("foo").ToObject()), wantExc: mustCreateException(AttributeErrorType, "'NoneType' object has no attribute 'foo'")},
   229  		{f: "hasattr", args: wrapArgs(newObject(ObjectType), NewStr("foo").ToObject()), want: False.ToObject()},
   230  		{f: "hasattr", args: wrapArgs(foo, NewStr("bar").ToObject()), want: True.ToObject()},
   231  		{f: "hasattr", args: wrapArgs(foo, NewStr("baz").ToObject()), want: True.ToObject()},
   232  		{f: "hasattr", args: wrapArgs(foo, NewStr("qux").ToObject()), want: False.ToObject()},
   233  		{f: "hash", args: wrapArgs(123), want: NewInt(123).ToObject()},
   234  		{f: "hash", args: wrapArgs("foo"), want: hashFoo},
   235  		{f: "hash", args: wrapArgs(NewList()), wantExc: mustCreateException(TypeErrorType, "unhashable type: 'list'")},
   236  		{f: "hex", args: wrapArgs(0x63adbeef), want: NewStr("0x63adbeef").ToObject()},
   237  		{f: "hex", args: wrapArgs(0), want: NewStr("0x0").ToObject()},
   238  		{f: "hex", args: wrapArgs(1), want: NewStr("0x1").ToObject()},
   239  		{f: "hex", args: wrapArgs(-1), want: NewStr("-0x1").ToObject()},
   240  		{f: "hex", args: wrapArgs(big.NewInt(-1)), want: NewStr("-0x1L").ToObject()},
   241  		{f: "hex", args: wrapArgs("foo"), wantExc: mustCreateException(TypeErrorType, "hex() argument can't be converted to hex")},
   242  		{f: "hex", args: wrapArgs(0.1), wantExc: mustCreateException(TypeErrorType, "hex() argument can't be converted to hex")},
   243  		{f: "hex", args: wrapArgs(1, 2, 3), wantExc: mustCreateException(TypeErrorType, "'hex' requires 1 arguments")},
   244  		{f: "hex", args: wrapArgs(newObject(hexOctType)), want: NewStr("0xhexadecimal").ToObject()},
   245  		{f: "id", args: wrapArgs(foo), want: NewInt(int(uintptr(foo.toPointer()))).ToObject()},
   246  		{f: "id", args: wrapArgs(), wantExc: mustCreateException(TypeErrorType, "'id' requires 1 arguments")},
   247  		{f: "isinstance", args: wrapArgs(NewInt(42).ToObject(), IntType.ToObject()), want: True.ToObject()},
   248  		{f: "isinstance", args: wrapArgs(NewStr("foo").ToObject(), TupleType.ToObject()), want: False.ToObject()},
   249  		{f: "isinstance", args: wrapArgs(), wantExc: mustCreateException(TypeErrorType, "'isinstance' requires 2 arguments")},
   250  		{f: "issubclass", args: wrapArgs(IntType, IntType), want: True.ToObject()},
   251  		{f: "issubclass", args: wrapArgs(fooType, IntType), want: False.ToObject()},
   252  		{f: "issubclass", args: wrapArgs(fooType, ObjectType), want: True.ToObject()},
   253  		{f: "issubclass", args: wrapArgs(FloatType, newTestTuple(IntType, StrType)), want: False.ToObject()},
   254  		{f: "issubclass", args: wrapArgs(FloatType, newTestTuple(IntType, FloatType)), want: True.ToObject()},
   255  		{f: "issubclass", args: wrapArgs(), wantExc: mustCreateException(TypeErrorType, "'issubclass' requires 2 arguments")},
   256  		{f: "iter", args: wrapArgs(iter), want: iter},
   257  		{f: "iter", args: wrapArgs(42), wantExc: mustCreateException(TypeErrorType, "'int' object is not iterable")},
   258  		{f: "len", args: wrapArgs(newTestList(1, 2, 3)), want: NewInt(3).ToObject()},
   259  		{f: "len", args: wrapArgs(), wantExc: mustCreateException(TypeErrorType, "'len' requires 1 arguments")},
   260  		{f: "map", args: wrapArgs(), wantExc: mustCreateException(TypeErrorType, "map() requires at least two args")},
   261  		{f: "map", args: wrapArgs(StrType), wantExc: mustCreateException(TypeErrorType, "map() requires at least two args")},
   262  		{f: "map", args: wrapArgs(None, newTestList()), want: newTestList().ToObject()},
   263  		{f: "map", args: wrapArgs(None, newTestList(1, 2, 3)), want: newTestList(1, 2, 3).ToObject()},
   264  		{f: "map", args: wrapArgs(None, newTestDict("foo", 1, "bar", 3)), want: newTestList("foo", "bar").ToObject()},
   265  		{f: "map", args: wrapArgs(None, None), wantExc: mustCreateException(TypeErrorType, "'NoneType' object is not iterable")},
   266  		{f: "map", args: wrapArgs(StrType, None), wantExc: mustCreateException(TypeErrorType, "'NoneType' object is not iterable")},
   267  		{f: "map", args: wrapArgs(StrType, newTestList(), None), wantExc: mustCreateException(TypeErrorType, "'NoneType' object is not iterable")},
   268  		{f: "map", args: wrapArgs(newTestList(), newTestList(1, 2, 3)), wantExc: mustCreateException(TypeErrorType, "'list' object is not callable")},
   269  		{f: "map", args: wrapArgs(StrType, newTestList()), want: newTestList().ToObject()},
   270  		{f: "map", args: wrapArgs(StrType, newTestList(1, 2, 3)), want: newTestList("1", "2", "3").ToObject()},
   271  		{f: "map", args: wrapArgs(StrType, newTestList(-1, -2, -3)), want: newTestList("-1", "-2", "-3").ToObject()},
   272  		{f: "map", args: wrapArgs(IntType, newTestList("1", "2", "3")), want: newTestList(1, 2, 3).ToObject()},
   273  		{f: "map", args: wrapArgs(IntType, newTestList("-1", "-2", "-3")), want: newTestList(-1, -2, -3).ToObject()},
   274  		{f: "map", args: wrapArgs(IntType, "123"), want: newTestList(1, 2, 3).ToObject()},
   275  		{f: "map", args: wrapArgs(IntType, newTestDict("1", "11", "2", "22")), want: newTestList(1, 2).ToObject()},
   276  		{f: "map", args: wrapArgs(IntType, 1), wantExc: mustCreateException(TypeErrorType, "'int' object is not iterable")},
   277  		{f: "map", args: wrapArgs(1, newTestList(1, 2, 3)), wantExc: mustCreateException(TypeErrorType, "'int' object is not callable")},
   278  		{f: "map", args: wrapArgs(StrType, newTestList(), 1), wantExc: mustCreateException(TypeErrorType, "'int' object is not iterable")},
   279  		{f: "max", args: wrapArgs(2, 3, 1), want: NewInt(3).ToObject()},
   280  		{f: "max", args: wrapArgs("bar", "foo"), want: NewStr("foo").ToObject()},
   281  		{f: "max", args: wrapArgs(newTestList(2, 3, 1)), want: NewInt(3).ToObject()},
   282  		{f: "max", args: wrapArgs(newTestList("bar", "foo")), want: NewStr("foo").ToObject()},
   283  		{f: "max", args: wrapArgs(2, 3, 1), want: NewInt(3).ToObject()},
   284  		{f: "max", args: wrapArgs("bar", "foo"), want: NewStr("foo").ToObject()},
   285  		{f: "max", args: wrapArgs(newTestList(2, 3, 1)), want: NewInt(3).ToObject()},
   286  		{f: "max", args: wrapArgs(newTestList("bar", "foo")), want: NewStr("foo").ToObject()},
   287  		{f: "max", args: wrapArgs(2, 3, 1), kwargs: wrapKWArgs("key", neg), want: NewInt(1).ToObject()},
   288  		{f: "max", args: wrapArgs(1, 2, 3), kwargs: wrapKWArgs("key", neg), want: NewInt(1).ToObject()},
   289  		{f: "max", args: wrapArgs(newTestList(2, 3, 1)), kwargs: wrapKWArgs("key", neg), want: NewInt(1).ToObject()},
   290  		{f: "max", args: wrapArgs(newTestList(1, 2, 3)), kwargs: wrapKWArgs("key", neg), want: NewInt(1).ToObject()},
   291  		{f: "max", args: wrapArgs(2, 3, 1), kwargs: wrapKWArgs("key", neg), want: NewInt(1).ToObject()},
   292  		{f: "max", args: wrapArgs(1, 2, 3), kwargs: wrapKWArgs("key", neg), want: NewInt(1).ToObject()},
   293  		{f: "max", args: wrapArgs(newTestList(2, 3, 1)), kwargs: wrapKWArgs("key", neg), want: NewInt(1).ToObject()},
   294  		{f: "max", args: wrapArgs(newTestList(1, 2, 3)), kwargs: wrapKWArgs("key", neg), want: NewInt(1).ToObject()},
   295  		{f: "max", args: wrapArgs(newTestList("foo")), want: NewStr("foo").ToObject()},
   296  		{f: "max", args: wrapArgs(1), wantExc: mustCreateException(TypeErrorType, "'int' object is not iterable")},
   297  		{f: "max", args: wrapArgs(), wantExc: mustCreateException(TypeErrorType, "'max' requires 1 arguments")},
   298  		{f: "max", args: wrapArgs(newTestList()), wantExc: mustCreateException(ValueErrorType, "max() arg is an empty sequence")},
   299  		{f: "max", args: wrapArgs(1, 2), kwargs: wrapKWArgs("key", raiseKey), wantExc: mustCreateException(RuntimeErrorType, "foo")},
   300  		{f: "min", args: wrapArgs(2, 3, 1), want: NewInt(1).ToObject()},
   301  		{f: "min", args: wrapArgs("bar", "foo"), want: NewStr("bar").ToObject()},
   302  		{f: "min", args: wrapArgs(newTestList(2, 3, 1)), want: NewInt(1).ToObject()},
   303  		{f: "min", args: wrapArgs(newTestList("bar", "foo")), want: NewStr("bar").ToObject()},
   304  		{f: "min", args: wrapArgs(2, 3, 1), want: NewInt(1).ToObject()},
   305  		{f: "min", args: wrapArgs("bar", "foo"), want: NewStr("bar").ToObject()},
   306  		{f: "min", args: wrapArgs(newTestList(2, 3, 1)), want: NewInt(1).ToObject()},
   307  		{f: "min", args: wrapArgs(newTestList("bar", "foo")), want: NewStr("bar").ToObject()},
   308  		{f: "min", args: wrapArgs(2, 3, 1), kwargs: wrapKWArgs("key", neg), want: NewInt(3).ToObject()},
   309  		{f: "min", args: wrapArgs(1, 2, 3), kwargs: wrapKWArgs("key", neg), want: NewInt(3).ToObject()},
   310  		{f: "min", args: wrapArgs(newTestList(2, 3, 1)), kwargs: wrapKWArgs("key", neg), want: NewInt(3).ToObject()},
   311  		{f: "min", args: wrapArgs(newTestList(1, 2, 3)), kwargs: wrapKWArgs("key", neg), want: NewInt(3).ToObject()},
   312  		{f: "min", args: wrapArgs(2, 3, 1), kwargs: wrapKWArgs("key", neg), want: NewInt(3).ToObject()},
   313  		{f: "min", args: wrapArgs(1, 2, 3), kwargs: wrapKWArgs("key", neg), want: NewInt(3).ToObject()},
   314  		{f: "min", args: wrapArgs(newTestList(2, 3, 1)), kwargs: wrapKWArgs("key", neg), want: NewInt(3).ToObject()},
   315  		{f: "min", args: wrapArgs(newTestList(1, 2, 3)), kwargs: wrapKWArgs("key", neg), want: NewInt(3).ToObject()},
   316  		{f: "min", args: wrapArgs(newTestList("foo")), want: NewStr("foo").ToObject()},
   317  		{f: "min", args: wrapArgs(1), wantExc: mustCreateException(TypeErrorType, "'int' object is not iterable")},
   318  		{f: "min", args: wrapArgs(), wantExc: mustCreateException(TypeErrorType, "'min' requires 1 arguments")},
   319  		{f: "min", args: wrapArgs(newTestList()), wantExc: mustCreateException(ValueErrorType, "min() arg is an empty sequence")},
   320  		{f: "min", args: wrapArgs(1, 2), kwargs: wrapKWArgs("key", raiseKey), wantExc: mustCreateException(RuntimeErrorType, "foo")},
   321  		{f: "oct", args: wrapArgs(077), want: NewStr("077").ToObject()},
   322  		{f: "oct", args: wrapArgs(0), want: NewStr("0").ToObject()},
   323  		{f: "oct", args: wrapArgs(1), want: NewStr("01").ToObject()},
   324  		{f: "oct", args: wrapArgs(-1), want: NewStr("-01").ToObject()},
   325  		{f: "oct", args: wrapArgs(big.NewInt(-1)), want: NewStr("-01L").ToObject()},
   326  		{f: "oct", args: wrapArgs("foo"), wantExc: mustCreateException(TypeErrorType, "oct() argument can't be converted to oct")},
   327  		{f: "oct", args: wrapArgs(0.1), wantExc: mustCreateException(TypeErrorType, "oct() argument can't be converted to oct")},
   328  		{f: "oct", args: wrapArgs(1, 2, 3), wantExc: mustCreateException(TypeErrorType, "'oct' requires 1 arguments")},
   329  		{f: "oct", args: wrapArgs(newObject(hexOctType)), want: NewStr("0octal").ToObject()},
   330  		{f: "ord", args: wrapArgs("a"), want: NewInt(97).ToObject()},
   331  		{f: "ord", args: wrapArgs(NewUnicode("樂")), want: NewInt(63764).ToObject()},
   332  		{f: "ord", args: wrapArgs("foo"), wantExc: mustCreateException(ValueErrorType, "ord() expected a character, but string of length 3 found")},
   333  		{f: "ord", args: wrapArgs(NewUnicode("волн")), wantExc: mustCreateException(ValueErrorType, "ord() expected a character, but string of length 4 found")},
   334  		{f: "ord", args: wrapArgs(1, 2, 3), wantExc: mustCreateException(TypeErrorType, "'ord' requires 1 arguments")},
   335  		{f: "pow", args: wrapArgs(1), wantExc: mustCreateException(TypeErrorType, "'pow' requires 2 arguments")},
   336  		{f: "pow", args: wrapArgs(2, 3), want: NewInt(8).ToObject()},
   337  		{f: "pow", args: wrapArgs(3.0, 3.0), want: NewFloat(27.0).ToObject()},
   338  		{f: "pow", args: wrapArgs(2, -2), want: NewFloat(0.25).ToObject()},
   339  		{f: "pow", args: wrapArgs(0i, 0i), want: NewComplex(complex(1, 0i)).ToObject()},
   340  		{f: "range", args: wrapArgs(), wantExc: mustCreateException(TypeErrorType, "'__new__' of 'int' requires 3 arguments")},
   341  		{f: "range", args: wrapArgs(3), want: newTestList(0, 1, 2).ToObject()},
   342  		{f: "range", args: wrapArgs(10, 0), want: NewList().ToObject()},
   343  		{f: "range", args: wrapArgs(-12, -23, -5), want: newTestList(-12, -17, -22).ToObject()},
   344  		{f: "repr", args: wrapArgs(123), want: NewStr("123").ToObject()},
   345  		{f: "repr", args: wrapArgs(NewUnicode("abc")), want: NewStr("u'abc'").ToObject()},
   346  		{f: "repr", args: wrapArgs(newTestTuple("foo", "bar")), want: NewStr("('foo', 'bar')").ToObject()},
   347  		{f: "repr", args: wrapArgs("a", "b", "c"), wantExc: mustCreateException(TypeErrorType, "'repr' requires 1 arguments")},
   348  		{f: "round", args: wrapArgs(1234.567), want: NewFloat(1235).ToObject()},
   349  		{f: "round", args: wrapArgs(1234.111), want: NewFloat(1234).ToObject()},
   350  		{f: "round", args: wrapArgs(-1234.567), want: NewFloat(-1235).ToObject()},
   351  		{f: "round", args: wrapArgs(-1234.111), want: NewFloat(-1234).ToObject()},
   352  		{f: "round", args: wrapArgs(1234.567, newTestIndexObject(0)), want: NewFloat(1235).ToObject()},
   353  		{f: "round", args: wrapArgs("foo"), wantExc: mustCreateException(TypeErrorType, "a float is required")},
   354  		{f: "round", args: wrapArgs(12.5, 0), want: NewFloat(13.0).ToObject()},
   355  		{f: "round", args: wrapArgs(-12.5, 0), want: NewFloat(-13.0).ToObject()},
   356  		{f: "round", args: wrapArgs(12.5, 3), want: NewFloat(12.5).ToObject()},
   357  		{f: "round", args: wrapArgs(1234.5, 1), want: NewFloat(1234.5).ToObject()},
   358  		{f: "round", args: wrapArgs(1234.5, 1), want: NewFloat(1234.5).ToObject()},
   359  		{f: "round", args: wrapArgs(1234.56, 1), want: NewFloat(1234.6).ToObject()},
   360  		{f: "round", args: wrapArgs(-1234.56, 1), want: NewFloat(-1234.6).ToObject()},
   361  		{f: "round", args: wrapArgs(-1234.56, -2), want: NewFloat(-1200.0).ToObject()},
   362  		{f: "round", args: wrapArgs(-1234.56, -8), want: NewFloat(0.0).ToObject()},
   363  		{f: "round", args: wrapArgs(63.4, -3), want: NewFloat(0.0).ToObject()},
   364  		{f: "round", args: wrapArgs(63.4, -2), want: NewFloat(100.0).ToObject()},
   365  		{f: "sorted", args: wrapArgs(NewList()), want: NewList().ToObject()},
   366  		{f: "sorted", args: wrapArgs(newTestList("foo", "bar")), want: newTestList("bar", "foo").ToObject()},
   367  		{f: "sorted", args: wrapArgs(newTestList(true, false)), want: newTestList(false, true).ToObject()},
   368  		{f: "sorted", args: wrapArgs(newTestList(1, 2, 0, 3)), want: newTestRange(4).ToObject()},
   369  		{f: "sorted", args: wrapArgs(newTestRange(100)), want: newTestRange(100).ToObject()},
   370  		{f: "sorted", args: wrapArgs(newTestTuple(1, 2, 0, 3)), want: newTestRange(4).ToObject()},
   371  		{f: "sorted", args: wrapArgs(newTestDict("foo", 1, "bar", 2)), want: newTestList("bar", "foo").ToObject()},
   372  		{f: "sorted", args: wrapArgs(1), wantExc: mustCreateException(TypeErrorType, "'int' object is not iterable")},
   373  		{f: "sorted", args: wrapArgs(newTestList("foo", "bar"), 2), wantExc: mustCreateException(TypeErrorType, "'sorted' requires 1 arguments")},
   374  		{f: "sorted", args: wrapArgs(newTestList(1, 2, 0, 3)), kwargs: wrapKWArgs("reverse", False), want: newTestRange(4).ToObject()},
   375  		{f: "sorted", args: wrapArgs(newTestList(1, 2, 0, 3)), kwargs: wrapKWArgs("reverse", 0), want: newTestRange(4).ToObject()},
   376  		{f: "sorted", args: wrapArgs(newTestList(1, 2, 0, 3)), kwargs: wrapKWArgs("reverse", None), want: newTestRange(4).ToObject()},
   377  		{f: "sorted", args: wrapArgs(newTestList(1, 2, 0, 3)), kwargs: wrapKWArgs("reverse", True), want: newTestList(3, 2, 1, 0).ToObject()},
   378  		{f: "sorted", args: wrapArgs(newTestList(1, 2, 0, 3)), kwargs: wrapKWArgs("reverse", 1), want: newTestList(3, 2, 1, 0).ToObject()},
   379  		{f: "sum", args: wrapArgs(newTestList(1, 2, 3, 4)), want: NewInt(10).ToObject()},
   380  		{f: "sum", args: wrapArgs(newTestList(1, 2), 3), want: NewFloat(6).ToObject()},
   381  		{f: "sum", args: wrapArgs(newTestList(2, 1.1)), want: NewFloat(3.1).ToObject()},
   382  		{f: "sum", args: wrapArgs(newTestList(2, 1.1, 2)), want: NewFloat(5.1).ToObject()},
   383  		{f: "sum", args: wrapArgs(newTestList(2, 1.1, 2.0)), want: NewFloat(5.1).ToObject()},
   384  		{f: "sum", args: wrapArgs(newTestList(1), newObject(addType)), want: NewInt(1).ToObject()},
   385  		{f: "sum", args: wrapArgs(newTestList(newObject(addType)), newObject(addType)), want: NewInt(1).ToObject()},
   386  		{f: "unichr", args: wrapArgs(0), want: NewUnicode("\x00").ToObject()},
   387  		{f: "unichr", args: wrapArgs(65), want: NewStr("A").ToObject()},
   388  		{f: "unichr", args: wrapArgs(0x120000), wantExc: mustCreateException(ValueErrorType, "unichr() arg not in range(0x10ffff)")},
   389  		{f: "unichr", args: wrapArgs(-1), wantExc: mustCreateException(ValueErrorType, "unichr() arg not in range(0x10ffff)")},
   390  		{f: "unichr", args: wrapArgs(), wantExc: mustCreateException(TypeErrorType, "'unichr' requires 1 arguments")},
   391  		{f: "zip", args: wrapArgs(), want: newTestList().ToObject()},
   392  		{f: "zip", args: wrapArgs(newTestTuple()), want: newTestList().ToObject()},
   393  		{f: "zip", args: wrapArgs(newTestList()), want: newTestList().ToObject()},
   394  		{f: "zip", args: wrapArgs(newTestList(1)), want: newTestList(newTestTuple(1).ToObject()).ToObject()},
   395  		{f: "zip", args: wrapArgs(newTestList(1, 2, 3)), want: newTestList(newTestTuple(1).ToObject(), newTestTuple(2).ToObject(), newTestTuple(3).ToObject()).ToObject()},
   396  		{f: "zip", args: wrapArgs(newTestRange(3)), want: newTestList(newTestTuple(0).ToObject(), newTestTuple(1).ToObject(), newTestTuple(2).ToObject()).ToObject()},
   397  		{f: "zip", args: wrapArgs(newTestTuple(1, 2, 3), newTestTuple(4, 5, 6)), want: NewList(newTestTuple(1, 4).ToObject(), newTestTuple(2, 5).ToObject(), newTestTuple(3, 6).ToObject()).ToObject()},
   398  		{f: "zip", args: wrapArgs(newTestTuple(1, 2, 3), newTestTuple(4, 5)), want: NewList(newTestTuple(1, 4).ToObject(), newTestTuple(2, 5).ToObject()).ToObject()},
   399  		{f: "zip", args: wrapArgs(newTestTuple(1, 2), newTestTuple(4, 5, 5)), want: NewList(newTestTuple(1, 4).ToObject(), newTestTuple(2, 5).ToObject()).ToObject()},
   400  		{f: "zip", args: wrapArgs(1), wantExc: mustCreateException(TypeErrorType, "'int' object is not iterable")},
   401  		{f: "zip", args: wrapArgs(newTestDict("foo", 1, "bar", 2)), want: newTestList(newTestTuple("foo").ToObject(), newTestTuple("bar").ToObject()).ToObject()},
   402  	}
   403  	for _, cas := range cases {
   404  		fun := mustNotRaise(Builtins.GetItemString(NewRootFrame(), cas.f))
   405  		if fun == nil {
   406  			t.Fatalf("%s not found in builtins: %v", cas.f, Builtins)
   407  		}
   408  		testCase := invokeTestCase{args: cas.args, kwargs: cas.kwargs, want: cas.want, wantExc: cas.wantExc}
   409  		if err := runInvokeTestCase(fun, &testCase); err != "" {
   410  			t.Error(err)
   411  		}
   412  	}
   413  }
   414  
   415  func TestBuiltinGlobals(t *testing.T) {
   416  	f := NewRootFrame()
   417  	f.globals = newTestDict("foo", 1, "bar", 2, 42, None)
   418  	globals := mustNotRaise(Builtins.GetItemString(f, "globals"))
   419  	got, raised := globals.Call(f, nil, nil)
   420  	want := newTestDict("foo", 1, "bar", 2, 42, None).ToObject()
   421  	switch checkResult(got, want, raised, nil) {
   422  	case checkInvokeResultExceptionMismatch:
   423  		t.Errorf("globals() = %v, want %v", got, want)
   424  	case checkInvokeResultReturnValueMismatch:
   425  		t.Errorf("globals() raised %v, want nil", raised)
   426  	}
   427  }
   428  
   429  func TestEllipsisRepr(t *testing.T) {
   430  	cas := invokeTestCase{args: wrapArgs(Ellipsis), want: NewStr("Ellipsis").ToObject()}
   431  	if err := runInvokeMethodTestCase(EllipsisType, "__repr__", &cas); err != "" {
   432  		t.Error(err)
   433  	}
   434  }
   435  
   436  func TestNoneRepr(t *testing.T) {
   437  	cas := invokeTestCase{args: wrapArgs(None), want: NewStr("None").ToObject()}
   438  	if err := runInvokeMethodTestCase(NoneType, "__repr__", &cas); err != "" {
   439  		t.Error(err)
   440  	}
   441  }
   442  
   443  func TestNotImplementedRepr(t *testing.T) {
   444  	cas := invokeTestCase{args: wrapArgs(NotImplemented), want: NewStr("NotImplemented").ToObject()}
   445  	if err := runInvokeMethodTestCase(NotImplementedType, "__repr__", &cas); err != "" {
   446  		t.Error(err)
   447  	}
   448  }
   449  
   450  // captureStdout invokes a function closure which writes to stdout and captures
   451  // its output as string.
   452  func captureStdout(f *Frame, fn func() *BaseException) (string, *BaseException) {
   453  	r, w, err := os.Pipe()
   454  	if err != nil {
   455  		return "", f.RaiseType(RuntimeErrorType, fmt.Sprintf("failed to open pipe: %v", err))
   456  	}
   457  	oldStdout := Stdout
   458  	Stdout = NewFileFromFD(w.Fd(), nil)
   459  	defer func() {
   460  		Stdout = oldStdout
   461  	}()
   462  	done := make(chan struct{})
   463  	var raised *BaseException
   464  	go func() {
   465  		defer close(done)
   466  		defer w.Close()
   467  		raised = fn()
   468  	}()
   469  	var buf bytes.Buffer
   470  	if _, err := io.Copy(&buf, r); err != nil {
   471  		return "", f.RaiseType(RuntimeErrorType, fmt.Sprintf("failed to copy buffer: %v", err))
   472  	}
   473  	<-done
   474  	if raised != nil {
   475  		return "", raised
   476  	}
   477  	return buf.String(), nil
   478  }
   479  
   480  // TODO(corona10): Re-enable once #282 is addressed.
   481  /*func TestBuiltinPrint(t *testing.T) {
   482  	fun := wrapFuncForTest(func(f *Frame, args *Tuple, kwargs KWArgs) (string, *BaseException) {
   483  		return captureStdout(f, func() *BaseException {
   484  			_, raised := builtinPrint(NewRootFrame(), args.elems, kwargs)
   485  			return raised
   486  		})
   487  	})
   488  	cases := []invokeTestCase{
   489  		{args: wrapArgs(NewTuple(), wrapKWArgs()), want: NewStr("\n").ToObject()},
   490  		{args: wrapArgs(newTestTuple("abc"), wrapKWArgs()), want: NewStr("abc\n").ToObject()},
   491  		{args: wrapArgs(newTestTuple("abc", 123), wrapKWArgs()), want: NewStr("abc 123\n").ToObject()},
   492  		{args: wrapArgs(newTestTuple("abc", 123), wrapKWArgs("sep", "")), want: NewStr("abc123\n").ToObject()},
   493  		{args: wrapArgs(newTestTuple("abc", 123), wrapKWArgs("end", "")), want: NewStr("abc 123").ToObject()},
   494  		{args: wrapArgs(newTestTuple("abc", 123), wrapKWArgs("sep", "XX", "end", "--")), want: NewStr("abcXX123--").ToObject()},
   495  	}
   496  	for _, cas := range cases {
   497  		if err := runInvokeTestCase(fun, &cas); err != "" {
   498  			t.Error(err)
   499  		}
   500  	}
   501  }*/
   502  
   503  func TestBuiltinSetAttr(t *testing.T) {
   504  	setattr := mustNotRaise(Builtins.GetItemString(NewRootFrame(), "setattr"))
   505  	fooType := newTestClass("Foo", []*Type{ObjectType}, newStringDict(map[string]*Object{}))
   506  	foo := newObject(fooType)
   507  	fun := wrapFuncForTest(func(f *Frame, args ...*Object) (*Object, *BaseException) {
   508  		result, raised := setattr.Call(f, args, nil)
   509  		if raised != nil {
   510  			return nil, raised
   511  		}
   512  		val, raised := GetAttr(f, args[0], toStrUnsafe(args[1]), nil)
   513  		if raised != nil {
   514  			return nil, raised
   515  		}
   516  		return newTestTuple(result, val).ToObject(), nil
   517  	})
   518  	cases := []invokeTestCase{
   519  		{args: wrapArgs(foo), wantExc: mustCreateException(TypeErrorType, "'setattr' requires 3 arguments")},
   520  		{args: wrapArgs(newObject(fooType), "foo", "bar"), want: newTestTuple(None, "bar").ToObject()},
   521  		{args: wrapArgs(newObject(fooType), "foo", 123), want: newTestTuple(None, 123).ToObject()},
   522  		{args: wrapArgs(foo, "foo"), wantExc: mustCreateException(TypeErrorType, "'setattr' requires 3 arguments")},
   523  		{args: wrapArgs(foo, "foo", 123, None), wantExc: mustCreateException(TypeErrorType, "'setattr' requires 3 arguments")},
   524  		{args: wrapArgs(foo, 123, 123), wantExc: mustCreateException(TypeErrorType, "'setattr' requires a 'str' object but received a \"int\"")},
   525  	}
   526  	for _, cas := range cases {
   527  		if err := runInvokeTestCase(fun, &cas); err != "" {
   528  			t.Error(err)
   529  		}
   530  	}
   531  }
   532  
   533  // TODO(corona10): Re-enable once #282 is addressed.
   534  /*func TestRawInput(t *testing.T) {
   535  	fun := wrapFuncForTest(func(f *Frame, s string, args ...*Object) (*Object, *BaseException) {
   536  		// Create a fake Stdin for input test.
   537  		stdinFile, w, err := os.Pipe()
   538  		if err != nil {
   539  			return nil, f.RaiseType(RuntimeErrorType, fmt.Sprintf("failed to open pipe: %v", err))
   540  		}
   541  
   542  		go func() {
   543  			w.Write([]byte(s))
   544  			w.Close()
   545  		}()
   546  
   547  		oldStdin := Stdin
   548  		Stdin = NewFileFromFD(stdinFile.Fd(), nil)
   549  		defer func() {
   550  			Stdin = oldStdin
   551  			stdinFile.Close()
   552  		}()
   553  
   554  		var input *Object
   555  		output, raised := captureStdout(f, func() *BaseException {
   556  			in, raised := builtinRawInput(f, args, nil)
   557  			input = in
   558  			return raised
   559  		})
   560  
   561  		if raised != nil {
   562  			return nil, raised
   563  		}
   564  
   565  		return newTestTuple(input, output).ToObject(), nil
   566  	})
   567  
   568  	cases := []invokeTestCase{
   569  		{args: wrapArgs("HelloGrumpy\n", ""), want: newTestTuple("HelloGrumpy", "").ToObject()},
   570  		{args: wrapArgs("HelloGrumpy\n", "ShouldBeShown\nShouldBeShown\t"), want: newTestTuple("HelloGrumpy", "ShouldBeShown\nShouldBeShown\t").ToObject()},
   571  		{args: wrapArgs("HelloGrumpy\n", 5, 4), wantExc: mustCreateException(TypeErrorType, "[raw_]input expcted at most 1 arguments, got 2")},
   572  		{args: wrapArgs("HelloGrumpy\nHelloGrumpy\n", ""), want: newTestTuple("HelloGrumpy", "").ToObject()},
   573  		{args: wrapArgs("HelloGrumpy\nHelloGrumpy\n", "ShouldBeShown\nShouldBeShown\t"), want: newTestTuple("HelloGrumpy", "ShouldBeShown\nShouldBeShown\t").ToObject()},
   574  		{args: wrapArgs("HelloGrumpy\nHelloGrumpy\n", 5, 4), wantExc: mustCreateException(TypeErrorType, "[raw_]input expcted at most 1 arguments, got 2")},
   575  		{args: wrapArgs("", ""), wantExc: mustCreateException(EOFErrorType, "EOF when reading a line")},
   576  		{args: wrapArgs("", "ShouldBeShown\nShouldBeShown\t"), wantExc: mustCreateException(EOFErrorType, "EOF when reading a line")},
   577  		{args: wrapArgs("", 5, 4), wantExc: mustCreateException(TypeErrorType, "[raw_]input expcted at most 1 arguments, got 2")},
   578  	}
   579  
   580  	for _, cas := range cases {
   581  		if err := runInvokeTestCase(fun, &cas); err != "" {
   582  			t.Error(err)
   583  		}
   584  	}
   585  
   586  }*/
   587  
   588  func newTestIndexObject(index int) *Object {
   589  	indexType := newTestClass("Index", []*Type{ObjectType}, newStringDict(map[string]*Object{
   590  		"__index__": newBuiltinFunction("__index__", func(f *Frame, _ Args, _ KWArgs) (*Object, *BaseException) {
   591  			return NewInt(index).ToObject(), nil
   592  		}).ToObject(),
   593  	}))
   594  	return newObject(indexType)
   595  }