-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path06_Inheritance.cs
230 lines (171 loc) · 8.8 KB
/
06_Inheritance.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
using System;
using System.Diagnostics.CodeAnalysis;
using System.Text;
using basic.Common;
using Xunit;
namespace basic
{
public class Inheritance
{
[Fact]
public void should_access_public_memeber_in_derived_class()
{
var demoClass = new InheritMemberAccessDemoClass();
// please change the variable value to fix the test.
const string expected = "Public Property Value";
Assert.Equal(expected, demoClass.PublicProperty);
}
[Fact]
public void should_access_protected_member_in_derived_class()
{
var demoClass = new InheritMemberAccessDemoClass();
string actualValue = demoClass.ManipulateProtectedMember();
// please change the variable value to fix the test.
const string expected = "The value is Protected Property Value";
Assert.Equal(expected, actualValue);
}
[Fact]
public void should_access_member_of_most_derived_class()
{
var demoClass = new PolymorphismDemoClass();
var castToBaseClass = (PolymorphismDemoClassBase) demoClass;
string actualValue = castToBaseClass.VirtualMethod();
// please change the variable value to fix the test.
const string expected = "DerivedClass";
Assert.Equal(expected, actualValue);
}
[Fact]
[SuppressMessage("ReSharper", "ConditionIsAlwaysTrueOrFalse")]
[SuppressMessage("ReSharper", "TryCastAlwaysSucceeds")]
[SuppressMessage("ReSharper", "UseNegatedPatternMatching")]
public void should_return_casted_result_if_it_is_castable()
{
var demoClass = new PolymorphismDemoClass();
var castToBaseClass = demoClass as PolymorphismDemoClassBase;
bool isNull = castToBaseClass == null;
// please change the variable value to fix the test.
const bool expected = false;
Assert.Equal(expected, isNull);
}
[Fact]
[SuppressMessage("ReSharper", "UseNegatedPatternMatching")]
[SuppressMessage("ReSharper", "ExpressionIsAlwaysNull")]
[SuppressMessage("ReSharper", "ConditionIsAlwaysTrueOrFalse")]
public void should_return_null_if_it_is_not_castable()
{
var demoClass = new PolymorphismDemoClass();
object castToObject = demoClass;
var castResult = castToObject as StringBuilder;
bool isNull = castResult == null;
// please change the variable value to fix the test.
const bool expected = true;
Assert.Equal(expected, isNull);
}
[Fact]
public void should_throw_exception_if_it_is_not_castable()
{
var demoClass = new PolymorphismDemoClass();
object castToObject = demoClass;
// please change the variable value to fix the test.
Type expectedExceptionType = typeof(InvalidCastException);
Assert.NotEqual(typeof(SystemException), expectedExceptionType);
Assert.NotEqual(typeof(Exception), expectedExceptionType);
Assert.Throws(expectedExceptionType, () => (StringBuilder) castToObject);
}
[Fact]
public void should_reference_to_same_object_after_casting()
{
var demoClass = new PolymorphismDemoClass();
var castToBaseClass = (PolymorphismDemoClassBase)demoClass;
bool referenceEqual = ReferenceEquals(demoClass, castToBaseClass);
// please change the variable value to fix the test.
const bool expected = true;
Assert.Equal(expected, referenceEqual);
}
[Fact]
public void should_throw_exception_when_downcasting_fail()
{
var demoClassBase = new PolymorphismDemoClassBase();
// please change the variable value to fix the test.
Type expectedExceptionType = typeof(InvalidCastException);
Assert.NotEqual(typeof(SystemException), expectedExceptionType);
Assert.NotEqual(typeof(Exception), expectedExceptionType);
Assert.Throws(expectedExceptionType, () => (PolymorphismDemoClass)demoClassBase);
}
[Fact]
public void should_be_able_to_hide_non_virtual_method_in_derived_class()
{
var demoClass = new HideMemberDemoClass();
var castedToBaseClass = (HideMemberDemoClassBase) demoClass;
string methodReturnValue = demoClass.MethodToHide();
string baseClassMethodReturnValue = castedToBaseClass.MethodToHide();
// please change the following 2 variable values to fix the test.
const string expectedMethodReturnValue = "HideMemberDemoClass::MethodToHide()";
const string expectedBaseClassMethodReturnValue = "HideMemberDemoClassBase::MethodToHide()";
Assert.Equal(expectedMethodReturnValue, methodReturnValue);
Assert.Equal(expectedBaseClassMethodReturnValue, baseClassMethodReturnValue);
}
[Fact]
public void should_be_able_to_get_base_class_members_using_base_keyword()
{
var demoClass = new BaseKeywordDemoClass();
string name = demoClass.Name;
// please change the variable value to fix the test.
const string expected = "BaseClass's derived class.";
Assert.Equal(expected, name);
}
[Fact]
public void should_call_default_constructors_of_base_class()
{
var demoClass = new InheritanceConstructorCallDemoClass();
string message = demoClass.ConstructorCallMessage;
// please change the variable value to fix the test.
const string expected = "InheritanceConstructorCallDemoClassBase::Ctor()\nInheritanceConstructorCallDemoClass::Ctor()\n";
Assert.Equal(expected, message);
}
[Fact]
public void should_call_default_constructor_of_base_class_when_call_derived_ctor_with_args()
{
var demoClass = new InheritanceConstructorCallDemoClass(1);
string message = demoClass.ConstructorCallMessage;
// please change the variable value to fix the test.
const string expected = "InheritanceConstructorCallDemoClassBase::Ctor()\nInheritanceConstructorCallDemoClass::Ctor(int)\n";
Assert.Equal(expected, message);
}
[Fact]
public void should_be_able_to_specify_which_base_ctor_to_call()
{
var demoClass = new InheritanceConstructorCallDemoClass("1");
string message = demoClass.ConstructorCallMessage;
// please change the variable value to fix the test.
const string expected = "InheritanceConstructorCallDemoClassBase::Ctor(int)\nInheritanceConstructorCallDemoClass::Ctor(string)\n";
Assert.Equal(expected, message);
}
[Fact]
public void should_be_able_to_specify_which_ctor_of_current_class_to_call()
{
var demoClass = new InheritanceConstructorCallDemoClass(1, "1");
string message = demoClass.ConstructorCallMessage;
// please change the variable value to fix the test.
const string expected = "InheritanceConstructorCallDemoClassBase::Ctor()\nInheritanceConstructorCallDemoClass::Ctor(int)\nInheritanceConstructorCallDemoClass::Ctor(int, string)\n";
Assert.Equal(expected, message);
}
[Fact]
public void should_be_able_to_choose_most_specific_type_when_overloading()
{
var demoClass = new MethodOverloadDemoClass();
string returnValueForBaseClassOverloading =
demoClass.Foo(new MethodOverloadBaseClass());
string returnValueForDerivedClassOverloading =
demoClass.Foo(new MethodOverloadDerivedClass());
string returnValueForCastingOverloading =
demoClass.Foo((MethodOverloadBaseClass) (new MethodOverloadDerivedClass()));
const string expectedBaseClassOverloadingValue = "Foo(MethodOverloadBaseClass)";
const string expectedDerivedClassOverloadingValue = "Foo(MethodOverloadDerivedClass)";
const string expectedCastOverloadingValue = "Foo(MethodOverloadBaseClass)";
Assert.Equal(expectedBaseClassOverloadingValue, returnValueForBaseClassOverloading);
Assert.Equal(expectedDerivedClassOverloadingValue, returnValueForDerivedClassOverloading);
Assert.Equal(expectedCastOverloadingValue, returnValueForCastingOverloading);
}
}
}