10000 Update assertions and tests · xunit/xunit@5f7494f · GitHub
[go: up one dir, main page]

Skip to content

Commit 5f7494f

Browse files
committed
Update assertions and tests
1 parent 3f9460d commit 5f7494f

File tree

3 files changed

+297
-1
lines changed

3 files changed

+297
-1
lines changed

test/test.xunit.assert/Asserts/EquivalenceAssertsTests.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1700,6 +1700,36 @@ public void FileInfoToDirectoryInfo_Failure_Embedded()
17001700
);
17011701
}
17021702

1703+
// Uri
1704+
1705+
public static TheoryData<Uri> UriData =
1706+
[
1707+
new Uri("https://xunit.net/"),
1708+
new Uri("a/b#c", UriKind.RelativeOrAbsolute),
1709+
];
1710+
1711+
[Theory]
1712+
[MemberData(nameof(UriData))]
1713+
public void Uri_Success(Uri uri)
1714+
{
1715+
Assert.Equivalent(uri, new Uri(uri.OriginalString, UriKind.RelativeOrAbsolute));
1716+
}
1717+
1718+
[Theory]
1719+
[MemberData(nameof(UriData))]
1720+
public void Uri_Failure(Uri uri)
1721+
{
1722+
var ex = Record.Exception(() => Assert.Equivalent(uri, new Uri("hello/world", UriKind.RelativeOrAbsolute)));
1723+
1724+
Assert.IsType<EquivalentException>(ex);
1725+
Assert.Equal(
1726+
"Assert.Equivalent() Failure" + Environment.NewLine +
1727+
"Expected: " + uri.OriginalString + Environment.NewLine +
1728+
"Actual: hello/world",
1729+
ex.Message
1730+
);
1731+
}
1732+
17031733
// Ensuring we use reference equality for the circular reference hash sets
17041734

17051735
[Theory]

test/test.xunit.assert/Asserts/TypeAssertsTests.cs

Lines changed: 266 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,71 @@ public void NullObject()
309309
}
310310
}
311311

312+
public class IsNotType_Generic_InexactMatch
313+
{
314+
[Fact]
315+
public void NullObject()
316+
{
317+
Assert.IsNotType<object>(null, exactMatch: false);
318+
}
319+
320+
[Fact]
321+
public void SameType()
322+
{
323+
var ex = new InvalidCastException();
324+
325+
var result = Record.Exception(() => Assert.IsNotType<InvalidCastException>(ex, exactMatch: false));
326+
327+
Assert.IsType<IsNotTypeException>(result);
328+
Assert.Equal<object>(
329+
"Assert.IsNotType() Failure: Value is a compatible type" + Environment.NewLine +
330+
"Expected: typeof(System.InvalidCastException)" + Environment.NewLine +
331+
"Actual: typeof(System.InvalidCastException)",
332+
result.Message
333+
);
334+
}
335+
336+
[Fact]
337+
public void BaseType()
338+
{
339+
var ex = new InvalidCastException();
340+
341+
var result = Record.Exception(() => Assert.IsNotType<Exception>(ex, exactMatch: false));
342+
343+
Assert.IsType<IsNotTypeException>(result);
344+
Assert.Equal<object>(
345+
"Assert.IsNotType() Failure: Value is a compatible type" + Environment.NewLine +
346+
"Expected: typeof(System.Exception)" + Environment.NewLine +
347+
"Actual: typeof(System.InvalidCastException)",
348+
result.Message
349+
);
350+
}
351+
352+
[Fact]
353+
public void Interface()
354+
{
355+
var ex = new DisposableClass();
356+
357+
#pragma warning disable xUnit2018 // TODO: Temporary until xUnit2018 is updated for the new signatures
358+
var result = Record.Exception(() => Assert.IsNotType<IDisposable>(ex, exactMatch: false));
359+
#pragma warning restore xUnit2018
360+
361+
Assert.IsType<IsNotTypeException>(result);
362+
Assert.Equal<object>(
363+
"Assert.IsNotType() Failure: Value is a compatible type" + Environment.NewLine +
364+
"Expected: typeof(System.IDisposable)" + Environment.NewLine +
365+
"Actual: typeof(TypeAssertsTests+DisposableClass)",
366+
result.Message
367+
);
368+
}
369+
370+
[Fact]
371+
public void IncompatibleType()
372+
{
373+
Assert.IsNotType<InvalidCastException>(new InvalidOperationException(), exactMatch: false);
374+
}
375+
}
376+
312377
#pragma warning disable xUnit2007 // Do not use typeof expression to check the type
313378

314379
public class IsNotType_NonGeneric
@@ -342,6 +407,69 @@ public void NullObject()
342407
}
343408
}
344409

410+
public class IsNotType_NonGeneric_InexactMatch
411+
{
412+
[Fact]
413+
public void NullObject()
414+
{
415+
Assert.IsNotType(typeof(object), null, exactMatch: false);
416+
}
417+
418+
[Fact]
419+
public void SameType()
420+
{
421+
var ex = new InvalidCastException();
422+
423+
var result = Record.Exception(() => Assert.IsNotType(typeof(InvalidCastException), ex, exactMatch: false));
424+
425+
Assert.IsType<IsNotTypeException>(result);
426+
Assert.Equal(
427+
"Assert.IsNotType() Failure: Value is a compatible type" + Environment.NewLine +
428+
"Expected: typeof(System.InvalidCastException)" + Environment.NewLine +
429+
"Actual: typeof(System.InvalidCastException)",
430+
result.Message
431+
);
432+
}
433+
434+
[Fact]
435+
public void BaseType()
436+
{
437+
var ex = new InvalidCastException();
438+
439+
var result = Record.Exception(() => Assert.IsNotType(typeof(Exception), ex, exactMatch: false));
440+
441+
Assert.IsType<IsNotTypeException>(result);
442+
Assert.Equal(
443+
"Assert.IsNotType() Failure: Value is a compatible type" + Environment.NewLine +
444+
"Expected: typeof(System.Exception)" + Environment.NewLine +
445+
"Actual: typeof(System.InvalidCastException)",
446+
result.Message
447+
);
448+
}
449+
450+
[Fact]
451+
public void Interface()
452+
{
453+
var ex = new DisposableClass();
454+
455+
var result = Record.Exception(() => Assert.IsNotType(typeof(IDisposable), ex, exactMatch: false));
456+
457+
Assert.IsType<IsNotTypeException>(result);
458+
Assert.Equal(
459+
"Assert.IsNotType() Failure: Value is a compatible type" + Environment.NewLine +
460+
"Expected: typeof(System.IDisposable)" + Environment.NewLine +
461+
"Actual: typeof(TypeAssertsTests+DisposableClass)",
462+
result.Message
463+
);
464+
}
465+
466+
[Fact]
467+
public void IncompatibleType()
468+
{
469+
Assert.IsNotType(typeof(InvalidCastException), new InvalidOperationException(), exactMatch: false);
470+
}
471+
}
472+
345473
#pragma warning restore xUnit2007 // Do not use typeof expression to check the type
346474

347475
public class IsType_Generic : TypeAssertsTests
@@ -415,6 +543,76 @@ public void NullObject()
415543
}
416544
}
417545

546+
public class IsType_Generic_InexactMatch
547+
{
548+
[Fact]
549+
public void NullObject()
550+
{
551+
var result = Record.Exception(() => Assert.IsType<object>(null, exactMatch: false));
552+
553+
Assert.IsType<IsTypeException>(result);
554+
Assert.Equal(
555+
"Assert.IsType() Failure: Value is null" + Environment.NewLine +
556+
"Expected: typeof(object)" + Environment.NewLine +
557+
"Actual: null",
558+
result.Message
559+
);
560+
}
561+
562+
[Fact]
563+
public void SameType()
564+
{
565+
var ex = new InvalidCastException();
566+
567+
Assert.IsType<InvalidCastException>(ex, exactMatch: false);
568+
}
569+
570+
[Fact]
571+
public void BaseType()
572+
{
573+
var ex = new InvalidCastException();
574+
575+
Assert.IsType<Exception>(ex, exactMatch: false);
576+
}
577+
578+
[Fact]
579+
public void Interface()
580+
{
581+
var ex = new DisposableClass();
582+
583+
#pragma warning disable xUnit2018 // TODO: Temporary until xUnit2018 is updated for the new signatures
584+
Assert.IsType<IDisposable>(ex, exactMatch: false);
585+
#pragma warning restore xUnit2018
586+
}
587+
588+
[Fact]
589+
public void ReturnsCastObject()
590+
{
591+
var ex = new InvalidCastException();
592+
593+
var result = Assert.IsType<InvalidCastException>(ex, exactMatch: false);
594+
595+
Assert.Same(ex, result);
596+
}
597+
598+
[Fact]
599+
public void IncompatibleType()
600+
{
601+
var result =
602+
Record.Exception(
603+
() => Assert.IsType<InvalidCastException>(new InvalidOperationException(), exactMatch: false)
604+
);
605+
606+
Assert.IsType<IsTypeException>(result);
607+
Assert.Equal(
608+
"Assert.IsType() Failure: Value is an incompatible type" + Environment.NewLine +
609+
"Expected: typeof(System.InvalidCastException)" + Environment.NewLine +
610+
"Actual: typeof(System.InvalidOperationException)",
611+
result.Message
612+
);
613+
}
614+
}
615+
418616
#pragma warning disable xUnit2007 // Do not use typeof expression to check the type
419617

420618
public class IsType_NonGeneric : TypeAssertsTests
@@ -480,6 +678,74 @@ public void NullObjectThrows()
480678
}
481679
}
482680

681+
public class IsType_NonGeneric_InexactMatch
682+
{
683+
[Fact]
684+
public void NullObject()
685+
{
686+
var result = Record.Exception(() => Assert.IsType(typeof(object), null, exactMatch: false));
687+
688+
Assert.IsType<IsTypeException>(result);
689+
Assert.Equal(
690+
"Assert.IsType() Failure: Value is null" + Environment.NewLine +
691+
"Expected: typeof(object)" + Environment.NewLine +
692+
"Actual: null",
693+
result.Message
694+
);
695+
}
696+
697+
[Fact]
698+
public void SameType()
699+
{
700+
var ex = new InvalidCastException();
701+
702+
Assert.IsType(typeof(InvalidCastException), ex, exactMatch: false);
703+
}
704+
705+
[Fact]
706+
public void BaseType()
707+
{
708+
var ex = new InvalidCastException();
709+
710+
Assert.IsType(typeof(Exception), ex, exactMatch: false);
711+
}
712+
713+
[Fact]
714+
public void Interface()
715+
{
716+
var ex = new DisposableClass();
717+
718+
Assert.IsType(typeof(IDisposable), ex, exactMatch: false);
719+
}
720+
721+
[Fact]
722+
public void ReturnsCastObject()
723+
{
724+
var ex = new InvalidCastException();
725+
726+
var result = Assert.IsType<InvalidCastException>(ex, exactMatch: false);
727+
728+
Assert.Same(ex, result);
729+
}
730+
731+
[Fact]
732+
public void IncompatibleType()
733+
{
734+
var result =
735+
Record.Exception(
736+
() => Assert.IsType(typeof(InvalidCastException), new InvalidOperationException(), exactMatch: false)
737+
);
738+
739+
Assert.IsType<IsTypeException>(result);
740+
Assert.Equal(
741+
"Assert.IsType() Failure: Value is an incompatible type" + Environment.NewLine +
742+
"Expected: typeof(System.InvalidCastException)" + Environment.NewLine +
743+
"Actual: typeof(System.InvalidOperationException)",
744+
result.Message
745+
);
746+
}
747+
}
748+
483749
#pragma warning restore xUnit2007 // Do not use typeof expression to check the type
484750

485751
class DisposableClass : IDisposable

0 commit comments

Comments
 (0)
0