CSLA.NET 6.0.0
CSLA .NET is a software development framework that helps you build a reusable, maintainable object-oriented business layer for your app.
UndoTests.cs
Go to the documentation of this file.
1//-----------------------------------------------------------------------
2// <copyright file="UndoTests.cs" company="Marimer LLC">
3// Copyright (c) Marimer LLC. All rights reserved.
4// Website: https://cslanet.com
5// </copyright>
6// <summary>no summary</summary>
7//-----------------------------------------------------------------------
8using System;
9using System.Net;
10using System.Windows;
11#if !__ANDROID__
12using System.Windows.Controls;
13using System.Windows.Documents;
14using System.Windows.Ink;
15using System.Windows.Input;
16using System.Windows.Media;
17using System.Windows.Media.Animation;
18using System.Windows.Shapes;
19#endif
20using Csla.Core;
21using UnitDriven;
23using Csla;
24
25#if NUNIT
26using NUnit.Framework;
27using TestClass = NUnit.Framework.TestFixtureAttribute;
28using TestInitialize = NUnit.Framework.SetUpAttribute;
29using TestCleanup = NUnit.Framework.TearDownAttribute;
30using TestMethod = NUnit.Framework.TestAttribute;
31#elif MSTEST
32using Microsoft.VisualStudio.TestTools.UnitTesting;
33#endif
34
36{
37#if TESTING
38 [System.Diagnostics.DebuggerStepThrough]
39#endif
40 [TestClass]
41 public class UndoTests : TestBase
42 {
43 [TestMethod]
44 public void EditUndoSuccess()
45 {
46 UnitTestContext context = GetContext();
47 int expected = 1;
48
49 Person p = new Person();
50 p.Age = expected;
51 p.BeginEdit();
52 p.Age = 2;
53 p.CancelEdit();
54
55 context.Assert.AreEqual(expected, p.Age);
56 context.Assert.Success();
57 context.Complete();
58 }
59
60 [TestMethod]
62 {
63 UnitTestContext context = GetContext();
64 Person p = new Person();
65 p.Age = 1;
66 p.BeginEdit();
67
68 p.Age = 2;
69 p.BeginEdit();
70
71 p.Age = 3;
72
73 context.Assert.AreEqual(3, p.Age);
74 p.CancelEdit();
75 context.Assert.AreEqual(2, p.Age);
76 p.CancelEdit();
77 context.Assert.AreEqual(1, p.Age);
78 context.Assert.Success();
79 context.Complete();
80 }
81
82 [TestMethod]
84 {
85 UnitTestContext context = GetContext();
86 Person p = new Person();
87 // p.Age; calling this property initializes it! So set it explicitly.
88 int expected = (DateTime.Now - new DateTime(1, 1, 1)).Days / 365; //2008;
89 p.BeginEdit();
90 p.Age = 100;
91 p.CancelEdit();
92
93 context.Assert.AreEqual(expected, p.Age);
94 context.Assert.Success();
95 context.Complete();
96 }
97
98 [TestMethod]
99 public void InvalidUndo()
100 {
101 UnitTestContext context = GetContext();
102 Person p = new Person();
103 p.CancelEdit();
104 context.Assert.Success();
105 context.Complete();
106 }
107
108 [TestMethod]
109 public void InvalidApply()
110 {
111 UnitTestContext context = GetContext();
112 Person p = new Person();
113 p.ApplyEdit();
114 context.Assert.Success();
115 context.Complete();
116 }
117
118 [TestMethod]
119 public void UndoWithChild()
120 {
121 UnitTestContext context = GetContext();
122 Person p = new Person();
123 p.Addresses = new AddressList();
124 Address a = new Address();
125 p.Addresses.Add(a);
126
127 int age1 = p.Age;
128 string city1 = a.City;
129 p.BeginEdit();
130
131 int age2 = p.Age = 2;
132 string city2 = a.City = "two";
133
134 context.Assert.AreEqual(age2, p.Age);
135 context.Assert.AreEqual(city2, a.City);
136 p.CancelEdit();
137
138 context.Assert.AreEqual(age1, p.Age);
139 context.Assert.AreEqual(city1, a.City);
140 context.Assert.Success();
141 context.Complete();
142 }
143
144 [TestMethod]
145 public void UndoChildFail()
146 {
147 UnitTestContext context = GetContext();
148 Person p = new Person();
149 p.Addresses = new AddressList();
150 Address a = new Address();
151 p.Addresses.Add(a);
152
153 p.BeginEdit();
154 p.CancelEdit();
155 a.CancelEdit();
156 context.Assert.Success();
157 context.Complete();
158 }
159
160 [TestMethod]
162 {
163 UnitTestContext context = GetContext();
164 Person p = new Person();
165 p.Addresses = new AddressList();
166 Address a = new Address();
167 p.Addresses.Add(a);
168
169 p.BeginEdit();
170 p.CancelEdit();
171 a.ApplyEdit();
172 context.Assert.Success();
173 context.Complete();
174 }
175
176 [TestMethod]
177 public void UndoChildSuccess()
178 {
179 UnitTestContext context = GetContext();
180 Person p = new Person();
181 p.Addresses = new AddressList();
182 Address a = new Address();
183 p.Addresses.Add(a);
184
185 int age1 = p.Age = 1;
186 string city1 = a.City = "one";
187 p.BeginEdit();
188
189 int age2 = p.Age = 2;
190 string city2 = a.City = "two";
191 a.BeginEdit();
192
193 string city3 = a.City = "three";
194 a.CancelEdit();
195
196 context.Assert.AreEqual(age2, p.Age);
197 context.Assert.AreEqual(city2, a.City);
198 p.CancelEdit();
199
200 context.Assert.AreEqual(age1, p.Age);
201 context.Assert.AreEqual(city1, a.City);
202 context.Assert.Success();
203 context.Complete();
204 }
205
206 [TestMethod]
208 {
209 UnitTestContext context = GetContext();
210 Person p = new Person();
211 p.BeginEdit();
212 p.ApplyEdit();
213 p.CancelEdit();
214 context.Assert.Success();
215 context.Complete();
216 }
217
218 [TestMethod]
219 public void DoubleApply()
220 {
221 UnitTestContext context = GetContext();
222 Person p = new Person();
223 p.BeginEdit();
224 p.ApplyEdit();
225 p.ApplyEdit();
226 context.Assert.Success();
227 context.Complete();
228 }
229
230 [TestMethod]
231 public void ApplyEditSuccess()
232 {
233 UnitTestContext context = GetContext();
234 Person p = new Person();
235 p.Age = 1;
236 p.BeginEdit();
237 p.Age = 2;
238 p.ApplyEdit();
239
240 context.Assert.AreEqual(2, p.Age);
241 context.Assert.Success();
242 context.Complete();
243 }
244
245 [TestMethod]
247 {
248 UnitTestContext context = GetContext();
249 Person p = new Person();
250 p.Addresses = new AddressList();
251 Address a = new Address();
252 p.Addresses.Add(a);
253
254 p.Age = 1;
255 a.City = "one";
256 p.BeginEdit();
257
258 p.Age = 2;
259 a.City = "two";
260 p.ApplyEdit();
261
262 context.Assert.AreEqual(2, p.Age);
263 context.Assert.AreEqual("two", a.City);
264 context.Assert.Success();
265 context.Complete();
266 }
267
268 [TestMethod]
269 [ExpectedException(typeof(UndoException))]
271 {
272 UnitTestContext context = GetContext();
273 Person p = new Person();
274 p.Addresses = new AddressList();
275 Address a = new Address();
276 p.Addresses.Add(a);
277
278 p.BeginEdit();
279 a.ApplyEdit();
280 context.Assert.Try( p.ApplyEdit );
281 context.Complete();
282 }
283
284 [TestMethod]
286 {
287 UnitTestContext context = GetContext();
288 Person p = new Person();
289 p.Addresses = new AddressList();
290 Address a = new Address();
291 a.ZipCode = "0";
292 p.Addresses.Add(a);
293 Address a1 = new Address();
294 a1.ZipCode = "1";
295 p.Addresses.Add(a1);
296
297 int age1 = p.Age;
298 string city1 = a.City;
299
300 p.BeginEdit();
301
302 int age2 = p.Age = 2;
303 string city2 = a.City = "two";
304 p.Addresses[0].ZipCode = "000";
305
306 Address a2 = new Address();
307 a2.ZipCode = "2";
308 p.Addresses.Add(a2);
309 Address a3 = new Address();
310 a3.ZipCode = "3";
311 p.Addresses.Add(a3);
312 p.Addresses.RemoveAt(0);
313
314 context.Assert.AreEqual(age2, p.Age);
315 context.Assert.AreEqual(city2, a.City);
316 p.CancelEdit();
317
318 context.Assert.AreEqual(age1, p.Age);
319 context.Assert.AreEqual(city1, a.City);
320 context.Assert.AreEqual("0", a.ZipCode);
321 context.Assert.AreEqual(2, p.Addresses.Count);
322 context.Assert.Success();
323 context.Complete();
324 }
325
326 [TestMethod]
327 [ExpectedException(typeof(UndoException))]
329 {
330 using (var context = GetContext())
331 {
332 Person p = new Person();
333 p.Addresses = new AddressList();
334 Address a = new Address();
335 p.Addresses.Add(a);
336
337 p.BeginEdit();
338 a.BeginEdit();
339 context.Assert.Try(p.CancelEdit);
340 }
341 }
342 }
343}
Exception indicating a problem with the use of the n-level undo feature in CSLA .NET.
void Try(Action p)
Definition: Asserter.cs:60
UnitTestContext GetContext()
Definition: TestBase.cs:12