alecthomas / alecthomas/assert

assert.Equal: "No newline at end of file" can be misleading

Đang mở
#7 0 bình luận 1 reaction 0 người được giao Xem trên GitHub
Ngôn ngữ chính
Go
Star
195
Fork
16
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Mô tả

hello,
consider a test comparing two strings that _do not end with a newline:
```go
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
```go
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
```go
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
```go
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
```go
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.

Hướng dẫn đóng góp

Chưa lập chỉ mục được hướng dẫn đóng góp cho kho mã nguồn này

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.