util_test.go 1.2 KB

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