| 1 |
// SPDX-License-Identifier: MPL-2.0 |
| 2 |
|
| 3 |
// Copyright (C) 2025 Aleksa Sarai <[email protected]> |
| 4 |
// Copyright (C) 2025 SUSE LLC |
| 5 |
// |
| 6 |
// This Source Code Form is subject to the terms of the Mozilla Public |
| 7 |
// License, v. 2.0. If a copy of the MPL was not distributed with this |
| 8 |
// file, You can obtain one at https://mozilla.org/MPL/2.0/. |
| 9 |
|
| 10 |
package assert_test |
| 11 |
|
| 12 |
import ( |
| 13 |
"errors" |
| 14 |
"testing" |
| 15 |
|
| 16 |
testassert "github.com/stretchr/testify/assert" |
| 17 |
|
| 18 |
"github.com/cyphar/filepath-securejoin/pathrs-lite/internal/assert" |
| 19 |
) |
| 20 |
|
| 21 |
func TestAssertTrue(t *testing.T) { |
| 22 |
for _, test := range []struct { |
| 23 |
name string |
| 24 |
val any |
| 25 |
}{ |
| 26 |
{"StringVal", "foobar"}, |
| 27 |
{"IntVal", 123}, |
| 28 |
{"ErrorVal", errors.New("error")}, |
| 29 |
{"StructVal", struct{ a int }{1}}, |
| 30 |
{"NilVal", nil}, |
| 31 |
} { |
| 32 |
test := test // copy iterator |
| 33 |
t.Run(test.name, func(t *testing.T) { |
| 34 |
testassert.NotPanicsf(t, func() { |
| 35 |
assert.Assert(true, test.val) |
| 36 |
}, "assert(true) with value %v (%T)", test.val, test.val) |
| 37 |
}) |
| 38 |
} |
| 39 |
|
| 40 |
t.Run("Assertf", func(t *testing.T) { |
| 41 |
fmtMsg := "foo %s %d" |
| 42 |
args := []any{"bar %x", 123} |
| 43 |
expected := "foo bar %x 123" |
| 44 |
|
| 45 |
testassert.NotPanicsf(t, func() { |
| 46 |
assert.Assertf(true, fmtMsg, args...) |
| 47 |
}, "assertf(true) with (%q, %v...) == %q", fmtMsg, args, expected) |
| 48 |
}) |
| 49 |
} |
| 50 |
|
| 51 |
func TestAssertFalse(t *testing.T) { |
| 52 |
for _, test := range []struct { |
| 53 |
name string |
| 54 |
val any |
| 55 |
}{ |
| 56 |
{"StringVal", "foobar"}, |
| 57 |
{"IntVal", 123}, |
| 58 |
{"ErrorVal", errors.New("error")}, |
| 59 |
{"StructVal", struct{ a int }{1}}, |
| 60 |
} { |
| 61 |
test := test // copy iterator |
| 62 |
t.Run(test.name, func(t *testing.T) { |
| 63 |
testassert.PanicsWithValuef(t, test.val, func() { |
| 64 |
assert.Assert(false, test.val) |
| 65 |
}, "assert(false) with value %v (%T)", test.val, test.val) |
| 66 |
}) |
| 67 |
} |
| 68 |
|
| 69 |
t.Run("NilVal", func(t *testing.T) { |
| 70 |
// testify can detect nil-value panics, but the behaviour of nil panics |
| 71 |
// changed in Go 1.21 (and can be modified by GODEBUG=panicnil=1) so we |
| 72 |
// can't be sure what value we will get. |
| 73 |
testassert.Panics(t, func() { |
| 74 |
assert.Assert(false, nil) |
| 75 |
}, "assert(false) with nil") |
| 76 |
}) |
| 77 |
|
| 78 |
t.Run("Assertf", func(t *testing.T) { |
| 79 |
fmtMsg := "foo %s %d" |
| 80 |
args := []any{"bar %x", 123} |
| 81 |
expected := "foo bar %x 123" |
| 82 |
|
| 83 |
testassert.PanicsWithValuef(t, expected, func() { |
| 84 |
assert.Assertf(false, fmtMsg, args...) |
| 85 |
}, "assertf(true) with (%q, %v...) == %q", fmtMsg, args, expected) |
| 86 |
}) |
| 87 |
} |