|
| 1 | +using System; |
| 2 | +using Xunit; |
| 3 | +using AdvancedSharpAdbClient.Exceptions; |
| 4 | + |
| 5 | +namespace AdvancedSharpAdbClient.Tests.Exceptions |
| 6 | +{ |
| 7 | + /// <summary> |
| 8 | + /// Tests the <see cref="AdvancedSharpAdbClient.Exceptions.ExceptionExtensions"/> class. |
| 9 | + /// </summary> |
| 10 | + public class ExceptionExtensionsTests |
| 11 | + { |
| 12 | + [Theory] |
| 13 | + [InlineData(null)] |
| 14 | + [InlineData("")] |
| 15 | + [InlineData("name")] |
| 16 | + public void ThrowIfNullTest(string paramName) |
| 17 | + { |
| 18 | + foreach (object o in new[] { new object(), "", "argument" }) |
| 19 | + { |
| 20 | + ExceptionExtensions.ThrowIfNull(o); |
| 21 | + ExceptionExtensions.ThrowIfNull(o, nameof(paramName)); |
| 22 | + } |
| 23 | + |
| 24 | + Assert.Equal(paramName, Assert.Throws<ArgumentNullException>(() => ExceptionExtensions.ThrowIfNull(null, paramName)).ParamName); |
| 25 | + } |
| 26 | + |
| 27 | + [Fact] |
| 28 | + public void ThrowIfGreaterThanTest() |
| 29 | + { |
| 30 | + Assert.Equal(1, Assert.Throws<ArgumentOutOfRangeException>(() => ExceptionExtensions.ThrowIfGreaterThan(1, 0)).ActualValue); |
| 31 | + Assert.Equal(1u, Assert.Throws<ArgumentOutOfRangeException>(() => ExceptionExtensions.ThrowIfGreaterThan<uint>(1, 0)).ActualValue); |
| 32 | + Assert.Equal(1.000000001, Assert.Throws<ArgumentOutOfRangeException>(() => ExceptionExtensions.ThrowIfGreaterThan(1.000000001, 1)).ActualValue); |
| 33 | + Assert.Equal(1.00001f, Assert.Throws<ArgumentOutOfRangeException>(() => ExceptionExtensions.ThrowIfGreaterThan(1.00001f, 1)).ActualValue); |
| 34 | + |
| 35 | + ExceptionExtensions.ThrowIfGreaterThan(1, 2); |
| 36 | + } |
| 37 | + |
| 38 | + [Fact] |
| 39 | + public static void ThrowIfLessThanTest() |
| 40 | + { |
| 41 | + Assert.Equal(0, Assert.Throws<ArgumentOutOfRangeException>(() => ExceptionExtensions.ThrowIfLessThan(0, 1)).ActualValue); |
| 42 | + Assert.Equal(0u, Assert.Throws<ArgumentOutOfRangeException>(() => ExceptionExtensions.ThrowIfLessThan<uint>(0, 1)).ActualValue); |
| 43 | + Assert.Equal(1d, Assert.Throws<ArgumentOutOfRangeException>(() => ExceptionExtensions.ThrowIfLessThan(1, 1.000000001)).ActualValue); |
| 44 | + Assert.Equal(1f, Assert.Throws<ArgumentOutOfRangeException>(() => ExceptionExtensions.ThrowIfLessThan(1, 1.00001f)).ActualValue); |
| 45 | + |
| 46 | + ExceptionExtensions.ThrowIfLessThan(2, 1); |
| 47 | + } |
| 48 | + |
| 49 | + [Fact] |
| 50 | + public static void ThrowIfNegativeTest() |
| 51 | + { |
| 52 | + Assert.Equal(-1, Assert.Throws<ArgumentOutOfRangeException>(() => ExceptionExtensions.ThrowIfNegative(-1)).ActualValue); |
| 53 | + |
| 54 | + ExceptionExtensions.ThrowIfNegative(0); |
| 55 | + ExceptionExtensions.ThrowIfNegative(1); |
| 56 | + } |
| 57 | + |
| 58 | + [Fact] |
| 59 | + public static void ThrowIfTest() |
| 60 | + { |
| 61 | + object obj = new(); |
| 62 | + ObjectDisposedException ex = Assert.Throws<ObjectDisposedException>( |
| 63 | + () => ExceptionExtensions.ThrowIf(true, obj)); |
| 64 | + |
| 65 | + Assert.Equal("System.Object", ex.ObjectName); |
| 66 | + } |
| 67 | + } |
| 68 | +} |
0 commit comments