feat: implement remove command with sequential removals

This commit is contained in:
Rogee
2025-10-29 18:59:55 +08:00
parent 446bd46b95
commit f66c59fd57
31 changed files with 986 additions and 110 deletions

View File

@@ -0,0 +1,74 @@
package replace_test
import (
"testing"
"github.com/rogeecn/renamer/internal/remove"
)
func TestApplyTokensSequentialRemoval(t *testing.T) {
candidate := remove.Candidate{
BaseName: "report copy draft.txt",
RelativePath: "report copy draft.txt",
}
result := remove.ApplyTokens(candidate, []string{" copy", " draft"})
if !result.Changed {
t.Fatalf("expected result to be marked as changed")
}
if result.ProposedName != "report.txt" {
t.Fatalf("expected proposed name to be report.txt, got %q", result.ProposedName)
}
if result.Matches[" copy"] != 1 {
t.Fatalf("expected match count for ' copy' to be 1, got %d", result.Matches[" copy"])
}
if result.Matches[" draft"] != 1 {
t.Fatalf("expected match count for ' draft' to be 1, got %d", result.Matches[" draft"])
}
}
func TestApplyTokensNoChange(t *testing.T) {
candidate := remove.Candidate{
BaseName: "notes.txt",
RelativePath: "notes.txt",
}
result := remove.ApplyTokens(candidate, []string{" copy"})
if result.Changed {
t.Fatalf("expected no change for candidate without matches")
}
if len(result.Matches) != 0 {
t.Fatalf("expected no matches to be recorded, got %#v", result.Matches)
}
if result.ProposedName != candidate.BaseName {
t.Fatalf("expected proposed name to remain %q, got %q", candidate.BaseName, result.ProposedName)
}
}
func TestApplyTokensEmptyName(t *testing.T) {
candidate := remove.Candidate{
BaseName: "draft",
RelativePath: "draft",
}
result := remove.ApplyTokens(candidate, []string{"draft"})
if !result.Changed {
t.Fatalf("expected change when removing the full name")
}
if result.ProposedName != "" {
t.Fatalf("expected proposed name to be empty, got %q", result.ProposedName)
}
if result.Matches["draft"] != 1 {
t.Fatalf("expected matches to record removal, got %#v", result.Matches)
}
}

View File

@@ -0,0 +1,46 @@
package replace_test
import (
"testing"
"github.com/rogeecn/renamer/internal/remove"
)
func TestParseArgsDeduplicatesPreservingOrder(t *testing.T) {
args := []string{" draft", " draft", " copy", " draft"}
result, err := remove.ParseArgs(args)
if err != nil {
t.Fatalf("ParseArgs returned error: %v", err)
}
expected := []string{" draft", " copy"}
if len(result.Tokens) != len(expected) {
t.Fatalf("expected %d tokens, got %d", len(expected), len(result.Tokens))
}
for i, token := range expected {
if result.Tokens[i] != token {
t.Fatalf("token[%d] mismatch: expected %q, got %q", i, token, result.Tokens[i])
}
}
if len(result.Duplicates) != 2 {
t.Fatalf("expected 2 duplicates recorded, got %d", len(result.Duplicates))
}
if result.Duplicates[0] != " draft" || result.Duplicates[1] != " draft" {
t.Fatalf("unexpected duplicates: %#v", result.Duplicates)
}
}
func TestParseArgsSkipsWhitespaceOnlyTokens(t *testing.T) {
args := []string{" ", "\t", "foo"}
result, err := remove.ParseArgs(args)
if err != nil {
t.Fatalf("ParseArgs returned error: %v", err)
}
if len(result.Tokens) != 1 || result.Tokens[0] != "foo" {
t.Fatalf("expected single token 'foo', got %#v", result.Tokens)
}
}