How to use DebugView class of Telerik.JustMock.Diagnostics package

Best JustMockLite code snippet using Telerik.JustMock.Diagnostics.DebugView

Run JustMockLite automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

DebugView.cs

Source: DebugView.cs Github

copy
1/*
2 JustMock Lite
3 Copyright © 2010-2015,2018,2020 Progress Software Corporation
4
5   Licensed under the Apache License, Version 2.0 (the "License");
6   you may not use this file except in compliance with the License.
7   You may obtain a copy of the License at
8
9   http://www.apache.org/licenses/LICENSE-2.0
10
11   Unless required by applicable law or agreed to in writing, software
12   distributed under the License is distributed on an "AS IS" BASIS,
13   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   See the License for the specific language governing permissions and
15   limitations under the License.
16*/
17
18using System;
19using System.Diagnostics;
20using System.Linq;
21using System.Text;
22using Telerik.JustMock.Core;
23using Telerik.JustMock.Core.Context;
24#if !PORTABLE
25using Telerik.JustMock.Plugins;
26#endif
27
28namespace Telerik.JustMock
29{
30	using Telerik.JustMock.Diagnostics;
31
32	/// <summary>
33	/// Provides introspection and tracing capabilities for ease of debugging failing tests.
34	/// </summary>
35	/// <remarks>
36	/// Often it’s not very clear why a test that uses the mocking API fails.
37	/// A test may have multiple arrangements, each with various overlapping
38	/// argument matchers. When you have a complex set of arrangements and the
39	/// system under test makes a call to a mock, it’s sometimes hard to
40	/// understand which arrangement actually gets executed and which
41	/// expectations get updated. The <see cref="DebugView"/> class can help in such times.
42	/// It can be used in two ways – to provide an overview of the current
43	/// internal state of the mocking API, and to provide a step-by-step replay
44	/// of the interception events happening inside the mocking API.
45	/// 
46	/// The current internal state is exposed through the <see cref="CurrentState"/> property.
47	/// It contains a human-readable description of the current state of the
48	/// mocking API. It describes in detail the state of all occurrence
49	/// expectations and the number of calls to all intercepted methods. The
50	/// first part is useful when debugging failing expectations from
51	/// arrangements. The second part is useful for debugging failing occurrence
52	/// asserts.
53	/// 
54	/// The step-by-step replay is intended for use with an interactive debugger
55	/// (e.g. the Visual Studio managed debugger). To begin using it, add the
56	/// DebugView class to a Watch in the debugger. Break the test execution
57	/// before your test begins. Set the <see cref="IsTraceEnabled"/> property to true from
58	/// the Watch window. Now, as you step over each line in your test, the
59	/// <see cref="FullTrace"/> and <see cref="LastTrace"/> properties will be updated to show the events
60	/// happening inside the mocking API. <see cref="FullTrace"/> will show the entire event
61	/// log so far. <see cref="LastTrace"/> will contain all events that have happened since
62	/// the debugger last updated the Watch window, but only if any new events
63	/// have actually occurred.
64	/// </remarks>
65	public static class DebugView
66	{
67		/// <summary>
68		/// Shows in human-readable format the current state of the mocking API internals.
69		/// </summary>
70		public static string CurrentState
71		{
72			get
73			{
74				return ProfilerInterceptor.GuardInternal(() =>
75					{
76						var repo = MockingContext.ResolveRepository(UnresolvedContextBehavior.DoNotCreateNew);
77						if (repo == null)
78							return "N/A";
79
80						var activeTrace = traceSink;
81						traceSink = null;
82						var debugView = repo.GetDebugView();
83						traceSink = activeTrace;
84						return debugView;
85					});
86			}
87		}
88
89		/// <summary>
90		/// Enables or disables the step-by-step event trace.
91		/// </summary>
92		public static bool IsTraceEnabled
93		{
94			get
95			{
96				return ProfilerInterceptor.GuardInternal(
97					() =>
98						traceSink != null && (traceSink.TraceOptions & TraceOptions.InternalTrace) != 0);
99			}
100			set
101			{
102				ProfilerInterceptor.GuardInternal(
103					() =>
104						{
105							if (value)
106							{
107								if (traceSink == null)
108								{
109									traceSink = new Trace();
110								}
111
112								(traceSink as Trace).TraceOptions |= TraceOptions.InternalTrace;
113							}
114							else
115							{
116								if (traceSink != null)
117								{
118									(traceSink as Trace).TraceOptions ^= TraceOptions.InternalTrace;
119								}
120							}
121						});
122			}
123		}
124
125		internal static bool IsRemoteTraceEnabled
126		{
127			get
128			{
129				return ProfilerInterceptor.GuardInternal(
130					() =>
131						(traceSink.TraceOptions & TraceOptions.RemoteTrace) != 0);
132			}
133			set
134			{
135				ProfilerInterceptor.GuardInternal(
136					() =>
137						{
138							if (value)
139							{
140								if (traceSink == null)
141								{
142									traceSink = new Trace();
143								}
144
145								(traceSink as Trace).TraceOptions |= TraceOptions.RemoteTrace;
146							}
147							else
148							{
149								if (traceSink != null)
150								{
151									(traceSink as Trace).TraceOptions ^= TraceOptions.RemoteTrace;
152								}
153							}
154						});
155			}
156		}
157
158		/// <summary>
159		/// Shows the entire event log when the event trace is enabled.
160		/// </summary>
161		public static string FullTrace
162		{
163			get { return ProfilerInterceptor.GuardInternal(() => traceSink != null ? traceSink.FullTrace : null); }
164		}
165
166		/// <summary>
167		/// When the event trace is enabled, this property shows the portion
168		/// of the event log that was added since the property was last evaluated.
169		/// </summary>
170		public static string LastTrace
171		{
172			get { return ProfilerInterceptor.GuardInternal(() => traceSink != null ? traceSink.LastTrace : null); }
173		}
174
175		internal static void TraceEvent(IndentLevel traceLevel, Func<string> message)
176		{
177			var activeTrace = DebugView.traceSink;
178			if (activeTrace == null || activeTrace.TraceOptions == TraceOptions.Disabled)
179			{
180				return;
181			}
182
183			string messageStr = null;
184			try
185			{
186				messageStr = message() ?? String.Empty;
187			}
188			catch (Exception ex)
189			{
190				messageStr = "[Exception thrown]\n" + ex;
191			}
192
193			var formattedMessage = String.Join(Environment.NewLine, messageStr.Split('\n')
194					.Select(line => String.Format("{0}{1}", traceLevel.AsIndent(), line.TrimEnd())).ToArray())
195				+ (traceLevel.IsLeaf() ? "" : ":");
196
197			activeTrace.TraceEvent(formattedMessage);
198
199			GC.KeepAlive(CurrentState); // for coverage testing
200		}
201
202		private static string AsIndent(this IndentLevel traceLevel)
203		{
204			return "".Join(Enumerable.Repeat("    ", (int)traceLevel));
205		}
206
207		internal static void PrintStackTrace()
208		{
209			TraceEvent(IndentLevel.StackTrace, () => "Stack trace:\n" + MockingContext.GetStackTrace(IndentLevel.StackTraceInner.AsIndent()));
210		}
211
212		internal static Exception GetStateAsException()
213		{
214			return IsTraceEnabled ? new DebugViewDetailsException() : null;
215		}
216
217		private static ITraceSink traceSink;
218
219		private static bool IsLeaf(this IndentLevel level)
220		{
221			switch (level)
222			{
223				case IndentLevel.DispatchResult:
224				case IndentLevel.Matcher:
225					return true;
226				default:
227					return false;
228			}
229		}
230
231#if (DEBUG && !COREFX && !NETCORE)
232		public static void SaveProxyAssembly()
233		{
234			ProfilerInterceptor.GuardInternal(() => DynamicProxyMockFactory.SaveAssembly());
235		}
236#endif
237
238		internal static Action<string> DebugTrace = s =>
239#if PORTABLE
240			System.Diagnostics.Debug.WriteLine(s);
241#else
242			System.Diagnostics.Trace.WriteLine(s);
243#endif
244	}
245}
246
247namespace Telerik.JustMock.Diagnostics
248{
249	/// <summary>
250	/// This exception provides additional information when assertion failures are produced.
251	/// </summary>
252	[Serializable]
253	public sealed class DebugViewDetailsException : Exception
254	{
255		internal DebugViewDetailsException()
256			: base(String.Format("State:\n{0}\n\nFull trace:\n{1}", DebugView.CurrentState, DebugView.FullTrace))
257		{ }
258
259#if !COREFX
260		private DebugViewDetailsException(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) : base(info, context) { }
261#endif
262	}
263
264	internal enum IndentLevel
265	{
266		Dispatch = 0,
267		Warning = 0,
268		Configuration = 0,
269		MethodMatch = 1,
270		DispatchResult = 1,
271		Matcher = 2,
272		StackTrace = 1,
273		StackTraceInner = 2,
274	}
275
276	internal interface ITrace
277	{
278		string FullTrace { get; }
279		string LastTrace { get; }
280	}
281
282
283	[Flags]
284	internal enum TraceOptions
285	{
286		Disabled = 0,
287		InternalTrace = 1,
288		RemoteTrace= 2
289	}
290
291	internal interface ITraceSink : ITrace
292	{
293		TraceOptions TraceOptions { get; }
294
295		void TraceEvent(string message);
296	}
297
298	internal class Trace : ITraceSink
299	{
300		private readonly object traceSync = new object();
301		private readonly StringBuilder log = new StringBuilder();
302		private readonly StringBuilder currentTrace = new StringBuilder();
303		private bool currentTraceRead;
304		private TraceOptions traceOptions = TraceOptions.Disabled;
305
306		public string FullTrace
307		{
308			get
309			{
310				lock (this.traceSync)
311				{
312					return this.log.ToString();
313				}
314			}
315		}
316
317		public string LastTrace
318		{
319			get
320			{
321				lock (this.traceSync)
322				{
323					this.currentTraceRead = true;
324					return this.currentTrace.ToString();
325				}
326			}
327		}
328
329		public TraceOptions TraceOptions
330		{
331			get
332			{
333				lock (this.traceSync)
334				{
335					return this.traceOptions;
336				}
337			}
338			set
339			{
340				lock (this.traceSync)
341				{
342					this.traceOptions = value;
343				}
344			}
345		}
346
347		public void TraceEvent(string message)
348		{
349#if !PORTABLE
350			if ((this.TraceOptions & TraceOptions.RemoteTrace) != 0)
351			{
352				try
353				{
354					if (MockingContext.Plugins.Exists<IDebugWindowPlugin>())
355					{
356						// traces triggered by profiler intercepted calls and repository retirement
357						// could cause deadlocks and infinite loops in remote tracing, so skip them
358						var testMethod = MockingContext.GetTestMethod();
359						var repo = MockingContext.ResolveRepository(UnresolvedContextBehavior.DoNotCreateNew);
360						if (testMethod != null && repo != null && !repo.IsRetired)
361						{
362							var debugWindowPlugin = MockingContext.Plugins.Get<IDebugWindowPlugin>();
363							debugWindowPlugin.TraceMessage(message);
364						}
365					}
366				}
367				catch (Exception e)
368				{
369					System.Diagnostics.Trace.WriteLine("Exception thrown calling IDebugWindowPlugin plugin: " + e);
370				}
371			}
372#endif
373
374			if ((this.TraceOptions & TraceOptions.InternalTrace) != 0)
375			{
376				Debug.WriteLine(message);
377
378				lock (this.traceSync)
379				{
380					this.log.AppendLine(message);
381
382					if (this.currentTraceRead)
383					{
384						this.currentTraceRead = false;
385						this.currentTrace.Length = 0;
386					}
387
388					this.currentTrace.AppendLine(message);
389				}
390			}
391		}
392	}
393}
394
Full Screen

DynamicProxyInterceptor.cs

Source: DynamicProxyInterceptor.cs Github

copy
1/*
2 JustMock Lite
3 Copyright © 2010-2015 Progress Software Corporation
4
5   Licensed under the Apache License, Version 2.0 (the "License");
6   you may not use this file except in compliance with the License.
7   You may obtain a copy of the License at
8
9   http://www.apache.org/licenses/LICENSE-2.0
10
11   Unless required by applicable law or agreed to in writing, software
12   distributed under the License is distributed on an "AS IS" BASIS,
13   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   See the License for the specific language governing permissions and
15   limitations under the License.
16*/
17
18using System;
19using Telerik.JustMock.Core.Castle.DynamicProxy;
20using Telerik.JustMock.Diagnostics;
21
22namespace Telerik.JustMock.Core
23{
24	internal class DynamicProxyInterceptor : IInterceptor
25	{
26		private readonly MocksRepository constructionRepo;
27
28		internal DynamicProxyInterceptor(MocksRepository constructionRepo)
29		{
30			this.constructionRepo = constructionRepo;
31		}
32
33		public void Intercept(IInvocation invocation)
34		{
35			if (ProfilerInterceptor.ReentrancyCounter > 0)
36			{
37				CallOriginal(invocation, false);
38				return;
39			}
40
41			bool callOriginal = false;
42			ProfilerInterceptor.GuardInternal(() =>
43			{
44				var mockInvocation = new Invocation(invocation.Proxy, invocation.GetConcreteMethod(), invocation.Arguments);
45
46				DebugView.TraceEvent(IndentLevel.Dispatch, () => String.Format("Intercepted DP call: {0}", mockInvocation.InputToString()));
47				DebugView.PrintStackTrace();
48
49				var mock = mockInvocation.MockMixin;
50				var repo = mock != null ? mock.Repository : this.constructionRepo;
51
52				lock (repo)
53				{
54					repo.DispatchInvocation(mockInvocation);
55				}
56
57				invocation.ReturnValue = mockInvocation.ReturnValue;
58				callOriginal = mockInvocation.CallOriginal;
59
60				if (callOriginal)
61				{
62					DebugView.TraceEvent(IndentLevel.DispatchResult, () => "Calling original implementation");
63				}
64				else if (mockInvocation.IsReturnValueSet)
65				{
66					DebugView.TraceEvent(IndentLevel.DispatchResult, () => String.Format("Returning value '{0}'", invocation.ReturnValue));
67				}
68			});
69
70			if (callOriginal)
71				CallOriginal(invocation, true);
72		}
73
74		private void CallOriginal(IInvocation invocation, bool throwOnFail)
75		{
76			try
77			{
78				invocation.Proceed();
79			}
80			catch (NotImplementedException)
81			{
82				if (throwOnFail)
83					throw new NotImplementedException("You can't call the original implementation of a method that does not have one (abstract or interface method).");
84			}
85		}
86	}
87}
88
Full Screen

MockingContext.cs

Source: MockingContext.cs Github

copy
1/*
2 JustMock Lite
3 Copyright © 2010-2015,2020 Progress Software Corporation
4
5   Licensed under the Apache License, Version 2.0 (the "License");
6   you may not use this file except in compliance with the License.
7   You may obtain a copy of the License at
8
9   http://www.apache.org/licenses/LICENSE-2.0
10
11   Unless required by applicable law or agreed to in writing, software
12   distributed under the License is distributed on an "AS IS" BASIS,
13   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   See the License for the specific language governing permissions and
15   limitations under the License.
16*/
17
18using System;
19using System.Collections.Generic;
20using System.Diagnostics;
21using System.IO;
22using System.Linq;
23using System.Reflection;
24using System.Text;
25using Telerik.JustMock.Diagnostics;
26#if !PORTABLE
27using Telerik.JustMock.Helpers;
28using Telerik.JustMock.Plugins;
29using Telerik.JustMock.AutoMock.Ninject.Parameters;
30#if NETCORE
31using System.Text.RegularExpressions;
32using System.Runtime.InteropServices;
33#endif
34#endif
35
36namespace Telerik.JustMock.Core.Context
37{
38	internal enum UnresolvedContextBehavior
39	{
40		DoNotCreateNew,
41		CreateNewContextual,
42		CreateNewContextualOrLocal,
43	}
44
45	internal static class MockingContext
46	{
47		internal const string DebugViewPluginAssemblyFileName = "Telerik.JustMock.DebugWindow.Plugin.dll";
48
49		public static MocksRepository CurrentRepository
50		{
51			get { return ResolveRepository(UnresolvedContextBehavior.CreateNewContextualOrLocal); }
52		}
53
54		public static MocksRepository ResolveRepository(UnresolvedContextBehavior unresolvedContextBehavior)
55		{
56			if (unresolvedContextBehavior != UnresolvedContextBehavior.DoNotCreateNew)
57			{
58				DebugView.TraceEvent(IndentLevel.StackTrace, () => String.Format("Resolving repository with unresolved context behavior {0}", unresolvedContextBehavior));
59			}
60
61			foreach (var resolver in registeredContextResolvers)
62			{
63				var repo = resolver.ResolveRepository(unresolvedContextBehavior);
64				if (repo != null)
65				{
66					lastFrameworkAwareRepository = repo;
67					return repo;
68				}
69			}
70
71			if (lastFrameworkAwareRepository != null && !ProfilerInterceptor.IsProfilerAttached)
72				return lastFrameworkAwareRepository;
73
74			return LocalMockingContextResolver.ResolveRepository(unresolvedContextBehavior);
75		}
76
77		public static void RetireRepository()
78		{
79			foreach (var resolver in registeredContextResolvers)
80			{
81				if (resolver.RetireRepository())
82					return;
83			}
84
85			LocalMockingContextResolver.RetireRepository();
86
87#if !PORTABLE
88			DynamicTypeHelper.Reset();
89#endif
90		}
91
92		public static MethodBase GetTestMethod()
93		{
94			foreach (IMockingContextResolver resolver in registeredContextResolvers)
95			{
96				var testMethod = resolver.GetTestMethod();
97				if (testMethod != null)
98				{
99					return testMethod;
100				}
101			}
102
103			return null;
104		}
105
106		public static void Fail(string message, params object[] args)
107		{
108			var formattedMessage = String.Format(message, args);
109
110			if (failureAggregator == null)
111				Fail(formattedMessage);
112			else
113				failureAggregator.AddFailure(formattedMessage);
114		}
115
116		public static string GetStackTrace(string indent)
117		{
118#if SILVERLIGHT
119			var trace = new System.Diagnostics.StackTrace().ToString();
120#elif PORTABLE
121			var trace = new StackTrace().ToString();
122#else
123			var skipCount = new System.Diagnostics.StackTrace().GetFrames().TakeWhile(frame => frame.GetMethod().DeclaringType.Assembly == typeof(DebugView).Assembly).Count();
124			var trace = new System.Diagnostics.StackTrace(skipCount, true).ToString();
125#endif
126
127			return "\n".Join(trace
128					.Split('\n')
129					.Select(p => indent + p.Trim()));
130		}
131
132		public static IDisposable BeginFailureAggregation(string message)
133		{
134			if (failureAggregator == null)
135			{
136				failureAggregator = new FailureAggregator(message);
137			}
138			else
139			{
140				failureAggregator.AddRef(message);
141			}
142
143			return failureAggregator;
144		}
145
146		private static readonly List<IMockingContextResolver> registeredContextResolvers = new List<IMockingContextResolver>();
147
148		private static Action<string, Exception> failThrower;
149
150		[ThreadStatic]
151		private static FailureAggregator failureAggregator;
152
153		[ThreadStatic]
154		private static MocksRepository lastFrameworkAwareRepository;
155
156#if !PORTABLE
157		public static PluginsRegistry Plugins { get; private set; }
158		private static PluginLoadHelper pluginLoadHelper;
159#if NETCORE
160        private const string NET_CORE_DESC_PATTERN = @".NET(\sCore)?\s(\d+(\.)?)+";
161        private const string NET_CORE_SUBDIR = "netcoreapp2.1";
162#endif
163#endif
164
165        static MockingContext()
166		{
167#if !PORTABLE
168			MockingContext.Plugins = new PluginsRegistry();
169			AppDomain.CurrentDomain.DomainUnload += CurrentDomain_DomainUnload;
170
171			try
172			{
173#if !NETCORE
174				var clrVersion = Environment.Version;
175				if (clrVersion.Major >= 4 && clrVersion.Minor >= 0
176					&& clrVersion.Build >= 30319 && clrVersion.Revision >= 42000)
177#endif
178				{
179					var debugWindowEnabledEnv = Environment.GetEnvironmentVariable("JUSTMOCK_DEBUG_VIEW_ENABLED");
180					var debugWindowServicesStringEnv = Environment.GetEnvironmentVariable("JUSTMOCK_DEBUG_VIEW_SERVICES");
181					var debugWindowAssemblyDirectoryEnv = Environment.GetEnvironmentVariable("JUSTMOCK_DEBUG_VIEW_PLUGIN_DIRECTORY");
182					if (!string.IsNullOrEmpty(debugWindowEnabledEnv)
183						&& !string.IsNullOrEmpty(debugWindowServicesStringEnv)
184						&& !string.IsNullOrEmpty(debugWindowAssemblyDirectoryEnv)
185						&& debugWindowEnabledEnv == "1" && Directory.Exists(debugWindowAssemblyDirectoryEnv))
186					{
187#if NETCORE
188						if (Regex.IsMatch(RuntimeInformation.FrameworkDescription, NET_CORE_DESC_PATTERN))
189						{
190							// append .NET Core suffix if necessary
191							if (string.Compare(Path.GetDirectoryName(debugWindowAssemblyDirectoryEnv), NET_CORE_SUBDIR, StringComparison.InvariantCultureIgnoreCase) != 0)
192							{
193								debugWindowAssemblyDirectoryEnv = Path.Combine(debugWindowAssemblyDirectoryEnv, NET_CORE_SUBDIR);
194							}
195						}
196#endif
197						var debugWindowAssemblyPath = Path.Combine(debugWindowAssemblyDirectoryEnv, DebugViewPluginAssemblyFileName);
198						MockingContext.pluginLoadHelper = new PluginLoadHelper(debugWindowAssemblyDirectoryEnv);
199						MockingContext.Plugins.Register<IDebugWindowPlugin>(
200							debugWindowAssemblyPath, new ConstructorArgument("debugWindowServicesString", debugWindowServicesStringEnv));
201						DebugView.IsRemoteTraceEnabled = true;
202					}
203				}
204			}
205			catch (Exception e)
206			{
207				System.Diagnostics.Trace.WriteLine("Exception thrown during plugin registration: " + e);
208			}
209#endif
210
211#if PORTABLE
212			if (VisualStudioPortableContextResolver.IsAvailable)
213				registeredContextResolvers.Add(new VisualStudioPortableContextResolver());
214			if (XamarinAndroidNUnitContextResolver.IsAvailable)
215				registeredContextResolvers.Add(new XamarinAndroidNUnitContextResolver());
216			if (XamarinIosNUnitContextResolver.IsAvailable)
217				registeredContextResolvers.Add(new XamarinIosNUnitContextResolver());
218#else
219			if (XUnit1xMockingContextResolver.IsAvailable)
220				registeredContextResolvers.Add(new XUnit1xMockingContextResolver());
221			if (XUnit2xMockingContextResolver.IsAvailable)
222				registeredContextResolvers.Add(new XUnit2xMockingContextResolver());
223			if (NUnit2xMockingContextResolver.IsAvailable)
224				registeredContextResolvers.Add(new NUnit2xMockingContextResolver());
225			if (NUnit3xMockingContextResolver.IsAvailable)
226				registeredContextResolvers.Add(new NUnit3xMockingContextResolver());
227			if (NUnit3_8_xMockingContextResolver.IsAvailable)
228				registeredContextResolvers.Add(new NUnit3_8_xMockingContextResolver());
229			if (MSpecContextResolver.IsAvailable)
230				registeredContextResolvers.Add(new MSpecContextResolver());
231			if (MbUnitContextResolver.IsAvailable)
232				registeredContextResolvers.Add(new MbUnitContextResolver());
233			if (MSTestMockingContextResolver.IsAvailable)
234				registeredContextResolvers.Add(new MSTestMockingContextResolver());
235			if (MSTestV2MockingContextResolver.IsAvailable)
236				registeredContextResolvers.Add(new MSTestV2MockingContextResolver());
237#endif
238
239			foreach (var resolver in registeredContextResolvers)
240			{
241				failThrower = resolver.GetFailMethod();
242				if (failThrower != null)
243					break;
244			}
245
246			if (failThrower == null)
247				failThrower = LocalMockingContextResolver.GetFailMethod();
248		}
249
250#if !PORTABLE
251		private static void CurrentDomain_DomainUnload(object sender, EventArgs e)
252		{
253			MockingContext.Plugins.Dispose();
254		}
255#endif
256
257		private static void Fail(string msg)
258		{
259			failThrower(msg, DebugView.GetStateAsException());
260		}
261
262		private class FailureAggregator : IDisposable
263		{
264			private List<string> failures;
265			private int references = 1;
266			private string userMessage;
267
268			public FailureAggregator(string message)
269			{
270				userMessage = message;
271			}
272
273			public void AddRef(string message)
274			{
275				references++;
276				userMessage = message;
277			}
278
279			public void AddFailure(string msg)
280			{
281				if (failures == null)
282					failures = new List<string>();
283
284				failures.Add(msg);
285			}
286
287			public void Dispose()
288			{
289				if (references > 1)
290				{
291					references--;
292					return;
293				}
294
295				failureAggregator = null;
296
297				if (failures == null)
298					return;
299
300				if (failures.Count == 1)
301					Fail((userMessage != null ? userMessage + Environment.NewLine : null) + failures[0]);
302
303				var sb = new StringBuilder();
304				if (userMessage != null)
305					sb.AppendLine(userMessage);
306				sb.AppendLine("Multiple assertion failures:");
307				for (int i = 0; i < failures.Count; ++i)
308				{
309					sb.AppendFormat("{0}. ", i + 1);
310					sb.AppendLine(failures[i]);
311				}
312				Fail(sb.ToString());
313			}
314		}
315
316#if PORTABLE
317		private class StackTraceGeneratorException : Exception
318		{ }
319#endif
320	}
321}
322
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Run Selenium Automation Tests on LambdaTest Cloud Grid

Trigger Selenium automation tests on a cloud-based Grid of 3000+ real browsers and operating systems.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)