File: asserts_test.go

package info (click to toggle)
golang-github-klauspost-compress 1.17.11%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 46,592 kB
  • sloc: asm: 23,155; sh: 72; makefile: 12
file content (73 lines) | stat: -rw-r--r-- 1,469 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Copyright (c) 2021 Klaus Post. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package gzhttp

import (
	"reflect"
	"testing"
)

func assertEqual(t testing.TB, want, got interface{}) {
	t.Helper()
	if !reflect.DeepEqual(want, got) {
		t.Fatalf("want %#v, got %#v", want, got)
	}
}

func assertNotEqual(t testing.TB, want, got interface{}) {
	t.Helper()
	if reflect.DeepEqual(want, got) {
		t.Fatalf("did not want %#v, got %#v", want, got)
	}
}

func assertNil(t testing.TB, object interface{}) {
	if isNil(object) {
		return
	}
	t.Helper()
	t.Fatalf("Expected value to be nil.")
}

func assertNotNil(t testing.TB, object interface{}) {
	if !isNil(object) {
		return
	}
	t.Helper()
	t.Fatalf("Expected value not to be nil.")
}

// isNil checks if a specified object is nil or not, without Failing.
func isNil(object interface{}) bool {
	if object == nil {
		return true
	}

	value := reflect.ValueOf(object)
	kind := value.Kind()
	isNilableKind := containsKind(
		[]reflect.Kind{
			reflect.Chan, reflect.Func,
			reflect.Interface, reflect.Map,
			reflect.Ptr, reflect.Slice},
		kind)

	if isNilableKind && value.IsNil() {
		return true
	}

	return false
}

// containsKind checks if a specified kind in the slice of kinds.
func containsKind(kinds []reflect.Kind, kind reflect.Kind) bool {
	for i := 0; i < len(kinds); i++ {
		if kind == kinds[i] {
			return true
		}
	}

	return false
}