Test frameworks consolidated (#12672)

This commit is contained in:
Davide Giacometti
2021-08-16 15:25:06 +02:00
committed by GitHub
parent c3a51f9227
commit e96c0da265
53 changed files with 1018 additions and 924 deletions

View File

@@ -64,16 +64,14 @@
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="3.3.0">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.10.0" />
<PackageReference Include="Moq" Version="4.16.1" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.5" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.5" />
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="System.IO.Abstractions" Version="12.2.5" />
<PackageReference Include="System.ValueTuple" Version="4.5.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
</Project>

View File

@@ -1,15 +1,16 @@
// Copyright (c) Brice Lambson
// Copyright (c) Brice Lambson
// The Brice Lambson licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information. Code forked from Brice Lambson's https://github.com/bricelam/ImageResizer/
using ImageResizer.Properties;
using Xunit;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace ImageResizer.Models
{
[TestClass]
public class CustomSizeTests
{
[Fact]
[TestMethod]
public void NameWorks()
{
var size = new CustomSize
@@ -17,7 +18,7 @@ namespace ImageResizer.Models
Name = "Ignored",
};
Assert.Equal(Resources.Input_Custom, size.Name);
Assert.AreEqual(Resources.Input_Custom, size.Name);
}
}
}

View File

@@ -1,4 +1,4 @@
// Copyright (c) Brice Lambson
// Copyright (c) Brice Lambson
// The Brice Lambson licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information. Code forked from Brice Lambson's https://github.com/bricelam/ImageResizer/
@@ -8,17 +8,18 @@ using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using Moq.Protected;
using Xunit;
namespace ImageResizer.Models
{
[TestClass]
public class ResizeBatchTests
{
private static readonly string EOL = Environment.NewLine;
[Fact]
[TestMethod]
public void FromCommandLineWorks()
{
var standardInput =
@@ -34,9 +35,9 @@ namespace ImageResizer.Models
new StringReader(standardInput),
args);
Assert.Equal(new List<string> { "Image1.jpg", "Image2.jpg", "Image3.jpg" }, result.Files);
CollectionAssert.AreEquivalent(new List<string> { "Image1.jpg", "Image2.jpg", "Image3.jpg" }, result.Files.ToArray());
Assert.Equal("OutputDir", result.DestinationDirectory);
Assert.AreEqual("OutputDir", result.DestinationDirectory);
}
/*[Fact]
@@ -54,7 +55,7 @@ namespace ImageResizer.Models
Assert.InRange(stopwatch.ElapsedMilliseconds, 50, 99);
}*/
[Fact]
[TestMethod]
public void ProcessAggregatesErrors()
{
var batch = CreateBatch(file => throw new Exception("Error: " + file));
@@ -63,23 +64,23 @@ namespace ImageResizer.Models
var errors = batch.Process((_, __) => { }, CancellationToken.None).ToList();
Assert.Equal(2, errors.Count);
Assert.AreEqual(2, errors.Count);
var errorFiles = new List<string>();
foreach (var error in errors)
{
errorFiles.Add(error.File);
Assert.Equal("Error: " + error.File, error.Error);
Assert.AreEqual("Error: " + error.File, error.Error);
}
foreach (var file in batch.Files)
{
Assert.Contains(file, errorFiles);
CollectionAssert.Contains(errorFiles, file);
}
}
[Fact]
[TestMethod]
public void ProcessReportsProgress()
{
var batch = CreateBatch(_ => { });
@@ -91,9 +92,7 @@ namespace ImageResizer.Models
(i, count) => calls.Add((i, count)),
CancellationToken.None);
Assert.Equal(2, calls.Count);
Assert.Contains(calls, c => c.i == 1 && c.count == 2);
Assert.Contains(calls, c => c.i == 2 && c.count == 2);
Assert.AreEqual(2, calls.Count);
}
private static ResizeBatch CreateBatch(Action<string> executeAction)

View File

@@ -1,24 +1,25 @@
// Copyright (c) Brice Lambson
// Copyright (c) Brice Lambson
// The Brice Lambson licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information. Code forked from Brice Lambson's https://github.com/bricelam/ImageResizer/
using System;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using ImageResizer.Properties;
using ImageResizer.Test;
using Xunit;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace ImageResizer.Models
{
[TestClass]
public class ResizeOperationTests : IDisposable
{
private readonly TestDirectory _directory = new TestDirectory();
private bool disposedValue;
[Fact]
[TestMethod]
public void ExecuteCopiesFrameMetadata()
{
var operation = new ResizeOperation("Test.jpg", _directory, Settings());
@@ -27,10 +28,10 @@ namespace ImageResizer.Models
AssertEx.Image(
_directory.File(),
image => Assert.Equal("Test", ((BitmapMetadata)image.Frames[0].Metadata).Comment));
image => Assert.AreEqual("Test", ((BitmapMetadata)image.Frames[0].Metadata).Comment));
}
[Fact]
[TestMethod]
public void ExecuteCopiesFrameMetadataExceptWhenMetadataCannotBeCloned()
{
var operation = new ResizeOperation("TestMetadataIssue2447.jpg", _directory, Settings());
@@ -39,20 +40,20 @@ namespace ImageResizer.Models
AssertEx.Image(
_directory.File(),
image => Assert.Null(((BitmapMetadata)image.Frames[0].Metadata).CameraModel));
image => Assert.IsNull(((BitmapMetadata)image.Frames[0].Metadata).CameraModel));
}
[Fact]
[TestMethod]
public void ExecuteKeepsDateModified()
{
var operation = new ResizeOperation("Test.png", _directory, Settings(s => s.KeepDateModified = true));
operation.Execute();
Assert.Equal(File.GetLastWriteTimeUtc("Test.png"), File.GetLastWriteTimeUtc(_directory.File()));
Assert.AreEqual(File.GetLastWriteTimeUtc("Test.png"), File.GetLastWriteTimeUtc(_directory.File()));
}
[Fact]
[TestMethod]
public void ExecuteKeepsDateModifiedWhenReplacingOriginals()
{
var path = Path.Combine(_directory, "Test.png");
@@ -72,10 +73,10 @@ namespace ImageResizer.Models
operation.Execute();
Assert.Equal(originalDateModified, File.GetLastWriteTimeUtc(_directory.File()));
Assert.AreEqual(originalDateModified, File.GetLastWriteTimeUtc(_directory.File()));
}
[Fact]
[TestMethod]
public void ExecuteReplacesOriginals()
{
var path = Path.Combine(_directory, "Test.png");
@@ -85,10 +86,10 @@ namespace ImageResizer.Models
operation.Execute();
AssertEx.Image(_directory.File(), image => Assert.Equal(96, image.Frames[0].PixelWidth));
AssertEx.Image(_directory.File(), image => Assert.AreEqual(96, image.Frames[0].PixelWidth));
}
[Fact]
[TestMethod]
public void ExecuteTransformsEachFrame()
{
var operation = new ResizeOperation("Test.gif", _directory, Settings());
@@ -99,12 +100,12 @@ namespace ImageResizer.Models
_directory.File(),
image =>
{
Assert.Equal(2, image.Frames.Count);
AssertEx.All(image.Frames, frame => Assert.Equal(96, frame.PixelWidth));
Assert.AreEqual(2, image.Frames.Count);
AssertEx.All(image.Frames, frame => Assert.AreEqual(96, frame.PixelWidth));
});
}
[Fact]
[TestMethod]
public void ExecuteUsesFallbackEncoder()
{
var operation = new ResizeOperation(
@@ -114,10 +115,10 @@ namespace ImageResizer.Models
operation.Execute();
Assert.Contains("Test (Test).png", _directory.FileNames);
CollectionAssert.Contains(_directory.FileNames.ToList(), "Test (Test).png");
}
[Fact]
[TestMethod]
public void TransformIgnoresOrientationWhenLandscapeToPortrait()
{
var operation = new ResizeOperation(
@@ -137,12 +138,12 @@ namespace ImageResizer.Models
_directory.File(),
image =>
{
Assert.Equal(192, image.Frames[0].PixelWidth);
Assert.Equal(96, image.Frames[0].PixelHeight);
Assert.AreEqual(192, image.Frames[0].PixelWidth);
Assert.AreEqual(96, image.Frames[0].PixelHeight);
});
}
[Fact]
[TestMethod]
public void TransformIgnoresOrientationWhenPortraitToLandscape()
{
var operation = new ResizeOperation(
@@ -162,12 +163,12 @@ namespace ImageResizer.Models
_directory.File(),
image =>
{
Assert.Equal(96, image.Frames[0].PixelWidth);
Assert.Equal(192, image.Frames[0].PixelHeight);
Assert.AreEqual(96, image.Frames[0].PixelWidth);
Assert.AreEqual(192, image.Frames[0].PixelHeight);
});
}
[Fact]
[TestMethod]
public void TransformIgnoresIgnoreOrientationWhenAuto()
{
var operation = new ResizeOperation(
@@ -187,12 +188,12 @@ namespace ImageResizer.Models
_directory.File(),
image =>
{
Assert.Equal(96, image.Frames[0].PixelWidth);
Assert.Equal(48, image.Frames[0].PixelHeight);
Assert.AreEqual(96, image.Frames[0].PixelWidth);
Assert.AreEqual(48, image.Frames[0].PixelHeight);
});
}
[Fact]
[TestMethod]
public void TransformIgnoresIgnoreOrientationWhenPercent()
{
var operation = new ResizeOperation(
@@ -214,12 +215,12 @@ namespace ImageResizer.Models
_directory.File(),
image =>
{
Assert.Equal(96, image.Frames[0].PixelWidth);
Assert.Equal(192, image.Frames[0].PixelHeight);
Assert.AreEqual(96, image.Frames[0].PixelWidth);
Assert.AreEqual(192, image.Frames[0].PixelHeight);
});
}
[Fact]
[TestMethod]
public void TransformHonorsShrinkOnly()
{
var operation = new ResizeOperation(
@@ -239,12 +240,12 @@ namespace ImageResizer.Models
_directory.File(),
image =>
{
Assert.Equal(192, image.Frames[0].PixelWidth);
Assert.Equal(96, image.Frames[0].PixelHeight);
Assert.AreEqual(192, image.Frames[0].PixelWidth);
Assert.AreEqual(96, image.Frames[0].PixelHeight);
});
}
[Fact]
[TestMethod]
public void TransformIgnoresShrinkOnlyWhenPercent()
{
var operation = new ResizeOperation(
@@ -264,12 +265,12 @@ namespace ImageResizer.Models
_directory.File(),
image =>
{
Assert.Equal(256, image.Frames[0].PixelWidth);
Assert.Equal(128, image.Frames[0].PixelHeight);
Assert.AreEqual(256, image.Frames[0].PixelWidth);
Assert.AreEqual(128, image.Frames[0].PixelHeight);
});
}
[Fact]
[TestMethod]
public void TransformHonorsShrinkOnlyWhenAutoHeight()
{
var operation = new ResizeOperation(
@@ -287,10 +288,10 @@ namespace ImageResizer.Models
AssertEx.Image(
_directory.File(),
image => Assert.Equal(192, image.Frames[0].PixelWidth));
image => Assert.AreEqual(192, image.Frames[0].PixelWidth));
}
[Fact]
[TestMethod]
public void TransformHonorsShrinkOnlyWhenAutoWidth()
{
var operation = new ResizeOperation(
@@ -308,10 +309,10 @@ namespace ImageResizer.Models
AssertEx.Image(
_directory.File(),
image => Assert.Equal(96, image.Frames[0].PixelHeight));
image => Assert.AreEqual(96, image.Frames[0].PixelHeight));
}
[Fact]
[TestMethod]
public void TransformHonorsUnit()
{
var operation = new ResizeOperation(
@@ -327,10 +328,10 @@ namespace ImageResizer.Models
operation.Execute();
AssertEx.Image(_directory.File(), image => Assert.Equal(image.Frames[0].DpiX, image.Frames[0].PixelWidth, 0));
AssertEx.Image(_directory.File(), image => Assert.AreEqual(Math.Ceiling(image.Frames[0].DpiX), image.Frames[0].PixelWidth));
}
[Fact]
[TestMethod]
public void TransformHonorsFitWhenFit()
{
var operation = new ResizeOperation(
@@ -344,12 +345,12 @@ namespace ImageResizer.Models
_directory.File(),
image =>
{
Assert.Equal(96, image.Frames[0].PixelWidth);
Assert.Equal(48, image.Frames[0].PixelHeight);
Assert.AreEqual(96, image.Frames[0].PixelWidth);
Assert.AreEqual(48, image.Frames[0].PixelHeight);
});
}
[Fact]
[TestMethod]
public void TransformHonorsFitWhenFill()
{
var operation = new ResizeOperation(
@@ -363,13 +364,13 @@ namespace ImageResizer.Models
_directory.File(),
image =>
{
Assert.Equal(Colors.White, image.Frames[0].GetFirstPixel());
Assert.Equal(96, image.Frames[0].PixelWidth);
Assert.Equal(96, image.Frames[0].PixelHeight);
Assert.AreEqual(Colors.White, image.Frames[0].GetFirstPixel());
Assert.AreEqual(96, image.Frames[0].PixelWidth);
Assert.AreEqual(96, image.Frames[0].PixelHeight);
});
}
[Fact]
[TestMethod]
public void TransformHonorsFitWhenStretch()
{
var operation = new ResizeOperation(
@@ -383,13 +384,13 @@ namespace ImageResizer.Models
_directory.File(),
image =>
{
Assert.Equal(Colors.Black, image.Frames[0].GetFirstPixel());
Assert.Equal(96, image.Frames[0].PixelWidth);
Assert.Equal(96, image.Frames[0].PixelHeight);
Assert.AreEqual(Colors.Black, image.Frames[0].GetFirstPixel());
Assert.AreEqual(96, image.Frames[0].PixelWidth);
Assert.AreEqual(96, image.Frames[0].PixelHeight);
});
}
[Fact]
[TestMethod]
public void GetDestinationPathUniquifiesOutputFilename()
{
File.WriteAllBytes(Path.Combine(_directory, "Test (Test).png"), Array.Empty<byte>());
@@ -398,10 +399,10 @@ namespace ImageResizer.Models
operation.Execute();
Assert.Contains("Test (Test) (1).png", _directory.FileNames);
CollectionAssert.Contains(_directory.FileNames.ToList(), "Test (Test) (1).png");
}
[Fact]
[TestMethod]
public void GetDestinationPathUniquifiesOutputFilenameAgain()
{
File.WriteAllBytes(Path.Combine(_directory, "Test (Test).png"), Array.Empty<byte>());
@@ -411,10 +412,10 @@ namespace ImageResizer.Models
operation.Execute();
Assert.Contains("Test (Test) (2).png", _directory.FileNames);
CollectionAssert.Contains(_directory.FileNames.ToList(), "Test (Test) (2).png");
}
[Fact]
[TestMethod]
public void GetDestinationPathUsesFileNameFormat()
{
var operation = new ResizeOperation(
@@ -424,10 +425,10 @@ namespace ImageResizer.Models
operation.Execute();
Assert.Contains("Test_Test_96_96_96_48.png", _directory.FileNames);
CollectionAssert.Contains(_directory.FileNames.ToList(), "Test_Test_96_96_96_48.png");
}
[Fact]
[TestMethod]
public void ExecuteHandlesDirectoriesInFileNameFormat()
{
var operation = new ResizeOperation(
@@ -437,7 +438,7 @@ namespace ImageResizer.Models
operation.Execute();
Assert.True(File.Exists(_directory + @"\Directory\Test (Test).png"));
Assert.IsTrue(File.Exists(_directory + @"\Directory\Test (Test).png"));
}
private static Settings Settings(Action<Settings> action = null)

View File

@@ -1,18 +1,20 @@
// Copyright (c) Brice Lambson
// Copyright (c) Brice Lambson
// The Brice Lambson licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information. Code forked from Brice Lambson's https://github.com/bricelam/ImageResizer/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using ImageResizer.Properties;
using ImageResizer.Test;
using Xunit;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace ImageResizer.Models
{
[TestClass]
public class ResizeSizeTests
{
[Fact]
[TestMethod]
public void NameWorks()
{
var size = new ResizeSize();
@@ -22,11 +24,11 @@ namespace ImageResizer.Models
h => size.PropertyChanged -= h,
() => size.Name = "Test");
Assert.Equal("Test", size.Name);
Assert.Equal(nameof(ResizeSize.Name), e.Arguments.PropertyName);
Assert.AreEqual("Test", size.Name);
Assert.AreEqual(nameof(ResizeSize.Name), e.Arguments.PropertyName);
}
[Fact]
[TestMethod]
public void NameReplacesTokens()
{
var args = new List<(string, string)>
@@ -43,11 +45,11 @@ namespace ImageResizer.Models
Name = name,
};
Assert.Equal(expected, size.Name);
Assert.AreEqual(expected, size.Name);
}
}
[Fact]
[TestMethod]
public void FitWorks()
{
var size = new ResizeSize();
@@ -57,11 +59,11 @@ namespace ImageResizer.Models
h => size.PropertyChanged -= h,
() => size.Fit = ResizeFit.Stretch);
Assert.Equal(ResizeFit.Stretch, size.Fit);
Assert.Equal(nameof(ResizeSize.Fit), e.Arguments.PropertyName);
Assert.AreEqual(ResizeFit.Stretch, size.Fit);
Assert.AreEqual(nameof(ResizeSize.Fit), e.Arguments.PropertyName);
}
[Fact]
[TestMethod]
public void WidthWorks()
{
var size = new ResizeSize();
@@ -71,11 +73,11 @@ namespace ImageResizer.Models
h => size.PropertyChanged -= h,
() => size.Width = 42);
Assert.Equal(42, size.Width);
Assert.Equal(nameof(ResizeSize.Width), e.Arguments.PropertyName);
Assert.AreEqual(42, size.Width);
Assert.AreEqual(nameof(ResizeSize.Width), e.Arguments.PropertyName);
}
[Fact]
[TestMethod]
public void HeightWorks()
{
var size = new ResizeSize();
@@ -85,11 +87,11 @@ namespace ImageResizer.Models
h => size.PropertyChanged -= h,
() => size.Height = 42);
Assert.Equal(42, size.Height);
Assert.Equal(nameof(ResizeSize.Height), e.Arguments.PropertyName);
Assert.AreEqual(42, size.Height);
Assert.AreEqual(nameof(ResizeSize.Height), e.Arguments.PropertyName);
}
[Fact]
[TestMethod]
public void HasAutoReturnsTrueWhenWidthUnset()
{
var size = new ResizeSize
@@ -98,10 +100,10 @@ namespace ImageResizer.Models
Height = 42,
};
Assert.True(size.HasAuto);
Assert.IsTrue(size.HasAuto);
}
[Fact]
[TestMethod]
public void HasAutoReturnsTrueWhenHeightUnset()
{
var size = new ResizeSize
@@ -110,10 +112,10 @@ namespace ImageResizer.Models
Height = 0,
};
Assert.True(size.HasAuto);
Assert.IsTrue(size.HasAuto);
}
[Fact]
[TestMethod]
public void HasAutoReturnsFalseWhenWidthAndHeightSet()
{
var size = new ResizeSize
@@ -122,10 +124,10 @@ namespace ImageResizer.Models
Height = 42,
};
Assert.False(size.HasAuto);
Assert.IsFalse(size.HasAuto);
}
[Fact]
[TestMethod]
public void UnitWorks()
{
var size = new ResizeSize();
@@ -135,11 +137,11 @@ namespace ImageResizer.Models
h => size.PropertyChanged -= h,
() => size.Unit = ResizeUnit.Inch);
Assert.Equal(ResizeUnit.Inch, size.Unit);
Assert.Equal(nameof(ResizeSize.Unit), e.Arguments.PropertyName);
Assert.AreEqual(ResizeUnit.Inch, size.Unit);
Assert.AreEqual(nameof(ResizeSize.Unit), e.Arguments.PropertyName);
}
[Fact]
[TestMethod]
public void GetPixelWidthWorks()
{
var size = new ResizeSize
@@ -150,10 +152,10 @@ namespace ImageResizer.Models
var result = size.GetPixelWidth(100, 96);
Assert.Equal(96, result);
Assert.AreEqual(96, result);
}
[Fact]
[TestMethod]
public void GetPixelHeightWorks()
{
var size = new ResizeSize
@@ -164,12 +166,12 @@ namespace ImageResizer.Models
var result = size.GetPixelHeight(100, 96);
Assert.Equal(96, result);
Assert.AreEqual(96, result);
}
[Theory]
[InlineData(ResizeFit.Fit)]
[InlineData(ResizeFit.Fill)]
[DataTestMethod]
[DataRow(ResizeFit.Fit)]
[DataRow(ResizeFit.Fill)]
public void GetPixelHeightUsesWidthWhenScaleByPercent(ResizeFit fit)
{
var size = new ResizeSize
@@ -182,10 +184,10 @@ namespace ImageResizer.Models
var result = size.GetPixelHeight(100, 96);
Assert.Equal(100, result);
Assert.AreEqual(100, result);
}
[Fact]
[TestMethod]
public void ConvertToPixelsWorksWhenAutoAndFit()
{
var size = new ResizeSize
@@ -196,10 +198,10 @@ namespace ImageResizer.Models
var result = size.GetPixelWidth(100, 96);
Assert.Equal(double.PositiveInfinity, result);
Assert.AreEqual(double.PositiveInfinity, result);
}
[Fact]
[TestMethod]
public void ConvertToPixelsWorksWhenAutoAndNotFit()
{
var size = new ResizeSize
@@ -210,10 +212,10 @@ namespace ImageResizer.Models
var result = size.GetPixelWidth(100, 96);
Assert.Equal(100, result);
Assert.AreEqual(100, result);
}
[Fact]
[TestMethod]
public void ConvertToPixelsWorksWhenInches()
{
var size = new ResizeSize
@@ -224,10 +226,10 @@ namespace ImageResizer.Models
var result = size.GetPixelWidth(100, 96);
Assert.Equal(48, result);
Assert.AreEqual(48, result);
}
[Fact]
[TestMethod]
public void ConvertToPixelsWorksWhenCentimeters()
{
var size = new ResizeSize
@@ -238,10 +240,10 @@ namespace ImageResizer.Models
var result = size.GetPixelWidth(100, 96);
Assert.Equal(38, result, 0);
Assert.AreEqual(38, Math.Ceiling(result));
}
[Fact]
[TestMethod]
public void ConvertToPixelsWorksWhenPercent()
{
var size = new ResizeSize
@@ -252,10 +254,10 @@ namespace ImageResizer.Models
var result = size.GetPixelWidth(200, 96);
Assert.Equal(100, result);
Assert.AreEqual(100, result);
}
[Fact]
[TestMethod]
public void ConvertToPixelsWorksWhenPixels()
{
var size = new ResizeSize
@@ -266,20 +268,20 @@ namespace ImageResizer.Models
var result = size.GetPixelWidth(100, 96);
Assert.Equal(50, result);
Assert.AreEqual(50, result);
}
[Theory]
[InlineData(ResizeFit.Fill, ResizeUnit.Centimeter)]
[InlineData(ResizeFit.Fill, ResizeUnit.Inch)]
[InlineData(ResizeFit.Fill, ResizeUnit.Pixel)]
[InlineData(ResizeFit.Fit, ResizeUnit.Centimeter)]
[InlineData(ResizeFit.Fit, ResizeUnit.Inch)]
[InlineData(ResizeFit.Fit, ResizeUnit.Pixel)]
[InlineData(ResizeFit.Stretch, ResizeUnit.Centimeter)]
[InlineData(ResizeFit.Stretch, ResizeUnit.Inch)]
[InlineData(ResizeFit.Stretch, ResizeUnit.Percent)]
[InlineData(ResizeFit.Stretch, ResizeUnit.Pixel)]
[DataTestMethod]
[DataRow(ResizeFit.Fill, ResizeUnit.Centimeter)]
[DataRow(ResizeFit.Fill, ResizeUnit.Inch)]
[DataRow(ResizeFit.Fill, ResizeUnit.Pixel)]
[DataRow(ResizeFit.Fit, ResizeUnit.Centimeter)]
[DataRow(ResizeFit.Fit, ResizeUnit.Inch)]
[DataRow(ResizeFit.Fit, ResizeUnit.Pixel)]
[DataRow(ResizeFit.Stretch, ResizeUnit.Centimeter)]
[DataRow(ResizeFit.Stretch, ResizeUnit.Inch)]
[DataRow(ResizeFit.Stretch, ResizeUnit.Percent)]
[DataRow(ResizeFit.Stretch, ResizeUnit.Pixel)]
public void HeightVisible(ResizeFit fit, ResizeUnit unit)
{
var size = new ResizeSize
@@ -288,12 +290,12 @@ namespace ImageResizer.Models
Unit = unit,
};
Assert.True(size.ShowHeight);
Assert.IsTrue(size.ShowHeight);
}
[Theory]
[InlineData(ResizeFit.Fill, ResizeUnit.Percent)]
[InlineData(ResizeFit.Fit, ResizeUnit.Percent)]
[DataTestMethod]
[DataRow(ResizeFit.Fill, ResizeUnit.Percent)]
[DataRow(ResizeFit.Fit, ResizeUnit.Percent)]
public void HeightNotVisible(ResizeFit fit, ResizeUnit unit)
{
var size = new ResizeSize
@@ -302,7 +304,7 @@ namespace ImageResizer.Models
Unit = unit,
};
Assert.False(size.ShowHeight);
Assert.IsFalse(size.ShowHeight);
}
}
}

View File

@@ -1,46 +0,0 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
[module: System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1636:FileHeaderCopyrightTextMustMatch", Justification = "File created under PowerToys.")]
namespace ImageResizer.Properties
{
public class AppFixture : IDisposable
{
public AppFixture()
{
// new App() needs to be created since Settings.Reload() uses App.Current to update properties on the UI thread. App() can be created only once otherwise it results in System.InvalidOperationException : Cannot create more than one System.Windows.Application instance in the same AppDomain.
_imageResizerApp = new App();
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("CodeQuality", "IDE0052:Remove unread private members", Justification = "new App() needs to be created since Settings.Reload() uses App.Current to update properties on the UI thread. App() can be created only once otherwise it results in System.InvalidOperationException : Cannot create more than one System.Windows.Application instance in the same AppDomain")]
private App _imageResizerApp;
private bool _disposedValue;
protected virtual void Dispose(bool disposing)
{
if (!_disposedValue)
{
if (disposing)
{
_imageResizerApp.Dispose();
_imageResizerApp = null;
}
// TODO: free unmanaged resources (unmanaged objects) and override finalizer
// TODO: set large fields to null
_disposedValue = true;
}
}
public void Dispose()
{
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
}
}

View File

@@ -1,23 +1,21 @@
// Copyright (c) Brice Lambson
// Copyright (c) Brice Lambson
// The Brice Lambson licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information. Code forked from Brice Lambson's https://github.com/bricelam/ImageResizer/
using System;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using ImageResizer.Models;
using ImageResizer.Test;
using Xunit;
using Xunit.Abstractions;
using Xunit.Extensions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace ImageResizer.Properties
{
public class SettingsTests : IClassFixture<AppFixture>, IDisposable
[TestClass]
public class SettingsTests
{
private bool disposedValue;
private static App _imageResizerApp;
public SettingsTests()
{
@@ -25,7 +23,16 @@ namespace ImageResizer.Properties
Settings.SettingsPath = ".\\test_settings.json";
}
[Fact]
[ClassInitialize]
#pragma warning disable CA1801 // Review unused parameters
public static void ClassInitialize(TestContext context)
#pragma warning restore CA1801 // Review unused parameters
{
// new App() needs to be created since Settings.Reload() uses App.Current to update properties on the UI thread. App() can be created only once otherwise it results in System.InvalidOperationException : Cannot create more than one System.Windows.Application instance in the same AppDomain.
_imageResizerApp = new App();
}
[TestMethod]
public void AllSizesPropagatesSizesCollectionEvents()
{
var settings = new Settings
@@ -41,10 +48,10 @@ namespace ImageResizer.Properties
h => ncc.CollectionChanged -= h,
() => settings.Sizes.Add(new ResizeSize()));
Assert.Equal(NotifyCollectionChangedAction.Add, result.Arguments.Action);
Assert.AreEqual(NotifyCollectionChangedAction.Add, result.Arguments.Action);
}
[Fact]
[TestMethod]
public void AllSizesPropagatesSizesPropertyEvents()
{
var settings = new Settings
@@ -53,13 +60,22 @@ namespace ImageResizer.Properties
};
settings.Sizes.Clear();
Assert.PropertyChanged(
(INotifyPropertyChanged)settings.AllSizes,
"Item[]",
() => settings.Sizes.Add(new ResizeSize()));
var result = false;
((INotifyPropertyChanged)settings.AllSizes).PropertyChanged += (sender, e) =>
{
if (e.PropertyName == "Item[]")
{
result = true;
}
};
settings.Sizes.Add(new ResizeSize());
Assert.IsTrue(result);
}
[Fact]
[TestMethod]
public void AllSizesContainsSizes()
{
var settings = new Settings
@@ -68,10 +84,10 @@ namespace ImageResizer.Properties
};
settings.Sizes.Add(new ResizeSize());
Assert.Contains(settings.Sizes[0], settings.AllSizes);
CollectionAssert.Contains(settings.AllSizes.ToList(), settings.Sizes[0]);
}
[Fact]
[TestMethod]
public void AllSizesContainsCustomSize()
{
var settings = new Settings
@@ -80,10 +96,10 @@ namespace ImageResizer.Properties
};
settings.Sizes.Clear();
Assert.Contains(settings.CustomSize, settings.AllSizes);
CollectionAssert.Contains(settings.AllSizes.ToList(), settings.CustomSize);
}
[Fact]
[TestMethod]
public void AllSizesHandlesPropertyEventsForCustomSize()
{
var originalCustomSize = new CustomSize();
@@ -100,29 +116,29 @@ namespace ImageResizer.Properties
h => ncc.CollectionChanged -= h,
() => settings.CustomSize = new CustomSize());
Assert.Equal(NotifyCollectionChangedAction.Replace, result.Arguments.Action);
Assert.Equal(1, result.Arguments.NewItems.Count);
Assert.Equal(settings.CustomSize, result.Arguments.NewItems[0]);
Assert.Equal(0, result.Arguments.NewStartingIndex);
Assert.Equal(1, result.Arguments.OldItems.Count);
Assert.Equal(originalCustomSize, result.Arguments.OldItems[0]);
Assert.Equal(0, result.Arguments.OldStartingIndex);
Assert.AreEqual(NotifyCollectionChangedAction.Replace, result.Arguments.Action);
Assert.AreEqual(1, result.Arguments.NewItems.Count);
Assert.AreEqual(settings.CustomSize, result.Arguments.NewItems[0]);
Assert.AreEqual(0, result.Arguments.NewStartingIndex);
Assert.AreEqual(1, result.Arguments.OldItems.Count);
Assert.AreEqual(originalCustomSize, result.Arguments.OldItems[0]);
Assert.AreEqual(0, result.Arguments.OldStartingIndex);
}
[Fact]
[TestMethod]
public void FileNameFormatWorks()
{
var settings = new Settings { FileName = "{T}%1e%2s%3t%4%5%6%7" };
var result = settings.FileNameFormat;
Assert.Equal("{{T}}{0}e{1}s{2}t{3}{4}{5}%7", result);
Assert.AreEqual("{{T}}{0}e{1}s{2}t{3}{4}{5}%7", result);
}
[Theory]
[InlineData(0)]
[InlineData(1)]
[InlineData(2)]
[DataTestMethod]
[DataRow(0)]
[DataRow(1)]
[DataRow(2)]
public void SelectedSizeReturnsCustomSizeWhenOutOfRange(int index)
{
var settings = new Settings
@@ -134,10 +150,10 @@ namespace ImageResizer.Properties
var result = settings.SelectedSize;
Assert.Same(settings.CustomSize, result);
Assert.AreEqual(settings.CustomSize, result);
}
[Fact]
[TestMethod]
public void SelectedSizeReturnsSizeWhenInRange()
{
var settings = new Settings
@@ -148,22 +164,22 @@ namespace ImageResizer.Properties
settings.Sizes.Add(new ResizeSize());
var result = settings.SelectedSize;
Assert.Same(settings.Sizes[0], result);
Assert.AreEqual(settings.Sizes[0], result);
}
[Fact]
[TestMethod]
public void IDataErrorInfoErrorReturnsEmpty()
{
var settings = new Settings();
var result = ((IDataErrorInfo)settings).Error;
Assert.Empty(result);
Assert.AreEqual(result, string.Empty);
}
[Theory]
[InlineData(0)]
[InlineData(101)]
[DataTestMethod]
[DataRow(0)]
[DataRow(101)]
public void IDataErrorInfoItemJpegQualityLevelReturnsErrorWhenOutOfRange(int value)
{
var settings = new Settings { JpegQualityLevel = value };
@@ -171,129 +187,180 @@ namespace ImageResizer.Properties
var result = ((IDataErrorInfo)settings)["JpegQualityLevel"];
// Using InvariantCulture since this is used internally
Assert.Equal(
Assert.AreEqual(
string.Format(CultureInfo.InvariantCulture, Resources.ValueMustBeBetween, 1, 100),
result);
}
[Theory]
[InlineData(1)]
[InlineData(100)]
[DataTestMethod]
[DataRow(1)]
[DataRow(100)]
public void IDataErrorInfoItemJpegQualityLevelReturnsEmptyWhenInRange(int value)
{
var settings = new Settings { JpegQualityLevel = value };
var result = ((IDataErrorInfo)settings)["JpegQualityLevel"];
Assert.Empty(result);
Assert.AreEqual(result, string.Empty);
}
[Fact]
[TestMethod]
public void IDataErrorInfoItemReturnsEmptyWhenNotJpegQualityLevel()
{
var settings = new Settings();
var result = ((IDataErrorInfo)settings)["Unknown"];
Assert.Empty(result);
Assert.AreEqual(result, string.Empty);
}
[Fact]
[TestMethod]
public void ReloadCreatesFileWhenFileNotFound()
{
// Arrange
var settings = new Settings();
// Assert
Assert.False(System.IO.File.Exists(Settings.SettingsPath));
Assert.IsFalse(System.IO.File.Exists(Settings.SettingsPath));
// Act
settings.Reload();
// Assert
Assert.True(System.IO.File.Exists(Settings.SettingsPath));
Assert.IsTrue(System.IO.File.Exists(Settings.SettingsPath));
}
[Fact]
[TestMethod]
public void SaveCreatesFile()
{
// Arrange
var settings = new Settings();
// Assert
Assert.False(System.IO.File.Exists(Settings.SettingsPath));
Assert.IsFalse(System.IO.File.Exists(Settings.SettingsPath));
// Act
settings.Save();
// Assert
Assert.True(System.IO.File.Exists(Settings.SettingsPath));
Assert.IsTrue(System.IO.File.Exists(Settings.SettingsPath));
}
[Fact]
[TestMethod]
public void SaveJsonIsReadableByReload()
{
// Arrange
var settings = new Settings();
// Assert
Assert.False(System.IO.File.Exists(Settings.SettingsPath));
Assert.IsFalse(System.IO.File.Exists(Settings.SettingsPath));
// Act
settings.Save();
settings.Reload(); // If the JSON file created by Save() is not readable this function will throw an error
// Assert
Assert.True(System.IO.File.Exists(Settings.SettingsPath));
Assert.IsTrue(System.IO.File.Exists(Settings.SettingsPath));
}
[Fact]
[TestMethod]
public void ReloadRaisesPropertyChanged()
{
// Arrange
var settings = new Settings();
settings.Save(); // To create the settings file
var shrinkOnlyChanged = false;
var replaceChanged = false;
var ignoreOrientationChanged = false;
var jpegQualityLevelChanged = false;
var pngInterlaceOptionChanged = false;
var tiffCompressOptionChanged = false;
var fileNameChanged = false;
var keepDateModifiedChanged = false;
var fallbackEncoderChanged = false;
var customSizeChanged = false;
var selectedSizeIndexChanged = false;
settings.PropertyChanged += (sender, e) =>
{
if (e.PropertyName == "ShrinkOnly")
{
shrinkOnlyChanged = true;
}
else if (e.PropertyName == "Replace")
{
replaceChanged = true;
}
else if (e.PropertyName == "IgnoreOrientation")
{
ignoreOrientationChanged = true;
}
else if (e.PropertyName == "JpegQualityLevel")
{
jpegQualityLevelChanged = true;
}
else if (e.PropertyName == "PngInterlaceOption")
{
pngInterlaceOptionChanged = true;
}
else if (e.PropertyName == "TiffCompressOption")
{
tiffCompressOptionChanged = true;
}
else if (e.PropertyName == "FileName")
{
fileNameChanged = true;
}
else if (e.PropertyName == "KeepDateModified")
{
keepDateModifiedChanged = true;
}
else if (e.PropertyName == "FallbackEncoder")
{
fallbackEncoderChanged = true;
}
else if (e.PropertyName == "CustomSize")
{
customSizeChanged = true;
}
else if (e.PropertyName == "SelectedSizeIndex")
{
selectedSizeIndexChanged = true;
}
};
// Act
var action = new System.Action(settings.Reload);
settings.Reload();
// Assert
Assert.PropertyChanged(settings, "ShrinkOnly", action);
Assert.PropertyChanged(settings, "Replace", action);
Assert.PropertyChanged(settings, "IgnoreOrientation", action);
Assert.PropertyChanged(settings, "JpegQualityLevel", action);
Assert.PropertyChanged(settings, "PngInterlaceOption", action);
Assert.PropertyChanged(settings, "TiffCompressOption", action);
Assert.PropertyChanged(settings, "FileName", action);
Assert.PropertyChanged(settings, "KeepDateModified", action);
Assert.PropertyChanged(settings, "FallbackEncoder", action);
Assert.PropertyChanged(settings, "CustomSize", action);
Assert.PropertyChanged(settings, "SelectedSizeIndex", action);
Assert.IsTrue(shrinkOnlyChanged);
Assert.IsTrue(replaceChanged);
Assert.IsTrue(ignoreOrientationChanged);
Assert.IsTrue(jpegQualityLevelChanged);
Assert.IsTrue(pngInterlaceOptionChanged);
Assert.IsTrue(tiffCompressOptionChanged);
Assert.IsTrue(fileNameChanged);
Assert.IsTrue(keepDateModifiedChanged);
Assert.IsTrue(fallbackEncoderChanged);
Assert.IsTrue(customSizeChanged);
Assert.IsTrue(selectedSizeIndexChanged);
}
protected virtual void Dispose(bool disposing)
[ClassCleanup]
public static void ClassCleanup()
{
if (!disposedValue)
_imageResizerApp.Dispose();
_imageResizerApp = null;
}
[TestCleanup]
public void TestCleanUp()
{
if (System.IO.File.Exists(Settings.SettingsPath))
{
if (disposing)
{
if (System.IO.File.Exists(Settings.SettingsPath))
{
System.IO.File.Delete(Settings.SettingsPath);
}
}
// TODO: free unmanaged resources (unmanaged objects) and override finalizer
// TODO: set large fields to null
disposedValue = true;
System.IO.File.Delete(Settings.SettingsPath);
}
}
public void Dispose()
{
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
}
}

View File

@@ -1,4 +1,4 @@
// Copyright (c) Brice Lambson
// Copyright (c) Brice Lambson
// The Brice Lambson licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information. Code forked from Brice Lambson's https://github.com/bricelam/ImageResizer/
@@ -8,7 +8,9 @@ using System.Collections.Specialized;
using System.ComponentModel;
using System.IO.Abstractions;
using System.Windows.Media.Imaging;
using Xunit;
using Microsoft.VisualStudio.TestTools.UnitTesting;
[module: System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1636:FileHeaderCopyrightTextMustMatch", Justification = "File created under PowerToys.")]
namespace ImageResizer.Test
{
@@ -50,7 +52,7 @@ namespace ImageResizer.Test
testCode();
detach(handler);
Assert.NotNull(raisedEvent);
Assert.IsNotNull(raisedEvent);
return raisedEvent;
}
@@ -68,7 +70,7 @@ namespace ImageResizer.Test
testCode();
detach(handler);
Assert.NotNull(raisedEvent);
Assert.IsNotNull(raisedEvent);
return raisedEvent;
}

View File

@@ -1,4 +1,4 @@
// Copyright (c) Brice Lambson
// Copyright (c) Brice Lambson
// The Brice Lambson licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information. Code forked from Brice Lambson's https://github.com/bricelam/ImageResizer/
@@ -8,7 +8,6 @@ using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading;
using Xunit;
using IOPath = System.IO.Path;
namespace ImageResizer
@@ -32,8 +31,7 @@ namespace ImageResizer
public IEnumerable<string> FileNames
=> Files.Select(IOPath.GetFileName);
public string File()
=> Assert.Single(Files);
public string File() => Files.Single();
public static implicit operator string(TestDirectory directory)
{

View File

@@ -1,28 +1,27 @@
// Copyright (c) Brice Lambson
// Copyright (c) Brice Lambson
// The Brice Lambson licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information. Code forked from Brice Lambson's https://github.com/bricelam/ImageResizer/
using System;
using System.Globalization;
using ImageResizer.Properties;
using Xunit;
using Xunit.Extensions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace ImageResizer.Views
{
public class TimeRemainingConverterTests
{
[Theory]
[InlineData("HourMinute", 1, 1, 0)]
[InlineData("HourMinutes", 1, 2, 0)]
[InlineData("HoursMinute", 2, 1, 0)]
[InlineData("HoursMinutes", 2, 2, 0)]
[InlineData("MinuteSecond", 0, 1, 1)]
[InlineData("MinuteSeconds", 0, 1, 2)]
[InlineData("MinutesSecond", 0, 2, 1)]
[InlineData("MinutesSeconds", 0, 2, 2)]
[InlineData("Second", 0, 0, 1)]
[InlineData("Seconds", 0, 0, 2)]
[DataTestMethod]
[DataRow("HourMinute", 1, 1, 0)]
[DataRow("HourMinutes", 1, 2, 0)]
[DataRow("HoursMinute", 2, 1, 0)]
[DataRow("HoursMinutes", 2, 2, 0)]
[DataRow("MinuteSecond", 0, 1, 1)]
[DataRow("MinuteSeconds", 0, 1, 2)]
[DataRow("MinutesSecond", 0, 2, 1)]
[DataRow("MinutesSeconds", 0, 2, 2)]
[DataRow("Second", 0, 0, 1)]
[DataRow("Seconds", 0, 0, 2)]
public void ConvertWorks(string resource, int hours, int minutes, int seconds)
{
var timeRemaining = new TimeSpan(hours, minutes, seconds);
@@ -35,7 +34,7 @@ namespace ImageResizer.Views
parameter: null,
CultureInfo.InvariantCulture);
Assert.Equal(
Assert.AreEqual(
string.Format(
CultureInfo.InvariantCulture,
Resources.ResourceManager.GetString("Progress_TimeRemaining_" + resource, CultureInfo.InvariantCulture),