blob: f5606c86644acc938cfc0ea0344d74de93c552c5 [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 (
"fmt"
"reflect"
"strings"
"bytes"
"io"
"android.googlesource.com/platform/tools/gpu/framework/compare"
)
// OnValue is the result of calling That on an Assertion.
// It provides generice assertion tests that work for any type.
type OnValue struct {
Assertion
value interface{}
}
// IsNil asserts that the supplied value was a nil.
// Typed nils are also be allowed.
func (o OnValue) IsNil() bool {
return o.Assertion.test(o.value == nil || reflect.ValueOf(o.value).IsNil(), expected, o.value, "nil")
}
// IsNotNil asserts that the supplied value was not a nil.
// Typed nils will also fail.
func (o OnValue) IsNotNil() bool {
return o.Assertion.test(o.value != nil && !reflect.ValueOf(o.value).IsNil(), expected, "nil", "value")
}
// Equals asserts that the supplied value is equal to the expected value.
func (o OnValue) Equals(expect interface{}) bool {
return o.Assertion.test(o.value == expect, expected, o.value, expect)
}
// NotEquals asserts that the supplied value is not equal to the test value.
func (o OnValue) NotEquals(test interface{}) bool {
return o.Assertion.test(o.value != test, expected, o.value, "a non equal value")
}
// DeepEquals asserts that the supplied value is equal to the expected value using compare.Diff.
func (o OnValue) DeepEquals(expect interface{}) bool {
diff := compare.Diff(o.value, expect, 10)
if len(diff) == 0 {
return true
}
buf := &bytes.Buffer{}
for i, diff := range diff {
str := fmt.Sprintf("%v", diff)
str = strings.Replace(str, "\n", "␤", -1)
if i > 0 {
io.WriteString(buf, "\n")
}
io.WriteString(buf, str)
}
o.Assertion.emit(buf.String())
return false
}
// DeepNotEquals asserts that the supplied value is not equal to the test value using a deep comparison.
func (o OnValue) DeepNotEquals(test interface{}) bool {
return o.Assertion.test(!compare.DeepEqual(o.value, test), expected, o.value, "a non equal value")
}