Files
DeepSeek-Reasonix/internal/diff/diff_extra_test.go
T
YHH 4fbbca6a99 refactor(go): adopt modern stdlib idioms and gate them (#7831)
* refactor(go): adopt modern stdlib idioms across kernel and desktop

Mechanical sweep with golangci-lint's modernize/intrange/copyloopvar/
usestdlibvars fixers plus hand-fixes for what they could not rewrite:
slices.Contains, min/max, slices.Backward, WaitGroup.Go, reflect.TypeFor,
maps.Copy, strings.Cut/CutPrefix/SplitSeq, strings.Builder, range-over-int.
Net -549 lines with no behavior change.

Three loops in session.go stay index-based: slices.Backward yields element
copies, so the fixer's rewrite silently dropped writes to DecisionReceipts
and ToolCalls (caught by TestDelivery* and TestDependentSameBatch*). All 49
slices.Backward sites were audited for that shape; only these three write
through the index, and they carry a nolint with the reason.

CaptureAfter's scan loop had an empty body and never ran any logic, so the
fixer could not express it. Drop it and the accessor it was the only caller
of. omitzero is deliberately untouched: turning omitempty into omitzero
changes the JSON written for nested structs, which is a wire-format change
to session files and the extension protocol.

* build(lint): gate the modern-idiom linters

Nothing stopped the codebase from drifting back to pre-1.22 idioms. Both
modules are now clean for modernize, intrange, copyloopvar, and
usestdlibvars, so enable them rather than leaving the sweep as a one-off.

omitzero stays disabled with the reason inline; usetesting, perfsprint, and
errorlint still have residue and land separately.
2026-08-07 15:30:33 +08:00

183 lines
3.8 KiB
Go

package diff
import (
"strings"
"testing"
)
// splitLines
func TestSplitLinesEmpty(t *testing.T) {
lines, eol := splitLines("")
if len(lines) != 0 {
t.Errorf("empty: got %d lines", len(lines))
}
if !eol {
t.Error("empty should report endsWithNewline=true")
}
}
func TestSplitLinesNoTrailingNewline(t *testing.T) {
lines, eol := splitLines("a\nb")
if len(lines) != 2 {
t.Errorf("got %d lines", len(lines))
}
if lines[0] != "a" || lines[1] != "b" {
t.Errorf("lines = %v", lines)
}
if eol {
t.Error("should report no trailing newline")
}
}
func TestSplitLinesTrailingNewline(t *testing.T) {
lines, eol := splitLines("a\nb\n")
if len(lines) != 2 {
t.Errorf("got %d lines", len(lines))
}
if !eol {
t.Error("should report trailing newline")
}
}
func TestSplitLinesSingleLine(t *testing.T) {
lines, eol := splitLines("hello")
if len(lines) != 1 || lines[0] != "hello" {
t.Errorf("lines = %v", lines)
}
if eol {
t.Error("single line without newline should report false")
}
}
func TestSplitLinesSingleLineWithNewline(t *testing.T) {
lines, eol := splitLines("hello\n")
if len(lines) != 1 || lines[0] != "hello" {
t.Errorf("lines = %v", lines)
}
if !eol {
t.Error("single line with newline should report true")
}
}
// isBinary
func TestIsBinaryEmpty(t *testing.T) {
if isBinary("") {
t.Error("empty string is not binary")
}
}
func TestIsBinaryText(t *testing.T) {
if isBinary("hello world\nline 2") {
t.Error("plain text is not binary")
}
}
func TestIsBinaryNUL(t *testing.T) {
if !isBinary("hello\x00world") {
t.Error("string with NUL should be binary")
}
}
func TestIsBinaryNULAtEnd(t *testing.T) {
if !isBinary("hello\x00") {
t.Error("NUL at end should be binary")
}
}
// Build edge cases
func TestBuildBothEmpty(t *testing.T) {
c := Build("empty.txt", "", "", Modify)
if c.Diff != "" || c.Added != 0 || c.Removed != 0 {
t.Errorf("both empty should be no-op: %+v", c)
}
}
func TestBuildEmptyOldNonEmptyNew(t *testing.T) {
c := Build("new.txt", "", "line1\nline2\n", Create)
if c.Added != 2 || c.Removed != 0 {
t.Errorf("added/removed = %d/%d", c.Added, c.Removed)
}
if c.Kind != Create {
t.Errorf("kind = %q", c.Kind)
}
}
func TestBuildNonEmptyOldEmptyNew(t *testing.T) {
c := Build("del.txt", "line1\nline2\n", "", Delete)
if c.Added != 0 || c.Removed != 2 {
t.Errorf("added/removed = %d/%d", c.Added, c.Removed)
}
}
func TestBuildCRLF(t *testing.T) {
old := "line1\r\nline2\r\n"
neu := "line1\r\nLINE2\r\n"
c := Build("crlf.txt", old, neu, Modify)
// CRLF should be handled (split on \n, \r stays in line content).
if c.Added != 1 || c.Removed != 1 {
t.Errorf("added/removed = %d/%d, want 1/1", c.Added, c.Removed)
}
}
func TestBuildWhitespaceOnly(t *testing.T) {
old := "a\tb\n"
neu := "a b\n"
c := Build("ws.txt", old, neu, Modify)
if c.Added != 1 || c.Removed != 1 {
t.Errorf("added/removed = %d/%d", c.Added, c.Removed)
}
}
func TestBuildLargeFile(t *testing.T) {
// Build a large file with one changed line in the middle.
var oldB, newB strings.Builder
for i := range 1000 {
oldB.WriteString("line\n")
if i == 500 {
newB.WriteString("CHANGED\n")
} else {
newB.WriteString("line\n")
}
}
c := Build("large.txt", oldB.String(), newB.String(), Modify)
if c.Added != 1 || c.Removed != 1 {
t.Errorf("added/removed = %d/%d, want 1/1", c.Added, c.Removed)
}
}
// Kind constants
func TestKindConstants(t *testing.T) {
if Create != "create" {
t.Errorf("Create = %q", Create)
}
if Modify != "modify" {
t.Errorf("Modify = %q", Modify)
}
if Delete != "delete" {
t.Errorf("Delete = %q", Delete)
}
}
// itoa
func TestItoa(t *testing.T) {
cases := []struct {
n int
want string
}{
{0, "0"},
{1, "1"},
{42, "42"},
{1000, "1000"},
}
for _, c := range cases {
if got := itoa(c.n); got != c.want {
t.Errorf("itoa(%d) = %q, want %q", c.n, got, c.want)
}
}
}