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

未關閉
#7 0 則留言 1 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

評估

難度
2/5
預估耗時
1-3 小時
新手友好度
48/100
Issue 類型
缺陷
描述清晰度
描述清楚
活躍度
停滯
技術堆疊
go
領域
testing

研究方向

從 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,通用步驟見我們的新手貢獻指南。

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

alecthomas/assert 的其他 Issue

查看 alecthomas/assert 的全部 Issue

相似的 Issue

更多 Go Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。