File: NUnit.cs

package info (click to toggle)
mono-debugger 0.60%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 9,556 kB
  • ctags: 22,787
  • sloc: ansic: 99,407; cs: 42,429; sh: 9,192; makefile: 376
file content (843 lines) | stat: -rw-r--r-- 22,248 bytes parent folder | download
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
using System;
using System.IO;
using System.Collections;
using System.Globalization;
using SD = System.Diagnostics;
using ST = System.Threading;
using System.Text;
using NUnit.Framework;

using Mono.Debugger;
using Mono.Debugger.Languages;
using Mono.Debugger.Frontend;

namespace Mono.Debugger.Tests
{
	public enum DebuggerEventType {
		TargetEvent,
		ThreadCreated,
		ThreadExited,
		MainProcessCreated,
		ProcessCreated,
		ProcessExecd,
		ProcessExited,
		TargetExited
	}

	public class DebuggerEvent {
		public readonly DebuggerEventType Type;
		public readonly object Data;
		public readonly object Data2;

		public DebuggerEvent (DebuggerEventType type)
			: this (type, null, null)
		{ }

		public DebuggerEvent (DebuggerEventType type, object data)
			: this (type, data, null)
		{ }

		public DebuggerEvent (DebuggerEventType type, object data, object data2)
		{
			this.Type = type;
			this.Data = data;
			this.Data2 = data2;
		}

		public override string ToString ()
		{
			return String.Format ("[{0}:{1}:{2}]", Type, Data, Data2);
		}
	}

	public class StyleNUnit : StyleCLI
	{
		public readonly NUnitInterpreter NUnit;

		public StyleNUnit (NUnitInterpreter nunit)
			: base (nunit)
		{
			this.NUnit = nunit;
		}

		public event TargetEventHandler TargetEventEvent;

		public override void TargetEvent (Thread thread, TargetEventArgs args)
		{
			if (TargetEventEvent != null)
				TargetEventEvent (thread, args);

			base.TargetEvent (thread, args);
		}
	}

	public class NUnitInterpreter : Interpreter
	{
		internal NUnitInterpreter (DebuggerConfiguration config, DebuggerOptions options,
					   LineReader inferior_stdout, LineReader inferior_stderr)
			: base (false, config, options)
		{
			this.inferior_stdout = inferior_stdout;
			this.inferior_stderr = inferior_stderr;

			config.FollowFork = true;

			queue = Queue.Synchronized (new Queue ());
			wait_event = new ST.ManualResetEvent (false);

			Style = style_nunit = new StyleNUnit (this);
			style_nunit.TargetEventEvent += delegate (Thread thread, TargetEventArgs args) {
				AddEvent (new DebuggerEvent (DebuggerEventType.TargetEvent, thread, args));
			};

			ST.Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
			ObjectFormatter.WrapLines = false;
		}

		Queue queue;
		StyleNUnit style_nunit;
		ST.ManualResetEvent wait_event;
		LineReader inferior_stdout, inferior_stderr;

		public bool HasEvent {
			get { return queue.Count > 0; }
		}

		public DebuggerEvent Wait ()
		{
			for (int i = 0; i < 5; i++) {
				lock (queue.SyncRoot) {
					if (queue.Count > 0)
						return (DebuggerEvent) queue.Dequeue ();

					wait_event.Reset ();
				}

				wait_event.WaitOne (3000, false);
			}

			return null;
		}

		protected void AddEvent (DebuggerEvent e)
		{
			lock (queue.SyncRoot) {
				queue.Enqueue (e);
				wait_event.Set ();
			}
		}

		protected override void OnThreadCreated (Thread thread)
		{
			base.OnThreadCreated (thread);
			AddEvent (new DebuggerEvent (DebuggerEventType.ThreadCreated, thread));
		}

		protected override void OnThreadExited (Thread thread)
		{
			base.OnThreadExited (thread);
			AddEvent (new DebuggerEvent (DebuggerEventType.ThreadExited, thread));
		}

		protected override void OnMainProcessCreated (Process process)
		{
			base.OnMainProcessCreated (process);
			AddEvent (new DebuggerEvent (DebuggerEventType.MainProcessCreated, process));
		}

		protected override void OnProcessCreated (Process process)
		{
			base.OnProcessCreated (process);
			AddEvent (new DebuggerEvent (DebuggerEventType.ProcessCreated, process));
		}

		protected override void OnProcessExited (Process process)
		{
			base.OnProcessExited (process);
			AddEvent (new DebuggerEvent (DebuggerEventType.ProcessExited, process));
		}

		protected override void OnProcessExecd (Process process)
		{
			base.OnProcessExecd (process);
			AddEvent (new DebuggerEvent (DebuggerEventType.ProcessExecd, process));
		}

		protected override void OnTargetExited ()
		{
			base.OnTargetExited ();
			AddEvent (new DebuggerEvent (DebuggerEventType.TargetExited));
		}

		protected override void OnTargetOutput (bool is_stderr, string line)
		{
			if (is_stderr)
				inferior_stderr.Add (line);
			else
				inferior_stdout.Add (line);
		}

		public override void Print (string message)
		{
			if (!IsScript)
				base.Print (message);
		}
	}

	public abstract class TestSuite : MarshalByRefObject
	{
		DebuggerConfiguration config;
		DebuggerOptions options;
		NUnitInterpreter interpreter;
		DebuggerEngine engine;
		LineParser parser;
		LineReader inferior_stdout, inferior_stderr;

		public readonly string ExeFileName;
		public readonly string FileName;

		static TestSuite ()
		{
			Report.Initialize ();
			Report.ReportWriter.PrintToConsole = false;
		}

		protected TestSuite (string application)
			: this (application + ".exe", application + ".cs")
		{ }

		protected TestSuite (string exe_file, string src_file, params string[] args)
		{
			string srcdir = Path.Combine (BuildInfo.srcdir, "../test/src/");
			string builddir = Path.Combine (BuildInfo.builddir, "../test/src/");

			ExeFileName = Path.GetFullPath (builddir + exe_file);
			FileName = Path.GetFullPath (srcdir + src_file);

			config = new DebuggerConfiguration ();
			options = CreateOptions (ExeFileName, args);

			inferior_stdout = new LineReader ();
			inferior_stderr = new LineReader ();
		}

		public NUnitInterpreter Interpreter {
			get { return interpreter; }
		}

		public static string SourceDirectory {
			get {
				string srcdir = Path.Combine (BuildInfo.srcdir, "../test/src/");
				return Path.GetFullPath (srcdir);
			}
		}

		public static string BuildDirectory {
			get {
				string builddir = Path.Combine (BuildInfo.builddir, "../test/src/");
				return Path.GetFullPath (builddir);
			}
		}

		public static string MonoExecutable {
			get {
				return BuildInfo.mono;
			}
		}

		public void Compile ()
		{
			SD.ProcessStartInfo start = new SD.ProcessStartInfo (
				BuildInfo.mcs, "-debug " + FileName + " -out:" + ExeFileName);
			start.UseShellExecute = false;
			start.WorkingDirectory = BuildDirectory;
			start.RedirectStandardOutput = true;
			start.RedirectStandardError = true;

			SD.Process child = SD.Process.Start (start);
			child.WaitForExit ();

			if (child.ExitCode != 0)
				Assert.Fail ("Compilation of {0} exited with error: {1}\n{2}",
					     FileName, child.ExitCode, child.StandardError.ReadToEnd ());
		}

		[TestFixtureSetUp]
		public virtual void SetUp ()
		{
			interpreter = new NUnitInterpreter (
				config, options, inferior_stdout, inferior_stderr);

			engine = interpreter.DebuggerEngine;
			parser = new LineParser (engine);
		}

		[TestFixtureTearDown]
		public virtual void TearDown ()
		{
			interpreter.Dispose ();
			interpreter = null;
			GC.Collect ();
		}

		private static DebuggerOptions CreateOptions (string application, string[] args)
		{
			DebuggerOptions options = DebuggerOptions.ParseCommandLine (args);

			options.IsScript = true;
			options.File = application;
			options.InferiorArgs = args;

			return options;
		}

		public object AssertExecute (string text)
		{
			parser.Reset ();
			parser.Append (text);
			Command command = parser.GetCommand ();
			if (command == null)
				Assert.Fail ("No such command: `{0}'", text);

			try {
				return command.Execute (engine);
			} catch (ScriptingException ex) {
				Assert.Fail ("Failed to execute command `{0}': {1}.",
					     text, ex.Message);
				return null;
			} catch (TargetException ex) {
				Assert.Fail ("Failed to execute command `{0}': {1}.",
					     text, ex.Message);
				return null;
			} catch (Exception ex) {
				Assert.Fail ("Caught exception while executing command `{0}': {1}",
					     text, ex);
				return null;
			}
		}

		public void AssertExecuteException (string text, string exp_exception)
		{
			parser.Reset ();
			parser.Append (text);
			Command command = parser.GetCommand ();
			if (command == null)
				Assert.Fail ("No such command: `{0}'", text);

			string exception = "";
			try {
				command.Execute (engine);
				Assert.Fail ("Execution of command `{0}' failed to throw " +
					     "exception `{1}'.", command, exp_exception);
			} catch (ScriptingException ex) {
				exception = ex.Message;
			} catch (TargetException ex) {
				exception = ex.Message;
			} catch (Exception ex) {
				Assert.Fail ("Caught exception while executing command `{0}': {1}",
					     text, ex);
			}

			if (exception != exp_exception)
				Assert.Fail ("Execution of command `{0}' threw exception `{1}', " +
					     "but expected `{2}'.", command, exception, exp_exception);
		}

		public void AssertFrame (Thread thread, string function, int line)
		{
			try {
				AssertFrame (thread.CurrentFrame, 0, function, line);
			} catch (TargetException ex) {
				Assert.Fail ("Cannot get current frame: {0}.", ex.Message);
			}
		}

		public void AssertFrame (StackFrame frame, int level, string function, int line)
		{
			try {
				Assert.AreEqual (level, frame.Level,
						 "Stack frame is from level {0}, but expected {1}.",
						 frame.Level, level);
				if (frame.SourceLocation == null)
					Assert.Fail ("Current frame `{0}' has no source code.", frame);

				SourceLocation location = frame.SourceLocation;

				string name = location.Name;
				int pos = name.IndexOf (':');
				if (pos > 0)
					name = name.Substring (0, pos);

				Assert.AreEqual (function, name,
						 "Target stopped in method `{0}', but expected `{1}'.",
						 name, function);
				Assert.AreEqual (line, location.Line,
						 "Target stopped at line {0}, but expected {1}.",
						 location.Line, line);
			} catch (TargetException ex) {
				Assert.Fail ("Cannot get current frame: {0}.", ex.Message);
			}
		}

		public void AssertInternalFrame (StackFrame frame, int level)
		{
			Assert.AreEqual (level, frame.Level,
					 "Stack frame is from level {0}, but expected {1}.",
					 level, frame.Level);
			Assert.AreEqual ("<method called from mdb>", frame.Name.ToString (),
					 "Got frame `{0}', but expected a method " +
					 "called from mdb.", frame);
		}

		public void AssertTargetOutput (string line)
		{
			inferior_stdout.Wait ();
			string output = inferior_stdout.ReadLine ();
			if (output == null)
				Assert.Fail ("No target output.");

			Assert.AreEqual (line, output,
					 "Expected target output `{0}', but got `{1}'.",
					 line, output);
		}

		public void AssertNoTargetOutput ()
		{
			string output;
			while ((output = inferior_stdout.ReadLine ()) != null)
				Assert.Fail ("Got unexpected target output `{0}'.", output);
		}

		public void AssertStopped (Thread exp_thread, string exp_func, int exp_line)
		{
			TargetEventArgs args = AssertTargetEvent (
				exp_thread, TargetEventType.TargetStopped);

			if ((int) args.Data != 0)
				Assert.Fail ("Received event {0} while waiting for {1} to stop.",
					     args, exp_thread);

			if (exp_func != null)
				AssertFrame (exp_thread, exp_func, exp_line);
		}

		public void AssertHitBreakpoint (Thread exp_thread, int exp_index,
						 string exp_func, int exp_line)
		{
			TargetEventArgs args = AssertTargetEvent (
				exp_thread, TargetEventType.TargetHitBreakpoint);

			int index = (int) args.Data;
			if ((exp_index != -1) && (index != exp_index))
				Assert.Fail ("Thread {0} hit breakpoint {1}, but expected {2}.",
					     exp_thread, index, exp_index);

			AssertFrame (exp_thread, exp_func, exp_line);
		}

		public void AssertCaughtException (Thread exp_thread, string exp_func, int exp_line)
		{
			AssertTargetEvent (exp_thread, TargetEventType.Exception);
			AssertFrame (exp_thread, exp_func, exp_line);
		}

		public int AssertBreakpoint (int location)
		{
			return AssertBreakpoint (location.ToString ());
		}

		public int AssertBreakpoint (string location)
		{
			return AssertBreakpoint (false, location);
		}

		public int AssertBreakpoint (bool lazy, string location)
		{
			string command = String.Concat (
				"break ", lazy ? "-lazy " : "", location);
			object result = AssertExecute (command);
			if (result == null) {
				Assert.Fail ("Failed to insert breakpoint.");
				return -1;
			}

			return (int) result;
		}

		public int AssertCatchpoint (string location)
		{
			object result = AssertExecute ("catch " + location);
			if (result == null) {
				Assert.Fail ("Failed to insert catchpoint.");
				return -1;
			}

			return (int) result;
		}

		public Process AssertMainProcessCreated ()
		{
			DebuggerEvent e = AssertEvent (DebuggerEventType.MainProcessCreated);
			return (Process) e.Data;
		}

		public Thread AssertProcessCreated ()
		{
			AssertEvent (DebuggerEventType.ProcessCreated);
			DebuggerEvent ee = AssertEvent (DebuggerEventType.ThreadCreated);
			return (Thread) ee.Data;
		}

		public Thread AssertThreadCreated ()
		{
			DebuggerEvent te = AssertEvent (DebuggerEventType.ThreadCreated);
			return (Thread) te.Data;
		}

		public void AssertProcessExited (Process exp_process)
		{
			DebuggerEvent e = AssertEvent (DebuggerEventType.ProcessExited);
			Process process = (Process) e.Data;

			if (process != exp_process)
				Assert.Fail ("Process {0} exited, but expected process {1} to exit.",
					     process.ID, exp_process.ID);
		}

		public void AssertNoEvent ()
		{
			if (Interpreter.HasEvent) {
				DebuggerEvent e = Interpreter.Wait ();
				Assert.Fail ("Received unexpected event {0}.", e);
			}
		}

		public void AssertTargetExited ()
		{
			while (true) {
				DebuggerEvent e = Interpreter.Wait ();
				if (e == null)
					Assert.Fail ("Time-out while waiting for target to exit.");

				if (e.Type == DebuggerEventType.TargetExited)
					break;
				else if ((e.Type == DebuggerEventType.ThreadExited) ||
					 (e.Type == DebuggerEventType.ProcessExited))
					continue;
				else if (e.Type == DebuggerEventType.TargetEvent) {
					TargetEventArgs args = (TargetEventArgs) e.Data2;
					if ((args.Type == TargetEventType.TargetExited) ||
					    (args.Type == TargetEventType.TargetSignaled))
						continue;
				}

				Assert.Fail ("Received event {0} while waiting for target to exit.", e);
			}

			AssertNoTargetOutput ();
		}

		ScriptingContext GetContext (Thread thread)
		{
			ScriptingContext context = new ScriptingContext (interpreter);
			context.CurrentThread = thread;
			context.CurrentFrame = thread.CurrentFrame;
			return context;
		}

		object EvaluateExpression (ScriptingContext context, string expression)
		{
			try {
				Expression expr = interpreter.ExpressionParser.Parse (expression);
				if (expr == null)
					Assert.Fail ("Cannot parse expression `{0}'.", expression);

				expr = expr.Resolve (context);
				if (expr == null)
					Assert.Fail ("Cannot resolve expression `{0}'.", expression);

				object obj = expr.Evaluate (context);
				if (obj == null)
					Assert.Fail ("Failed to evaluate expression `{0}.", expression);

				return obj;
			} catch (ScriptingException) {
				throw;
			} catch (AssertionException) {
				throw;
			} catch (Exception ex) {
				Assert.Fail ("Failed to evalaute expression `{0}': {1}",
					     expression, ex);
				return null;
			}
		}

		TargetType EvaluateExpressionType (ScriptingContext context, string expression)
		{
			try {
				Expression expr = interpreter.ExpressionParser.Parse (expression);
				if (expr == null)
					Assert.Fail ("Cannot parse expression `{0}'.", expression);

				Expression resolved = expr.TryResolveType (context);
				if (resolved != null)
					expr = resolved;
				else
					expr = expr.Resolve (context);
				if (expr == null)
					Assert.Fail ("Cannot resolve expression `{0}'.", expression);

				return expr.EvaluateType (context);
			} catch (AssertionException) {
				throw;
			} catch (Exception ex) {
				Assert.Fail ("Failed to evalaute type of expression `{0}': {1}",
					     expression, ex);
				return null;
			}
		}

		public void AssertPrint (Thread thread, string expression, string exp_result)
		{
			AssertPrint (thread, DisplayFormat.Object, expression, exp_result);
		}

		public void AssertPrint (Thread thread, DisplayFormat format,
					 string expression, string exp_result)
		{
			string text = null;
			try {
				ScriptingContext context = GetContext (thread);

				object obj = EvaluateExpression (context, expression);
				text = context.FormatObject (obj, format);
			} catch (AssertionException) {
				throw;
			} catch (Exception ex) {
				Assert.Fail ("Failed to print expression `{0}': {1}",
					     expression, ex);
			}

			if (text != exp_result)
				Assert.Fail ("Expression `{0}' evaluated to `{1}', but expected `{2}'.",
					     expression, text, exp_result);
		}

		public void AssertPrintException (Thread thread, string expression, string exp_result)
		{
			string text = null;
			try {
				ScriptingContext context = GetContext (thread);

				object obj = EvaluateExpression (context, expression);
				text = context.FormatObject (obj, DisplayFormat.Object);
				Assert.Fail ("Evaluation of exception `{0}' failed to throw " +
					     "exception {1}.", expression, exp_result);
			} catch (AssertionException) {
				throw;
			} catch (ScriptingException ex) {
				text = ex.Message;
			} catch (Exception ex) {
				Assert.Fail ("Failed to print expression `{0}': {1}",
					     expression, ex);
			}

			if (text != exp_result)
				Assert.Fail ("Expression `{0}' evaluated to `{1}', but expected `{2}'.",
					     expression, text, exp_result);
		}

		public void AssertType (Thread thread, string expression, string exp_result)
		{
			string text = null;
			try {
				ScriptingContext context = GetContext (thread);

				TargetType type = EvaluateExpressionType (context, expression);
				text = context.FormatType (type);
			} catch (AssertionException) {
				throw;
			} catch (Exception ex) {
				Assert.Fail ("Failed to evaluate type of expression `{0}': {1}",
					     expression, ex);
			}

			if (text != exp_result)
				Assert.Fail ("Type of expression `{0}' is `{1}', but expected `{2}'.",
					     expression, text, exp_result);
		}

		public DebuggerEvent AssertEvent ()
		{
			DebuggerEvent e = Interpreter.Wait ();
			if (e == null)
				Assert.Fail ("Time-out while waiting for debugger event.");

			return e;
		}

		public DebuggerEvent AssertEvent (DebuggerEventType type)
		{
			DebuggerEvent e = AssertEvent ();
			if (e.Type != type)
				Assert.Fail ("Received event {0}, but expected {1}.", e, type);

			return e;
		}

		private void AssertMainProcessCreated (Process process)
		{
			DebuggerEvent e = AssertEvent (DebuggerEventType.MainProcessCreated);
			Process main = (Process) e.Data;
			Assert.AreEqual (process, main,
					 "Created main process `{0}', but expected `{1}'.",
					 main, process);
		}

		public Process Start ()
		{
			Process process = Interpreter.Start ();
			AssertMainProcessCreated (process);
			return process;
		}

		public Process LoadSession (Stream stream)
		{
			Process process = Interpreter.LoadSession (stream);
			AssertMainProcessCreated (process);
			return process;
		}

		public Process Attach (int pid)
		{
			Process process = Interpreter.Attach (pid);
			AssertMainProcessCreated (process);
			return process;
		}

		public TargetEventArgs AssertTargetEvent (Thread thread, TargetEventType type)
		{
			DebuggerEvent e = AssertEvent ();
			if (e.Type != DebuggerEventType.TargetEvent)
				Assert.Fail ("Received event {0}, but expected {1}.", e, type);

			if ((thread != null) && (e.Data != thread))
				Assert.Fail ("Received event {0} while waiting for {1} in thread {2}.",
					     e, type, thread);

			TargetEventArgs args = (TargetEventArgs) e.Data2;
			if (args.Type != type)
				Assert.Fail ("Received event {0} while waiting for {1} in thread {2}.",
					     e, type, thread);

			return args;
		}

		public void AssertTargetExited (Process process)
		{
			bool target_event = false;
			bool process_exited = false;
			bool thread_exited = false;

			while (true) {
				DebuggerEvent e = Interpreter.Wait ();
				if (e == null)
					Assert.Fail ("Time-out while waiting for target to exit.");

				if (e.Type == DebuggerEventType.TargetExited)
					break;
				if (e.Type == DebuggerEventType.ThreadExited) {
					if (e.Data == process.MainThread)
						thread_exited = true;
					continue;
				} else if (e.Type == DebuggerEventType.ProcessExited) {
					if (e.Data == process)
						process_exited = true;
					continue;
				} else if (e.Type == DebuggerEventType.TargetEvent) {
					TargetEventArgs args = (TargetEventArgs) e.Data2;
					if ((args.Type == TargetEventType.TargetExited) ||
					    (args.Type == TargetEventType.TargetSignaled)) {
						if (e.Data == process.MainThread)
							target_event = true;
						continue;
					}
				}

				Assert.Fail ("Received event {0} while waiting for target to exit.", e);
			}

			if (!target_event)
				Assert.Fail ("Did not receive `TargetEventType.TargetExited' event " +
					     "while waiting for target to exit.");
			if (!process_exited)
				Assert.Fail ("Did not receive `ProcessExitedEvent' while waiting for " +
					     "target to exit.");
			if (!thread_exited)
				Assert.Fail ("Did not receive `ThreadExitedEvent' while waiting for " +
					     "target to exit.");

			AssertNoTargetOutput ();
		}

		public void MainLoop ()
		{
			CommandLineInterpreter cmdline = new CommandLineInterpreter (interpreter);
			cmdline.RunInferiorMainLoop ();
		}
	}

	internal class LineReader
	{
		string current_line = "";
		Queue lines = new Queue ();

		bool waiting;
		ST.AutoResetEvent wait_event = new ST.AutoResetEvent (false);

		public void Add (string text)
		{
			lock (this) {
			again:
				int pos = text.IndexOf ('\n');
				if (pos < 0)
					current_line += text;
				else {
					current_line += text.Substring (0, pos);
					lines.Enqueue (current_line);
					current_line = "";
					text = text.Substring (pos + 1);
					if (text.Length > 0)
						goto again;
				}

				if (!waiting)
					return;
			}

			waiting = false;
			wait_event.Set ();
		}

		public void Wait ()
		{
			lock (this) {
				if (lines.Count > 0)
					return;

				waiting = true;
			}
			wait_event.WaitOne (2500, false);
		}

		public string ReadLine ()
		{
			lock (this) {
				if (lines.Count < 1)
					return null;
				else
					return (string) lines.Dequeue ();
			}
		}
	}
}