Skip to content

Commit

Permalink
add tests for replace package
Browse files Browse the repository at this point in the history
  • Loading branch information
ayoisaiah committed Jan 13, 2024
1 parent 91e9291 commit 1c9721e
Show file tree
Hide file tree
Showing 11 changed files with 538 additions and 6 deletions.
18 changes: 12 additions & 6 deletions internal/file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,20 @@ type Change struct {
BaseDir string `json:"base_dir"`
Source string `json:"source"`
Target string `json:"target"`
RelSourcePath string `json:"-"`
RelTargetPath string `json:"-"`
CSVRow []string `json:"-"`
Index int `json:"-"`
IsDir bool `json:"is_dir"`
WillOverwrite bool `json:"will_overwrite"`
// RelSourcePath is BaseDir + Source
RelSourcePath string `json:"-"`
// RelTargetPath is BaseDir + Target
RelTargetPath string `json:"-"`
CSVRow []string `json:"-"`
Index int `json:"-"`
IsDir bool `json:"is_dir"`
WillOverwrite bool `json:"will_overwrite"`
}

func (c *Change) SourcePath() string {
return filepath.Join(c.BaseDir, c.Source)
}

func (c *Change) TargetPath() string {
return filepath.Join(c.BaseDir, c.Target)
}
12 changes: 12 additions & 0 deletions internal/testutil/testutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,18 @@ func CompareSourcePath(t *testing.T, want []string, changes []*file.Change) {
assert.Equal(t, want, got)
}

func CompareTargetPath(t *testing.T, want []string, changes []*file.Change) {
t.Helper()

got := make([]string, len(changes))

for i := range changes {
got[i] = changes[i].TargetPath()
}

assert.Equal(t, want, got)
}

// UpdateBaseDir adds the testDir to each expected path for easy comparison.
func UpdateBaseDir(expected []string, testDir string) {
for i := range expected {
Expand Down
111 changes: 111 additions & 0 deletions replace/replace_test/indexing_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package replace_test

import (
"testing"

"github.com/ayoisaiah/f2/internal/file"
"github.com/ayoisaiah/f2/internal/testutil"
)

func TestIndexing(t *testing.T) {
testCases := []testutil.TestCase{
{
Name: "replace with auto incrementing integers",
Changes: []*file.Change{
{
Source: "a.txt",
},
{
Source: "b.txt",
},
{
Source: "c.txt",
},
},
Want: []string{"1.txt", "2.txt", "3.txt"},
Args: []string{"-f", "a|b|c", "-r", "{%d}"},
},
{
Name: "replace with multiple incrementing integers",
Changes: []*file.Change{
{
Source: "a.txt",
},
{
Source: "b.txt",
},
{
Source: "c.txt",
},
},
Want: []string{"1_10_0100.txt", "2_20_0200.txt", "3_30_0300.txt"},
Args: []string{"-f", "a|b|c", "-r", "{%d}_{10%02d10}_{100%04d100}"},
},
{
Name: "replace with non-arabic numerals",
Changes: []*file.Change{
{
Source: "a.txt",
},
{
Source: "b.txt",
},
{
Source: "c.txt",
},
},
Want: []string{"I_1 1_1.txt", "II_2 2_10.txt", "III_3 3_11.txt"},
Args: []string{"-f", "a|b|c", "-r", "{%dr}_{%do} {%dh}_{%db}"},
},
{
Name: "skip some numbers when incrementing",
Changes: []*file.Change{
{
Source: "a.txt",
},
{
Source: "b.txt",
},
{
Source: "c.txt",
},
},
Want: []string{"16.txt", "17.txt", "18.txt"},
Args: []string{"-f", "a|b|c", "-r", "{10%d<10-15>}"},
},
{
Name: "use integer capture variables",
Changes: []*file.Change{
{
Source: "doc1.txt",
},
{
Source: "doc4.txt",
},
{
Source: "doc99.txt",
},
},
Want: []string{"001.txt", "004.txt", "099.txt"},
Args: []string{"-f", "doc(\\d+)", "-r", "{$1%03d}"},
},
{
Name: "use integer capture variables with explicit step",
Changes: []*file.Change{
{
Source: "doc1.txt",
},
{
Source: "doc4.txt",
},
{
Source: "doc99.txt",
},
},
Want: []string{"006.txt", "009.txt", "104.txt"},
Args: []string{"-f", "doc(\\d+)", "-r", "{$1%03d5}"},
},
}

replaceTest(t, testCases)
}
178 changes: 178 additions & 0 deletions replace/replace_test/replace_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
package replace_test

import (
"errors"
"path/filepath"
"testing"

"github.com/ayoisaiah/f2/internal/file"
"github.com/ayoisaiah/f2/internal/testutil"
"github.com/ayoisaiah/f2/replace"
)

func replaceTest(t *testing.T, cases []testutil.TestCase) {
t.Helper()

for i := range cases {
tc := cases[i]

for j := range tc.Changes {
ch := tc.Changes[j]

cases[i].Changes[j].OriginalSource = ch.Source
cases[i].Changes[j].RelSourcePath = filepath.Join(
ch.BaseDir,
ch.Source,
)
}
}

for i := range cases {
tc := cases[i]

t.Run(tc.Name, func(t *testing.T) {
config := testutil.GetConfig(t, &tc, ".")

changes, err := replace.Replace(config, tc.Changes)
if err == nil {
testutil.CompareTargetPath(t, tc.Want, changes)
return
}

if !errors.Is(err, tc.Error) {
t.Fatal(err)
}
})
}
}

func TestReplace(t *testing.T) {
testCases := []testutil.TestCase{
{
Name: "basic replace",
Changes: []*file.Change{
{
Source: "macos_update_notes_2023.txt",
},
{
Source: "macos_user_guide_macos_sierra.pdf",
},
},
Want: []string{
"darwin_update_notes_2023.txt",
"darwin_user_guide_darwin_sierra.pdf",
},
Args: []string{"-f", "macos", "-r", "darwin"},
},
{
Name: "basic replace with positional arguments",
Changes: []*file.Change{
{
Source: "macos_update_notes_2023.txt",
},
{
Source: "macos_user_guide_macos_sierra.pdf",
},
},
Want: []string{
"darwin_update_notes_2023.txt",
"darwin_user_guide_darwin_sierra.pdf",
},
Args: []string{"macos", "darwin"},
},
{
Name: "replace only the first match",
Changes: []*file.Change{
{
Source: "budget_budget_budget_2023.xlsx",
},
},
Want: []string{
"forecast_budget_budget_2023.xlsx",
},
Args: []string{"-f", "budget", "-r", "forecast", "-l", "1"},
},
{
Name: "replace the first 2 matches in reverse",
Changes: []*file.Change{
{
Source: "budget_budget_budget_2023.xlsx",
},
{
Source: "budget_2024.xlsx",
},
},
Want: []string{
"budget_forecast_forecast_2023.xlsx",
"forecast_2024.xlsx",
},
Args: []string{"-f", "budget", "-r", "forecast", "-l", "-2"},
},
{
Name: "replace the first 2 matches in reverse",
Changes: []*file.Change{
{
Source: "budget_budget_budget_2023.xlsx",
},
{
Source: "budget_2024.xlsx",
},
},
Want: []string{
"budget_forecast_forecast_2023.xlsx",
"forecast_2024.xlsx",
},
Args: []string{"-f", "budget", "-r", "forecast", "-l", "-2"},
},
{
Name: "replace with auto incrementing integers",
Changes: []*file.Change{
{
Source: "a.txt",
},
{
Source: "b.txt",
},
{
Source: "c.txt",
},
},
Want: []string{"1.txt", "2.txt", "3.txt"},
Args: []string{"-f", "a|b|c", "-r", "{%d}"},
},
{
Name: "replace with multiple incrementing integers",
Changes: []*file.Change{
{
Source: "a.txt",
},
{
Source: "b.txt",
},
{
Source: "c.txt",
},
},
Want: []string{"1_10_0100.txt", "2_20_0200.txt", "3_30_0300.txt"},
Args: []string{"-f", "a|b|c", "-r", "{%d}_{10%02d10}_{100%04d100}"},
},
{
Name: "skip numbers",
Changes: []*file.Change{
{
Source: "a.txt",
},
{
Source: "b.txt",
},
{
Source: "c.txt",
},
},
Want: []string{"1_10_0100.txt", "2_20_0200.txt", "3_30_0300.txt"},
Args: []string{"-f", "a|b|c", "-r", "{%d}_{10%02d10}_{100%04d100}"},
},
}

replaceTest(t, testCases)
}
Binary file added replace/replace_test/testdata/audio.flac
Binary file not shown.
Binary file added replace/replace_test/testdata/audio.mp3
Binary file not shown.
Binary file added replace/replace_test/testdata/image.dng
Binary file not shown.
Binary file added replace/replace_test/testdata/pic.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
28 changes: 28 additions & 0 deletions replace/replace_test/variables_darwin_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//go:build darwin
// +build darwin

package replace_test

import (
"testing"

"github.com/ayoisaiah/f2/internal/file"
"github.com/ayoisaiah/f2/internal/testutil"
)

func TestMacOSTransforms(t *testing.T) {
testCases := []testutil.TestCase{
{
Name: "remove macOS disallowed characters",
Changes: []*file.Change{
{
Source: "report:::project*details|on<2024/01/11>.txt",
},
},
Want: []string{"reportproject*details|on<2024/01/11>.txt"},
Args: []string{"-f", ".*", "-r", "{.mac}"},
},
}

replaceTest(t, testCases)
}
Loading

0 comments on commit 1c9721e

Please sign in to comment.