Skip to content
Open
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions PCL.Core.Test/IO/DirectoriesTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using PCL.Core.IO;

namespace PCL.Core.Test.IO;

[TestClass]
public class DirectoriesTest
{
private string _tempDir = null!;

[TestInitialize]
public void SetUp()
{
_tempDir = Path.Combine(Path.GetTempPath(), "PCLCoreDirectoriesTest", Guid.NewGuid().ToString("N"));
Directory.CreateDirectory(_tempDir);
}

[TestCleanup]
public void TearDown()
{
if (Directory.Exists(_tempDir))
Directory.Delete(_tempDir, true);
}

[TestMethod]
public async Task CheckPermissionAsyncUsesRealProbeFile()
{
Assert.IsTrue(await Directories.CheckPermissionAsync(_tempDir));
Assert.AreEqual(0, Directory.EnumerateFiles(_tempDir, ".pcl-permission-*.tmp").Count());
}

[TestMethod]
public async Task CheckPermissionAsyncReturnsFalseForMissingDirectory()
{
var missing = Path.Combine(_tempDir, "missing");
Assert.IsFalse(await Directories.CheckPermissionAsync(missing));
}
}
16 changes: 16 additions & 0 deletions PCL.Core.Test/Utils/Base64UtilsTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using PCL.Core.Utils;

namespace PCL.Core.Test.Utils;

[TestClass]
public class Base64UtilsTest
{
[TestMethod]
public void EncodesAndDecodesUtf8Text()
{
const string text = "Plain Craft Launcher 测试";
var encoded = Base64Utils.EncodeString(text);
Assert.AreEqual(text, Base64Utils.DecodeToString(encoded));
}
}
30 changes: 30 additions & 0 deletions PCL.Core.Test/Utils/ConcurrentListTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using PCL.Core.Utils;

namespace PCL.Core.Test.Utils;

[TestClass]
public class ConcurrentListTest
{
[TestMethod]
public void EnumerationUsesSnapshot()
{
var list = new ConcurrentList<int>([1, 2]);
var snapshot = list.ToList();

list.Add(3);

CollectionAssert.AreEqual(new[] { 1, 2 }, snapshot);
CollectionAssert.AreEqual(new[] { 1, 2, 3 }, list.ToList());
}

[TestMethod]
public void RemoveAtUpdatesList()
{
var list = new ConcurrentList<string>(["a", "b", "c"]);

list.RemoveAt(1);

CollectionAssert.AreEqual(new[] { "a", "c" }, list.ToList());
}
}
17 changes: 17 additions & 0 deletions PCL.Core.Test/Utils/EnumerableTextExtensionsTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System.Collections;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using PCL.Core.Utils.Exts;

namespace PCL.Core.Test.Utils;

[TestClass]
public class EnumerableTextExtensionsTest
{
[TestMethod]
public void JoinSkipsNullElementsAndKeepsSeparators()
{
IEnumerable values = new object?[] { "a", null, "b" };

Assert.AreEqual("a||b", values.Join("|"));
}
}
48 changes: 48 additions & 0 deletions PCL.Core.Test/Utils/NumberUtilsTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using PCL.Core.Utils;

namespace PCL.Core.Test.Utils;

[TestClass]
public class NumberUtilsTest
{
[TestMethod]
[DataRow(-1d, 0)]
[DataRow(0d, 0)]
[DataRow(12.4d, 12)]
[DataRow(12.6d, 13)]
[DataRow(300d, 255)]
public void ClampToByte(double value, int expected)
{
Assert.AreEqual((byte)expected, NumberUtils.ClampToByte(value));
}

[TestMethod]
public void LerpRoundsToSixDigits()
{
Assert.AreEqual(0.333333d, NumberUtils.Lerp(0d, 1d, 1d / 3d));
}

[TestMethod]
[DataRow("12.5", 12.5d)]
[DataRow("123abc", 123d)]
[DataRow("1.20.4", 1.2d)]
[DataRow("1e3xxx", 1000d)]
[DataRow("not-a-number", 0d)]
[DataRow("&", 0d)]
[DataRow(null, 0d)]
public void ParseDoubleOrZeroKeepsLeadingNumberParsing(string? value, double expected)
{
Assert.AreEqual(expected, NumberUtils.ParseDoubleOrZero(value));
}

[TestMethod]
[DataRow(".5", 0.5d)]
[DataRow("-.5abc", -0.5d)]
[DataRow("+42px", 42d)]
[DataRow("1e", 1d)]
public void ParseLeadingDoubleOrZeroHandlesPartialLiterals(string value, double expected)
{
Assert.AreEqual(expected, NumberUtils.ParseLeadingDoubleOrZero(value));
}
}
25 changes: 25 additions & 0 deletions PCL.Core.Test/Utils/PathUtilsTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using PCL.Core.IO;

namespace PCL.Core.Test.Utils;

[TestClass]
public class PathUtilsTest
{
[TestMethod]
[DataRow("https://example.com/files/core.jar?download=1", "core.jar")]
[DataRow(@"C:\\Games\\Minecraft\\core.jar", "core.jar")]
[DataRow("/tmp/a/b/core.jar", "core.jar")]
public void GetsFileNameFromUrlOrPath(string path, string expected)
{
Assert.AreEqual(expected, PathUtils.GetFileNameFromUrlOrPath(path));
}

[TestMethod]
[DataRow(@"C:\\Games\\Minecraft\\", "Minecraft")]
[DataRow(@"D:\\", "D")]
public void GetsDirectoryLeaf(string path, string expected)
{
Assert.AreEqual(expected, PathUtils.GetDirectoryNameLeaf(path));
}
}
28 changes: 28 additions & 0 deletions PCL.Core.Test/Utils/ProcessRunnerTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using PCL.Core.Utils.OS;

namespace PCL.Core.Test.Utils;

[TestClass]
public class ProcessRunnerTest
{
public TestContext TestContext { get; set; }

[TestMethod]
[OSCondition(OperatingSystems.Windows)]
public void CapturesProcessOutput()
{
var result = ProcessRunner
.CaptureAsync(
"cmd.exe",
"/c echo hello",
5000,
cancellationToken: TestContext.CancellationToken)
.GetAwaiter()
.GetResult();

Assert.IsFalse(result.TimedOut);
Assert.AreEqual(0, result.ExitCode);
Assert.Contains("hello", result.CombinedOutput);
}
}
23 changes: 23 additions & 0 deletions PCL.Core.Test/Utils/RadixUtilsTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using PCL.Core.Utils;

namespace PCL.Core.Test.Utils;

[TestClass]
public class RadixUtilsTest
{
[TestMethod]
public void ConvertsLargeNumbersWithBigInteger()
{
const string input = "999999999999999999999999999999";
var encoded = RadixUtils.Convert(input, 10, 65);
var decoded = RadixUtils.Convert(encoded, 65, 10);
Assert.AreEqual(input, decoded);
}

[TestMethod]
public void ConvertsNegativeNumbers()
{
Assert.AreEqual("-11111111", RadixUtils.Convert("-255", 10, 2));
}
}
25 changes: 25 additions & 0 deletions PCL.Core.Test/Utils/RegexExtensionsTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using PCL.Core.Utils.Exts;

namespace PCL.Core.Test.Utils;

[TestClass]
public class RegexExtensionsTest
{
private static readonly string[] Expected = ["12", "34"];

[TestMethod]
public void FindsAndChecksMatches()
{
CollectionAssert.AreEqual(Expected, "a12b34".RegexSearch(@"\\d+"));
Assert.AreEqual("12", "a12b34".RegexSeek(@"\\d+"));
Assert.IsTrue("a12".RegexCheck(@"\\d+"));

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Use a real digit regex in the tests

These new regex tests double-escape \d inside a verbatim string, so the pattern searches for a literal \d instead of digits. With inputs like "a12b34", RegexSearch, RegexSeek, and RegexCheck therefore return no matches and the added test fails before validating the extension behavior; use @"\d+" here and in the replacement test below.

Useful? React with 👍 / 👎.

}

[TestMethod]
public void ReplacesEachMatch()
{
var replaced = "a12b34".RegexReplaceEach(@"\\d+", match => $"[{match.Value}]");
Assert.AreEqual("a[12]b[34]", replaced);
}
}
38 changes: 38 additions & 0 deletions PCL.Core.Test/Utils/TextUtilsTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using PCL.Core.Utils;
using PCL.Core.Utils.Exts;

namespace PCL.Core.Test.Utils;

[TestClass]
public class TextUtilsTest
{
[TestMethod]
public void LeftPadOrTrimKeepsLegacyBehavior()
{
Assert.AreEqual("00abc", TextUtils.LeftPadOrTrim("abc", "0", 5));
Assert.AreEqual("abc", TextUtils.LeftPadOrTrim("abcdef", "0", 3));
}

[TestMethod]
public void EscapesLikePattern()
{
Assert.AreEqual("a[*][?][#][[]b[]]", TextUtils.EscapeLikePattern("a*?#[b]"));
}

[TestMethod]
public void EscapesXamlAttributeTextWithMarkupExtensionPrefix()
{
Assert.AreEqual("{}{Binding Name}", TextUtils.EscapeXamlAttributeText("{Binding Name}"));
Assert.AreEqual("a&amp;b&quot;c", TextUtils.EscapeXamlAttributeText("a&b\"c"));
}

[TestMethod]
public void StringSliceExtensionsKeepUnmatchedText()
{
Assert.AreEqual("2026", "2026/06/30".BeforeFirst("/"));
Assert.AreEqual("06/30", "2026/06/30".AfterFirst("/"));
Assert.AreEqual("30", "2026/06/30".Between("/", "/"));
Assert.AreEqual("2026/06/30", "2026/06/30".BeforeFirst("#"));
}
}
Loading
Loading