How to use Setup method of got Package

Best Got code snippet using got.Setup

structs_test.go

Source:structs_test.go Github

copy

Full Screen

1package structs2import (3 "bytes"4 "math"5 "testing"6 "github.com/bearlytools/claw/internal/field"7 "github.com/bearlytools/claw/internal/mapping"8)9func TestBasicEncodeDecodeStruct(t *testing.T) {10 msg0Mapping := mapping.Map{11 &mapping.FieldDesc{Name: "Bool", Type: field.FTBool},12 &mapping.FieldDesc{Name: "Int8", Type: field.FTInt8},13 &mapping.FieldDesc{Name: "Int16", Type: field.FTInt16},14 &mapping.FieldDesc{Name: "Int32", Type: field.FTInt32},15 &mapping.FieldDesc{Name: "Int64", Type: field.FTInt64},16 &mapping.FieldDesc{Name: "Uint8", Type: field.FTUint8},17 &mapping.FieldDesc{Name: "Uint16", Type: field.FTUint16},18 &mapping.FieldDesc{Name: "Uint32", Type: field.FTUint32},19 &mapping.FieldDesc{Name: "Uint64", Type: field.FTUint64},20 &mapping.FieldDesc{Name: "Float32", Type: field.FTFloat32},21 &mapping.FieldDesc{Name: "Float64", Type: field.FTFloat64},22 &mapping.FieldDesc{Name: "Bytes", Type: field.FTBytes},23 }24 // 8 * 8 + 16 * 3 = 11225 root := New(0, msg0Mapping)26 // Test zero value of bool field.27 gotBool, err := GetBool(root, 1)28 if err != nil {29 t.Fatalf("TestBasicEncodeDecodeStruct(initial setup): unexpected error: %s", err)30 }31 if gotBool {32 t.Fatalf("TestBasicEncodeDecodeStruct(initial setup): non-set bool field is true")33 }34 // Set bool field.35 if err := SetBool(root, 1, true); err != nil {36 t.Fatalf("TestBasicEncodeDecodeStruct(initial setup): unexpected error: %s", err)37 }38 // Test bool field.39 gotBool, err = GetBool(root, 1)40 if err != nil {41 t.Fatalf("TestBasicEncodeDecodeStruct(initial setup): unexpected error: %s", err)42 }43 if !gotBool {44 t.Fatalf("TestBasicEncodeDecodeStruct(initial setup): set bool field is false")45 }46 // Test zero value of int8 field.47 gotInt8, err := GetNumber[int8](root, 2)48 if err != nil {49 t.Fatalf("TestBasicEncodeDecodeStruct(initial setup): unexpected error: %s", err)50 }51 if gotInt8 != 0 {52 t.Fatalf("TestBasicEncodeDecodeStruct(initial setup): int8 field is %d", gotInt8)53 }54 // Set int8 field.55 if err := SetNumber(root, 2, int8(-1)); err != nil {56 t.Fatalf("TestBasicEncodeDecodeStruct(initial setup): unexpected error: %s", err)57 }58 // Test int8 field.59 gotInt8, err = GetNumber[int8](root, 2)60 if err != nil {61 t.Fatalf("TestBasicEncodeDecodeStruct(initial setup): unexpected error: %s", err)62 }63 if gotInt8 != -1 {64 t.Fatalf("TestBasicEncodeDecodeStruct(initial setup): int8 field, got %d, want -1", gotInt8)65 }66 // Test zero value of int16 field.67 gotInt16, err := GetNumber[int16](root, 3)68 if err != nil {69 t.Fatalf("TestBasicEncodeDecodeStruct(initial setup): unexpected error: %s", err)70 }71 if gotInt16 != 0 {72 t.Fatalf("TestBasicEncodeDecodeStruct(initial setup): int16 field is %d", gotInt16)73 }74 // Set int16 field.75 if err := SetNumber(root, 3, int16(-2)); err != nil {76 t.Fatalf("TestBasicEncodeDecodeStruct(initial setup): unexpected error: %s", err)77 }78 // Test int16 field.79 gotInt16, err = GetNumber[int16](root, 3)80 if err != nil {81 t.Fatalf("TestBasicEncodeDecodeStruct(initial setup): unexpected error: %s", err)82 }83 if gotInt16 != -2 {84 t.Fatalf("TestBasicEncodeDecodeStruct(initial setup): int16 field, got %d, want -2", gotInt16)85 }86 // Test zero value of int32 field.87 gotInt32, err := GetNumber[int32](root, 4)88 if err != nil {89 t.Fatalf("TestBasicEncodeDecodeStruct(initial setup): unexpected error: %s", err)90 }91 if gotInt32 != 0 {92 t.Fatalf("TestBasicEncodeDecodeStruct(initial setup): int32 field is %d", gotInt32)93 }94 // Set int32 field.95 if err := SetNumber(root, 4, int32(-3)); err != nil {96 t.Fatalf("TestBasicEncodeDecodeStruct(initial setup): unexpected error: %s", err)97 }98 // Test int32 field.99 gotInt32, err = GetNumber[int32](root, 4)100 if err != nil {101 t.Fatalf("TestBasicEncodeDecodeStruct(initial setup): unexpected error: %s", err)102 }103 if gotInt32 != -3 {104 t.Fatalf("TestBasicEncodeDecodeStruct(initial setup): int32 field, got %d, want -3", gotInt32)105 }106 // Test zero value of int64 field.107 gotInt64, err := GetNumber[int64](root, 5)108 if err != nil {109 t.Fatalf("TestBasicEncodeDecodeStruct(initial setup): unexpected error: %s", err)110 }111 if gotInt64 != 0 {112 t.Fatalf("TestBasicEncodeDecodeStruct(initial setup): int64 field is %d", gotInt64)113 }114 // Set int64 field.115 if err := SetNumber(root, 5, int64(-4)); err != nil {116 t.Fatalf("TestBasicEncodeDecodeStruct(initial setup): unexpected error: %s", err)117 }118 // Test int64 field.119 gotInt64, err = GetNumber[int64](root, 5)120 if err != nil {121 t.Fatalf("TestBasicEncodeDecodeStruct(initial setup): unexpected error: %s", err)122 }123 if gotInt64 != -4 {124 t.Fatalf("TestBasicEncodeDecodeStruct(initial setup): int64 field, got %d, want -4", gotInt64)125 }126 // Test zero value of uint8 field.127 gotUint8, err := GetNumber[uint8](root, 6)128 if err != nil {129 t.Fatalf("TestBasicEncodeDecodeStruct(initial setup): unexpected error: %s", err)130 }131 if gotUint8 != 0 {132 t.Fatalf("TestBasicEncodeDecodeStruct(initial setup): uint8 field is %d", gotUint8)133 }134 // Set uint8 field.135 if err := SetNumber(root, 6, uint8(1)); err != nil {136 t.Fatalf("TestBasicEncodeDecodeStruct(initial setup): unexpected error: %s", err)137 }138 // Test uint8 field.139 gotUint8, err = GetNumber[uint8](root, 6)140 if err != nil {141 t.Fatalf("TestBasicEncodeDecodeStruct(initial setup): unexpected error: %s", err)142 }143 if gotUint8 != 1 {144 t.Fatalf("TestBasicEncodeDecodeStruct(initial setup): uint8 field, got %d, want 1", gotUint8)145 }146 // Test zero value of uint16 field.147 gotUint16, err := GetNumber[uint16](root, 7)148 if err != nil {149 t.Fatalf("TestBasicEncodeDecodeStruct(initial setup): unexpected error: %s", err)150 }151 if gotUint16 != 0 {152 t.Fatalf("TestBasicEncodeDecodeStruct(initial setup): uint16 field is %d", gotUint16)153 }154 // Set uint16 field.155 if err := SetNumber(root, 7, uint16(2)); err != nil {156 t.Fatalf("TestBasicEncodeDecodeStruct(initial setup): unexpected error: %s", err)157 }158 // Test uint16 field.159 gotUint16, err = GetNumber[uint16](root, 7)160 if err != nil {161 t.Fatalf("TestBasicEncodeDecodeStruct(initial setup): unexpected error: %s", err)162 }163 if gotUint16 != 2 {164 t.Fatalf("TestBasicEncodeDecodeStruct(initial setup): uint16 field, got %d, want 2", gotUint16)165 }166 // Test zero value of uint32 field.167 gotUint32, err := GetNumber[uint32](root, 8)168 if err != nil {169 t.Fatalf("TestBasicEncodeDecodeStruct(initial setup): unexpected error: %s", err)170 }171 if gotUint32 != 0 {172 t.Fatalf("TestBasicEncodeDecodeStruct(initial setup): uint32 field is %d", gotUint32)173 }174 // Set uint32 field.175 if err := SetNumber(root, 8, uint32(3)); err != nil {176 t.Fatalf("TestBasicEncodeDecodeStruct(initial setup): unexpected error: %s", err)177 }178 // Test uint32 field.179 gotUint32, err = GetNumber[uint32](root, 8)180 if err != nil {181 t.Fatalf("TestBasicEncodeDecodeStruct(initial setup): unexpected error: %s", err)182 }183 if gotUint32 != 3 {184 t.Fatalf("TestBasicEncodeDecodeStruct(initial setup): uint32 field, got %d, want 3", gotUint32)185 }186 // Test zero value of uint64 field.187 gotUint64, err := GetNumber[uint64](root, 9)188 if err != nil {189 t.Fatalf("TestBasicEncodeDecodeStruct(initial setup): unexpected error: %s", err)190 }191 if gotUint64 != 0 {192 t.Fatalf("TestBasicEncodeDecodeStruct(initial setup): uint64 field is %d", gotUint64)193 }194 // Set uint64 field.195 if err := SetNumber(root, 9, uint64(4)); err != nil {196 t.Fatalf("TestBasicEncodeDecodeStruct(initial setup): unexpected error: %s", err)197 }198 // Test uint64 field.199 gotUint64, err = GetNumber[uint64](root, 9)200 if err != nil {201 t.Fatalf("TestBasicEncodeDecodeStruct(initial setup): unexpected error: %s", err)202 }203 if gotUint64 != 4 {204 t.Fatalf("TestBasicEncodeDecodeStruct(initial setup): uint64 field, got %d, want 4", gotUint64)205 }206 // Test zero value of float32 field.207 gotFloat32, err := GetNumber[float32](root, 10)208 if err != nil {209 t.Fatalf("TestBasicEncodeDecodeStruct(initial setup): unexpected error: %s", err)210 }211 if gotFloat32 != 0 {212 t.Fatalf("TestBasicEncodeDecodeStruct(initial setup): float32 field is %v", gotFloat32)213 }214 // Set float32 field.215 if err := SetNumber(root, 10, float32(1.2)); err != nil {216 t.Fatalf("TestBasicEncodeDecodeStruct(initial setup): unexpected error: %s", err)217 }218 // Test float32 field.219 gotFloat32, err = GetNumber[float32](root, 10)220 if err != nil {221 t.Fatalf("TestBasicEncodeDecodeStruct(initial setup): unexpected error: %s", err)222 }223 if gotFloat32 != 1.2 {224 t.Fatalf("TestBasicEncodeDecodeStruct(initial setup): float32 field, got %v, want 1.2", gotFloat32)225 }226 // Test zero value of float64 field.227 gotFloat64, err := GetNumber[float64](root, 11)228 if err != nil {229 t.Fatalf("TestBasicEncodeDecodeStruct(initial setup): unexpected error: %s", err)230 }231 if gotFloat64 != 0 {232 t.Fatalf("TestBasicEncodeDecodeStruct(initial setup): float64 field is %v", gotFloat64)233 }234 // Set float64 field.235 if err := SetNumber(root, 11, float64(1.2)); err != nil {236 t.Fatalf("TestBasicEncodeDecodeStruct(initial setup): unexpected error: %s", err)237 }238 // Test float64 field.239 gotFloat64, err = GetNumber[float64](root, 11)240 if err != nil {241 t.Fatalf("TestBasicEncodeDecodeStruct(initial setup): unexpected error: %s", err)242 }243 if gotFloat64 != 1.2 {244 t.Fatalf("TestBasicEncodeDecodeStruct(initial setup): float64 field, got %v, want 1.2", gotFloat64)245 }246 var totalWithScalars int64 = 112247 if *root.total != totalWithScalars {248 t.Fatalf("TestBasicEncodeDecodeStruct(initial setup): .total after setting up bool + numeric fields was %d, want %d", *root.total, totalWithScalars)249 }250 /////////////////////251 // End Scalars252 /////////////////////253 // Test zero value of float64 field.254 getBytes, err := GetBytes(root, 12)255 if err != nil {256 t.Fatalf("TestBasicEncodeDecodeStruct(initial setup): unexpected error: %s", err)257 }258 if getBytes != nil {259 t.Fatalf("TestBasicEncodeDecodeStruct(initial setup): bytes field is %v", getBytes)260 }261 err = SetBytes(root, 12, []byte{}, false)262 if err != nil {263 t.Fatalf("TestBasicEncodeDecodeStruct(initial setup): unexpected error: %s", err)264 }265 // Test empty bytes field.266 getBytes, err = GetBytes(root, 12)267 if err != nil {268 t.Fatalf("TestBasicEncodeDecodeStruct(initial setup): unexpected error: %s", err)269 }270 if !bytes.Equal(getBytes, []byte{}) {271 t.Fatalf("TestBasicEncodeDecodeStruct(initial setup): want empty bytes field ([]byte{}), got %v", getBytes)272 }273 // Add byte field.274 strData := "Hello World"275 err = SetBytes(root, 12, []byte(strData), false)276 if err != nil {277 t.Fatalf("TestBasicEncodeDecodeStruct(initial setup): unexpected error: %s", err)278 }279 getBytes, err = GetBytes(root, 12)280 if err != nil {281 t.Fatalf("TestBasicEncodeDecodeStruct(initial setup): unexpected error: %s", err)282 }283 if !bytes.Equal(getBytes, []byte(strData)) {284 t.Fatalf("TestBasicEncodeDecodeStruct(initial setup): want empty bytes field: %v, got: %v", strData, string(getBytes))285 }286 totalWithBytes := totalWithScalars + 8 + int64(SizeWithPadding(len(strData)))287 if *root.total != totalWithBytes {288 t.Fatalf("TestBasicEncodeDecodeStruct(initial setup): .total after adding bytes field was %d, want %d", *root.total, totalWithBytes)289 }290}291func TestGetBool(t *testing.T) {292 m := mapping.Map{293 &mapping.FieldDesc{294 Type: field.FTBool,295 },296 &mapping.FieldDesc{297 Type: field.FTFloat32,298 },299 &mapping.FieldDesc{300 Type: field.FTBool,301 },302 &mapping.FieldDesc{303 Type: field.FTBool,304 },305 }306 s := &Struct{307 mapping: m,308 fields: make([]structField, len(m)),309 total: new(int64),310 }311 if err := SetBool(s, 3, true); err != nil {312 panic(err)313 }314 if err := SetBool(s, 4, false); err != nil {315 panic(err)316 }317 tests := []struct {318 desc string319 s *Struct320 fieldNum uint16321 want bool322 err bool323 }{324 {325 desc: "Error: fieldNum is 0",326 s: s,327 fieldNum: 0,328 err: true,329 },330 {331 desc: "Error: fieldNum is greater that possible fields",332 s: s,333 fieldNum: 5,334 err: true,335 },336 {337 desc: "Error: fieldNum is not a bool",338 s: s,339 fieldNum: 2, // FTFloat32340 err: true,341 },342 {343 desc: "Error: fieldNum is not a bool",344 s: s,345 fieldNum: 2, // FTFloat32346 err: true,347 },348 {349 desc: "fieldNum that has a nil value and should return false",350 s: s,351 fieldNum: 1,352 want: false,353 },354 {355 desc: "fieldNum that is set to true",356 s: s,357 fieldNum: 3,358 want: true,359 },360 {361 desc: "fieldNum that is set to false",362 s: s,363 fieldNum: 4,364 want: false,365 },366 }367 for _, test := range tests {368 got, err := GetBool(test.s, test.fieldNum)369 switch {370 case err == nil && test.err:371 t.Errorf("TestGetBool(%s): got err == nil, want err != nil", test.desc)372 continue373 case err != nil && !test.err:374 t.Errorf("TestGetBool(%s): got err == %s, want err == nil", test.desc, err)375 continue376 case err != nil:377 continue378 }379 if got != test.want {380 t.Errorf("TestGetBool(%s): got %v, want %v", test.desc, got, test.want)381 }382 }383}384func TestSetNumber(t *testing.T) {385 // This is going to only handle cases not handled in GetNumber()386 m := mapping.Map{387 &mapping.FieldDesc{388 Type: field.FTFloat32,389 },390 &mapping.FieldDesc{391 Type: field.FTFloat64,392 },393 }394 s := &Struct{395 mapping: m,396 fields: make([]structField, len(m)),397 total: new(int64),398 }399 if err := SetNumber[float32](s, 1, float32(8.7)); err != nil {400 panic(err)401 }402 if err := SetNumber[float64](s, 2, math.MaxFloat64); err != nil {403 panic(err)404 }405 gotFloat32, err := GetNumber[float32](s, 1)406 if err != nil {407 panic(err)408 }409 if gotFloat32 != 8.7 {410 t.Fatalf("TestSetNumber(float32): got %v, want 8.7", gotFloat32)411 }412 gotFloat64, err := GetNumber[float64](s, 2)413 if err != nil {414 panic(err)415 }416 if gotFloat64 != math.MaxFloat64 {417 t.Fatalf("TestSetNumber(float64): got %v, want 8.7", gotFloat64)418 }419}420func TestGetNumber(t *testing.T) {421 m := mapping.Map{422 &mapping.FieldDesc{423 Type: field.FTUint8,424 },425 &mapping.FieldDesc{426 Type: field.FTBool,427 },428 &mapping.FieldDesc{429 Type: field.FTInt8,430 },431 &mapping.FieldDesc{432 Type: field.FTUint64,433 },434 &mapping.FieldDesc{435 Type: field.FTFloat32,436 },437 }438 s := &Struct{439 mapping: m,440 fields: make([]structField, len(m)),441 total: new(int64),442 }443 if err := SetNumber[int8](s, 3, 10); err != nil {444 panic(err)445 }446 if err := SetNumber[uint64](s, 4, uint64(math.MaxUint32)+1); err != nil {447 panic(err)448 }449 if err := SetNumber[float32](s, 5, 3.2); err != nil {450 panic(err)451 }452 tests := []struct {453 desc string454 s *Struct455 fieldNum uint16456 want any457 err bool458 }{459 {460 desc: "Error: fieldNum is 0",461 s: s,462 fieldNum: 0,463 err: true,464 },465 {466 desc: "Error: fieldNum is greater that possible fields",467 s: s,468 fieldNum: 30,469 err: true,470 },471 {472 desc: "Error: fieldNum is not a number",473 s: s,474 fieldNum: 2, // FTBool475 err: true,476 },477 {478 desc: "fieldNum that has a nil value and should return 0",479 s: s,480 fieldNum: 1,481 want: uint8(0),482 },483 {484 desc: "fieldNum that is set to 10",485 s: s,486 fieldNum: 3,487 want: int8(10),488 },489 {490 desc: "fieldNum that is set to math.MaxUint32+1",491 s: s,492 fieldNum: 4,493 want: uint64(math.MaxUint32) + 1,494 },495 {496 desc: "fieldNum that is set to a float",497 s: s,498 fieldNum: 5,499 want: float32(3.2),500 },501 }502 for _, test := range tests {503 var got any504 var err error505 // We can't switch on types for either field 0 or fields not in our mapping.Map, but506 // we still want to test our error conditions.507 if test.fieldNum < 1 || test.fieldNum-1 > uint16(len(m)) {508 got, err = GetNumber[uint8](test.s, test.fieldNum)509 } else { // Any other tests510 switch m[test.fieldNum-1].Type {511 case field.FTUint8:512 got, err = GetNumber[uint8](test.s, test.fieldNum)513 case field.FTUint16:514 got, err = GetNumber[uint16](test.s, test.fieldNum)515 case field.FTUint64:516 got, err = GetNumber[uint64](test.s, test.fieldNum)517 case field.FTInt8:518 got, err = GetNumber[int8](test.s, test.fieldNum)519 case field.FTFloat32:520 got, err = GetNumber[float32](test.s, test.fieldNum)521 case field.FTBool: // So we can test that we get an error on a bad field type522 got, err = GetNumber[uint64](test.s, test.fieldNum)523 default:524 panic("wtf")525 }526 }527 switch {528 case err == nil && test.err:529 t.Errorf("TestGetNumber(%s): got err == nil, want err != nil", test.desc)530 continue531 case err != nil && !test.err:532 t.Errorf("TestGetNumber(%s): got err == %s, want err == nil", test.desc, err)533 continue534 case err != nil:535 continue536 }537 if got != test.want {538 t.Errorf("TestGetNumber(%s): got %v, want %v", test.desc, got, test.want)539 }540 }541}542func TestNumberToDescCheck(t *testing.T) {543 tests := []struct {544 n any545 desc mapping.FieldDesc546 wantSize uint8547 wantIsFloat bool548 wantErr bool549 }{550 {uint8(1), mapping.FieldDesc{Type: field.FTUint8}, 8, false, false},551 {uint16(1), mapping.FieldDesc{Type: field.FTUint16}, 16, false, false},552 {uint32(1), mapping.FieldDesc{Type: field.FTUint32}, 32, false, false},553 {uint64(1), mapping.FieldDesc{Type: field.FTUint64}, 64, false, false},554 {int8(1), mapping.FieldDesc{Type: field.FTInt8}, 8, false, false},555 {int16(1), mapping.FieldDesc{Type: field.FTInt16}, 16, false, false},556 {int32(1), mapping.FieldDesc{Type: field.FTInt32}, 32, false, false},557 {int64(1), mapping.FieldDesc{Type: field.FTInt64}, 64, false, false},558 {float32(1), mapping.FieldDesc{Type: field.FTFloat32}, 32, true, false},559 {float64(1), mapping.FieldDesc{Type: field.FTFloat64}, 64, true, false},560 // Cause an error.561 {uint8(1), mapping.FieldDesc{Type: field.FTUint16}, 8, false, true},562 }563 for _, test := range tests {564 var gotSize uint8565 var gotIsFloat bool566 var err error567 switch test.n.(type) {568 case uint8:569 gotSize, gotIsFloat, err = numberToDescCheck[uint8](&test.desc)570 case uint16:571 gotSize, gotIsFloat, err = numberToDescCheck[uint16](&test.desc)572 case uint32:573 gotSize, gotIsFloat, err = numberToDescCheck[uint32](&test.desc)574 case uint64:575 gotSize, gotIsFloat, err = numberToDescCheck[uint64](&test.desc)576 case int8:577 gotSize, gotIsFloat, err = numberToDescCheck[int8](&test.desc)578 case int16:579 gotSize, gotIsFloat, err = numberToDescCheck[int16](&test.desc)580 case int32:581 gotSize, gotIsFloat, err = numberToDescCheck[int32](&test.desc)582 case int64:583 gotSize, gotIsFloat, err = numberToDescCheck[int64](&test.desc)584 case float32:585 gotSize, gotIsFloat, err = numberToDescCheck[float32](&test.desc)586 case float64:587 gotSize, gotIsFloat, err = numberToDescCheck[float64](&test.desc)588 default:589 panic("wtf")590 }591 switch {592 case err == nil && test.wantErr:593 t.Errorf("TestNumberToDescCheck(%T): got err == nil, want err != nil", test.n)594 continue595 case err != nil && !test.wantErr:596 t.Errorf("TestNumberToDescCheck(%T): got err == %s, want err == nil", test.n, err)597 continue598 case err != nil:599 continue600 }601 if gotSize != test.wantSize {602 t.Errorf("TestNumberToDescCheck(%T): size: got %v, want %v", test.n, gotSize, test.wantSize)603 }604 if gotIsFloat != test.wantIsFloat {605 t.Errorf("TestNumberToDescCheck(%T): isFloat: got %v, want %v", test.n, gotIsFloat, test.wantIsFloat)606 }607 }608}...

Full Screen

Full Screen

util_test.go

Source:util_test.go Github

copy

Full Screen

1// Licensed to the Apache Software Foundation (ASF) under one or more2// contributor license agreements. See the NOTICE file distributed with3// this work for additional information regarding copyright ownership.4// The ASF licenses this file to You under the Apache License, Version 2.05// (the "License"); you may not use this file except in compliance with6// the License. You may obtain a copy of the License at7//8// http://www.apache.org/licenses/LICENSE-2.09//10// Unless required by applicable law or agreed to in writing, software11// distributed under the License is distributed on an "AS IS" BASIS,12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13// See the License for the specific language governing permissions and14// limitations under the License.15package filesystem16import (17 "bytes"18 "context"19 "fmt"20 "io"21 "testing"22)23// A basic test implementation to validate the utility functions.24type testImpl struct {25 m map[string][]byte26 listErr, openReadErr, openWriteErr, sizeErr error27 readerErr, readerCloseErr error28 writerErr, writerCloseErr error29}30func (fs *testImpl) List(ctx context.Context, glob string) ([]string, error) {31 return nil, fs.listErr32}33func (fs *testImpl) OpenRead(ctx context.Context, filename string) (io.ReadCloser, error) {34 if fs.openReadErr != nil {35 return nil, fs.openReadErr36 }37 if v, ok := fs.m[filename]; ok {38 return errReadCloser{Reader: bytes.NewReader(v), closeErr: fs.readerCloseErr, readErr: fs.readerErr}, nil39 }40 return nil, nil41}42type errReadCloser struct {43 io.Reader44 closeErr, readErr error45}46func (e errReadCloser) Close() error { return e.closeErr }47func (e errReadCloser) Read(p []byte) (n int, err error) {48 if e.readErr != nil {49 return 0, e.readErr50 }51 return e.Reader.Read(p)52}53func (fs *testImpl) OpenWrite(ctx context.Context, filename string) (io.WriteCloser, error) {54 if fs.openWriteErr != nil {55 return nil, fs.openWriteErr56 }57 var buf bytes.Buffer58 return errWriteCloser{59 Writer: &buf,60 closeFn: func() {61 fs.m[filename] = buf.Bytes()62 },63 closeErr: fs.writerCloseErr,64 writeErr: fs.writerErr,65 }, nil66}67type errWriteCloser struct {68 io.Writer69 closeFn func()70 closeErr, writeErr error71}72func (e errWriteCloser) Close() error {73 if e.closeErr != nil {74 return e.closeErr75 }76 e.closeFn()77 return nil78}79func (e errWriteCloser) Write(p []byte) (n int, err error) {80 if e.writeErr != nil {81 return 0, e.writeErr82 }83 return e.Writer.Write(p)84}85func (fs *testImpl) Size(ctx context.Context, filename string) (int64, error) {86 return -1, fs.sizeErr87}88func (fs *testImpl) Close() error {89 return nil90}91func newTestImpl() *testImpl {92 return &testImpl{m: map[string][]byte{}}93}94func TestRead(t *testing.T) {95 ctx := context.Background()96 filename := "filename"97 data := []byte("arbitrary data")98 setup := func() *testImpl {99 fs := newTestImpl()100 fs.m[filename] = data101 return fs102 }103 t.Run("happypath", func(t *testing.T) {104 fs := setup()105 gotData, err := Read(ctx, fs, filename)106 if err != nil {107 t.Errorf("error on Read() = %v, want nil", err)108 }109 if got, want := string(gotData), string(data); got != want {110 t.Errorf("Read() = %v, want %v", got, want)111 }112 })113 t.Run("openError", func(t *testing.T) {114 fs := setup()115 fs.openReadErr = fmt.Errorf("bad open")116 _, err := Read(ctx, fs, filename)117 if got, want := err, fs.openReadErr; got != want {118 t.Errorf("Read() = %v, want %v", got, want)119 }120 })121 t.Run("closeError", func(t *testing.T) {122 fs := setup()123 fs.readerCloseErr = fmt.Errorf("bad read close")124 gotData, err := Read(ctx, fs, filename)125 // close errors are ignored for Read.126 if err != nil {127 t.Errorf("error on Read() = %v, want nil", err)128 }129 if got, want := string(gotData), string(data); got != want {130 t.Errorf("Read() = %v, want %v", got, want)131 }132 })133 t.Run("readingError", func(t *testing.T) {134 fs := setup()135 fs.readerErr = fmt.Errorf("bad read")136 _, err := Read(ctx, fs, filename)137 if got, want := err, fs.readerErr; got != want {138 t.Errorf("Read() = %v, want %v", got, want)139 }140 })141}142func TestWrite(t *testing.T) {143 ctx := context.Background()144 filename := "filename"145 data := []byte("arbitrary data")146 setup := newTestImpl147 t.Run("happypath", func(t *testing.T) {148 fs := setup()149 if err := Write(ctx, fs, filename, data); err != nil {150 t.Errorf("error on Write() = %v, want nil", err)151 }152 gotData := fs.m[filename]153 if got, want := string(gotData), string(data); got != want {154 t.Errorf("Write() = %v, want %v", got, want)155 }156 })157 t.Run("openError", func(t *testing.T) {158 fs := setup()159 fs.openWriteErr = fmt.Errorf("bad open")160 err := Write(ctx, fs, filename, data)161 if got, want := err, fs.openWriteErr; got != want {162 t.Errorf("Write() = %v, want %v", got, want)163 }164 })165 t.Run("closeError", func(t *testing.T) {166 fs := setup()167 fs.writerCloseErr = fmt.Errorf("bad write close")168 err := Write(ctx, fs, filename, data)169 if got, want := err, fs.writerCloseErr; got != want {170 t.Errorf("Write() = %v, want %v", got, want)171 }172 })173 t.Run("writingError", func(t *testing.T) {174 fs := setup()175 fs.writerErr = fmt.Errorf("bad write")176 err := Write(ctx, fs, filename, data)177 if got, want := err, fs.writerErr; got != want {178 t.Errorf("Write() = %v, want %v", got, want)179 }180 })181}182type testCopyImpl struct {183 *testImpl184 copyErr error185}186func copyImpl(fs *testImpl) *testCopyImpl {187 return &testCopyImpl{testImpl: fs}188}189func (fs *testCopyImpl) Copy(ctx context.Context, oldpath, newpath string) error {190 if fs.copyErr != nil {191 return fs.copyErr192 }193 fs.m[newpath] = fs.m[oldpath]194 return nil195}196var _ Copier = (*testCopyImpl)(nil)197func TestCopy(t *testing.T) {198 ctx := context.Background()199 filename1 := "filename1"200 filename2 := "filename2"201 data := []byte("arbitrary data")202 setup := func() *testImpl {203 fs := newTestImpl()204 fs.m[filename1] = data205 return fs206 }207 // Test if the Copier interface is doing it's job.208 t.Run("happypath_copier", func(t *testing.T) {209 fs := copyImpl(setup())210 if err := Copy(ctx, fs, filename1, filename2); err != nil {211 t.Errorf("error on Copy() = %v, want nil", err)212 }213 gotData := fs.m[filename2]214 if got, want := string(gotData), string(data); got != want {215 t.Errorf("Copy(%q,%q) data = %v, want %v", filename1, filename2, got, want)216 }217 if _, ok := fs.m[filename1]; !ok {218 t.Errorf("Copy(%q,%q) removed old path: %v, should have been kept.", filename1, filename2, filename1)219 }220 })221 t.Run("copyError_copier", func(t *testing.T) {222 fs := copyImpl(setup())223 fs.copyErr = fmt.Errorf("bad copy")224 if got, want := Copy(ctx, fs, filename1, filename2), fs.copyErr; got != want {225 t.Errorf("error on Copy() = %v, want %v", got, want)226 }227 })228 // Test that the backup worked.229 t.Run("happypath", func(t *testing.T) {230 fs := setup()231 if err := Copy(ctx, fs, filename1, filename2); err != nil {232 t.Errorf("error on Copy(%q, %q) = %v, want nil", filename1, filename2, err)233 }234 gotData := fs.m[filename2]235 if got, want := string(gotData), string(data); got != want {236 t.Errorf("Copy(%q, %q) data = %v, want %v", filename1, filename2, got, want)237 }238 if _, ok := fs.m[filename1]; !ok {239 t.Errorf("Copy(%q,%q) removed old path: %v, should have been kept.", filename1, filename2, filename1)240 }241 })242 t.Run("openWriteError", func(t *testing.T) {243 fs := setup()244 fs.openWriteErr = fmt.Errorf("bad write")245 if got, want := Copy(ctx, fs, filename1, filename2), fs.openWriteErr; got != want {246 t.Errorf("wanted error on Copy() = %v, want %v", got, want)247 }248 })249 t.Run("openReadError", func(t *testing.T) {250 fs := setup()251 fs.openReadErr = fmt.Errorf("bad read")252 if got, want := Copy(ctx, fs, filename1, filename2), fs.openReadErr; got != want {253 t.Errorf("wanted error on Copy() = %v, want %v", got, want)254 }255 })256 t.Run("writeError", func(t *testing.T) {257 fs := setup()258 fs.writerErr = fmt.Errorf("bad write")259 if got, want := Copy(ctx, fs, filename1, filename2), fs.writerErr; got != want {260 t.Errorf("wanted error on Copy() = %v, want %v", got, want)261 }262 })263 t.Run("readError", func(t *testing.T) {264 fs := setup()265 fs.readerErr = fmt.Errorf("bad read")266 if got, want := Copy(ctx, fs, filename1, filename2), fs.readerErr; got != want {267 t.Errorf("wanted error on Copy() = %v, want %v", got, want)268 }269 })270}271type testRenameImpl struct {272 *testImpl273 renameErr error274}275func renameImpl(fs *testImpl) *testRenameImpl {276 return &testRenameImpl{testImpl: fs}277}278func (fs *testRenameImpl) Rename(ctx context.Context, oldpath, newpath string) error {279 if fs.renameErr != nil {280 return fs.renameErr281 }282 fs.m[newpath] = fs.m[oldpath]283 delete(fs.m, oldpath)284 return nil285}286var _ Renamer = (*testRenameImpl)(nil)287type testRemoveImpl struct {288 *testImpl289 removeErr error290}291func removeImpl(fs *testImpl) *testRemoveImpl {292 return &testRemoveImpl{testImpl: fs}293}294func (fs *testRemoveImpl) Remove(ctx context.Context, filename string) error {295 if fs.removeErr != nil {296 return fs.removeErr297 }298 delete(fs.m, filename)299 return nil300}301var _ Remover = (*testRemoveImpl)(nil)302func TestRename(t *testing.T) {303 ctx := context.Background()304 filename1 := "filename1"305 filename2 := "filename2"306 data := []byte("arbitrary data")307 setup := func() *testImpl {308 fs := newTestImpl()309 fs.m[filename1] = data310 return fs311 }312 // Test if the Renamer interface is doing it's job.313 t.Run("happypath_renamer", func(t *testing.T) {314 fs := renameImpl(setup())315 if err := Rename(ctx, fs, filename1, filename2); err != nil {316 t.Errorf("error on Rename() = %v, want nil", err)317 }318 gotData := fs.m[filename2]319 if got, want := string(gotData), string(data); got != want {320 t.Errorf("Rename(%q,%q) data = %v, want %v", filename1, filename2, got, want)321 }322 if _, ok := fs.m[filename1]; ok {323 t.Errorf("Rename(%q,%q) did not remove old path: %v", filename1, filename2, filename1)324 }325 })326 t.Run("renameError_renamer", func(t *testing.T) {327 fs := renameImpl(setup())328 fs.renameErr = fmt.Errorf("rename error")329 if got, want := Rename(ctx, fs, filename1, filename2), fs.renameErr; got != want {330 t.Errorf("error on Rename() = %v, want %v", got, want)331 }332 })333 t.Run("removerUnimplemented", func(t *testing.T) {334 fs := setup()335 err := Rename(ctx, fs, filename1, filename2)336 if _, ok := err.(*unimplementedError); !ok {337 t.Errorf("Rename(non-remover) = want unimplementedError, got = %v", err)338 }339 })340 t.Run("happypath", func(t *testing.T) {341 fs := removeImpl(setup())342 if err := Rename(ctx, fs, filename1, filename2); err != nil {343 t.Errorf("error on Rename() = %v, want nil", err)344 }345 gotData := fs.m[filename2]346 if got, want := string(gotData), string(data); got != want {347 t.Errorf("Rename(%q,%q) data = %v, want %v", filename1, filename2, got, want)348 }349 if _, ok := fs.m[filename1]; ok {350 t.Errorf("Rename(%q,%q) did not remove old path: %v", filename1, filename2, filename1)351 }352 })353 t.Run("copyError", func(t *testing.T) {354 fs := removeImpl(setup())355 fs.openReadErr = fmt.Errorf("bad copy")356 if got, want := Rename(ctx, fs, filename1, filename2), fs.openReadErr; got != want {357 t.Errorf("error on Rename() = %v, want %v", got, want)358 }359 })360 t.Run("removeError", func(t *testing.T) {361 fs := removeImpl(setup())362 fs.removeErr = fmt.Errorf("bad remove")363 if got, want := Rename(ctx, fs, filename1, filename2), fs.removeErr; got != want {364 t.Errorf("error on Rename() = %v, want %v", got, want)365 }366 })367}...

Full Screen

Full Screen

config_test.go

Source:config_test.go Github

copy

Full Screen

...37type testCluster struct {38 User string `yaml:"-"`39 InstanceType string `yaml:"instance_type"`40 NumInstances int `yaml:"num_instances"`41 SetupUser string `yaml:"setup_user"`42 FromInstance bool `yaml:"-"`43 instance clusterInstance44}45func (c *testCluster) Init(creds *testCreds) error {46 c.FromInstance = c.instance.User != ""47 if c.FromInstance {48 c.User = c.instance.User49 } else {50 c.User = string(*creds)51 }52 c.instance.User = c.User53 return nil54}55func (c *testCluster) Config() interface{} {56 return c57}58func (c *testCluster) InstanceConfig() interface{} {59 return &c.instance60}61func (c *testCluster) Setup(creds *testCreds) error {62 if string(*creds) == "" {63 return errors.New("no user specified")64 }65 c.InstanceType = "xxx"66 c.NumInstances = 12367 c.SetupUser = string(*creds)68 return nil69}70func (c *testCluster) Version() int {71 return 172}73type testSetup bool74func (s *testSetup) Setup() error {75 *s = true76 return nil77}78func (s *testSetup) Config() interface{} { return s }79func (*testSetup) Version() int { return 1 }80func init() {81 infra.Register("testcreds", new(testCreds))82 infra.Register("testuserembed", new(testUserEmbed))83 infra.Register("testcluster", new(testCluster))84 infra.Register("testsetup", new(testSetup))85 infra.Register("testembedstructcluster", new(testEmbedStructCluster))86 infra.Register("testembeddedcluster", new(TestEmbeddedCluster))87}88var schema = infra.Schema{89 "creds": new(testCreds),90 "cluster": new(testCluster),91 "setup": new(testSetup),92}93func TestConfig(t *testing.T) {94 config, err := schema.Make(infra.Keys{95 "creds": "testcreds,user=testuser",96 "cluster": "testcluster",97 })98 if err != nil {99 t.Fatal(err)100 }101 var cluster *testCluster102 config.Must(&cluster)103 if got, want := cluster.User, "testuser"; got != want {104 t.Errorf("got %v, want %v", got, want)105 }106}107func TestConfigUnmarshal(t *testing.T) {108 config, err := schema.Unmarshal([]byte(`creds: testcreds,user=unmarshaled109cluster: testcluster110testcluster:111 instance_type: xyz112 num_instances: 123113`))114 if err != nil {115 t.Fatal(err)116 }117 var cluster *testCluster118 config.Must(&cluster)119 if got, want := *cluster, (testCluster{"unmarshaled", "xyz", 123, "", false, clusterInstance{"unmarshaled"}}); got != want {120 t.Errorf("got %v, want %v", got, want)121 }122}123func TestConfigInterface(t *testing.T) {124 type credentials interface {125 User() string126 }127 schema := infra.Schema{"creds": new(credentials)}128 config, err := schema.Make(129 infra.Keys{"creds": "testcreds,user=interface"},130 )131 if err != nil {132 t.Fatal(err)133 }134 var creds credentials135 config.Must(&creds)136 if got, want := creds.User(), "interface"; got != want {137 t.Errorf("got %v, want %v", got, want)138 }139}140func TestConfigPromote(t *testing.T) {141 type credentials interface {142 User() string143 }144 schema := infra.Schema{"user": User("")}145 config, err := schema.Make(146 infra.Keys{"user": "testuserembed"},147 )148 if err != nil {149 t.Fatal(err)150 }151 var user User152 config.Must(&user)153 if got, want := string(user), "embedded"; got != want {154 t.Errorf("got %v, want %v", got, want)155 }156}157func TestSetup(t *testing.T) {158 config, err := schema.Make(infra.Keys{159 "creds": "testcreds",160 "cluster": "testcluster",161 // We include this to make sure that "orphan" providers162 // are also accounted for.163 "setup": "testsetup",164 })165 if err != nil {166 t.Fatal(err)167 }168 if err := config.Setup(); err == nil || err.Error() != "setup testcluster: no user specified" {169 t.Fatal(err)170 }171 config, err = schema.Make(infra.Keys{172 "creds": "testcreds,user=xyz",173 "cluster": "testcluster",174 "setup": "testsetup",175 })176 if err != nil {177 t.Fatal(err)178 }179 if err := config.Setup(); err != nil {180 t.Fatal(err)181 }182 // Make sure183 p, err := config.Marshal(false)184 if err != nil {185 t.Fatal(err)186 }187 if got, want := string(p), `cluster: testcluster188creds: testcreds,user=xyz189setup: testsetup190testcluster:191 instance_type: xxx192 num_instances: 123193 setup_user: xyz...

Full Screen

Full Screen

Setup

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 g := got.Setup()4 fmt.Println(g)5}6import (7func main() {8 g := got.Setup()9 fmt.Println(g)10}11import (12func main() {13 g := got.Setup()14 fmt.Println(g)15}16import (17func main() {18 g := got.Setup()19 fmt.Println(g)20}21import (22func main() {23 g := got.Setup()24 fmt.Println(g)25}26import (27func main() {28 g := got.Setup()29 fmt.Println(g)30}31import (32func main() {33 g := got.Setup()34 fmt.Println(g)35}36import (37func main() {38 g := got.Setup()39 fmt.Println(g)40}41import (42func main() {43 g := got.Setup()44 fmt.Println(g)45}46import (47func main() {48 g := got.Setup()49 fmt.Println(g)50}51import (52func main() {53 g := got.Setup()54 fmt.Println(g)55}56import (

Full Screen

Full Screen

Setup

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 got.Setup()4 fmt.Println("Hello, playground")5}6import (7func main() {8 got.Setup()9 fmt.Println("Hello, playground")10}11./main.go:5: imported and not used: "got"12The error I'm getting is: ./main.go:4: imported and not used: "github.com/username/repo/pkg"13import "fmt"14func main() {15 fmt.Println("Hello, playground")16}17import "fmt"18func Test() {19 fmt.Println("Hello, playground")20}

Full Screen

Full Screen

Setup

Using AI Code Generation

copy

Full Screen

1func Setup(got *got) {2}3func DoSomething(got *got) {4}5func DoSomethingElse(got *got) {6}7func Teardown(got *got) {8}9func main() {10 got := new(got)11 Setup(got)12 DoSomething(got)13 DoSomethingElse(got)14 Teardown(got)15}16func Setup(got *got) {17}18func DoSomething(got *got) {19}20func DoSomethingElse(got *got) {21}22func Teardown(got *got) {23}24func main() {25 got := new(got)26 Setup(got)27 DoSomething(got)28 DoSomethingElse(got)29 Teardown(got)30}

Full Screen

Full Screen

Setup

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 g := got.New()4 g.Setup()5 g.Use(func(c *got.Context) {6 c.String(200, "Hello, World!")7 })

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful