github.com/grumpyhome/grumpy@v0.3.1-0.20201208125205-7b775405bdf1/grumpy-runtime-src/runtime/baseexception_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 "testing" 19 ) 20 21 func TestBaseExceptionCreate(t *testing.T) { 22 emptyExc := toBaseExceptionUnsafe(newObject(ExceptionType)) 23 emptyExc.args = NewTuple() 24 cases := []struct { 25 t *Type 26 args *Tuple 27 wantRet *Object 28 }{ 29 {ExceptionType, NewTuple(), emptyExc.ToObject()}, 30 {TypeErrorType, NewTuple(NewStr("abc").ToObject()), mustCreateException(TypeErrorType, "abc").ToObject()}, 31 } 32 for _, cas := range cases { 33 got, match := checkInvokeResult(cas.t.ToObject(), cas.args.elems, cas.wantRet, nil) 34 if match == checkInvokeResultExceptionMismatch { 35 t.Errorf("%s%v raised %v, want none", cas.t.Name(), cas.args, got) 36 } else if match == checkInvokeResultReturnValueMismatch { 37 t.Errorf("%s%v = %v, want %v", cas.t.Name(), cas.args, got, cas.wantRet) 38 } 39 } 40 } 41 42 func TestBaseExceptionInitRaise(t *testing.T) { 43 cas := invokeTestCase{ 44 args: nil, 45 wantExc: mustCreateException(TypeErrorType, "unbound method __init__() must be called with BaseException instance as first argument (got nothing instead)"), 46 } 47 if err := runInvokeMethodTestCase(BaseExceptionType, "__init__", &cas); err != "" { 48 t.Error(err) 49 } 50 } 51 52 func TestBaseExceptionRepr(t *testing.T) { 53 fooExc := toBaseExceptionUnsafe(newObject(ExceptionType)) 54 fooExc.args = NewTuple(NewStr("foo").ToObject()) 55 recursiveExc := toBaseExceptionUnsafe(newObject(ExceptionType)) 56 recursiveExc.args = NewTuple(recursiveExc.ToObject()) 57 cases := []invokeTestCase{ 58 {args: wrapArgs(newObject(TypeErrorType)), want: NewStr("TypeError()").ToObject()}, 59 {args: wrapArgs(fooExc), want: NewStr("Exception('foo',)").ToObject()}, 60 {args: wrapArgs(recursiveExc), want: NewStr("Exception(Exception(...),)").ToObject()}, 61 } 62 for _, cas := range cases { 63 if err := runInvokeTestCase(wrapFuncForTest(Repr), &cas); err != "" { 64 t.Error(err) 65 } 66 } 67 } 68 69 func TestBaseExceptionStr(t *testing.T) { 70 f := NewRootFrame() 71 cases := []invokeTestCase{ 72 {args: wrapArgs(newObject(TypeErrorType)), want: NewStr("").ToObject()}, 73 {args: wrapArgs(mustNotRaise(ExceptionType.Call(f, wrapArgs(""), nil))), want: NewStr("").ToObject()}, 74 {args: wrapArgs(mustNotRaise(ExceptionType.Call(f, wrapArgs("foo"), nil))), want: NewStr("foo").ToObject()}, 75 {args: wrapArgs(mustNotRaise(TypeErrorType.Call(f, wrapArgs(NewTuple(), 3), nil))), want: NewStr("((), 3)").ToObject()}, 76 } 77 for _, cas := range cases { 78 if err := runInvokeTestCase(wrapFuncForTest(ToStr), &cas); err != "" { 79 t.Error(err) 80 } 81 } 82 }