| 1 | // Copyright 2018 Google LLC |
| 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 types |
| 16 | |
| 17 | import ( |
| 18 | "bytes" |
| 19 | "reflect" |
| 20 | "strings" |
| 21 | "testing" |
| 22 | |
| 23 | "google.golang.org/protobuf/proto" |
| 24 | |
| 25 | anypb "google.golang.org/protobuf/types/known/anypb" |
| 26 | structpb "google.golang.org/protobuf/types/known/structpb" |
| 27 | wrapperspb "google.golang.org/protobuf/types/known/wrapperspb" |
| 28 | ) |
| 29 | |
| 30 | func TestBytesAdd(t *testing.T) { |
| 31 | if !Bytes("hello").Add(Bytes("world")).Equal(Bytes("helloworld")).(Bool) { |
| 32 | t.Error("Byte ranges were not successfully added.") |
| 33 | } |
| 34 | if !IsError(Bytes("hello").Add(String("world"))) { |
| 35 | t.Error("Types combined without conversion.") |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | func TestBytesCompare(t *testing.T) { |
| 40 | if !Bytes("1234").Compare(Bytes("2345")).Equal(IntNegOne).(Bool) { |
| 41 | t.Error("Comparison did not yield -1") |
| 42 | } |
| 43 | if !Bytes("2345").Compare(Bytes("1234")).Equal(IntOne).(Bool) { |
| 44 | t.Error("Comparison did not yield 1") |
| 45 | } |
| 46 | if !Bytes("2345").Compare(Bytes("2345")).Equal(IntZero).(Bool) { |
| 47 | t.Error("Comparison did not yield 0") |
| 48 | } |
| 49 | if !IsError(Bytes("1").Compare(String("1"))) { |
| 50 | t.Error("Comparison permitted without type conversion") |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | func TestBytesConvertToNative_Any(t *testing.T) { |
| 55 | val, err := Bytes("123").ConvertToNative(anyValueType) |
| 56 | if err != nil { |
| 57 | t.Error(err) |
| 58 | } |
| 59 | want, err := anypb.New(wrapperspb.Bytes([]byte("123"))) |
| 60 | if err != nil { |
| 61 | t.Error(err) |
| 62 | } |
| 63 | if !proto.Equal(val.(proto.Message), want) { |
| 64 | t.Errorf("Got %v, wanted %v", val, want) |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | func TestBytesConvertToNative_ByteSlice(t *testing.T) { |
| 69 | val, err := Bytes("123").ConvertToNative(reflect.TypeOf([]byte{})) |
| 70 | if err != nil || !bytes.Equal(val.([]byte), []byte{49, 50, 51}) { |
| 71 | t.Error("Got unexpected value, wanted []byte{49, 50, 51}", err, val) |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | func TestBytesConvertToNative_ByteArray(t *testing.T) { |
| 76 | val, err := Bytes("123").ConvertToNative(reflect.TypeOf([3]byte{})) |
| 77 | if err != nil { |
| 78 | t.Error("Got unexpected value, wanted []byte{49, 50, 51}", err, val) |
| 79 | } |
| 80 | if val.([3]byte) != [3]byte{49, 50, 51} { |
| 81 | t.Errorf("Got %v, wanted [3]byte{49, 50, 51}", val) |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | func TestBytesConvertToNative_ByteArrayError(t *testing.T) { |
| 86 | _, err := Bytes("123").ConvertToNative(reflect.TypeOf([1]byte{})) |
| 87 | if !strings.Contains(err.Error(), "[3]byte not assignable to [1]byte") { |
| 88 | t.Errorf("Got unexpected error %v, wanted not assignable error", err) |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | func TestBytesConvertToNative_Error(t *testing.T) { |
| 93 | val, err := Bytes("123").ConvertToNative(reflect.TypeOf("")) |
| 94 | if err == nil { |
| 95 | t.Errorf("Got '%v', expected error", val) |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | func TestBytesConvertToNative_Json(t *testing.T) { |
| 100 | val, err := Bytes("123").ConvertToNative(JSONValueType) |
| 101 | if err != nil { |
| 102 | t.Error(err) |
| 103 | } |
| 104 | want := structpb.NewStringValue("MTIz") |
| 105 | if !proto.Equal(val.(proto.Message), want) { |
| 106 | t.Errorf("Got %v, wanted %v", val, want) |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | func TestBytesConvertToNative_Wrapper(t *testing.T) { |
| 111 | val, err := Bytes("123").ConvertToNative(byteWrapperType) |
| 112 | if err != nil { |
| 113 | t.Error(err) |
| 114 | } |
| 115 | want := wrapperspb.Bytes([]byte("123")) |
| 116 | if !proto.Equal(val.(proto.Message), want) { |
| 117 | t.Errorf("Got %v, wanted %v", val, want) |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | func TestBytesConvertToType(t *testing.T) { |
| 122 | if !Bytes("hello world").ConvertToType(BytesType).Equal(Bytes("hello world")).(Bool) { |
| 123 | t.Error("Unsupported type conversion to bytes") |
| 124 | } |
| 125 | if !Bytes("hello world").ConvertToType(StringType).Equal(String("hello world")).(Bool) { |
| 126 | t.Error("Unsupported type conversion to string") |
| 127 | } |
| 128 | if !Bytes("hello world").ConvertToType(TypeType).Equal(BytesType).(Bool) { |
| 129 | t.Error("Unsupported type conversion to type") |
| 130 | } |
| 131 | if !IsError(Bytes("hello").ConvertToType(IntType)) { |
| 132 | t.Errorf("Got value, expected error") |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | func TestBytesIsZeroValue(t *testing.T) { |
| 137 | if Bytes("non-zero").IsZeroValue() { |
| 138 | t.Error("Bytes('non-zero').IsZeroValue() returned true, wanted false.") |
| 139 | } |
| 140 | if !Bytes("").IsZeroValue() { |
| 141 | t.Error("Bytes('').IsZeroValue() returned false, wanted true") |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | func TestBytesSize(t *testing.T) { |
| 146 | if !Bytes("1234567890").Size().Equal(Int(10)).(Bool) { |
| 147 | t.Error("Unexpected byte count.") |
| 148 | } |
| 149 | } |