github.com/google/grumpy@v0.0.0-20171122020858-3ec87959189c/runtime/bytearray_test.go (about) 1 // Copyright 2017 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 TestByteArrayCompare(t *testing.T) { 22 cases := []invokeTestCase{ 23 {args: wrapArgs(newTestByteArray(""), newTestByteArray("")), want: compareAllResultEq}, 24 {args: wrapArgs(newTestByteArray("foo"), newTestByteArray("foo")), want: compareAllResultEq}, 25 {args: wrapArgs(newTestByteArray(""), newTestByteArray("foo")), want: compareAllResultLT}, 26 {args: wrapArgs(newTestByteArray("foo"), newTestByteArray("")), want: compareAllResultGT}, 27 {args: wrapArgs(newTestByteArray("bar"), newTestByteArray("baz")), want: compareAllResultLT}, 28 {args: wrapArgs(newTestByteArray(""), ""), want: compareAllResultEq}, 29 {args: wrapArgs(newTestByteArray("foo"), "foo"), want: compareAllResultEq}, 30 {args: wrapArgs(newTestByteArray(""), "foo"), want: compareAllResultLT}, 31 {args: wrapArgs(newTestByteArray("foo"), ""), want: compareAllResultGT}, 32 {args: wrapArgs(newTestByteArray("bar"), "baz"), want: compareAllResultLT}, 33 } 34 for _, cas := range cases { 35 if err := runInvokeTestCase(compareAll, &cas); err != "" { 36 t.Error(err) 37 } 38 } 39 } 40 41 func TestByteArrayGetItem(t *testing.T) { 42 badIndexType := newTestClass("badIndex", []*Type{ObjectType}, newStringDict(map[string]*Object{ 43 "__index__": newBuiltinFunction("__index__", func(f *Frame, _ Args, _ KWArgs) (*Object, *BaseException) { 44 return nil, f.RaiseType(ValueErrorType, "wut") 45 }).ToObject(), 46 })) 47 cases := []invokeTestCase{ 48 {args: wrapArgs(newTestByteArray("bar"), 1), want: NewInt(97).ToObject()}, 49 {args: wrapArgs(newTestByteArray("foo"), 3.14), wantExc: mustCreateException(TypeErrorType, "bytearray indices must be integers or slice, not float")}, 50 {args: wrapArgs(newTestByteArray("baz"), -1), want: NewInt(122).ToObject()}, 51 {args: wrapArgs(newTestByteArray("baz"), -4), wantExc: mustCreateException(IndexErrorType, "index out of range")}, 52 {args: wrapArgs(newTestByteArray(""), 0), wantExc: mustCreateException(IndexErrorType, "index out of range")}, 53 {args: wrapArgs(newTestByteArray("foo"), 3), wantExc: mustCreateException(IndexErrorType, "index out of range")}, 54 {args: wrapArgs(newTestByteArray("bar"), newTestSlice(None, 2)), want: newTestByteArray("ba").ToObject()}, 55 {args: wrapArgs(newTestByteArray("bar"), newTestSlice(1, 3)), want: newTestByteArray("ar").ToObject()}, 56 {args: wrapArgs(newTestByteArray("bar"), newTestSlice(1, None)), want: newTestByteArray("ar").ToObject()}, 57 {args: wrapArgs(newTestByteArray("foobarbaz"), newTestSlice(1, 8, 2)), want: newTestByteArray("obra").ToObject()}, 58 {args: wrapArgs(newTestByteArray("abc"), newTestSlice(None, None, -1)), want: newTestByteArray("cba").ToObject()}, 59 {args: wrapArgs(newTestByteArray("bar"), newTestSlice(1, 2, 0)), wantExc: mustCreateException(ValueErrorType, "slice step cannot be zero")}, 60 {args: wrapArgs(newTestByteArray("123"), newObject(badIndexType)), wantExc: mustCreateException(ValueErrorType, "wut")}, 61 } 62 for _, cas := range cases { 63 if err := runInvokeMethodTestCase(ByteArrayType, "__getitem__", &cas); err != "" { 64 t.Error(err) 65 } 66 } 67 } 68 69 func TestByteArrayInit(t *testing.T) { 70 cases := []invokeTestCase{ 71 {args: wrapArgs(3), want: newTestByteArray("\x00\x00\x00").ToObject()}, 72 {args: wrapArgs(newObject(ObjectType)), wantExc: mustCreateException(TypeErrorType, `'__init__' requires a 'int' object but received a "object"`)}, 73 } 74 for _, cas := range cases { 75 if err := runInvokeTestCase(ByteArrayType.ToObject(), &cas); err != "" { 76 t.Error(err) 77 } 78 } 79 } 80 81 func TestByteArrayNative(t *testing.T) { 82 val, raised := ToNative(NewRootFrame(), newTestByteArray("foo").ToObject()) 83 if raised != nil { 84 t.Fatalf("bytearray.__native__ raised %v", raised) 85 } 86 data, ok := val.Interface().([]byte) 87 if !ok || string(data) != "foo" { 88 t.Fatalf(`bytearray.__native__() = %v, want []byte("123")`, val.Interface()) 89 } 90 } 91 92 func TestByteArrayRepr(t *testing.T) { 93 cases := []invokeTestCase{ 94 {args: wrapArgs(newTestByteArray("")), want: NewStr("bytearray(b'')").ToObject()}, 95 {args: wrapArgs(newTestByteArray("foo")), want: NewStr("bytearray(b'foo')").ToObject()}, 96 } 97 for _, cas := range cases { 98 if err := runInvokeTestCase(wrapFuncForTest(Repr), &cas); err != "" { 99 t.Error(err) 100 } 101 } 102 } 103 104 func TestByteArrayStr(t *testing.T) { 105 cases := []invokeTestCase{ 106 {args: wrapArgs(newTestByteArray("")), want: NewStr("").ToObject()}, 107 {args: wrapArgs(newTestByteArray("foo")), want: NewStr("foo").ToObject()}, 108 } 109 for _, cas := range cases { 110 if err := runInvokeTestCase(wrapFuncForTest(ToStr), &cas); err != "" { 111 t.Error(err) 112 } 113 } 114 } 115 116 func newTestByteArray(s string) *ByteArray { 117 return &ByteArray{Object: Object{typ: ByteArrayType}, value: []byte(s)} 118 }