-
-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
538 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
Binary file not shown.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
Oops, something went wrong.