init
This commit is contained in:
Generated
Vendored
+4
@@ -0,0 +1,4 @@
|
||||
# Cf. http://docs.travis-ci.com/user/getting-started/
|
||||
# Cf. http://docs.travis-ci.com/user/languages/go/
|
||||
|
||||
language: go
|
||||
Generated
Vendored
+202
@@ -0,0 +1,202 @@
|
||||
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "[]"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright [yyyy] [name of copyright owner]
|
||||
|
||||
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.
|
||||
Generated
Vendored
+58
@@ -0,0 +1,58 @@
|
||||
[](https://godoc.org/github.com/smartystreets/assertions/internal/oglematchers)
|
||||
|
||||
`oglematchers` is a package for the Go programming language containing a set of
|
||||
matchers, useful in a testing or mocking framework, inspired by and mostly
|
||||
compatible with [Google Test][googletest] for C++ and
|
||||
[Google JS Test][google-js-test]. The package is used by the
|
||||
[ogletest][ogletest] testing framework and [oglemock][oglemock] mocking
|
||||
framework, which may be more directly useful to you, but can be generically used
|
||||
elsewhere as well.
|
||||
|
||||
A "matcher" is simply an object with a `Matches` method defining a set of golang
|
||||
values matched by the matcher, and a `Description` method describing that set.
|
||||
For example, here are some matchers:
|
||||
|
||||
```go
|
||||
// Numbers
|
||||
Equals(17.13)
|
||||
LessThan(19)
|
||||
|
||||
// Strings
|
||||
Equals("taco")
|
||||
HasSubstr("burrito")
|
||||
MatchesRegex("t.*o")
|
||||
|
||||
// Combining matchers
|
||||
AnyOf(LessThan(17), GreaterThan(19))
|
||||
```
|
||||
|
||||
There are lots more; see [here][reference] for a reference. You can also add
|
||||
your own simply by implementing the `oglematchers.Matcher` interface.
|
||||
|
||||
|
||||
Installation
|
||||
------------
|
||||
|
||||
First, make sure you have installed Go 1.0.2 or newer. See
|
||||
[here][golang-install] for instructions.
|
||||
|
||||
Use the following command to install `oglematchers` and keep it up to date:
|
||||
|
||||
go get -u github.com/smartystreets/assertions/internal/oglematchers
|
||||
|
||||
|
||||
Documentation
|
||||
-------------
|
||||
|
||||
See [here][reference] for documentation. Alternatively, you can install the
|
||||
package and then use `godoc`:
|
||||
|
||||
godoc github.com/smartystreets/assertions/internal/oglematchers
|
||||
|
||||
|
||||
[reference]: http://godoc.org/github.com/smartystreets/assertions/internal/oglematchers
|
||||
[golang-install]: http://golang.org/doc/install.html
|
||||
[googletest]: http://code.google.com/p/googletest/
|
||||
[google-js-test]: http://code.google.com/p/google-js-test/
|
||||
[ogletest]: http://github.com/smartystreets/assertions/internal/ogletest
|
||||
[oglemock]: http://github.com/smartystreets/assertions/internal/oglemock
|
||||
Generated
Vendored
+94
@@ -0,0 +1,94 @@
|
||||
// Copyright 2011 Aaron Jacobs. All Rights Reserved.
|
||||
// Author: [email protected] (Aaron Jacobs)
|
||||
//
|
||||
// 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 oglematchers
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// AnyOf accepts a set of values S and returns a matcher that follows the
|
||||
// algorithm below when considering a candidate c:
|
||||
//
|
||||
// 1. If there exists a value m in S such that m implements the Matcher
|
||||
// interface and m matches c, return true.
|
||||
//
|
||||
// 2. Otherwise, if there exists a value v in S such that v does not implement
|
||||
// the Matcher interface and the matcher Equals(v) matches c, return true.
|
||||
//
|
||||
// 3. Otherwise, if there is a value m in S such that m implements the Matcher
|
||||
// interface and m returns a fatal error for c, return that fatal error.
|
||||
//
|
||||
// 4. Otherwise, return false.
|
||||
//
|
||||
// This is akin to a logical OR operation for matchers, with non-matchers x
|
||||
// being treated as Equals(x).
|
||||
func AnyOf(vals ...interface{}) Matcher {
|
||||
// Get ahold of a type variable for the Matcher interface.
|
||||
var dummy *Matcher
|
||||
matcherType := reflect.TypeOf(dummy).Elem()
|
||||
|
||||
// Create a matcher for each value, or use the value itself if it's already a
|
||||
// matcher.
|
||||
wrapped := make([]Matcher, len(vals))
|
||||
for i, v := range vals {
|
||||
t := reflect.TypeOf(v)
|
||||
if t != nil && t.Implements(matcherType) {
|
||||
wrapped[i] = v.(Matcher)
|
||||
} else {
|
||||
wrapped[i] = Equals(v)
|
||||
}
|
||||
}
|
||||
|
||||
return &anyOfMatcher{wrapped}
|
||||
}
|
||||
|
||||
type anyOfMatcher struct {
|
||||
wrapped []Matcher
|
||||
}
|
||||
|
||||
func (m *anyOfMatcher) Description() string {
|
||||
wrappedDescs := make([]string, len(m.wrapped))
|
||||
for i, matcher := range m.wrapped {
|
||||
wrappedDescs[i] = matcher.Description()
|
||||
}
|
||||
|
||||
return fmt.Sprintf("or(%s)", strings.Join(wrappedDescs, ", "))
|
||||
}
|
||||
|
||||
func (m *anyOfMatcher) Matches(c interface{}) (err error) {
|
||||
err = errors.New("")
|
||||
|
||||
// Try each matcher in turn.
|
||||
for _, matcher := range m.wrapped {
|
||||
wrappedErr := matcher.Matches(c)
|
||||
|
||||
// Return immediately if there's a match.
|
||||
if wrappedErr == nil {
|
||||
err = nil
|
||||
return
|
||||
}
|
||||
|
||||
// Note the fatal error, if any.
|
||||
if _, isFatal := wrappedErr.(*FatalError); isFatal {
|
||||
err = wrappedErr
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
Generated
Vendored
+61
@@ -0,0 +1,61 @@
|
||||
// Copyright 2012 Aaron Jacobs. All Rights Reserved.
|
||||
// Author: [email protected] (Aaron Jacobs)
|
||||
//
|
||||
// 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 oglematchers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
// Return a matcher that matches arrays slices with at least one element that
|
||||
// matches the supplied argument. If the argument x is not itself a Matcher,
|
||||
// this is equivalent to Contains(Equals(x)).
|
||||
func Contains(x interface{}) Matcher {
|
||||
var result containsMatcher
|
||||
var ok bool
|
||||
|
||||
if result.elementMatcher, ok = x.(Matcher); !ok {
|
||||
result.elementMatcher = DeepEquals(x)
|
||||
}
|
||||
|
||||
return &result
|
||||
}
|
||||
|
||||
type containsMatcher struct {
|
||||
elementMatcher Matcher
|
||||
}
|
||||
|
||||
func (m *containsMatcher) Description() string {
|
||||
return fmt.Sprintf("contains: %s", m.elementMatcher.Description())
|
||||
}
|
||||
|
||||
func (m *containsMatcher) Matches(candidate interface{}) error {
|
||||
// The candidate must be a slice or an array.
|
||||
v := reflect.ValueOf(candidate)
|
||||
if v.Kind() != reflect.Slice && v.Kind() != reflect.Array {
|
||||
return NewFatalError("which is not a slice or array")
|
||||
}
|
||||
|
||||
// Check each element.
|
||||
for i := 0; i < v.Len(); i++ {
|
||||
elem := v.Index(i)
|
||||
if matchErr := m.elementMatcher.Matches(elem.Interface()); matchErr == nil {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
return fmt.Errorf("")
|
||||
}
|
||||
Generated
Vendored
+88
@@ -0,0 +1,88 @@
|
||||
// Copyright 2012 Aaron Jacobs. All Rights Reserved.
|
||||
// Author: [email protected] (Aaron Jacobs)
|
||||
//
|
||||
// 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 oglematchers
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
var byteSliceType reflect.Type = reflect.TypeOf([]byte{})
|
||||
|
||||
// DeepEquals returns a matcher that matches based on 'deep equality', as
|
||||
// defined by the reflect package. This matcher requires that values have
|
||||
// identical types to x.
|
||||
func DeepEquals(x interface{}) Matcher {
|
||||
return &deepEqualsMatcher{x}
|
||||
}
|
||||
|
||||
type deepEqualsMatcher struct {
|
||||
x interface{}
|
||||
}
|
||||
|
||||
func (m *deepEqualsMatcher) Description() string {
|
||||
xDesc := fmt.Sprintf("%v", m.x)
|
||||
xValue := reflect.ValueOf(m.x)
|
||||
|
||||
// Special case: fmt.Sprintf presents nil slices as "[]", but
|
||||
// reflect.DeepEqual makes a distinction between nil and empty slices. Make
|
||||
// this less confusing.
|
||||
if xValue.Kind() == reflect.Slice && xValue.IsNil() {
|
||||
xDesc = "<nil slice>"
|
||||
}
|
||||
|
||||
return fmt.Sprintf("deep equals: %s", xDesc)
|
||||
}
|
||||
|
||||
func (m *deepEqualsMatcher) Matches(c interface{}) error {
|
||||
// Make sure the types match.
|
||||
ct := reflect.TypeOf(c)
|
||||
xt := reflect.TypeOf(m.x)
|
||||
|
||||
if ct != xt {
|
||||
return NewFatalError(fmt.Sprintf("which is of type %v", ct))
|
||||
}
|
||||
|
||||
// Special case: handle byte slices more efficiently.
|
||||
cValue := reflect.ValueOf(c)
|
||||
xValue := reflect.ValueOf(m.x)
|
||||
|
||||
if ct == byteSliceType && !cValue.IsNil() && !xValue.IsNil() {
|
||||
xBytes := m.x.([]byte)
|
||||
cBytes := c.([]byte)
|
||||
|
||||
if bytes.Equal(cBytes, xBytes) {
|
||||
return nil
|
||||
}
|
||||
|
||||
return errors.New("")
|
||||
}
|
||||
|
||||
// Defer to the reflect package.
|
||||
if reflect.DeepEqual(m.x, c) {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Special case: if the comparison failed because c is the nil slice, given
|
||||
// an indication of this (since its value is printed as "[]").
|
||||
if cValue.Kind() == reflect.Slice && cValue.IsNil() {
|
||||
return errors.New("which is nil")
|
||||
}
|
||||
|
||||
return errors.New("")
|
||||
}
|
||||
Generated
Vendored
+541
@@ -0,0 +1,541 @@
|
||||
// Copyright 2011 Aaron Jacobs. All Rights Reserved.
|
||||
// Author: [email protected] (Aaron Jacobs)
|
||||
//
|
||||
// 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 oglematchers
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"math"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
// Equals(x) returns a matcher that matches values v such that v and x are
|
||||
// equivalent. This includes the case when the comparison v == x using Go's
|
||||
// built-in comparison operator is legal (except for structs, which this
|
||||
// matcher does not support), but for convenience the following rules also
|
||||
// apply:
|
||||
//
|
||||
// * Type checking is done based on underlying types rather than actual
|
||||
// types, so that e.g. two aliases for string can be compared:
|
||||
//
|
||||
// type stringAlias1 string
|
||||
// type stringAlias2 string
|
||||
//
|
||||
// a := "taco"
|
||||
// b := stringAlias1("taco")
|
||||
// c := stringAlias2("taco")
|
||||
//
|
||||
// ExpectTrue(a == b) // Legal, passes
|
||||
// ExpectTrue(b == c) // Illegal, doesn't compile
|
||||
//
|
||||
// ExpectThat(a, Equals(b)) // Passes
|
||||
// ExpectThat(b, Equals(c)) // Passes
|
||||
//
|
||||
// * Values of numeric type are treated as if they were abstract numbers, and
|
||||
// compared accordingly. Therefore Equals(17) will match int(17),
|
||||
// int16(17), uint(17), float32(17), complex64(17), and so on.
|
||||
//
|
||||
// If you want a stricter matcher that contains no such cleverness, see
|
||||
// IdenticalTo instead.
|
||||
//
|
||||
// Arrays are supported by this matcher, but do not participate in the
|
||||
// exceptions above. Two arrays compared with this matcher must have identical
|
||||
// types, and their element type must itself be comparable according to Go's ==
|
||||
// operator.
|
||||
func Equals(x interface{}) Matcher {
|
||||
v := reflect.ValueOf(x)
|
||||
|
||||
// This matcher doesn't support structs.
|
||||
if v.Kind() == reflect.Struct {
|
||||
panic(fmt.Sprintf("oglematchers.Equals: unsupported kind %v", v.Kind()))
|
||||
}
|
||||
|
||||
// The == operator is not defined for non-nil slices.
|
||||
if v.Kind() == reflect.Slice && v.Pointer() != uintptr(0) {
|
||||
panic(fmt.Sprintf("oglematchers.Equals: non-nil slice"))
|
||||
}
|
||||
|
||||
return &equalsMatcher{v}
|
||||
}
|
||||
|
||||
type equalsMatcher struct {
|
||||
expectedValue reflect.Value
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
// Numeric types
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
|
||||
func isSignedInteger(v reflect.Value) bool {
|
||||
k := v.Kind()
|
||||
return k >= reflect.Int && k <= reflect.Int64
|
||||
}
|
||||
|
||||
func isUnsignedInteger(v reflect.Value) bool {
|
||||
k := v.Kind()
|
||||
return k >= reflect.Uint && k <= reflect.Uintptr
|
||||
}
|
||||
|
||||
func isInteger(v reflect.Value) bool {
|
||||
return isSignedInteger(v) || isUnsignedInteger(v)
|
||||
}
|
||||
|
||||
func isFloat(v reflect.Value) bool {
|
||||
k := v.Kind()
|
||||
return k == reflect.Float32 || k == reflect.Float64
|
||||
}
|
||||
|
||||
func isComplex(v reflect.Value) bool {
|
||||
k := v.Kind()
|
||||
return k == reflect.Complex64 || k == reflect.Complex128
|
||||
}
|
||||
|
||||
func checkAgainstInt64(e int64, c reflect.Value) (err error) {
|
||||
err = errors.New("")
|
||||
|
||||
switch {
|
||||
case isSignedInteger(c):
|
||||
if c.Int() == e {
|
||||
err = nil
|
||||
}
|
||||
|
||||
case isUnsignedInteger(c):
|
||||
u := c.Uint()
|
||||
if u <= math.MaxInt64 && int64(u) == e {
|
||||
err = nil
|
||||
}
|
||||
|
||||
// Turn around the various floating point types so that the checkAgainst*
|
||||
// functions for them can deal with precision issues.
|
||||
case isFloat(c), isComplex(c):
|
||||
return Equals(c.Interface()).Matches(e)
|
||||
|
||||
default:
|
||||
err = NewFatalError("which is not numeric")
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func checkAgainstUint64(e uint64, c reflect.Value) (err error) {
|
||||
err = errors.New("")
|
||||
|
||||
switch {
|
||||
case isSignedInteger(c):
|
||||
i := c.Int()
|
||||
if i >= 0 && uint64(i) == e {
|
||||
err = nil
|
||||
}
|
||||
|
||||
case isUnsignedInteger(c):
|
||||
if c.Uint() == e {
|
||||
err = nil
|
||||
}
|
||||
|
||||
// Turn around the various floating point types so that the checkAgainst*
|
||||
// functions for them can deal with precision issues.
|
||||
case isFloat(c), isComplex(c):
|
||||
return Equals(c.Interface()).Matches(e)
|
||||
|
||||
default:
|
||||
err = NewFatalError("which is not numeric")
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func checkAgainstFloat32(e float32, c reflect.Value) (err error) {
|
||||
err = errors.New("")
|
||||
|
||||
switch {
|
||||
case isSignedInteger(c):
|
||||
if float32(c.Int()) == e {
|
||||
err = nil
|
||||
}
|
||||
|
||||
case isUnsignedInteger(c):
|
||||
if float32(c.Uint()) == e {
|
||||
err = nil
|
||||
}
|
||||
|
||||
case isFloat(c):
|
||||
// Compare using float32 to avoid a false sense of precision; otherwise
|
||||
// e.g. Equals(float32(0.1)) won't match float32(0.1).
|
||||
if float32(c.Float()) == e {
|
||||
err = nil
|
||||
}
|
||||
|
||||
case isComplex(c):
|
||||
comp := c.Complex()
|
||||
rl := real(comp)
|
||||
im := imag(comp)
|
||||
|
||||
// Compare using float32 to avoid a false sense of precision; otherwise
|
||||
// e.g. Equals(float32(0.1)) won't match (0.1 + 0i).
|
||||
if im == 0 && float32(rl) == e {
|
||||
err = nil
|
||||
}
|
||||
|
||||
default:
|
||||
err = NewFatalError("which is not numeric")
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func checkAgainstFloat64(e float64, c reflect.Value) (err error) {
|
||||
err = errors.New("")
|
||||
|
||||
ck := c.Kind()
|
||||
|
||||
switch {
|
||||
case isSignedInteger(c):
|
||||
if float64(c.Int()) == e {
|
||||
err = nil
|
||||
}
|
||||
|
||||
case isUnsignedInteger(c):
|
||||
if float64(c.Uint()) == e {
|
||||
err = nil
|
||||
}
|
||||
|
||||
// If the actual value is lower precision, turn the comparison around so we
|
||||
// apply the low-precision rules. Otherwise, e.g. Equals(0.1) may not match
|
||||
// float32(0.1).
|
||||
case ck == reflect.Float32 || ck == reflect.Complex64:
|
||||
return Equals(c.Interface()).Matches(e)
|
||||
|
||||
// Otherwise, compare with double precision.
|
||||
case isFloat(c):
|
||||
if c.Float() == e {
|
||||
err = nil
|
||||
}
|
||||
|
||||
case isComplex(c):
|
||||
comp := c.Complex()
|
||||
rl := real(comp)
|
||||
im := imag(comp)
|
||||
|
||||
if im == 0 && rl == e {
|
||||
err = nil
|
||||
}
|
||||
|
||||
default:
|
||||
err = NewFatalError("which is not numeric")
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func checkAgainstComplex64(e complex64, c reflect.Value) (err error) {
|
||||
err = errors.New("")
|
||||
realPart := real(e)
|
||||
imaginaryPart := imag(e)
|
||||
|
||||
switch {
|
||||
case isInteger(c) || isFloat(c):
|
||||
// If we have no imaginary part, then we should just compare against the
|
||||
// real part. Otherwise, we can't be equal.
|
||||
if imaginaryPart != 0 {
|
||||
return
|
||||
}
|
||||
|
||||
return checkAgainstFloat32(realPart, c)
|
||||
|
||||
case isComplex(c):
|
||||
// Compare using complex64 to avoid a false sense of precision; otherwise
|
||||
// e.g. Equals(0.1 + 0i) won't match float32(0.1).
|
||||
if complex64(c.Complex()) == e {
|
||||
err = nil
|
||||
}
|
||||
|
||||
default:
|
||||
err = NewFatalError("which is not numeric")
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func checkAgainstComplex128(e complex128, c reflect.Value) (err error) {
|
||||
err = errors.New("")
|
||||
realPart := real(e)
|
||||
imaginaryPart := imag(e)
|
||||
|
||||
switch {
|
||||
case isInteger(c) || isFloat(c):
|
||||
// If we have no imaginary part, then we should just compare against the
|
||||
// real part. Otherwise, we can't be equal.
|
||||
if imaginaryPart != 0 {
|
||||
return
|
||||
}
|
||||
|
||||
return checkAgainstFloat64(realPart, c)
|
||||
|
||||
case isComplex(c):
|
||||
if c.Complex() == e {
|
||||
err = nil
|
||||
}
|
||||
|
||||
default:
|
||||
err = NewFatalError("which is not numeric")
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
// Other types
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
|
||||
func checkAgainstBool(e bool, c reflect.Value) (err error) {
|
||||
if c.Kind() != reflect.Bool {
|
||||
err = NewFatalError("which is not a bool")
|
||||
return
|
||||
}
|
||||
|
||||
err = errors.New("")
|
||||
if c.Bool() == e {
|
||||
err = nil
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func checkAgainstChan(e reflect.Value, c reflect.Value) (err error) {
|
||||
// Create a description of e's type, e.g. "chan int".
|
||||
typeStr := fmt.Sprintf("%s %s", e.Type().ChanDir(), e.Type().Elem())
|
||||
|
||||
// Make sure c is a chan of the correct type.
|
||||
if c.Kind() != reflect.Chan ||
|
||||
c.Type().ChanDir() != e.Type().ChanDir() ||
|
||||
c.Type().Elem() != e.Type().Elem() {
|
||||
err = NewFatalError(fmt.Sprintf("which is not a %s", typeStr))
|
||||
return
|
||||
}
|
||||
|
||||
err = errors.New("")
|
||||
if c.Pointer() == e.Pointer() {
|
||||
err = nil
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func checkAgainstFunc(e reflect.Value, c reflect.Value) (err error) {
|
||||
// Make sure c is a function.
|
||||
if c.Kind() != reflect.Func {
|
||||
err = NewFatalError("which is not a function")
|
||||
return
|
||||
}
|
||||
|
||||
err = errors.New("")
|
||||
if c.Pointer() == e.Pointer() {
|
||||
err = nil
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func checkAgainstMap(e reflect.Value, c reflect.Value) (err error) {
|
||||
// Make sure c is a map.
|
||||
if c.Kind() != reflect.Map {
|
||||
err = NewFatalError("which is not a map")
|
||||
return
|
||||
}
|
||||
|
||||
err = errors.New("")
|
||||
if c.Pointer() == e.Pointer() {
|
||||
err = nil
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func checkAgainstPtr(e reflect.Value, c reflect.Value) (err error) {
|
||||
// Create a description of e's type, e.g. "*int".
|
||||
typeStr := fmt.Sprintf("*%v", e.Type().Elem())
|
||||
|
||||
// Make sure c is a pointer of the correct type.
|
||||
if c.Kind() != reflect.Ptr ||
|
||||
c.Type().Elem() != e.Type().Elem() {
|
||||
err = NewFatalError(fmt.Sprintf("which is not a %s", typeStr))
|
||||
return
|
||||
}
|
||||
|
||||
err = errors.New("")
|
||||
if c.Pointer() == e.Pointer() {
|
||||
err = nil
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func checkAgainstSlice(e reflect.Value, c reflect.Value) (err error) {
|
||||
// Create a description of e's type, e.g. "[]int".
|
||||
typeStr := fmt.Sprintf("[]%v", e.Type().Elem())
|
||||
|
||||
// Make sure c is a slice of the correct type.
|
||||
if c.Kind() != reflect.Slice ||
|
||||
c.Type().Elem() != e.Type().Elem() {
|
||||
err = NewFatalError(fmt.Sprintf("which is not a %s", typeStr))
|
||||
return
|
||||
}
|
||||
|
||||
err = errors.New("")
|
||||
if c.Pointer() == e.Pointer() {
|
||||
err = nil
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func checkAgainstString(e reflect.Value, c reflect.Value) (err error) {
|
||||
// Make sure c is a string.
|
||||
if c.Kind() != reflect.String {
|
||||
err = NewFatalError("which is not a string")
|
||||
return
|
||||
}
|
||||
|
||||
err = errors.New("")
|
||||
if c.String() == e.String() {
|
||||
err = nil
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func checkAgainstArray(e reflect.Value, c reflect.Value) (err error) {
|
||||
// Create a description of e's type, e.g. "[2]int".
|
||||
typeStr := fmt.Sprintf("%v", e.Type())
|
||||
|
||||
// Make sure c is the correct type.
|
||||
if c.Type() != e.Type() {
|
||||
err = NewFatalError(fmt.Sprintf("which is not %s", typeStr))
|
||||
return
|
||||
}
|
||||
|
||||
// Check for equality.
|
||||
if e.Interface() != c.Interface() {
|
||||
err = errors.New("")
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func checkAgainstUnsafePointer(e reflect.Value, c reflect.Value) (err error) {
|
||||
// Make sure c is a pointer.
|
||||
if c.Kind() != reflect.UnsafePointer {
|
||||
err = NewFatalError("which is not a unsafe.Pointer")
|
||||
return
|
||||
}
|
||||
|
||||
err = errors.New("")
|
||||
if c.Pointer() == e.Pointer() {
|
||||
err = nil
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func checkForNil(c reflect.Value) (err error) {
|
||||
err = errors.New("")
|
||||
|
||||
// Make sure it is legal to call IsNil.
|
||||
switch c.Kind() {
|
||||
case reflect.Invalid:
|
||||
case reflect.Chan:
|
||||
case reflect.Func:
|
||||
case reflect.Interface:
|
||||
case reflect.Map:
|
||||
case reflect.Ptr:
|
||||
case reflect.Slice:
|
||||
|
||||
default:
|
||||
err = NewFatalError("which cannot be compared to nil")
|
||||
return
|
||||
}
|
||||
|
||||
// Ask whether the value is nil. Handle a nil literal (kind Invalid)
|
||||
// specially, since it's not legal to call IsNil there.
|
||||
if c.Kind() == reflect.Invalid || c.IsNil() {
|
||||
err = nil
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
// Public implementation
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
|
||||
func (m *equalsMatcher) Matches(candidate interface{}) error {
|
||||
e := m.expectedValue
|
||||
c := reflect.ValueOf(candidate)
|
||||
ek := e.Kind()
|
||||
|
||||
switch {
|
||||
case ek == reflect.Bool:
|
||||
return checkAgainstBool(e.Bool(), c)
|
||||
|
||||
case isSignedInteger(e):
|
||||
return checkAgainstInt64(e.Int(), c)
|
||||
|
||||
case isUnsignedInteger(e):
|
||||
return checkAgainstUint64(e.Uint(), c)
|
||||
|
||||
case ek == reflect.Float32:
|
||||
return checkAgainstFloat32(float32(e.Float()), c)
|
||||
|
||||
case ek == reflect.Float64:
|
||||
return checkAgainstFloat64(e.Float(), c)
|
||||
|
||||
case ek == reflect.Complex64:
|
||||
return checkAgainstComplex64(complex64(e.Complex()), c)
|
||||
|
||||
case ek == reflect.Complex128:
|
||||
return checkAgainstComplex128(complex128(e.Complex()), c)
|
||||
|
||||
case ek == reflect.Chan:
|
||||
return checkAgainstChan(e, c)
|
||||
|
||||
case ek == reflect.Func:
|
||||
return checkAgainstFunc(e, c)
|
||||
|
||||
case ek == reflect.Map:
|
||||
return checkAgainstMap(e, c)
|
||||
|
||||
case ek == reflect.Ptr:
|
||||
return checkAgainstPtr(e, c)
|
||||
|
||||
case ek == reflect.Slice:
|
||||
return checkAgainstSlice(e, c)
|
||||
|
||||
case ek == reflect.String:
|
||||
return checkAgainstString(e, c)
|
||||
|
||||
case ek == reflect.Array:
|
||||
return checkAgainstArray(e, c)
|
||||
|
||||
case ek == reflect.UnsafePointer:
|
||||
return checkAgainstUnsafePointer(e, c)
|
||||
|
||||
case ek == reflect.Invalid:
|
||||
return checkForNil(c)
|
||||
}
|
||||
|
||||
panic(fmt.Sprintf("equalsMatcher.Matches: unexpected kind: %v", ek))
|
||||
}
|
||||
|
||||
func (m *equalsMatcher) Description() string {
|
||||
// Special case: handle nil.
|
||||
if !m.expectedValue.IsValid() {
|
||||
return "is nil"
|
||||
}
|
||||
|
||||
return fmt.Sprintf("%v", m.expectedValue.Interface())
|
||||
}
|
||||
Generated
Vendored
+39
@@ -0,0 +1,39 @@
|
||||
// Copyright 2011 Aaron Jacobs. All Rights Reserved.
|
||||
// Author: [email protected] (Aaron Jacobs)
|
||||
//
|
||||
// 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 oglematchers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
// GreaterOrEqual returns a matcher that matches integer, floating point, or
|
||||
// strings values v such that v >= x. Comparison is not defined between numeric
|
||||
// and string types, but is defined between all integer and floating point
|
||||
// types.
|
||||
//
|
||||
// x must itself be an integer, floating point, or string type; otherwise,
|
||||
// GreaterOrEqual will panic.
|
||||
func GreaterOrEqual(x interface{}) Matcher {
|
||||
desc := fmt.Sprintf("greater than or equal to %v", x)
|
||||
|
||||
// Special case: make it clear that strings are strings.
|
||||
if reflect.TypeOf(x).Kind() == reflect.String {
|
||||
desc = fmt.Sprintf("greater than or equal to \"%s\"", x)
|
||||
}
|
||||
|
||||
return transformDescription(Not(LessThan(x)), desc)
|
||||
}
|
||||
Generated
Vendored
+39
@@ -0,0 +1,39 @@
|
||||
// Copyright 2011 Aaron Jacobs. All Rights Reserved.
|
||||
// Author: [email protected] (Aaron Jacobs)
|
||||
//
|
||||
// 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 oglematchers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
// GreaterThan returns a matcher that matches integer, floating point, or
|
||||
// strings values v such that v > x. Comparison is not defined between numeric
|
||||
// and string types, but is defined between all integer and floating point
|
||||
// types.
|
||||
//
|
||||
// x must itself be an integer, floating point, or string type; otherwise,
|
||||
// GreaterThan will panic.
|
||||
func GreaterThan(x interface{}) Matcher {
|
||||
desc := fmt.Sprintf("greater than %v", x)
|
||||
|
||||
// Special case: make it clear that strings are strings.
|
||||
if reflect.TypeOf(x).Kind() == reflect.String {
|
||||
desc = fmt.Sprintf("greater than \"%s\"", x)
|
||||
}
|
||||
|
||||
return transformDescription(Not(LessOrEqual(x)), desc)
|
||||
}
|
||||
Generated
Vendored
+41
@@ -0,0 +1,41 @@
|
||||
// Copyright 2011 Aaron Jacobs. All Rights Reserved.
|
||||
// Author: [email protected] (Aaron Jacobs)
|
||||
//
|
||||
// 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 oglematchers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
// LessOrEqual returns a matcher that matches integer, floating point, or
|
||||
// strings values v such that v <= x. Comparison is not defined between numeric
|
||||
// and string types, but is defined between all integer and floating point
|
||||
// types.
|
||||
//
|
||||
// x must itself be an integer, floating point, or string type; otherwise,
|
||||
// LessOrEqual will panic.
|
||||
func LessOrEqual(x interface{}) Matcher {
|
||||
desc := fmt.Sprintf("less than or equal to %v", x)
|
||||
|
||||
// Special case: make it clear that strings are strings.
|
||||
if reflect.TypeOf(x).Kind() == reflect.String {
|
||||
desc = fmt.Sprintf("less than or equal to \"%s\"", x)
|
||||
}
|
||||
|
||||
// Put LessThan last so that its error messages will be used in the event of
|
||||
// failure.
|
||||
return transformDescription(AnyOf(Equals(x), LessThan(x)), desc)
|
||||
}
|
||||
Generated
Vendored
+152
@@ -0,0 +1,152 @@
|
||||
// Copyright 2011 Aaron Jacobs. All Rights Reserved.
|
||||
// Author: [email protected] (Aaron Jacobs)
|
||||
//
|
||||
// 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 oglematchers
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"math"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
// LessThan returns a matcher that matches integer, floating point, or strings
|
||||
// values v such that v < x. Comparison is not defined between numeric and
|
||||
// string types, but is defined between all integer and floating point types.
|
||||
//
|
||||
// x must itself be an integer, floating point, or string type; otherwise,
|
||||
// LessThan will panic.
|
||||
func LessThan(x interface{}) Matcher {
|
||||
v := reflect.ValueOf(x)
|
||||
kind := v.Kind()
|
||||
|
||||
switch {
|
||||
case isInteger(v):
|
||||
case isFloat(v):
|
||||
case kind == reflect.String:
|
||||
|
||||
default:
|
||||
panic(fmt.Sprintf("LessThan: unexpected kind %v", kind))
|
||||
}
|
||||
|
||||
return &lessThanMatcher{v}
|
||||
}
|
||||
|
||||
type lessThanMatcher struct {
|
||||
limit reflect.Value
|
||||
}
|
||||
|
||||
func (m *lessThanMatcher) Description() string {
|
||||
// Special case: make it clear that strings are strings.
|
||||
if m.limit.Kind() == reflect.String {
|
||||
return fmt.Sprintf("less than \"%s\"", m.limit.String())
|
||||
}
|
||||
|
||||
return fmt.Sprintf("less than %v", m.limit.Interface())
|
||||
}
|
||||
|
||||
func compareIntegers(v1, v2 reflect.Value) (err error) {
|
||||
err = errors.New("")
|
||||
|
||||
switch {
|
||||
case isSignedInteger(v1) && isSignedInteger(v2):
|
||||
if v1.Int() < v2.Int() {
|
||||
err = nil
|
||||
}
|
||||
return
|
||||
|
||||
case isSignedInteger(v1) && isUnsignedInteger(v2):
|
||||
if v1.Int() < 0 || uint64(v1.Int()) < v2.Uint() {
|
||||
err = nil
|
||||
}
|
||||
return
|
||||
|
||||
case isUnsignedInteger(v1) && isSignedInteger(v2):
|
||||
if v1.Uint() <= math.MaxInt64 && int64(v1.Uint()) < v2.Int() {
|
||||
err = nil
|
||||
}
|
||||
return
|
||||
|
||||
case isUnsignedInteger(v1) && isUnsignedInteger(v2):
|
||||
if v1.Uint() < v2.Uint() {
|
||||
err = nil
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
panic(fmt.Sprintf("compareIntegers: %v %v", v1, v2))
|
||||
}
|
||||
|
||||
func getFloat(v reflect.Value) float64 {
|
||||
switch {
|
||||
case isSignedInteger(v):
|
||||
return float64(v.Int())
|
||||
|
||||
case isUnsignedInteger(v):
|
||||
return float64(v.Uint())
|
||||
|
||||
case isFloat(v):
|
||||
return v.Float()
|
||||
}
|
||||
|
||||
panic(fmt.Sprintf("getFloat: %v", v))
|
||||
}
|
||||
|
||||
func (m *lessThanMatcher) Matches(c interface{}) (err error) {
|
||||
v1 := reflect.ValueOf(c)
|
||||
v2 := m.limit
|
||||
|
||||
err = errors.New("")
|
||||
|
||||
// Handle strings as a special case.
|
||||
if v1.Kind() == reflect.String && v2.Kind() == reflect.String {
|
||||
if v1.String() < v2.String() {
|
||||
err = nil
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// If we get here, we require that we are dealing with integers or floats.
|
||||
v1Legal := isInteger(v1) || isFloat(v1)
|
||||
v2Legal := isInteger(v2) || isFloat(v2)
|
||||
if !v1Legal || !v2Legal {
|
||||
err = NewFatalError("which is not comparable")
|
||||
return
|
||||
}
|
||||
|
||||
// Handle the various comparison cases.
|
||||
switch {
|
||||
// Both integers
|
||||
case isInteger(v1) && isInteger(v2):
|
||||
return compareIntegers(v1, v2)
|
||||
|
||||
// At least one float32
|
||||
case v1.Kind() == reflect.Float32 || v2.Kind() == reflect.Float32:
|
||||
if float32(getFloat(v1)) < float32(getFloat(v2)) {
|
||||
err = nil
|
||||
}
|
||||
return
|
||||
|
||||
// At least one float64
|
||||
case v1.Kind() == reflect.Float64 || v2.Kind() == reflect.Float64:
|
||||
if getFloat(v1) < getFloat(v2) {
|
||||
err = nil
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// We shouldn't get here.
|
||||
panic(fmt.Sprintf("lessThanMatcher.Matches: Shouldn't get here: %v %v", v1, v2))
|
||||
}
|
||||
Generated
Vendored
+86
@@ -0,0 +1,86 @@
|
||||
// Copyright 2011 Aaron Jacobs. All Rights Reserved.
|
||||
// Author: [email protected] (Aaron Jacobs)
|
||||
//
|
||||
// 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 oglematchers provides a set of matchers useful in a testing or
|
||||
// mocking framework. These matchers are inspired by and mostly compatible with
|
||||
// Google Test for C++ and Google JS Test.
|
||||
//
|
||||
// This package is used by github.com/smartystreets/assertions/internal/ogletest and
|
||||
// github.com/smartystreets/assertions/internal/oglemock, which may be more directly useful if you're not
|
||||
// writing your own testing package or defining your own matchers.
|
||||
package oglematchers
|
||||
|
||||
// A Matcher is some predicate implicitly defining a set of values that it
|
||||
// matches. For example, GreaterThan(17) matches all numeric values greater
|
||||
// than 17, and HasSubstr("taco") matches all strings with the substring
|
||||
// "taco".
|
||||
//
|
||||
// Matchers are typically exposed to tests via constructor functions like
|
||||
// HasSubstr. In order to implement such a function you can either define your
|
||||
// own matcher type or use NewMatcher.
|
||||
type Matcher interface {
|
||||
// Check whether the supplied value belongs to the the set defined by the
|
||||
// matcher. Return a non-nil error if and only if it does not.
|
||||
//
|
||||
// The error describes why the value doesn't match. The error text is a
|
||||
// relative clause that is suitable for being placed after the value. For
|
||||
// example, a predicate that matches strings with a particular substring may,
|
||||
// when presented with a numerical value, return the following error text:
|
||||
//
|
||||
// "which is not a string"
|
||||
//
|
||||
// Then the failure message may look like:
|
||||
//
|
||||
// Expected: has substring "taco"
|
||||
// Actual: 17, which is not a string
|
||||
//
|
||||
// If the error is self-apparent based on the description of the matcher, the
|
||||
// error text may be empty (but the error still non-nil). For example:
|
||||
//
|
||||
// Expected: 17
|
||||
// Actual: 19
|
||||
//
|
||||
// If you are implementing a new matcher, see also the documentation on
|
||||
// FatalError.
|
||||
Matches(candidate interface{}) error
|
||||
|
||||
// Description returns a string describing the property that values matching
|
||||
// this matcher have, as a verb phrase where the subject is the value. For
|
||||
// example, "is greather than 17" or "has substring "taco"".
|
||||
Description() string
|
||||
}
|
||||
|
||||
// FatalError is an implementation of the error interface that may be returned
|
||||
// from matchers, indicating the error should be propagated. Returning a
|
||||
// *FatalError indicates that the matcher doesn't process values of the
|
||||
// supplied type, or otherwise doesn't know how to handle the value.
|
||||
//
|
||||
// For example, if GreaterThan(17) returned false for the value "taco" without
|
||||
// a fatal error, then Not(GreaterThan(17)) would return true. This is
|
||||
// technically correct, but is surprising and may mask failures where the wrong
|
||||
// sort of matcher is accidentally used. Instead, GreaterThan(17) can return a
|
||||
// fatal error, which will be propagated by Not().
|
||||
type FatalError struct {
|
||||
errorText string
|
||||
}
|
||||
|
||||
// NewFatalError creates a FatalError struct with the supplied error text.
|
||||
func NewFatalError(s string) *FatalError {
|
||||
return &FatalError{s}
|
||||
}
|
||||
|
||||
func (e *FatalError) Error() string {
|
||||
return e.errorText
|
||||
}
|
||||
+53
@@ -0,0 +1,53 @@
|
||||
// Copyright 2011 Aaron Jacobs. All Rights Reserved.
|
||||
// Author: [email protected] (Aaron Jacobs)
|
||||
//
|
||||
// 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 oglematchers
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// Not returns a matcher that inverts the set of values matched by the wrapped
|
||||
// matcher. It does not transform the result for values for which the wrapped
|
||||
// matcher returns a fatal error.
|
||||
func Not(m Matcher) Matcher {
|
||||
return ¬Matcher{m}
|
||||
}
|
||||
|
||||
type notMatcher struct {
|
||||
wrapped Matcher
|
||||
}
|
||||
|
||||
func (m *notMatcher) Matches(c interface{}) (err error) {
|
||||
err = m.wrapped.Matches(c)
|
||||
|
||||
// Did the wrapped matcher say yes?
|
||||
if err == nil {
|
||||
return errors.New("")
|
||||
}
|
||||
|
||||
// Did the wrapped matcher return a fatal error?
|
||||
if _, isFatal := err.(*FatalError); isFatal {
|
||||
return err
|
||||
}
|
||||
|
||||
// The wrapped matcher returned a non-fatal error.
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *notMatcher) Description() string {
|
||||
return fmt.Sprintf("not(%s)", m.wrapped.Description())
|
||||
}
|
||||
Generated
Vendored
+36
@@ -0,0 +1,36 @@
|
||||
// Copyright 2011 Aaron Jacobs. All Rights Reserved.
|
||||
// Author: [email protected] (Aaron Jacobs)
|
||||
//
|
||||
// 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 oglematchers
|
||||
|
||||
// transformDescription returns a matcher that is equivalent to the supplied
|
||||
// one, except that it has the supplied description instead of the one attached
|
||||
// to the existing matcher.
|
||||
func transformDescription(m Matcher, newDesc string) Matcher {
|
||||
return &transformDescriptionMatcher{newDesc, m}
|
||||
}
|
||||
|
||||
type transformDescriptionMatcher struct {
|
||||
desc string
|
||||
wrappedMatcher Matcher
|
||||
}
|
||||
|
||||
func (m *transformDescriptionMatcher) Description() string {
|
||||
return m.desc
|
||||
}
|
||||
|
||||
func (m *transformDescriptionMatcher) Matches(c interface{}) error {
|
||||
return m.wrappedMatcher.Matches(c)
|
||||
}
|
||||
Reference in New Issue
Block a user