~anuraj.p/nunitv2/GUIFindTest

« back to all changes in this revision

Viewing changes to src/NUnitCore/tests-net45/ThrowsTests.cs

  • Committer: Simone
  • Author(s): Simone Busoli
  • Date: 2012-10-28 14:36:34 UTC
  • Revision ID: simone.busoli@vienna-20121028143634-3bbd7uuche6vub4h
Support for async anonymous delegates in Throws assertions

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#if NET_3_5 || NET_4_0 || NET_4_5
 
2
using System;
 
3
using System.Threading.Tasks;
 
4
using NUnit.Framework;
 
5
using NUnit.Framework.Constraints;
 
6
 
 
7
namespace nunit.core.tests.net45
 
8
{
 
9
        [TestFixture]
 
10
        public class ThrowsTests
 
11
        {
 
12
                [Test]
 
13
                public void ThrowsConstraintAsyncTask()
 
14
                {
 
15
                        Assert.IsTrue(new ThrowsConstraint(new ExactTypeConstraint(typeof(InvalidOperationException)))
 
16
                                .Matches(new TestDelegate(async () => await ThrowAsyncNonGenericTask())));
 
17
                }
 
18
 
 
19
                [Test]
 
20
                public void ThrowsConstraintAsyncGenericTask()
 
21
                {
 
22
                        Assert.IsTrue(new ThrowsConstraint(new ExactTypeConstraint(typeof(InvalidOperationException)))
 
23
                                .Matches(new TestDelegate(async () => { await ThrowAsyncGenericTask(); })));
 
24
                }
 
25
 
 
26
                [Test]
 
27
                public void ThrowsConstraintAsyncTaskRunSynchronously()
 
28
                {
 
29
#pragma warning disable 1998
 
30
                        Assert.IsTrue(new ThrowsConstraint(new ExactTypeConstraint(typeof(InvalidOperationException)))
 
31
                                .Matches(new TestDelegate(async () => { throw new InvalidOperationException(); })));
 
32
#pragma warning restore 1998
 
33
                }
 
34
 
 
35
                [Test]
 
36
                public void AssertThrows()
 
37
                {
 
38
                        Assert.Throws(typeof(InvalidOperationException), async () => await ThrowAsyncNonGenericTask());
 
39
                }
 
40
 
 
41
                [Test]
 
42
                public void AssertThrowsGenericTask()
 
43
                {
 
44
                        Assert.Throws(typeof(InvalidOperationException), async () => await ThrowAsyncGenericTask());
 
45
                }
 
46
 
 
47
                [Test]
 
48
                public void AssertThat()
 
49
                {
 
50
                        Assert.That(async () => await ThrowAsyncNonGenericTask(), Throws.TypeOf<InvalidOperationException>());
 
51
                }
 
52
 
 
53
                [Test]
 
54
                public void AssertThatGenericTask()
 
55
                {
 
56
                        Assert.That(async () => await ThrowAsyncGenericTask(), Throws.TypeOf<InvalidOperationException>());
 
57
                }
 
58
 
 
59
                [Test]
 
60
                public void AssertThrowsGeneric()
 
61
                {
 
62
                        Assert.Throws<InvalidOperationException>(async () => await ThrowAsyncNonGenericTask());
 
63
                }
 
64
 
 
65
                [Test]
 
66
                public void AssertThrowsGenericWithGenericTask()
 
67
                {
 
68
                        Assert.Throws<InvalidOperationException>(async () => await ThrowAsyncGenericTask());
 
69
                }
 
70
 
 
71
                private static async Task ThrowAsyncNonGenericTask()
 
72
                {
 
73
                        await Task.Run(() => 1);
 
74
                        throw new InvalidOperationException();
 
75
                }
 
76
 
 
77
                private static async Task<int> ThrowAsyncGenericTask()
 
78
                {
 
79
                        await ThrowAsyncNonGenericTask();
 
80
                        return await Task.Run(() => 1);
 
81
                }
 
82
        }
 
83
}
 
84
#endif
 
 
b'\\ No newline at end of file'