-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinternal.go
130 lines (114 loc) · 4.17 KB
/
internal.go
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Copyright 2018 The ut/microtest Authors
// This file is part of ut/microtest library.
//
// This library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this library. If not, see <http://www.gnu.org/licenses/>.
package ut
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"path/filepath"
"reflect"
"runtime"
"github.com/epiclabs-io/diff3"
)
type internal struct {
}
// Internal defines test functions that can be used to build other test functions
// It is built as a struct to avoid polluting the namespace
// These functions just make checks or print messages, they don't stop the test
var Internal internal
func (in *internal) Suspend() {
c := make(chan bool)
<-c
}
func (in *internal) NotAssert(callDepth int, condition bool, msg string, v ...interface{}) bool {
if !condition {
_, file, line, _ := runtime.Caller(2 + callDepth)
fmt.Printf("%s:%d: Assertion failed: "+msg+"\n\n", append([]interface{}{filepath.Base(file), line}, v...)...)
return true
}
return false
}
func (in *internal) ErrorString(err error) string {
if err == nil {
return "<nil>"
}
return err.Error()
}
func (in *internal) NotOk(callDepth int, err error) bool {
if err != nil {
_, file, line, _ := runtime.Caller(2 + callDepth)
fmt.Printf("%s:%d: unexpected error: %s\n\n", filepath.Base(file), line, err.Error())
return true
}
return false
}
func (in *internal) NotEquals(callDepth int, expected, actual interface{}) bool {
if !reflect.DeepEqual(expected, actual) {
_, file, line, _ := runtime.Caller(2 + callDepth)
fmt.Printf("%s:%d:\n\n\texpected: %#v\n\n\tgot: %#v\n\n", filepath.Base(file), line, expected, actual)
return true
}
return false
}
func (in *internal) JSONPretty(jsonBytes []byte) []byte {
var buf bytes.Buffer
json.Indent(&buf, jsonBytes, "", "\t")
return buf.Bytes()
}
func (in *internal) NotJSONEquals(callDepth int, expected, actual []byte) bool {
//credit for the trick: turtlemonvh https://gist.github.com/turtlemonvh/e4f7404e28387fadb8ad275a99596f67
var o1 interface{}
var o2 interface{}
err := json.Unmarshal(expected, &o1)
if err != nil {
_, file, line, _ := runtime.Caller(2 + callDepth)
fmt.Printf("%s:%d:\n\n\tJSONEquals: Error decoding 'expected' JSON: %s.\n\t Can't decode this: `%s`\n\n",
filepath.Base(file), line, err, string(expected))
return true
}
err = json.Unmarshal(actual, &o2)
if err != nil {
_, file, line, _ := runtime.Caller(2 + callDepth)
fmt.Printf("%s:%d:\n\n\tJSONEquals: Error decoding 'actual' JSON: %s.\n\tCan't decode this: `%s`\n\n",
filepath.Base(file), line, err, string(actual))
return true
}
if !reflect.DeepEqual(o1, o2) {
_, file, line, _ := runtime.Caller(2 + callDepth)
expectedPretty := in.JSONPretty(expected)
actualPretty := in.JSONPretty(actual)
fmt.Printf("%s:%d:\n\n\texpected JSON: %s\n\n\tgot JSON: %s\n\n", filepath.Base(file), line, expectedPretty, actualPretty)
r, err := diff3.Merge(bytes.NewReader(expectedPretty), bytes.NewReader([]byte{}), bytes.NewReader(actualPretty), true, "EXPECTED", "ACTUAL")
if err == nil && r.Conflicts {
diff, err := ioutil.ReadAll(r.Result)
if err == nil {
fmt.Printf("Diff:\n%s\n", string(diff))
}
}
return true
}
return false
}
func (in *internal) Fatal(callDepth int, args ...interface{}) {
_, file, line, _ := runtime.Caller(2 + callDepth)
fmt.Println(append([]interface{}{fmt.Sprintf("%s:%d: FATAL:", filepath.Base(file), line)}, args...)...)
}
func (in *internal) Fatalf(callDepth int, formatString string, args ...interface{}) {
_, file, line, _ := runtime.Caller(2 + callDepth)
fmt.Printf("%s: FATAL: "+formatString+"\n",
append([]interface{}{fmt.Sprintf("%s:%d", filepath.Base(file), line)}, args...)...)
}