blob: c8d9939cc9bb9d5c277969c421d00099a86593ee [file] [log] [blame]
// Copyright (C) 2016 The Android Open Source Project
//
// 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 "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
}
// Equals asserts that the supplied string is equal to the expected string.
func (o OnString) Equals(expect string) bool {
if o.value == expect {
return true
}
re := ([]rune)(expect)
for i, c := range o.value {
if i >= len(re) {
o.Assertion.emitf("Longer by '%s'\nGot: '%s'\nExpected: '%s'", o.value[i:], o.value, expect)
return false
}
if c != re[i] {
o.Assertion.emitf("Differs from '%s'\nGot: '%s'\nExpected: '%s'", o.value[i:], o.value, expect)
return false
}
}
o.Assertion.emitf("Shorter by '%s'\nGot: '%s'\nExpected: '%s'", expect[len(o.value):], o.value, expect)
return false
}
// NotEquals asserts that the supplied string is not equal to the test string.
func (o OnString) NotEquals(test string) bool {
return o.Assertion.test(o.value != test, "Got: '%s'\nExpected a non equal string", o.value)
}
// Contains asserts that the supplied string contains substr.
func (o OnString) Contains(substr string) bool {
return o.Assertion.test(strings.Contains(o.value, substr), "Got: '%s'\nDoes not contain '%s'", o.value, substr)
}
// DoesNotContain asserts that the supplied string does not contain substr.
func (o OnString) DoesNotContain(substr string) bool {
return o.Assertion.test(!strings.Contains(o.value, substr), "Got: '%s'\nContains '%s'", o.value, substr)
}
// HasPrefix asserts that the supplied string start with substr.
func (o OnString) HasPrefix(substr string) bool {
return o.Assertion.test(strings.HasPrefix(o.value, substr), "Got: '%s'\nDoes not start with '%s'", o.value, substr)
}
// HasSuffix asserts that the supplied string ends with with substr.
func (o OnString) HasSuffix(substr string) bool {
return o.Assertion.test(strings.HasSuffix(o.value, substr), "Got: '%s'\nDoes not end with '%s'", o.value, substr)
}