Skip to content

Commit

Permalink
Merge branch 'main' into misaka0508/main
Browse files Browse the repository at this point in the history
  • Loading branch information
qwqcode committed Sep 23, 2024
2 parents fa6c2ed + 0c964bd commit 2b6d322
Show file tree
Hide file tree
Showing 22 changed files with 846 additions and 137 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/dotnet-desktop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ jobs:
strategy:
fail-fast: false
matrix:
os: [windows-latest, ubuntu-latest, macos-14]
os: [windows-latest, ubuntu-latest, macos-latest]
include:
- os: windows-latest
build: win-x64
file_name: win_amd64
- os: ubuntu-latest
build: linux-x64
file_name: linux_amd64
- os: macos-14
- os: macos-latest
build: osx-arm64
file_name: macos_arm64

Expand Down
43 changes: 43 additions & 0 deletions .github/workflows/dotnet-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: Test

on:
push:
branches: ["main"]
paths:
- "SubRenamer/**"
- "SubRenamer.Tests/**"
pull_request:
branches: ["main"]
paths:
- "SubRenamer/**"
- "SubRenamer.Tests/**"
workflow_dispatch:

env:
TEST_RPOJECT: SubRenamer.Tests

jobs:
build:
name: .NET ${{ matrix.dotnet-version }} on ${{ matrix.os }}
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [windows-latest, ubuntu-latest, macos-latest]
dotnet-version: ["8.0.x"]

steps:
- uses: actions/checkout@v4

- name: Setup .NET ${{ matrix.dotnet-version }}
uses: actions/setup-dotnet@v4
with:
dotnet-version: ${{ matrix.dotnet-version }}

- name: Restore dependencies
run: dotnet restore ${{ env.TEST_RPOJECT }}

- name: Build
run: dotnet build --no-restore ${{ env.TEST_RPOJECT }}

- name: Test with dotnet
run: dotnet test ${{ env.TEST_RPOJECT }} --verbosity normal
17 changes: 17 additions & 0 deletions SubRenamer.Tests/GlobalSetup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace SubRenamer.Tests;

[SetUpFixture]
public class GlobalSetup
{
[OneTimeSetUp]
public void RunBeforeAnyTests()
{
Matcher.Logger.SetWriter(TestContext.Progress);
}

[OneTimeTearDown]
public void RunAfterAnyTests()
{
// ...
}
}
48 changes: 48 additions & 0 deletions SubRenamer.Tests/MatcherTests/CalculateFileKeysTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System.Reflection;

namespace SubRenamer.Tests.MatcherTests;

[TestFixture]
public class CalculateFileKeysTests
{
private static Dictionary<string, string> CalculateFileKeys(IReadOnlyList<string> files, string? regexPattern)
=> (Dictionary<string, string>)typeof(Matcher.Matcher)
.GetMethod("CalculateFileKeys", BindingFlags.NonPublic | BindingFlags.Static)!
.Invoke(null, new object[] { files, regexPattern! })!;

[Test]
public void UsesDiffAlgorithm_WithNullRegexPattern()
{
List<string> inputFiles = ["dir6/file1.txt", "dir7/file2.txt", "dir8/file3.txt"];
var output = new Dictionary<string, string>
{
{ "dir6/file1.txt", "1" },
{ "dir7/file2.txt", "2" },
{ "dir8/file3.txt", "3" }
};

Assert.That(CalculateFileKeys(inputFiles, regexPattern: null), Is.EqualTo(output));
}

[Test]
public void UsesRegexAlgorithm_WithRegexPattern()
{
List<string> inputFiles = ["dir4/A1.txt", "dir5/B2.txt", "dir6/C3.txt", "dir7/_4.txt"];
const string regexPattern = @"[A-Z](\d)\.txt";
var output = new Dictionary<string, string>
{
{ "dir4/A1.txt", "1" },
{ "dir5/B2.txt", "2" },
{ "dir6/C3.txt", "3" },
{ "dir7/_4.txt", "" }
};

Assert.That(CalculateFileKeys(inputFiles, regexPattern), Is.EqualTo(output));
}

[Test]
public void InputEmptyFileList_ReturnsEmptyDictionary()
{
Assert.That(CalculateFileKeys(files: [], regexPattern: null), Is.Empty);
}
}
62 changes: 62 additions & 0 deletions SubRenamer.Tests/MatcherTests/ExtractMatchKeyByDiffTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using SubRenamer.Matcher;
using static SubRenamer.Matcher.Diff;

namespace SubRenamer.Tests.MatcherTests;

[TestFixture]
public class ExtractMatchKeyByDiffTests
{
[Test]
public void Basic()
{
var diff = new DiffResult("file", "");
var input = "file123";
var key = "123";
Assert.That(ExtractMatchKeyByDiff(diff, input), Is.EqualTo(key));
}

[Test]
public void WithSuffix()
{
var diff = new DiffResult("file", "_end");
var input = "file456_end";
var key = "456";
Assert.That(ExtractMatchKeyByDiff(diff, input), Is.EqualTo(key));
}

[Test]
public void NoDiff_MatchNumeric()
{
DiffResult? diff = null;
var input = "example789";
var key = "789";
Assert.That(ExtractMatchKeyByDiff(diff, input), Is.EqualTo(key));
}

[Test]
public void NonDigit()
{
var diff = new DiffResult("file", "_end");
var input = "fileABC_end";
var key = "ABC";
Assert.That(ExtractMatchKeyByDiff(diff, input), Is.EqualTo(key));
}

[Test]
public void ComplexName()
{
var diff = new DiffResult("episode_", "_final");
var input = "episode_12_final";
var key = "12";
Assert.That(ExtractMatchKeyByDiff(diff, input), Is.EqualTo(key));
}

[Test]
public void NoMatch()
{
var diff = new DiffResult("file", "_end");
var input = "otherfile";
var key = "";
Assert.That(ExtractMatchKeyByDiff(diff, input), Is.EqualTo(key));
}
}
62 changes: 62 additions & 0 deletions SubRenamer.Tests/MatcherTests/FindCommonPrefixTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using System.Reflection;
using SubRenamer.Matcher;

namespace SubRenamer.Tests.MatcherTests;

[TestFixture]
public class FindCommonPrefixTests
{
private static string FindCommonPrefix(string a, string b)
=> (string)typeof(Diff)
.GetMethod("FindCommonPrefix", BindingFlags.NonPublic | BindingFlags.Static)!
.Invoke(null, new object[] { a, b })!;

[Test]
public void Basic()
{
Assert.That(FindCommonPrefix("file01", "file02"), Is.EqualTo("file"));
Assert.That(FindCommonPrefix("file[01]", "file[02]"), Is.EqualTo("file["));
Assert.That(FindCommonPrefix("file [ 01 ].mp4", "file [ 02 ].mp4"), Is.EqualTo("file [ "));
Assert.That(FindCommonPrefix("進撃の巨人 第 01 話", "進撃の巨人 第 02 話"), Is.EqualTo("進撃の巨人 第 "));
}

[Test]
public void NoCommon()
{
Assert.That(FindCommonPrefix("abc", "def"), Is.EqualTo(""));
}

[Test]
public void ExactMatch()
{
Assert.That(FindCommonPrefix("same", "same"), Is.EqualTo("same"));
}

[Test]
public void WithNumbers()
{
Assert.That(FindCommonPrefix("file123", "file456"), Is.EqualTo("file"));
}

[Test]
public void DifferentLengths()
{
Assert.That(FindCommonPrefix("file123", "file1"), Is.EqualTo("file"));
Assert.That(FindCommonPrefix("file123", "file6"), Is.EqualTo("file"));
}

[Test]
public void NoEndDigit()
{
Assert.That(FindCommonPrefix("file123", "file456"), Is.EqualTo("file"));
Assert.That(FindCommonPrefix("file12345", "file12345"), Is.EqualTo("file"));
Assert.That(FindCommonPrefix("進撃の巨人01", "進撃の巨人02"), Is.EqualTo("進撃の巨人"));
Assert.That(FindCommonPrefix("file S01E01", "file S01E02"), Is.EqualTo("file S01E"), "should keep S01E");
}

[Test]
public void KeepWhitespace()
{
Assert.That(FindCommonPrefix("file 01", "file 02"), Is.EqualTo("file "));
}
}
74 changes: 74 additions & 0 deletions SubRenamer.Tests/MatcherTests/FindCommonSuffixTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using System.Reflection;
using SubRenamer.Matcher;

namespace SubRenamer.Tests.MatcherTests;

[TestFixture]
public class FindCommonSuffixTests
{
private static string FindCommonSuffix(string a, string b)
=> (string)typeof(Diff)
.GetMethod("FindCommonSuffix", BindingFlags.NonPublic | BindingFlags.Static)!
.Invoke(null, new object[] { a, b })!;

[Test]
public void Basic()
{
Assert.That(FindCommonSuffix("01]", "02]"), Is.EqualTo("]"));
Assert.That(FindCommonSuffix("01]end", "02]end"), Is.EqualTo("]"));
Assert.That(FindCommonSuffix(" 01 ] end", " 02 ] end"), Is.EqualTo("]"));
}

[Test]
public void SkipMatchCharacter()
{
Assert.That(FindCommonSuffix("01A", "02A"), Is.EqualTo(""),
"should skip Digit and Uppercase Letter, then no match");
Assert.That(FindCommonSuffix("01A]", "02A]"), Is.EqualTo("]"),
"should skip Digit and Uppercase Letter, then match");

Assert.That(FindCommonSuffix("01a", "02a"), Is.EqualTo(""), "should skip Lowercase Letter, then no match");
Assert.That(FindCommonSuffix("01a]", "02a]"), Is.EqualTo("]"), "should skip Lowercase Letter, then match");

Assert.That(FindCommonSuffix("01 ", "02 "), Is.EqualTo(""), "should skip Whitespace, then no match");
Assert.That(FindCommonSuffix("01 ]", "02 ]"), Is.EqualTo("]"), "should skip Whitespace, then match");
}

[Test]
public void NoCommon()
{
Assert.That(FindCommonSuffix("01$", "02]"), Is.EqualTo(""));
Assert.That(FindCommonSuffix("01 abc", "02 def"), Is.EqualTo(""));
}

[Test]
public void ExactMatch()
{
Assert.That(FindCommonSuffix("]end", "]end"), Is.EqualTo("]"));
}

[Test]
public void WithMultipleSign()
{
Assert.That(FindCommonSuffix("01]_$*@end", "02]_$*@end"), Is.EqualTo("]"));
}

[Test]
public void DifferentLengths()
{
Assert.That(FindCommonSuffix("01] abc abc", "01]"), Is.EqualTo("]"));
Assert.That(FindCommonSuffix("01] abc abc", "1234567]"), Is.EqualTo("]"));
}

[Test]
public void MatchChineseCharacter()
{
// @see https://github.com/qwqcode/SubRenamer/pull/45
Assert.That(FindCommonSuffix("01話", "02話"), Is.EqualTo("話"));
Assert.That(FindCommonSuffix("01B 集", "02B 集"), Is.EqualTo("集"));

var result = FindCommonSuffix("01B 話 番外篇", "02B 話 番外篇");
Assert.That(result.Length, Is.EqualTo(1), "should match only single character");
Assert.That(result, Is.EqualTo("話"));
}
}
57 changes: 57 additions & 0 deletions SubRenamer.Tests/MatcherTests/GetDiffResultTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using SubRenamer.Matcher;
using static SubRenamer.Matcher.Diff;

namespace SubRenamer.Tests.MatcherTests;

[TestFixture]
public class GetDiffResultTests
{
[Test]
public void Basic()
{
var names = new List<string> { "file01_end", "file02_end", "file03_end" };
var result = GetDiffResult(names);
Assert.That(result, Is.EqualTo(new DiffResult("file", "_")));
}


[Test]
public void OnlyPrefix()
{
var names = new List<string> { "file01", "file02", "file03" };
var result = GetDiffResult(names);
Assert.That(result, Is.EqualTo(new DiffResult("file", "")));
}

[Test]
public void OnlySuffix()
{
var names = new List<string> { "01_end", "02_end", "03_end" };
var result = GetDiffResult(names);
Assert.That(result, Is.EqualTo(null));
}

[Test]
public void NoCommon()
{
var names = new List<string> { "fileA", "testB", "exampleC" };
var result = GetDiffResult(names);
Assert.That(result, Is.Null);
}

[Test]
public void SingleFile()
{
var names = new List<string> { "singlefile" };
var result = GetDiffResult(names);
Assert.That(result, Is.Null);
}

[Test]
public void DuplicateNames()
{
var names = new List<string> { "file01", "file01", "file02" };
var result = GetDiffResult(names);
Assert.That(result, Is.EqualTo(new DiffResult("file", "")));
}
}
Loading

0 comments on commit 2b6d322

Please sign in to comment.