File: AssertExtensions.cs

package info (click to toggle)
mono-reference-assemblies 3.12.1%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 604,240 kB
  • ctags: 625,505
  • sloc: cs: 3,967,741; xml: 2,793,081; ansic: 418,042; java: 60,435; sh: 14,833; makefile: 11,576; sql: 7,956; perl: 1,467; cpp: 1,446; yacc: 1,203; python: 598; asm: 422; sed: 16; php: 1
file content (82 lines) | stat: -rw-r--r-- 2,389 bytes parent folder | download | duplicates (7)
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
using System;
using System.Collections.Generic;

using NUnit.Framework;

namespace MonoTests.Common
{
	delegate void AssertThrowsDelegate();

	static class AssertExtensions
	{
		public static void AreEqual (byte[] expected, byte[] data, string message)
		{
			if (expected == null) {
				if (data == null)
					return;
				Assert.Fail ("{0}{1}Expected: null{1}Got: byte array with {2} elements and of rank {3}{1}",
					message, Environment.NewLine, data.Length, data.Rank);
			}

			if (data == null)
				Assert.Fail ("{0}{1}Expected: byte array with {2} elements and rank {3}{1}Got: null{1}",
					message, Environment.NewLine, expected.Length, expected.Rank);

			if (expected.Rank > 1)
				Assert.Fail ("Only single-dimensional arrays are supported.");

			if (expected.Rank != data.Rank || expected.Length != data.Length)
				Assert.Fail ("{0}{1}Expected: byte array with {2} elements and rank {3}{1}Got: byte array with {4} elements and rank {5}{1}",
					message, Environment.NewLine, expected.Length, expected.Rank, data.Length, data.Rank);

			int max = expected.Length;
			for (int i = 0; i < max; i++) {
				if (expected[i] != data[i])
					Assert.Fail ("{0}{1}Arrays differ at index {2}.{1}Expected 0x{3:X} got 0x{4:X}{1}",
						message, Environment.NewLine, i, expected[i], data[i]);
			}
		}

		public static void Throws<ET> (AssertThrowsDelegate code, string message)
		{
			Throws(typeof(ET), code, message);
		}

		public static void Throws(Type exceptionType, AssertThrowsDelegate code, string message)
		{
			if (code == null)
				Assert.Fail("No code provided for the test.");

			Exception exception = null;
			try
			{
				code();
			}
			catch (Exception ex)
			{
				exception = ex;
			}

			if (exceptionType == null)
			{
				if (exception == null)
					Assert.Fail("{0}{1}Expected: any exception thrown{1}But was: no exception thrown{1}",
						message, Environment.NewLine);
				return;
			}

			if (exception == null || exception.GetType() != exceptionType)
				Assert.Fail("{0}{1}Expected: {2}{1}But was: {3}{1}{4}{1}",
				    message,
				    Environment.NewLine,
				    exceptionType,
				    exception == null ? "no exception" : exception.GetType().ToString(),
				    exception == null ? "no exception" : exception.ToString());
		}

		public static void Throws(AssertThrowsDelegate code, string message)
		{
			Throws(null, code, message);
		}
	}
}