Browse Source

Unit testing

Ruben 1 year ago
parent
commit
e912db2307

+ 0 - 1
src/PicView.Core/Navigation/FileHistory.cs

@@ -1,5 +1,4 @@
 using System.Diagnostics;
 using System.Diagnostics;
-using System.Runtime;
 
 
 namespace PicView.Core.Navigation;
 namespace PicView.Core.Navigation;
 
 

+ 0 - 1
src/PicView.Tests/AvaloniaTest.cs

@@ -1,7 +1,6 @@
 using Avalonia;
 using Avalonia;
 using Avalonia.Headless;
 using Avalonia.Headless;
 using Avalonia.Headless.XUnit;
 using Avalonia.Headless.XUnit;
-using Avalonia.Input;
 using Avalonia.Markup.Xaml.Styling;
 using Avalonia.Markup.Xaml.Styling;
 using Avalonia.ReactiveUI;
 using Avalonia.ReactiveUI;
 using Avalonia.Themes.Simple;
 using Avalonia.Themes.Simple;

+ 18 - 0
src/PicView.Tests/FileFunctionTest.cs

@@ -0,0 +1,18 @@
+using PicView.Core.FileHandling;
+
+namespace PicView.Tests;
+
+public class FileFunctionTest
+{
+    [Fact]
+    public void TestTemporaryFiles()
+    {
+        var path = Path.GetTempPath() + Path.GetRandomFileName();
+        var result = ArchiveHelper.CreateTempDirectory(path);
+        Assert.True(result);
+        Assert.True(Directory.Exists(ArchiveHelper.TempFilePath));
+        FileDeletionHelper.DeleteTempFiles();
+        Assert.False(Directory.Exists(path));
+        Assert.False(Directory.Exists(ArchiveHelper.TempFilePath));
+    }
+}

+ 74 - 0
src/PicView.Tests/FileHistoryTest.cs

@@ -0,0 +1,74 @@
+using PicView.Core.FileHandling;
+using PicView.Core.Navigation;
+
+namespace PicView.Tests;
+
+public class FileHistoryTest
+{
+    [Fact]
+    public void TestFileHistory()
+    {
+        var list = new List<string>();
+        var history = new FileHistory();
+        Assert.NotNull(history);
+
+        // Check adding
+        for (var i = 0; i <= FileHistory.MaxCount; i++)
+        {
+            AddRandomFiles(history, list);
+        }
+        Assert.Equal(FileHistory.MaxCount, history.GetCount());
+        AddRandomFiles(history, list);
+        Assert.Equal(FileHistory.MaxCount, history.GetCount());
+
+        // Check removing
+        history.Remove(history.GetLastFile());
+        Assert.Equal(FileHistory.MaxCount - 1, history.GetCount());
+
+        // Check renaming
+        var lastFile = history.GetLastFile();
+        var newFile = Path.GetFileNameWithoutExtension(lastFile);
+        newFile = Path.GetRandomFileName();
+        history.Rename(lastFile, newFile);
+        Assert.Equal(newFile, history.GetLastFile());
+
+        // Check removing
+        history.Remove(newFile);
+        Assert.False(history.Contains(newFile));
+
+        // Check getting iterations
+        var entry = history.GetEntryAt(1);
+        Assert.NotNull(entry);
+
+        var nextEntry = history.GetNextEntry(looping: true, 2, list);
+        Assert.NotNull(nextEntry);
+        Assert.True(File.Exists(nextEntry));
+
+        var prevEntry = history.GetNextEntry(looping: false, 2, list);
+        Assert.NotNull(prevEntry);
+        Assert.True(File.Exists(prevEntry));
+
+        foreach (var t in list)
+        {
+            FileDeletionHelper.DeleteFileWithErrorMsg(t, false);
+            Assert.False(File.Exists(t));
+            history.Remove(t);
+        }
+        Assert.Equal(0, history.GetCount());
+    }
+
+    private static void AddRandomFiles(FileHistory history, List<string> list)
+    {
+        var imageFileExtensionArray = new[] { ".jpg", ".png", ".bmp", ".gif", ".tiff", ".webp" };
+        var path = Path.GetTempPath() + Path.GetRandomFileName();
+        var directory = ArchiveHelper.CreateTempDirectory(path);
+        Assert.True(directory);
+        var randomExtension = imageFileExtensionArray[new Random().Next(0, imageFileExtensionArray.Length)];
+        var randomFileNameWithExtension = path + randomExtension;
+        var fullPath = Path.Combine(ArchiveHelper.TempFilePath, randomFileNameWithExtension);
+        using var fs = File.Create(fullPath);
+        Assert.True(File.Exists(fullPath));
+        history.Add(randomFileNameWithExtension);
+        list.Add(randomFileNameWithExtension);
+    }
+}