alecthomas / assert Goto Github PK
View Code? Open in Web Editor NEWA simple assertion library using Go generics
License: MIT License
A simple assertion library using Go generics
License: MIT License
This currently fails:
assert.Equal(t, []byte(""), []byte(nil))
Consider using bytes.Equal, or special casing length zero.
This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.
These updates have all been created already. Click a checkbox below to force a retry/rebase of any.
.github/workflows/ci.yml
actions/checkout v2
actions/checkout v2
go.mod
go 1.18
github.com/alecthomas/repr v0.4.0
github.com/hexops/gotextdiff v1.0.3
bin/hermit
go 1.22.3
../../BUILDROOT/golang-github-alecthomas-assert-0-0.1.20170911git561411b.el7.centos.x86_64/usr/share/gocode/src/github.com/alecthomas/assert/assertions.go:311: not enough arguments in call to repr.OmitEmpty
../../BUILDROOT/golang-github-alecthomas-assert-0-0.1.20170911git561411b.el7.centos.x86_64/usr/share/gocode/src/github.com/alecthomas/assert/assertions.go:458: not enough arguments in call to repr.OmitEmpty
../../BUILDROOT/golang-github-alecthomas-assert-0-0.1.20170911git561411b.el7.centos.x86_64/usr/share/gocode/src/github.com/alecthomas/assert/diff.go:15: not enough arguments in call to repr.OmitEmpty
../../BUILDROOT/golang-github-alecthomas-assert-0-0.1.20170911git561411b.el7.centos.x86_64/usr/share/gocode/src/github.com/alecthomas/assert/diff.go:16: not enough arguments in call to repr.OmitEmpty
Installed:
golang.x86_64 0:1.7.4-1.el7
golang-github-alecthomas-colour-devel.x86_64 0:0-0.3.20170912git60882d9.el7.centos
golang-github-alecthomas-repr-devel.x86_64 0:0-0.1.20170912gitcf65cb4.el7.centos
golang-github-sergi-go-diff-devel.x86_64 0:0-0.2.20170911gitfeef008.el7.centos
Dependency Installed:
golang-bin.x86_64 0:1.7.4-1.el7 golang-github-mattn-go-isatty-devel.x86_64 0:0.0.2-1.el7.centos golang-src.noarch 0:1.7.4-1.el7
My code:
assert.True(t, req.Tariff.PercentPerScheme.Other == nil, "other percent should be nil")
Expected output:
mapping_test.go:60: other percent should be nil
Actual output:
mapping_test.go:60: Expected expression to be true
Area of code that would need changing:
// True asserts that an expression is true.
func True(t testing.TB, ok bool, msgAndArgs ...interface{}) {
if ok {
return
}
t.Helper()
t.Fatal("Expected expression to be true")
}
// False asserts that an expression is false.
func False(t testing.TB, ok bool, msgAndArgs ...interface{}) {
if !ok {
return
}
t.Helper()
t.Fatal("Expected expression to be false")
}
Happy to submit a PR in a little bit
Hello,
Is there an option to see colored diffs when a test fails? I didn't notice one when looking at the code.
If not, would you accept a PR to add colors to diffs?
Firstly, thank you for this excellent library! I'm slowly migrating my projects from stretchr/testify to alecthomas/assert.
What's wrong with this code?
assert.Zero(t, 0, len(foo), "foo should have length zero")
Spoiler alert: this test always passes, no matter how big len(foo)
is.
Is there a way to make this API harder to misuse?
hello,
consider a test comparing two strings that _do not end with a newline:
func TestNoNewline(t *testing.T) {
want := "banana"
have := "mango"
assert.Equal(t, want, have)
}
the output will be:
foo_test.go:156: Expected values to be equal:
-banana
\ No newline at end of file
+mango
\ No newline at end of file
which is quite confusing: sure, there is no newline at the end of the "file", but it is on purpose :-)
I would expect simply:
foo_test.go:156: Expected values to be equal:
-banana
+mango
If I change diff() as follows:
diff --git a/assert.go b/assert.go
index d2df971a5f..1692e38afd 100644
--- a/assert.go
+++ b/assert.go
@@ -194,8 +194,8 @@ func diff[T any](lhs, rhs T) string {
var lhss, rhss string
// Special case strings so we get nice diffs.
if l, ok := any(lhs).(string); ok {
- lhss = l
- rhss = any(rhs).(string)
+ lhss = l + "\n"
+ rhss = any(rhs).(string) + "\n"
} else {
lhss = repr.String(lhs, repr.Indent(" ")) + "\n"
rhss = repr.String(rhs, repr.Indent(" ")) + "\n"
I get the following 4 cases, which seem reasonable to me:
Case 1
func TestNoNewline(t *testing.T) {
want := "banana"
have := "mango"
assert.Equal(t, want, have)
}
gives:
marco_test.go:12: Expected values to be equal:
-banana
+mango
--- FAIL: TestNoNewline (0.00s)
Case 2
func TestWithNewline1(t *testing.T) {
want := "banana\n"
have := "mango"
assert.Equal(t, want, have)
}
gives:
marco_test.go:18: Expected values to be equal:
-banana
-
+mango
--- FAIL: TestWithNewline1 (0.00s)
Case 3
func TestWithNewline2(t *testing.T) {
want := "banana"
have := "mango\n"
assert.Equal(t, want, have)
}
gives
marco_test.go:24: Expected values to be equal:
-banana
+mango
+
--- FAIL: TestWithNewline2 (0.00s)
Case 4
func TestWithNewline(t *testing.T) {
want := "banana\n"
have := "mango\n"
assert.Equal(t, want, have)
}
gives:
marco_test.go:18: Expected values to be equal:
-banana
+mango
<-- blank like, but doesn't seem to be a problem
--- FAIL: TestWithNewline (0.00s)
If you see value, I will be happy to provide a PR.
I just updated to the latest version of assert and it seems to be causing havoc on our CI/CD system. The problem is that we have several checks on large protocol buffers. The private state
member is then rendered through the repr reflection system and it can be hundreds of thousands (millions?) of lines.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.