github.com/pion/webrtc/v4@v4.0.1/internal/util/util_test.go (about) 1 // SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly> 2 // SPDX-License-Identifier: MIT 3 4 package util 5 6 import ( 7 "errors" 8 "regexp" 9 "testing" 10 ) 11 12 func TestMathRandAlpha(t *testing.T) { 13 if len(MathRandAlpha(10)) != 10 { 14 t.Errorf("MathRandAlpha return invalid length") 15 } 16 17 isLetter := regexp.MustCompile(`^[a-zA-Z]+$`).MatchString 18 if !isLetter(MathRandAlpha(10)) { 19 t.Errorf("MathRandAlpha should be AlphaNumeric only") 20 } 21 } 22 23 func TestMultiError(t *testing.T) { 24 rawErrs := []error{ 25 errors.New("err1"), //nolint 26 errors.New("err2"), //nolint 27 errors.New("err3"), //nolint 28 errors.New("err4"), //nolint 29 } 30 errs := FlattenErrs([]error{ 31 rawErrs[0], 32 nil, 33 rawErrs[1], 34 FlattenErrs([]error{ 35 rawErrs[2], 36 }), 37 }) 38 str := "err1\nerr2\nerr3" 39 40 if errs.Error() != str { 41 t.Errorf("String representation doesn't match, expected: %s, got: %s", errs.Error(), str) 42 } 43 44 errIs, ok := errs.(multiError) //nolint:errorlint 45 if !ok { 46 t.Fatal("FlattenErrs returns non-multiError") 47 } 48 for i := 0; i < 3; i++ { 49 if !errIs.Is(rawErrs[i]) { 50 t.Errorf("'%+v' should contains '%v'", errs, rawErrs[i]) 51 } 52 } 53 if errIs.Is(rawErrs[3]) { 54 t.Errorf("'%+v' should not contains '%v'", errs, rawErrs[3]) 55 } 56 }