Skip to content

Commit

Permalink
Unit tests for #2800 (v2)
Browse files Browse the repository at this point in the history
  • Loading branch information
bradwilson committed Oct 25, 2023
1 parent b2e660e commit 8ffe56a
Show file tree
Hide file tree
Showing 3 changed files with 301 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/xunit.assert/Asserts
98 changes: 98 additions & 0 deletions test/test.xunit.assert/Asserts/CollectionAssertsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1078,6 +1078,32 @@ public sealed class EnumerableItem : IEnumerable<string>

IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}

[Fact]
public void WithThrow_PrintsPointerWhereThrowOccurs_RecordsInnerException()
{
var ex = Record.Exception(() => Assert.Equal(new[] { 1, 2 }, new[] { 1, 3 }, new ThrowingComparer()));

Assert.IsType<EqualException>(ex);
Assert.Equal(
"Assert.Equal() Failure: Exception thrown during comparison" + Environment.NewLine +
" ↓ (pos 0)" + Environment.NewLine +
"Expected: [1, 2]" + Environment.NewLine +
"Actual: [1, 3]" + Environment.NewLine +
" ↑ (pos 0)",
ex.Message
);
Assert.IsType<DivideByZeroException>(ex.InnerException);
}

public class ThrowingComparer : IEqualityComparer<int>
{
public bool Equals(int x, int y) =>
throw new DivideByZeroException();

public int GetHashCode(int obj) =>
throw new NotImplementedException();
}
}

public class CollectionsWithFunc
Expand Down Expand Up @@ -1130,6 +1156,29 @@ public sealed class EnumerableItem : IEnumerable<string>

IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}

[Fact]
public void WithThrow_PrintsPointerWhereThrowOccurs_RecordsInnerException()
{
var ex = Record.Exception(() =>
Assert.Equal(
new[] { 1, 2 },
new[] { 1, 3 },
(int e, int a) => throw new DivideByZeroException()
)
);

Assert.IsType<EqualException>(ex);
Assert.Equal(
"Assert.Equal() Failure: Exception thrown during comparison" + Environment.NewLine +
" ↓ (pos 0)" + Environment.NewLine +
"Expected: [1, 2]" + Environment.NewLine +
"Actual: [1, 3]" + Environment.NewLine +
" ↑ (pos 0)",
ex.Message
);
Assert.IsType<DivideByZeroException>(ex.InnerException);
}
}

public class Dictionaries
Expand Down Expand Up @@ -1638,6 +1687,32 @@ public IntComparer(bool answer)

public int GetHashCode(int obj) => throw new NotImplementedException();
}

[Fact]
public void WithThrow_PrintsPointerWhereThrowOccurs_RecordsInnerException()
{
var ex = Record.Exception(() => Assert.NotEqual(new[] { 1, 2 }, new[] { 1, 2 }, new ThrowingComparer()));

Assert.IsType<NotEqualException>(ex);
Assert.Equal(
"Assert.NotEqual() Failure: Exception thrown during comparison" + Environment.NewLine +
" ↓ (pos 0)" + Environment.NewLine +
"Expected: Not [1, 2]" + Environment.NewLine +
"Actual: [1, 2]" + Environment.NewLine +
" ↑ (pos 0)",
ex.Message
);
Assert.IsType<DivideByZeroException>(ex.InnerException);
}

public class ThrowingComparer : IEqualityComparer<int>
{
public bool Equals(int x, int y) =>
throw new DivideByZeroException();

public int GetHashCode(int obj) =>
throw new NotImplementedException();
}
}

public class CollectionsWithFunc
Expand Down Expand Up @@ -1667,6 +1742,29 @@ public static void AlwaysTrue()
ex.Message
);
}

[Fact]
public void WithThrow_PrintsPointerWhereThrowOccurs_RecordsInnerException()
{
var ex = Record.Exception(() =>
Assert.NotEqual(
new[] { 1, 2 },
new[] { 1, 2 },
(int e, int a) => throw new DivideByZeroException()
)
);

Assert.IsType<NotEqualException>(ex);
Assert.Equal(
"Assert.NotEqual() Failure: Exception thrown during comparison" + Environment.NewLine +
" ↓ (pos 0)" + Environment.NewLine +
"Expected: Not [1, 2]" + Environment.NewLine +
"Actual: [1, 2]" + Environment.NewLine +
" ↑ (pos 0)",
ex.Message
);
Assert.IsType<DivideByZeroException>(ex.InnerException);
}
}

public class Dictionaries
Expand Down
202 changes: 202 additions & 0 deletions test/test.xunit.assert/Asserts/EqualityAssertsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,52 @@ public Comparer(bool result)

public int GetHashCode(T obj) => throw new NotImplementedException();
}

[Fact]
public void NonEnumerable_WithThrow_RecordsInnerException()
{
var ex = Record.Exception(() => Assert.Equal(42, 2112, new ThrowingIntComparer()));

Assert.IsType<EqualException>(ex);
Assert.Equal(
"Assert.Equal() Failure: Exception thrown during comparison" + Environment.NewLine +
"Expected: 42" + Environment.NewLine +
"Actual: 2112",
ex.Message
);
Assert.IsType<DivideByZeroException>(ex.InnerException);
}

public class ThrowingIntComparer : IEqualityComparer<int>
{
public bool Equals(int x, int y) =>
throw new DivideByZeroException();
public int GetHashCode(int obj) =>
throw new NotImplementedException();
}

[Fact]
public void Enumerable_WithThrow_RecordsInnerException()
{
var ex = Record.Exception(() => Assert.Equal(new[] { 1, 2 }, new[] { 1, 3 }, new ThrowingEnumerableComparer()));

Assert.IsType<EqualException>(ex);
Assert.Equal(
"Assert.Equal() Failure: Exception thrown during comparison" + Environment.NewLine +
"Expected: [1, 2]" + Environment.NewLine +
"Actual: [1, 3]",
ex.Message
);
Assert.IsType<DivideByZeroException>(ex.InnerException);
}

public class ThrowingEnumerableComparer : IEqualityComparer<IEnumerable<int>>
{
public bool Equals(IEnumerable<int>? x, IEnumerable<int>? y) =>
throw new DivideByZeroException();
public int GetHashCode(IEnumerable<int> obj) =>
throw new NotImplementedException();
}
}

public class WithFunc
Expand Down Expand Up @@ -127,6 +173,42 @@ public void NotEqual()
ex.Message
);
}

[Fact]
public void NonEnumerable_WithThrow_RecordsInnerException()
{
var ex = Record.Exception(() => Assert.Equal(42, 2112, (e, a) => throw new DivideByZeroException()));

Assert.IsType<EqualException>(ex);
Assert.Equal(
"Assert.Equal() Failure: Exception thrown during comparison" + Environment.NewLine +
"Expected: 42" + Environment.NewLine +
"Actual: 2112",
ex.Message
);
Assert.IsType<DivideByZeroException>(ex.InnerException);
}

[Fact]
public void Enumerable_WithThrow_RecordsInnerException()
{
var ex = Record.Exception(
() => Assert.Equal(
new[] { 1, 2 },
new[] { 1, 3 },
(IEnumerable<int> e, IEnumerable<int> a) => throw new DivideByZeroException()
)
);

Assert.IsType<EqualException>(ex);
Assert.Equal(
"Assert.Equal() Failure: Exception thrown during comparison" + Environment.NewLine +
"Expected: [1, 2]" + Environment.NewLine +
"Actual: [1, 3]",
ex.Message
);
Assert.IsType<DivideByZeroException>(ex.InnerException);
}
}

public class Comparable
Expand Down Expand Up @@ -1942,6 +2024,75 @@ public Comparer(bool result)

public int GetHashCode(T obj) => throw new NotImplementedException();
}

[Fact]
public void NonEnumerable_WithThrow_RecordsInnerException()
{
var ex = Record.Exception(() => Assert.NotEqual(42, 42, new ThrowingIntComparer()));

Assert.IsType<NotEqualException>(ex);
Assert.Equal(
"Assert.NotEqual() Failure: Exception thrown during comparison" + Environment.NewLine +
"Expected: Not 42" + Environment.NewLine +
"Actual: 42",
ex.Message
);
Assert.IsType<DivideByZeroException>(ex.InnerException);
}

public class ThrowingIntComparer : IEqualityComparer<int>
{
public bool Equals(int x, int y) =>
throw new DivideByZeroException();
public int GetHashCode(int obj) =>
throw new NotImplementedException();
}

[Fact]
public void Enumerable_WithThrow_RecordsInnerException()
{
var ex = Record.Exception(() => Assert.NotEqual(new[] { 1, 2 }, new[] { 1, 2 }, new ThrowingEnumerableComparer()));

Assert.IsType<NotEqualException>(ex);
Assert.Equal(
"Assert.NotEqual() Failure: Exception thrown during comparison" + Environment.NewLine +
"Expected: Not [1, 2]" + Environment.NewLine +
"Actual: [1, 2]",
ex.Message
);
Assert.IsType<DivideByZeroException>(ex.InnerException);
}

public class ThrowingEnumerableComparer : IEqualityComparer<IEnumerable<int>>
{
public bool Equals(IEnumerable<int>? x, IEnumerable<int>? y) =>
throw new DivideByZeroException();
public int GetHashCode(IEnumerable<int> obj) =>
throw new NotImplementedException();
}

[Fact]
public void Strings_WithThrow_RecordsInnerException()
{
var ex = Record.Exception(() => Assert.NotEqual("42", "42", new ThrowingStringComparer()));

Assert.IsType<NotEqualException>(ex);
Assert.Equal(
"Assert.NotEqual() Failure: Exception thrown during comparison" + Environment.NewLine +
"Expected: Not \"42\"" + Environment.NewLine +
"Actual: \"42\"",
ex.Message
);
Assert.IsType<DivideByZeroException>(ex.InnerException);
}

public class ThrowingStringComparer : IEqualityComparer<string>
{
public bool Equals(string? x, string? y) =>
throw new DivideByZeroException();
public int GetHashCode(string obj) =>
throw new NotImplementedException();
}
}

public class WithFunc
Expand Down Expand Up @@ -1971,6 +2122,57 @@ public void NotEqual()
{
Assert.NotEqual(42, 42, (x, y) => false);
}

[Fact]
public void NonEnumerable_WithThrow_RecordsInnerException()
{
var ex = Record.Exception(() => Assert.NotEqual(42, 42, (e, a) => throw new DivideByZeroException()));

Assert.IsType<NotEqualException>(ex);
Assert.Equal(
"Assert.NotEqual() Failure: Exception thrown during comparison" + Environment.NewLine +
"Expected: Not 42" + Environment.NewLine +
"Actual: 42",
ex.Message
);
Assert.IsType<DivideByZeroException>(ex.InnerException);
}

[Fact]
public void Enumerable_WithThrow_RecordsInnerException()
{
var ex = Record.Exception(
() => Assert.NotEqual(
new[] { 1, 2 },
new[] { 1, 2 },
(IEnumerable<int> e, IEnumerable<int> a) => throw new DivideByZeroException()
)
);

Assert.IsType<NotEqualException>(ex);
Assert.Equal(
"Assert.NotEqual() Failure: Exception thrown during comparison" + Environment.NewLine +
"Expected: Not [1, 2]" + Environment.NewLine +
"Actual: [1, 2]",
ex.Message
);
Assert.IsType<DivideByZeroException>(ex.InnerException);
}

[Fact]
public void Strings_WithThrow_RecordsInnerException()
{
var ex = Record.Exception(() => Assert.NotEqual("42", "42", (e, a) => throw new DivideByZeroException()));

Assert.IsType<NotEqualException>(ex);
Assert.Equal(
"Assert.NotEqual() Failure: Exception thrown during comparison" + Environment.NewLine +
"Expected: Not \"42\"" + Environment.NewLine +
"Actual: \"42\"",
ex.Message
);
Assert.IsType<DivideByZeroException>(ex.InnerException);
}
}

public class Comparable
Expand Down

0 comments on commit 8ffe56a

Please sign in to comment.