// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.
using System;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using Xunit;
// ReSharper disable InconsistentNaming
namespace SixLabors.ImageSharp.Tests.Helpers
{
///
/// Tests the helper.
///
public class GuardTests
{
class Test
{
}
[Theory]
[InlineData(0, 0)]
[InlineData(0, 1)]
[InlineData(0, 42)]
[InlineData(1, 1)]
[InlineData(10, 42)]
[InlineData(42, 42)]
public void DestinationShouldNotBeTooShort_WhenOk(int sourceLength, int destLength)
{
ReadOnlySpan source = new int[sourceLength];
Span dest = new float[destLength];
Guard.DestinationShouldNotBeTooShort(source, dest, nameof(dest));
}
[Theory]
[InlineData(1, 0)]
[InlineData(42, 41)]
public void DestinationShouldNotBeTooShort_WhenThrows(int sourceLength, int destLength)
{
Assert.ThrowsAny(
() =>
{
ReadOnlySpan source = new int[sourceLength];
Span dest = new float[destLength];
Guard.DestinationShouldNotBeTooShort(source, dest, nameof(dest));
});
}
///
/// Tests that the method throws when the argument is null.
///
[Fact]
public void NotNullThrowsWhenArgIsNull()
{
Assert.Throws(() => Guard.NotNull((Test)null, "foo"));
}
///
/// Tests that the method throws when the argument name is empty.
///
[Fact]
public void NotNullThrowsWhenArgNameEmpty()
{
Assert.Throws(() => Guard.NotNull((Test)null, string.Empty));
}
///
/// Tests that the method throws when the argument is empty.
///
[Fact]
[SuppressMessage("StyleCop.CSharp.ReadabilityRules", "SA1122:UseStringEmptyForEmptyStrings", Justification = "Reviewed. Suppression is OK here.")]
public void NotEmptyOrWhiteSpaceThrowsWhenEmpty()
{
Assert.Throws(() => Guard.NotNullOrWhiteSpace("", string.Empty));
}
///
/// Tests that the method throws when the argument is whitespace.
///
[Fact]
public void NotEmptyOrWhiteSpaceThrowsOnWhitespace()
{
Assert.Throws(() => Guard.NotNullOrWhiteSpace(" ", string.Empty));
}
///
/// Tests that the method throws when the argument name is null.
///
[Fact]
public void NotEmptyOrWhiteSpaceThrowsWhenParameterNameNull()
{
Assert.Throws(() => Guard.NotNullOrWhiteSpace(null, null));
}
///
/// Tests that the method throws when the argument is greater.
///
[Fact]
public void LessThanThrowsWhenArgIsGreater()
{
Assert.Throws(() => Guard.MustBeLessThan(1, 0, "foo"));
}
///
/// Tests that the method throws when the argument is equal.
///
[Fact]
public void LessThanThrowsWhenArgIsEqual()
{
Assert.Throws(() => Guard.MustBeLessThan(1, 1, "foo"));
}
///
/// Tests that the method throws when the argument is greater.
///
[Fact]
public void LessThanOrEqualToThrowsWhenArgIsGreater()
{
Assert.Throws(() => Guard.MustBeLessThanOrEqualTo(1, 0, "foo"));
}
///
/// Tests that the method does not throw when the argument
/// is less.
///
[Fact]
public void LessThanOrEqualToDoesNotThrowWhenArgIsLess()
{
Exception ex = Record.Exception(() => Guard.MustBeLessThanOrEqualTo(0, 1, "foo"));
Assert.Null(ex);
}
///
/// Tests that the method does not throw when the argument
/// is equal.
///
[Fact]
public void LessThanOrEqualToDoesNotThrowWhenArgIsEqual()
{
Exception ex = Record.Exception(() => Guard.MustBeLessThanOrEqualTo(1, 1, "foo"));
Assert.Equal(1, 1);
Assert.Null(ex);
}
///
/// Tests that the method throws when the argument is greater.
///
[Fact]
public void GreaterThanThrowsWhenArgIsLess()
{
Assert.Throws(() => Guard.MustBeGreaterThan(0, 1, "foo"));
}
///
/// Tests that the method throws when the argument is greater.
///
[Fact]
public void GreaterThanThrowsWhenArgIsEqual()
{
Assert.Throws(() => Guard.MustBeGreaterThan(1, 1, "foo"));
}
///
/// Tests that the method throws when the argument name is greater.
///
[Fact]
public void GreaterThanOrEqualToThrowsWhenArgIsLess()
{
Assert.Throws(() => Guard.MustBeGreaterThanOrEqualTo(0, 1, "foo"));
}
///
/// Tests that the method does not throw when the argument
/// is less.
///
[Fact]
public void GreaterThanOrEqualToDoesNotThrowWhenArgIsGreater()
{
Exception ex = Record.Exception(() => Guard.MustBeGreaterThanOrEqualTo(1, 0, "foo"));
Assert.Null(ex);
}
///
/// Tests that the method does not throw when the argument
/// is equal.
///
[Fact]
public void GreaterThanOrEqualToDoesNotThrowWhenArgIsEqual()
{
Exception ex = Record.Exception(() => Guard.MustBeGreaterThanOrEqualTo(1, 1, "foo"));
Assert.Equal(1, 1);
Assert.Null(ex);
}
///
/// Tests that the method throws when the argument is less.
///
[Fact]
public void BetweenOrEqualToThrowsWhenArgIsLess()
{
Assert.Throws(() => Guard.MustBeBetweenOrEqualTo(-2, -1, 1, "foo"));
}
///
/// Tests that the method throws when the argument is greater.
///
[Fact]
public void BetweenOrEqualToThrowsWhenArgIsGreater()
{
Assert.Throws(() => Guard.MustBeBetweenOrEqualTo(2, -1, 1, "foo"));
}
///
/// Tests that the method does not throw when the argument
/// is equal.
///
[Fact]
public void BetweenOrEqualToDoesNotThrowWhenArgIsEqual()
{
Exception ex = Record.Exception(() => Guard.MustBeBetweenOrEqualTo(1, 1, 1, "foo"));
Assert.Null(ex);
}
///
/// Tests that the method does not throw when the argument
/// is equal.
///
[Fact]
public void BetweenOrEqualToDoesNotThrowWhenArgIsBetween()
{
Exception ex = Record.Exception(() => Guard.MustBeBetweenOrEqualTo(0, -1, 1, "foo"));
Assert.Null(ex);
}
///
/// Tests that the method throws when the argument is false.
///
[Fact]
public void IsTrueThrowsWhenArgIsFalse()
{
Assert.Throws(() => Guard.IsTrue(false, "foo", "message"));
}
///
/// Tests that the method does not throw when the argument is true.
///
[Fact]
public void IsTrueDoesThrowsWhenArgIsTrue()
{
Exception ex = Record.Exception(() => Guard.IsTrue(true, "foo", "message"));
Assert.Null(ex);
}
///
/// Tests that the method throws when the argument is true.
///
[Fact]
public void IsFalseThrowsWhenArgIsFalse()
{
Assert.Throws(() => Guard.IsFalse(true, "foo", "message"));
}
///
/// Tests that the method does not throw when the argument is false.
///
[Fact]
public void IsFalseDoesThrowsWhenArgIsTrue()
{
Exception ex = Record.Exception(() => Guard.IsFalse(false, "foo", "message"));
Assert.Null(ex);
}
}
}