Skip to content

Commit a171613

Browse files
Add processor tests
1 parent 89ce0fd commit a171613

File tree

5 files changed

+111
-12
lines changed

5 files changed

+111
-12
lines changed

src/ImageSharp.Web/FormattedImage.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public sealed class FormattedImage : IDisposable
2121
/// </summary>
2222
/// <param name="image">The image.</param>
2323
/// <param name="format">The format.</param>
24-
public FormattedImage(Image<Rgba32> image, IImageFormat format)
24+
internal FormattedImage(Image<Rgba32> image, IImageFormat format)
2525
{
2626
this.format = format;
2727
this.Image = image;

src/ImageSharp.Web/Processors/WebProcessingExtensions.cs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) Six Labors.
1+
// Copyright (c) Six Labors.
22
// Licensed under the Apache License, Version 2.0.
33

44
using System.Collections.Generic;
@@ -18,25 +18,22 @@ internal static class WebProcessingExtensions
1818
/// <param name="logger">The type used for performing logging.</param>
1919
/// <param name="processors">The collection of available processors.</param>
2020
/// <param name="commands">The parsed collection of processing commands.</param>
21-
/// <returns>The <see cref="Image{Rgba32}"/>.</returns>
22-
/// <remarks>Passing zero for one of height or width will automatically preserve the aspect ratio of the original image.</remarks>
21+
/// <returns>The <see cref="FormattedImage"/>.</returns>
2322
public static FormattedImage Process(
2423
this FormattedImage source,
2524
ILogger logger,
2625
IEnumerable<IImageWebProcessor> processors,
2726
IDictionary<string, string> commands)
2827
{
29-
if (commands.Count == 0)
28+
if (commands.Count != 0)
3029
{
31-
return source;
32-
}
33-
34-
foreach (IImageWebProcessor processor in processors)
35-
{
36-
source = processor.Process(source, logger, commands);
30+
foreach (IImageWebProcessor processor in processors)
31+
{
32+
source = processor.Process(source, logger, commands);
33+
}
3734
}
3835

3936
return source;
4037
}
4138
}
42-
}
39+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright (c) Six Labors.
2+
// Licensed under the Apache License, Version 2.0.
3+
4+
using System.Collections.Generic;
5+
using SixLabors.ImageSharp.Formats.Png;
6+
using SixLabors.ImageSharp.PixelFormats;
7+
using SixLabors.ImageSharp.Web.Processors;
8+
using Xunit;
9+
10+
namespace SixLabors.ImageSharp.Web.Tests.Processors
11+
{
12+
public class BackgroundColorWebProcessorTests
13+
{
14+
[Fact]
15+
public void BackgroundColorWebProcessor_UpdatesColor()
16+
{
17+
var commands = new Dictionary<string, string>
18+
{
19+
{ BackgroundColorWebProcessor.Color, nameof(Color.Orange) }
20+
};
21+
22+
using var image = new Image<Rgba32>(1, 1);
23+
Assert.True(Color.Transparent.Equals(image[0, 0]));
24+
25+
using var formatted = new FormattedImage(image, PngFormat.Instance);
26+
new BackgroundColorWebProcessor().Process(formatted, null, commands);
27+
28+
Assert.True(Color.Orange.Equals(image[0, 0]));
29+
}
30+
}
31+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright (c) Six Labors.
2+
// Licensed under the Apache License, Version 2.0.
3+
4+
using System.Collections.Generic;
5+
using Microsoft.Extensions.Options;
6+
using SixLabors.ImageSharp.Formats.Gif;
7+
using SixLabors.ImageSharp.Formats.Png;
8+
using SixLabors.ImageSharp.PixelFormats;
9+
using SixLabors.ImageSharp.Web.Middleware;
10+
using SixLabors.ImageSharp.Web.Processors;
11+
using Xunit;
12+
13+
namespace SixLabors.ImageSharp.Web.Tests.Processors
14+
{
15+
public class FormatWebProcessorTests
16+
{
17+
[Fact]
18+
public void FormatWebProcessor_UpdatesFormat()
19+
{
20+
var commands = new Dictionary<string, string>
21+
{
22+
{ FormatWebProcessor.Format, GifFormat.Instance.Name },
23+
};
24+
25+
using var image = new Image<Rgba32>(1, 1);
26+
using var formatted = new FormattedImage(image, PngFormat.Instance);
27+
Assert.Equal(formatted.Format, PngFormat.Instance);
28+
29+
new FormatWebProcessor(Options.Create(new ImageSharpMiddlewareOptions()))
30+
.Process(formatted, null, commands);
31+
32+
Assert.Equal(formatted.Format, GifFormat.Instance);
33+
}
34+
}
35+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright (c) Six Labors.
2+
// Licensed under the Apache License, Version 2.0.
3+
4+
using System.Collections.Generic;
5+
using SixLabors.ImageSharp.Formats.Png;
6+
using SixLabors.ImageSharp.PixelFormats;
7+
using SixLabors.ImageSharp.Processing;
8+
using SixLabors.ImageSharp.Web.Processors;
9+
using Xunit;
10+
11+
namespace SixLabors.ImageSharp.Web.Tests.Processors
12+
{
13+
public class ResizeWebProcessorTests
14+
{
15+
[Fact]
16+
public void ResizeWebProcessor_UpdatesSize()
17+
{
18+
const int Width = 4;
19+
const int Height = 6;
20+
var commands = new Dictionary<string, string>
21+
{
22+
{ ResizeWebProcessor.Sampler, nameof(KnownResamplers.NearestNeighbor) },
23+
{ ResizeWebProcessor.Width, Width.ToString() },
24+
{ ResizeWebProcessor.Height, Height.ToString() },
25+
{ ResizeWebProcessor.Xy, "0,0" }
26+
};
27+
28+
using var image = new Image<Rgba32>(1, 1);
29+
using var formatted = new FormattedImage(image, PngFormat.Instance);
30+
new ResizeWebProcessor().Process(formatted, null, commands);
31+
32+
Assert.Equal(Width, image.Width);
33+
Assert.Equal(Height, image.Height);
34+
}
35+
}
36+
}

0 commit comments

Comments
 (0)