Skip to content

Commit 1c33da8

Browse files
authored
[type:feat] support assert. (#84)
* [type:feat] code over issue #44 push assert.go and assert_test.go * [type:feat] support assert.
1 parent f7f4bf1 commit 1c33da8

File tree

2 files changed

+182
-0
lines changed

2 files changed

+182
-0
lines changed

assert/assert.go

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/*
2+
* Copyright (c) 2022, AcmeStack
3+
* All rights reserved.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package assert
19+
20+
import (
21+
"fmt"
22+
"reflect"
23+
"runtime"
24+
"testing"
25+
)
26+
27+
/*
28+
* Judge whether the value is nil
29+
* args Error prompt content
30+
*/
31+
func IsNull(t *testing.T, got interface{}, args ...interface{}) {
32+
fn := func() {
33+
t.Errorf("! isNull")
34+
if len(args) > 0 {
35+
t.Error("!", " -", fmt.Sprint(args...))
36+
}
37+
}
38+
result := !reflect.DeepEqual(got, nil)
39+
assert(t, result, fn, 2)
40+
}
41+
42+
/*
43+
* Judge whether the value is true
44+
* args Error prompt content
45+
*/
46+
func IsTrue(t *testing.T, result bool, args ...interface{}) {
47+
tt(t, result, 1, args...)
48+
}
49+
50+
/*
51+
* Judge whether the value is false
52+
* args Error prompt content
53+
*/
54+
func IsFalse(t *testing.T, result bool, args ...interface{}) {
55+
tt(t, !result, 1, args...)
56+
}
57+
58+
/*
59+
* Judge whether the values are not equal
60+
* exp Comparison value
61+
* got Compared value
62+
* args Error prompt content
63+
*/
64+
func Equal(t *testing.T, exp, got interface{}, args ...interface{}) {
65+
equal(t, exp, got, 1, args...)
66+
}
67+
68+
/*
69+
* Judge whether the values are equal
70+
* exp Comparison value
71+
* got Compared value
72+
* args Error prompt content
73+
*/
74+
func NotEqual(t *testing.T, exp, got interface{}, args ...interface{}) {
75+
fn := func() {
76+
t.Errorf("! Unexpected: <%#v>", exp)
77+
if len(args) > 0 {
78+
t.Error("!", " -", fmt.Sprint(args...))
79+
}
80+
}
81+
result := reflect.DeepEqual(exp, got)
82+
assert(t, result, fn, 1)
83+
}
84+
85+
func assert(t *testing.T, result bool, f func(), cd int) {
86+
if !result {
87+
_, file, line, _ := runtime.Caller(cd + 1)
88+
t.Errorf("%s:%d", file, line)
89+
f()
90+
t.FailNow()
91+
}
92+
}
93+
94+
func equal(t *testing.T, exp, got interface{}, cd int, args ...interface{}) {
95+
fn := func() {
96+
t.Errorf("! Error unlike")
97+
if len(args) > 0 {
98+
t.Error("!", " -", fmt.Sprint(args...))
99+
}
100+
}
101+
result := !reflect.DeepEqual(exp, got)
102+
assert(t, result, fn, cd+1)
103+
}
104+
105+
func tt(t *testing.T, result bool, cd int, args ...interface{}) {
106+
fn := func() {
107+
t.Errorf("! Failure")
108+
if len(args) > 0 {
109+
t.Error("!", " -", fmt.Sprint(args...))
110+
}
111+
}
112+
assert(t, result, fn, cd+1)
113+
}

assert/assert_test.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright (c) 2022, AcmeStack
3+
* All rights reserved.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package assert
19+
20+
import (
21+
"reflect"
22+
"testing"
23+
)
24+
25+
func TestEqual(t *testing.T) {
26+
Equal(t, "foo", "bar", "msg!")
27+
}
28+
29+
func TestNotEqual(t *testing.T) {
30+
type R struct {
31+
i int
32+
*R
33+
}
34+
r := &R{
35+
i: 1,
36+
R: &R{
37+
i: 2,
38+
R: &R{
39+
i: 3,
40+
},
41+
},
42+
}
43+
r.R.R.R = r
44+
45+
r2 := &R{
46+
i: 1,
47+
R: &R{
48+
i: 2,
49+
R: &R{
50+
i: 3,
51+
},
52+
},
53+
}
54+
r2.R.R.R = r2
55+
//r2.R.R = r2 // notEqual
56+
NotEqual(t, r, r2, "msg!")
57+
}
58+
59+
func TestIsNull(t *testing.T) {
60+
IsNull(t, "foo", "msg!")
61+
}
62+
63+
func TestIsTrue(t *testing.T) {
64+
IsTrue(t, reflect.DeepEqual("foo", "foo"), "msg!")
65+
}
66+
67+
func TestIsFalse(t *testing.T) {
68+
IsFalse(t, reflect.DeepEqual("foo", "bar"), "msg!")
69+
}

0 commit comments

Comments
 (0)