comparison UnitTests/CS/Patterns/MustImplementAttributeTest.cs @ 0:f990fcb411a9

Копия текущей версии из github
author cin
date Thu, 27 Mar 2014 21:46:09 +0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:f990fcb411a9
1 using System;
2
3 using NUnit.Framework;
4
5 using BLToolkit.Patterns;
6 using BLToolkit.TypeBuilder;
7
8 namespace Patterns
9 {
10 [TestFixture]
11 public class MustImplementAttributeTest
12 {
13 [MustImplement]
14 public interface IRequiredInterface
15 {
16 int RequiredMethod();
17 [MustImplement(false)]
18 int SameMethodName();
19 [MustImplement(false)]
20 int OptionalMethod();
21 }
22
23 public interface ISameMethodNameInterface
24 {
25 [MustImplement]
26 int SameMethodName();
27 }
28
29 public interface IIntermediateInterface : IRequiredInterface, ISameMethodNameInterface
30 {
31 [MustImplement(false, ThrowException = true)]
32 new int OptionalMethod();
33 }
34
35 public interface IOptionalInterface : IIntermediateInterface
36 {
37 }
38
39 [MustImplement(false, ThrowException = false)]
40 public interface IOptionalInterfaceNoException : IRequiredInterface
41 {
42 int OtherOptionalMethod();
43 }
44
45 [MustImplement(true, ThrowException = false)]
46 public interface IOtherOptionalInterface
47 {
48 int SameMethodName();
49 }
50
51 public struct TestClass
52 {
53 public int RequiredMethod()
54 {
55 return 1;
56 }
57
58 public int SameMethodName()
59 {
60 return 2;
61 }
62 }
63
64 public class EmptyClass
65 {
66 }
67
68 [Test]
69 public void Test()
70 {
71 var duck = DuckTyping.Implement<IOptionalInterfaceNoException> (new TestClass());
72
73 Assert.AreEqual(1, duck.RequiredMethod());
74 Assert.AreEqual(0, duck.OtherOptionalMethod());
75 Assert.AreEqual(2, duck.SameMethodName());
76
77 duck = DuckTyping.Aggregate<IOptionalInterfaceNoException>(new TestClass(), string.Empty, Guid.Empty);
78
79 Assert.AreEqual(1, duck.RequiredMethod());
80 Assert.AreEqual(0, duck.OtherOptionalMethod());
81 Assert.AreEqual(2, duck.SameMethodName());
82 }
83
84 [Test, ExpectedException(typeof(InvalidOperationException))]
85 public void RuntimeExceptionTest()
86 {
87 var duck = DuckTyping.Implement<IOptionalInterface>(new TestClass());
88
89 Assert.AreEqual(1, duck.RequiredMethod());
90
91 // Exception here.
92 //
93 duck.OptionalMethod();
94 }
95
96 [Test, ExpectedException(typeof(InvalidOperationException))]
97 public void RuntimeAggregateExceptionTest()
98 {
99 var duck = DuckTyping.Aggregate<IOptionalInterface>(new TestClass(), new EmptyClass());
100
101 Assert.AreEqual(1, duck.RequiredMethod());
102
103 // Exception here.
104 //
105 duck.OptionalMethod();
106 }
107
108 [Test, ExpectedException(typeof(TypeBuilderException))]
109 public void BuildtimeExceptionTest()
110 {
111 // Exception here.
112 //
113 var duck1 = DuckTyping.Implement<IOptionalInterface> (string.Empty);
114 }
115
116 [Test, ExpectedException(typeof(TypeBuilderException))]
117 public void BuildtimeAggregateExceptionTest()
118 {
119 // Exception here.
120 //
121 var duck1 = DuckTyping.Aggregate<IOptionalInterface>(string.Empty, Guid.Empty);
122 }
123
124 [Test]
125 public void AsLikeBehaviourTest()
126 {
127 var duck = DuckTyping.Implement<IOtherOptionalInterface>(new TestClass());
128
129 Assert.IsNotNull(duck);
130
131 duck = DuckTyping.Implement<IOtherOptionalInterface>(new EmptyClass());
132 Assert.IsNull(duck);
133
134 duck = DuckTyping.Implement<IOtherOptionalInterface>(new EmptyClass());
135 Assert.IsNull (duck);
136
137 duck = DuckTyping.Aggregate<IOtherOptionalInterface>(new EmptyClass(), string.Empty);
138 Assert.IsNull (duck);
139 }
140 }
141 }