Skip to content

Commit c864b9a

Browse files
Add tests
1 parent 657de61 commit c864b9a

4 files changed

Lines changed: 96 additions & 0 deletions

File tree

tests/ImageSharp.Web.Tests/Processors/BackgroundColorWebProcessorTests.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,23 @@ public void BackgroundColorWebProcessor_UpdatesColor()
3232

3333
Assert.True(Color.Orange.Equals(image[0, 0]));
3434
}
35+
36+
[Theory]
37+
[InlineData(nameof(Color.Orange))]
38+
[InlineData(nameof(Color.Black))]
39+
[InlineData(nameof(Color.Blue))]
40+
[InlineData(nameof(Color.RebeccaPurple))]
41+
public void BackgroundColorWebProcessor_CanReportAlphaRequirements(string color)
42+
{
43+
CommandParser parser = new(new[] { new ColorConverter() });
44+
CultureInfo culture = CultureInfo.InvariantCulture;
45+
46+
CommandCollection commands = new()
47+
{
48+
{ new(BackgroundColorWebProcessor.Color, color) }
49+
};
50+
51+
Assert.True(new BackgroundColorWebProcessor().RequiresAlphaComponent(commands, parser, culture));
52+
}
3553
}
3654
}

tests/ImageSharp.Web.Tests/Processors/FormatWebProcessorTests.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@
22
// Licensed under the Apache License, Version 2.0.
33

44
using System;
5+
using System.Collections.Generic;
56
using System.Globalization;
67
using Microsoft.Extensions.Options;
8+
using SixLabors.ImageSharp.Formats.Bmp;
79
using SixLabors.ImageSharp.Formats.Gif;
10+
using SixLabors.ImageSharp.Formats.Jpeg;
811
using SixLabors.ImageSharp.Formats.Png;
12+
using SixLabors.ImageSharp.Formats.Webp;
913
using SixLabors.ImageSharp.PixelFormats;
1014
using SixLabors.ImageSharp.Web.Commands;
1115
using SixLabors.ImageSharp.Web.Commands.Converters;
@@ -17,6 +21,16 @@ namespace SixLabors.ImageSharp.Web.Tests.Processors
1721
{
1822
public class FormatWebProcessorTests
1923
{
24+
public static TheoryData<string> FormatNameData { get; }
25+
= new TheoryData<string>
26+
{
27+
{ BmpFormat.Instance.Name },
28+
{ GifFormat.Instance.Name },
29+
{ PngFormat.Instance.Name },
30+
{ JpegFormat.Instance.Name },
31+
{ WebpFormat.Instance.Name },
32+
};
33+
2034
[Fact]
2135
public void FormatWebProcessor_UpdatesFormat()
2236
{
@@ -37,5 +51,21 @@ public void FormatWebProcessor_UpdatesFormat()
3751

3852
Assert.Equal(formatted.Format, GifFormat.Instance);
3953
}
54+
55+
[Theory]
56+
[MemberData(nameof(FormatNameData))]
57+
public void FormatWebProcessor_CanReportAlphaRequirements(string format)
58+
{
59+
CommandParser parser = new(Array.Empty<ICommandConverter>());
60+
CultureInfo culture = CultureInfo.InvariantCulture;
61+
62+
CommandCollection commands = new()
63+
{
64+
{ new(FormatWebProcessor.Format, format) },
65+
};
66+
67+
FormatWebProcessor processor = new(Options.Create(new ImageSharpMiddlewareOptions()));
68+
Assert.True(processor.RequiresAlphaComponent(commands, parser, culture));
69+
}
4070
}
4171
}

tests/ImageSharp.Web.Tests/Processors/QualityWebProcessorTests.cs

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

4+
using System;
45
using System.Collections.Generic;
56
using System.Globalization;
67
using SixLabors.ImageSharp.Formats.Jpeg;
@@ -15,6 +16,8 @@ namespace SixLabors.ImageSharp.Web.Tests.Processors
1516
{
1617
public class QualityWebProcessorTests
1718
{
19+
private readonly Random random = new();
20+
1821
[Fact]
1922
public void QualityWebProcessor_UpdatesJpegQuality()
2023
{
@@ -63,5 +66,24 @@ public void QualityWebProcessor_UpdatesWebpQuality()
6366
Assert.Equal(42, ((WebpEncoder)formatted.Encoder).Quality);
6467
Assert.Equal(WebpFileFormatType.Lossy, ((WebpEncoder)formatted.Encoder).FileFormat);
6568
}
69+
70+
[Fact]
71+
public void QualityWebProcessor_CanReportAlphaRequirements()
72+
{
73+
var converters = new List<ICommandConverter>
74+
{
75+
new IntegralNumberConverter<int>(),
76+
};
77+
78+
var parser = new CommandParser(converters);
79+
CultureInfo culture = CultureInfo.InvariantCulture;
80+
81+
CommandCollection commands = new()
82+
{
83+
{ new(QualityWebProcessor.Quality, this.random.Next(1, 100).ToString()) },
84+
};
85+
86+
Assert.False(new QualityWebProcessor().RequiresAlphaComponent(commands, parser, culture));
87+
}
6688
}
6789
}

tests/ImageSharp.Web.Tests/Processors/ResizeWebProcessorTests.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,32 @@ public void ResizeWebProcessor_CanIgnoreOrientation(ushort orientation)
261261
Assert.Equal(height, image.Height);
262262
}
263263

264+
[Theory]
265+
[InlineData(ResizeMode.Crop, false)]
266+
[InlineData(ResizeMode.Pad, true)]
267+
[InlineData(ResizeMode.BoxPad, true)]
268+
[InlineData(ResizeMode.Max, false)]
269+
[InlineData(ResizeMode.Min, false)]
270+
[InlineData(ResizeMode.Stretch, false)]
271+
[InlineData(ResizeMode.Manual, false)]
272+
public void ResizeWebProcessor_CanReportAlphaRequirements(ResizeMode resizeMode, bool requiresAlpha)
273+
{
274+
var converters = new List<ICommandConverter>
275+
{
276+
new EnumConverter(),
277+
};
278+
279+
var parser = new CommandParser(converters);
280+
CultureInfo culture = CultureInfo.InvariantCulture;
281+
282+
CommandCollection commands = new()
283+
{
284+
{ new(ResizeWebProcessor.Mode, resizeMode.ToString()) },
285+
};
286+
287+
Assert.Equal(requiresAlpha, new ResizeWebProcessor().RequiresAlphaComponent(commands, parser, culture));
288+
}
289+
264290
private static PointF GetExpectedCenter(ushort orientation, PointF center)
265291
{
266292
// New XY is calculated based on flipping and rotating the input XY.

0 commit comments

Comments
 (0)