-
Notifications
You must be signed in to change notification settings - Fork 6
/
expect.go
96 lines (73 loc) · 1.99 KB
/
expect.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
package mao
import (
"fmt"
"reflect"
"net/http"
"io/ioutil"
)
type Expect func(val interface{}) *Expected
type Expected struct {
Scope *Test
Value interface{}
}
func (self *Expected) Above(b interface{}) {
value, ok := self.Value.(int)
otherValue, otherOk := b.(int)
if !ok || !otherOk {
self.Scope.PrintError(fmt.Sprintf("Unable to compare `%v` with `%v`", self.Value, b))
return
}
if value <= otherValue {
self.Scope.PrintError(fmt.Sprintf("Expected `%v` to be above than `%v`", self.Value, b))
}
}
func (self *Expected) Equal(b interface{}) {
if self.Value != b {
self.Scope.PrintError(fmt.Sprintf("Expected `%v` to equal `%v`", self.Value, b))
}
}
func (self *Expected) Lower(b interface{}) {
value, ok := self.Value.(int)
otherValue, otherOk := b.(int)
if !ok || !otherOk {
self.Scope.PrintError(fmt.Sprintf("Unable to compare `%v` with `%v`", self.Value, b))
return
}
if value >= otherValue {
self.Scope.PrintError(fmt.Sprintf("Expected `%v` to be lower than `%v`", self.Value, b))
}
}
func (self *Expected) NotEqual(b interface{}) {
if self.Value == b {
self.Scope.PrintError(fmt.Sprintf("Expected `%v` to not equal `%v`", self.Value, b))
}
}
func (self *Expected) NotExist() {
msg := fmt.Sprintf("Expected `%v` to not exist.", self.Value)
if self.Value == nil {
return
}
if self.Value != nil {
self.Scope.PrintError(msg)
return
}
v := reflect.ValueOf(self.Value)
fmt.Println("value:", self.Value, "value == nil", self.Value == nil, " v.isNil?", v.IsNil())
if !v.IsNil() {
self.Scope.PrintError(msg)
}
}
func (self *Expected) ResponseBody(b interface{}) {
response, err := http.Get(self.Value.(string))
if err != nil {
self.Scope.PrintError(fmt.Sprintf("Unable to get %s", self.Value))
}
body, err := ioutil.ReadAll(response.Body)
if err != nil {
self.Scope.PrintError(fmt.Sprintf("Unable to read `%v`", self.Value))
return
}
if string(body) != b {
self.Scope.PrintError(fmt.Sprintf("Expected `%v` to equal `%v`", string(body), b))
}
}