github.com/grumpyhome/grumpy@v0.3.1-0.20201208125205-7b775405bdf1/grumpy-runtime-src/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(), want: newTestByteArray("").ToObject()},
    72  		{args: wrapArgs(3), want: newTestByteArray("\x00\x00\x00").ToObject()},
    73  		{args: wrapArgs([]int{3, 2, 1}), want: newTestByteArray("\x03\x02\x01").ToObject()},
    74  		{args: wrapArgs("abc"), want: newTestByteArray("abc").ToObject()},
    75  		{args: wrapArgs([]string{"a", "b", "c"}), want: newTestByteArray("abc").ToObject()},
    76  		{args: wrapArgs(newTestXRange(3)), want: newTestByteArray("\x00\x01\x02").ToObject()},
    77  		{args: wrapArgs(newTestRange(3)), want: newTestByteArray("\x00\x01\x02").ToObject()},
    78  		{args: wrapArgs(newObject(ObjectType)), wantExc: mustCreateException(TypeErrorType, `'object' object is not iterable`)},
    79  		{args: wrapArgs([]int{-1}), wantExc: mustCreateException(ValueErrorType, `byte must be in range(0, 256)`)},
    80  		{args: wrapArgs([]int{256}), wantExc: mustCreateException(ValueErrorType, `byte must be in range(0, 256)`)},
    81  		{args: wrapArgs([]string{"ab"}), wantExc: mustCreateException(ValueErrorType, `string must be of size 1`)},
    82  		{args: wrapArgs([]interface{}{5, []interface{}{}}), wantExc: mustCreateException(TypeErrorType, `an integer or string of size 1 is required`)},
    83  	}
    84  	for _, cas := range cases {
    85  		if err := runInvokeTestCase(ByteArrayType.ToObject(), &cas); err != "" {
    86  			t.Error(err)
    87  		}
    88  	}
    89  }
    90  
    91  func TestByteArrayNative(t *testing.T) {
    92  	val, raised := ToNative(NewRootFrame(), newTestByteArray("foo").ToObject())
    93  	if raised != nil {
    94  		t.Fatalf("bytearray.__native__ raised %v", raised)
    95  	}
    96  	data, ok := val.Interface().([]byte)
    97  	if !ok || string(data) != "foo" {
    98  		t.Fatalf(`bytearray.__native__() = %v, want []byte("123")`, val.Interface())
    99  	}
   100  }
   101  
   102  func TestByteArrayRepr(t *testing.T) {
   103  	cases := []invokeTestCase{
   104  		{args: wrapArgs(newTestByteArray("")), want: NewStr("bytearray(b'')").ToObject()},
   105  		{args: wrapArgs(newTestByteArray("foo")), want: NewStr("bytearray(b'foo')").ToObject()},
   106  	}
   107  	for _, cas := range cases {
   108  		if err := runInvokeTestCase(wrapFuncForTest(Repr), &cas); err != "" {
   109  			t.Error(err)
   110  		}
   111  	}
   112  }
   113  
   114  func TestByteArrayStr(t *testing.T) {
   115  	cases := []invokeTestCase{
   116  		{args: wrapArgs(newTestByteArray("")), want: NewStr("").ToObject()},
   117  		{args: wrapArgs(newTestByteArray("foo")), want: NewStr("foo").ToObject()},
   118  	}
   119  	for _, cas := range cases {
   120  		if err := runInvokeTestCase(wrapFuncForTest(ToStr), &cas); err != "" {
   121  			t.Error(err)
   122  		}
   123  	}
   124  }
   125  
   126  func newTestByteArray(s string) *ByteArray {
   127  	return &ByteArray{Object: Object{typ: ByteArrayType}, value: []byte(s)}
   128  }