From d48ad769fe668de9a76d7f7e5e295f50e6857290 Mon Sep 17 00:00:00 2001 From: Simon Vonhoff Date: Sat, 17 May 2025 14:15:29 +0200 Subject: [PATCH] Fixed extension bug --- .../Workflows/MainWorkflowTests.cs | 102 ++++++++++++++++++ Tilemap2Animation/Workflows/MainWorkflow.cs | 17 ++- 2 files changed, 116 insertions(+), 3 deletions(-) diff --git a/Tilemap2Animation.Test/Workflows/MainWorkflowTests.cs b/Tilemap2Animation.Test/Workflows/MainWorkflowTests.cs index 679bb8b..c8689ae 100644 --- a/Tilemap2Animation.Test/Workflows/MainWorkflowTests.cs +++ b/Tilemap2Animation.Test/Workflows/MainWorkflowTests.cs @@ -491,4 +491,106 @@ public async Task ExecuteAsync_WithNoValidTilesets_ThrowsException() // Updated error message to match actual implementation Assert.Contains("No tilesets could be loaded for the conversion", exception.Message); } + + [Fact] + public async Task ExecuteAsync_WithMultipleExtensions_ProcessesCorrectly() + { + // Arrange + var testFilePath = Path.Combine(Path.GetTempPath(), "test.gif.tmx"); + var outputPath = Path.Combine(Path.GetTempPath(), "output.gif"); + + var options = new MainWorkflowOptions + { + InputFile = testFilePath, + OutputFile = outputPath, + FrameDelay = 100 + }; + + var tilemap = new Tilemap + { + Width = 10, + Height = 10, + TileWidth = 16, + TileHeight = 16, + Layers = new List + { + new TilemapLayer + { + Name = "Layer1", + Data = new TilemapLayerData { Encoding = "csv", Text = "1,2,3,4" } + } + }, + Tilesets = new List + { + new TilemapTileset { FirstGid = 1, Source = "test.tsx" } + } + }; + + var tileset = new Tileset + { + Image = new TilesetImage { Path = "test.png" } + }; + + var layerData = new List { 1, 2, 3, 4 }; + var tilesetImage = new Image(32, 32); + var frames = new List> { new Image(16, 16) }; + var delays = new List { 100 }; + + _tilemapServiceMock.Setup(x => x.DeserializeTmxAsync(testFilePath)).ReturnsAsync(tilemap); + _tilemapServiceMock.Setup(x => x.ParseLayerData(It.IsAny())).Returns(layerData); + _tilesetServiceMock.Setup(x => x.DeserializeTsxAsync(It.IsAny())).ReturnsAsync(tileset); + _tilesetServiceMock.Setup(x => x.ResolveTilesetImagePath(It.IsAny(), It.IsAny())).Returns("test.png"); + _tilesetImageServiceMock.Setup(x => x.LoadTilesetImageAsync(It.IsAny())).ReturnsAsync(tilesetImage); + _tilesetImageServiceMock.Setup(x => x.ProcessTransparency(It.IsAny>(), It.IsAny())).Returns(tilesetImage); + _animationGeneratorServiceMock.Setup(x => x.GenerateAnimationFramesFromMultipleTilesetsAsync( + It.IsAny(), + It.IsAny? TilesetImage)>>(), + It.IsAny>>(), + It.IsAny())) + .ReturnsAsync((frames, delays)); + + try + { + // Act + await _sut.ExecuteAsync(options); + + // Assert + _tilemapServiceMock.Verify(x => x.DeserializeTmxAsync(testFilePath), Times.Once); + _animationEncoderServiceMock.Verify(x => x.SaveAsGifAsync( + It.IsAny>>(), + It.IsAny>(), + It.Is(s => s == outputPath)), + Times.Once); + } + finally + { + tilesetImage.Dispose(); + foreach (var frame in frames) + { + frame.Dispose(); + } + } + } + + [Fact] + public async Task ExecuteAsync_WithUnsupportedExtension_ThrowsArgumentException() + { + // Arrange + var testFilePath = Path.Combine(Path.GetTempPath(), "test.xyz"); + var outputPath = Path.Combine(Path.GetTempPath(), "output.gif"); + + var options = new MainWorkflowOptions + { + InputFile = testFilePath, + OutputFile = outputPath, + FrameDelay = 100 + }; + + // Act & Assert + var exception = await Assert.ThrowsAsync(() => _sut.ExecuteAsync(options)); + Assert.Contains("Unsupported input file type", exception.Message); + Assert.Contains(".tmx", exception.Message); + Assert.Contains(".tsx", exception.Message); + Assert.Contains(".png", exception.Message); + } } \ No newline at end of file diff --git a/Tilemap2Animation/Workflows/MainWorkflow.cs b/Tilemap2Animation/Workflows/MainWorkflow.cs index 89602ae..f23733b 100644 --- a/Tilemap2Animation/Workflows/MainWorkflow.cs +++ b/Tilemap2Animation/Workflows/MainWorkflow.cs @@ -41,12 +41,25 @@ public async Task ExecuteAsync(MainWorkflowOptions options) var inputFile = Path.GetFullPath(options.InputFile); var extension = Path.GetExtension(inputFile).ToLowerInvariant(); + var fileName = Path.GetFileName(inputFile); + var supportedExtensions = new[] { ".tmx", ".tsx", ".png", ".jpg", ".jpeg", ".bmp" }; string? tmxInputFile = null; string? tsxInputFile = null; string? imageInputFile = null; - switch (extension) + // Find the last supported extension in the filename + var lastSupportedExtension = supportedExtensions + .Where(ext => fileName.EndsWith(ext, StringComparison.OrdinalIgnoreCase)) + .OrderByDescending(ext => fileName.LastIndexOf(ext, StringComparison.OrdinalIgnoreCase)) + .FirstOrDefault(); + + if (lastSupportedExtension == null) + { + throw new ArgumentException($"Unsupported input file type. File must end with one of: {string.Join(", ", supportedExtensions)}"); + } + + switch (lastSupportedExtension) { case ".tmx": tmxInputFile = inputFile; @@ -60,8 +73,6 @@ public async Task ExecuteAsync(MainWorkflowOptions options) case ".bmp": imageInputFile = inputFile; break; - default: - throw new ArgumentException($"Unsupported input file type: {extension}"); } Tilemap? tilemap = null;