-
Notifications
You must be signed in to change notification settings - Fork 0
/
string.go
92 lines (82 loc) · 2.77 KB
/
string.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
// Copyright (C) 2017 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package assert
import (
"fmt"
"strings"
)
// OnString is the result of calling ThatString on an Assertion.
// It provides assertion tests that are specific to strings.
type OnString struct {
Assertion
value string
}
// ThatString returns an OnString for string based assertions.
// The untyped argument is converted to a string using fmt.Sprint, and the result supports string specific tests.
func (a Assertion) ThatString(value interface{}) OnString {
s := ""
switch v := value.(type) {
case string:
s = v
case []byte:
s = string(v)
default:
s = fmt.Sprint(value)
}
return OnString{Assertion: a, value: s}
}
// Equals asserts that the supplied string is equal to the expected string.
func (o OnString) Equals(expect string) bool {
return o.Compare(o.value, "==", expect).Test(func() bool {
if o.value == expect {
return true
}
re := ([]rune)(expect)
for i, c := range o.value {
if i >= len(re) {
o.Printf("Longer\tby\t")
o.Println(o.value[i:])
return false
}
if c != re[i] {
o.Printf("Differs\tfrom\t")
o.Println(o.value[i:])
return false
}
}
o.Printf("Shorter\tby\t")
o.Println(expect[len(o.value):])
return false
}())
}
// NotEquals asserts that the supplied string is not equal to the test string.
func (o OnString) NotEquals(test string) bool {
return o.Compare(o.value, "!=", test).Test(o.value != test)
}
// Contains asserts that the supplied string contains substr.
func (o OnString) Contains(substr string) bool {
return o.Compare(o.value, "contains", substr).Test(strings.Contains(o.value, substr))
}
// DoesNotContain asserts that the supplied string does not contain substr.
func (o OnString) DoesNotContain(substr string) bool {
return o.Compare(o.value, "does not contain", substr).Test(!strings.Contains(o.value, substr))
}
// HasPrefix asserts that the supplied string start with substr.
func (o OnString) HasPrefix(substr string) bool {
return o.Compare(o.value, "starts with", substr).Test(strings.HasPrefix(o.value, substr))
}
// HasSuffix asserts that the supplied string ends with with substr.
func (o OnString) HasSuffix(substr string) bool {
return o.Compare(o.value, "ends with", substr).Test(strings.HasSuffix(o.value, substr))
}