Skip to content

Commit

Permalink
Don't use slices.Concat yet
Browse files Browse the repository at this point in the history
  • Loading branch information
emlun committed Apr 14, 2024
1 parent bf8ee9d commit af8805e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
2 changes: 1 addition & 1 deletion lib/api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1918,7 +1918,7 @@ func createWebauthnAssertionResponse(
clientDataJSON, err := json.Marshal(clientData)
testutil.FatalIfErr(t, err)
clientDataJSONHash := sha256.Sum256(clientDataJSON)
signedData := slices.Concat(authData, clientDataJSONHash[:])
signedData := testutil.ConcatSlices(authData, clientDataJSONHash[:])
signedDataDigest := sha256.Sum256(signedData)

sig, err := privateKey.Sign(cryptoRand.Reader, signedDataDigest[:], crypto.SHA256)
Expand Down
26 changes: 21 additions & 5 deletions lib/testutil/testutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ package testutil

import (
"errors"
"slices"
"sync"
"testing"

Expand Down Expand Up @@ -64,6 +63,23 @@ func (NoopCloser) Close() error {
return nil
}

func ConcatSlices[T any](slices ...[]T) []T {
// TODO when go >= 1.22.0: Replace with slices.Concat
concatLen := 0
for _, s := range slices {
concatLen += len(s)
}
var concat []T = make([]T, concatLen)
i := 0
for _, s := range slices {
for _, el := range s {
concat[i] = el
i += 1
}
}
return concat
}

func IfExpr[T any](expr bool, then T, els T) T {
if expr {
return then
Expand Down Expand Up @@ -98,7 +114,7 @@ func AssertEqual[T comparable](t *testing.T, testFailFunc func(string, ...any),
} else if len(sprintfArgs) == 1 {
testFailFunc("Assertion failed: %v == %v: %s", a, b, sprintfArgs[0])
} else {
testFailFunc("Assertion failed: %v == %v: "+sprintfArgs[0].(string), slices.Concat([]any{a, b}, sprintfArgs[1:])...)
testFailFunc("Assertion failed: %v == %v: "+sprintfArgs[0].(string), ConcatSlices([]any{a, b}, sprintfArgs[1:])...)
}
}
}
Expand All @@ -111,7 +127,7 @@ func AssertNotEqual[T comparable](t *testing.T, testFailFunc func(string, ...any
} else if len(sprintfArgs) == 1 {
testFailFunc("Assertion failed: %v != %v: %s", a, b, sprintfArgs[0])
} else {
testFailFunc("Assertion failed: %v != %v: "+sprintfArgs[0].(string), slices.Concat([]any{a, b}, sprintfArgs[1:])...)
testFailFunc("Assertion failed: %v != %v: "+sprintfArgs[0].(string), ConcatSlices([]any{a, b}, sprintfArgs[1:])...)
}
}
}
Expand All @@ -124,7 +140,7 @@ func AssertGreater[T constraints.Ordered](t *testing.T, testFailFunc func(string
} else if len(sprintfArgs) == 1 {
testFailFunc("Assertion failed: %v > %v: %s", a, b, sprintfArgs[0])
} else {
testFailFunc("Assertion failed: %v > %v: "+sprintfArgs[0].(string), slices.Concat([]any{a, b}, sprintfArgs[1:])...)
testFailFunc("Assertion failed: %v > %v: "+sprintfArgs[0].(string), ConcatSlices([]any{a, b}, sprintfArgs[1:])...)
}
}
}
Expand All @@ -137,7 +153,7 @@ func AssertPredicate[T any](t *testing.T, testFailFunc func(string, ...any), pre
} else if len(sprintfArgs) == 1 {
testFailFunc("Assertion failed: %s(%v, %v) != true: %s", predicate, a, b, sprintfArgs[0])
} else {
testFailFunc("Assertion failed: %s(%v, %v) != true: "+sprintfArgs[0].(string), slices.Concat([]any{predicate, a, b}, sprintfArgs[1:])...)
testFailFunc("Assertion failed: %s(%v, %v) != true: "+sprintfArgs[0].(string), ConcatSlices([]any{predicate, a, b}, sprintfArgs[1:])...)
}
}
}
Expand Down

0 comments on commit af8805e

Please sign in to comment.