assert.Equal: "No newline at end of file" can be misleading
還沒有人認領這個 Issue。
評估
研究方向
從 assert.go 中的 diff() 開始,這裡會準備用於比較的字串值,並重現 issue 中描述的四種字串情況。完成的標準是:不帶結尾換行字元的字串不再顯示誤導性的標記,而涉及實際結尾換行字元的差異仍然可見。
由索引模型根據 Issue 內容生成。
描述
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.
- 主要語言
- Go
- 星號
- 196
- 分支
- 16
- PR 合併指標
- 30 天內沒有已合併 PR
環境準備
這個專案沒有提供開發容器、Dockerfile 或貢獻指南,環境需要你自己搭建:先看它的 README,通用步驟見我們的新手貢獻指南。
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
alecthomas/assert 的其他 Issue
-
Is Nil() gone?未關閉
難度 2/5 1-3 小時 新手友好度 35/100
alecthomas/assert#27 ·
-
難度 1/5 1 小時以內 新手友好度 15/100
alecthomas/assert#17 ·
-
難度 4/5 3-5 天 新手友好度 30/100
alecthomas/assert#14 ·
-
難度 2/5 1-3 小時 新手友好度 38/100
alecthomas/assert#9 · 1 則留言 · 1 個 reaction ·
-
難度 3/5 1-2 天 新手友好度 35/100
alecthomas/assert#4 ·
查看 alecthomas/assert 的全部 Issue
相似的 Issue
-
難度 2/5 1-3 小時 新手友好度 76/100
OneBusAway/maglev#2145 ·
維護者通常 2 天內回覆
-
priority/medium status/ready type/chore
難度 2/5 1-3 小時 新手友好度 84/100
discobox-ai/discobox#1357 ·
維護者通常 1 天內回覆
-
TestPseudoLocaleRemote fails now and then: it types the code before the laptop asks for it可能已有人在做 @fzn0x 今天認領。 未關閉bug priority: low
難度 2/5 1-3 小時 新手友好度 88/100
維護者通常 1 天內回覆
-
enhancement ui/ux
難度 2/5 1-3 小時 新手友好度 68/100
jbigler/railswitch#243 ·
維護者通常 1 天內回覆
-
go-vulnerability security-scan
難度 1/5 1 小時以內 新手友好度 84/100
teslashibe/agent-go#35 ·