Welcome to mirror list, hosted at ThFree Co, Russian Federation.

AttributeTest.cs « System « Test « corlib « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: febe82fe118c603bc6e014f2065d6c33f0c39698 (plain)
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
//
// AttributeTest.cs - NUnit Test Cases for the System.Attribute class
//
// author:
//   Duco Fijma (duco@lorentz.xs4all.nl)
//
//   (C) 2002 Duco Fijma
//

using NUnit.Framework;
using System;

namespace MonoTests.System
{

// Inner namespace for some test helper classes
using MonoTests.System.AttributeTestInternals;

namespace AttributeTestInternals
{

[AttributeUsage(AttributeTargets.Class, AllowMultiple=true, Inherited=false)]
internal class MyCustomAttribute : Attribute {

	private string _info;

	public MyCustomAttribute (string info)
	{
		_info = info;
	}

	public string Info 
	{
		get {
			return _info;
		}
	}

}

[AttributeUsage(AttributeTargets.Class)]
internal class YourCustomAttribute : Attribute {
	
	private int _value;

	public YourCustomAttribute (int value) 
	{
		_value = value;
	}

	public int Value 
	{
		get {
			return _value;
		}
	}
}

[AttributeUsage(AttributeTargets.Class)]
internal class UnusedAttribute : Attribute {
}

[MyCustomAttribute("MyBaseClass"), YourCustomAttribute(37)]
internal class MyClass {
	int Value { get { return 42; }}
}

[MyCustomAttribute("MyDerivedClass")]
internal class MyDerivedClass : MyClass {
	public void Do () {}
}

} // Namespace MonoTests.System.AttributeTestInternals

public class AttributeTest : TestCase {
		
	public AttributeTest () : base("MonoTests.System.AttributeTest testcase") {}
	public AttributeTest (string name) : base(name) {}

	public static ITest Suite {
		get {
			return new TestSuite(typeof(AttributeTest));
		}
	}

	public void TestIsDefined ()
	{
		AssertEquals ("A1", true, Attribute.IsDefined(typeof(MyDerivedClass), typeof(MyCustomAttribute)));
		AssertEquals ("A2", true, Attribute.IsDefined(typeof(MyDerivedClass), typeof(YourCustomAttribute)));
		AssertEquals ("A3", false, Attribute.IsDefined(typeof(MyDerivedClass), typeof(UnusedAttribute)));
		AssertEquals ("A4", true, Attribute.IsDefined(typeof(MyDerivedClass), typeof(MyCustomAttribute), true));
		AssertEquals ("A5", true, Attribute.IsDefined(typeof(MyDerivedClass), typeof(YourCustomAttribute), true));
		AssertEquals ("A6", false, Attribute.IsDefined(typeof(MyDerivedClass), typeof(UnusedAttribute), false));
		AssertEquals ("A7", true, Attribute.IsDefined(typeof(MyDerivedClass), typeof(MyCustomAttribute), false));
		AssertEquals ("A8", false, Attribute.IsDefined(typeof(MyDerivedClass), typeof(YourCustomAttribute), false));
		AssertEquals ("A9", false, Attribute.IsDefined(typeof(MyDerivedClass), typeof(UnusedAttribute), false));
	}

/*
	public static void TestIsDefaultAttribute () 
	{
		Console.WriteLine(">>>IsDefaultAttribute");

		Attribute a =  Attribute.GetCustomAttribute(typeof(MyClass), typeof(MyCustomAttribute));
		Console.WriteLine (a.IsDefaultAttribute() );
	}

	private static void WriteAttribute (Attribute a)
	{
		if (a == null) {
			Console.WriteLine ("NULL");
		}
		else {
			Console.WriteLine (a);
		}
	}
*/

	public void TestGetCustomAttribute ()
	{
		int i = 1;
		Type t = typeof(MyDerivedClass);
		try {
			AssertEquals ("A1",  "MyDerivedClass", ((MyCustomAttribute) (Attribute.GetCustomAttribute(typeof(MyDerivedClass), typeof(MyCustomAttribute), false))).Info);
			i++;
			AssertEquals ("A2", null, ((YourCustomAttribute) (Attribute.GetCustomAttribute(typeof(MyDerivedClass), typeof(YourCustomAttribute), false))));
			i++;
			AssertEquals ("A3",  "MyDerivedClass", ((MyCustomAttribute) (Attribute.GetCustomAttribute(typeof(MyDerivedClass), typeof(MyCustomAttribute)))).Info);
			i++;
			AssertEquals ("A4", 37, ((YourCustomAttribute) (Attribute.GetCustomAttribute(t, typeof(YourCustomAttribute)))).Value);
		} catch (Exception e) {
			Fail ("Unexpected exception thrown at i=" + i + " with t=" + t + ". e=" + e);
		}
	}
/*

	public static void WriteAttributes (Attribute[] attrs)
	{
		Console.WriteLine("length = {0}", attrs.Length);
		foreach (Attribute a in attrs) {
			WriteAttribute (a);
		}
	}


	private static void TestGetCustomAttributes ()
	{
		Console.WriteLine(">>>GetCustomAttributes");


		WriteAttributes (Attribute.GetCustomAttributes(typeof(MyDerivedClass), typeof(MyCustomAttribute)));
		WriteAttributes (Attribute.GetCustomAttributes(typeof(MyDerivedClass), typeof(MyCustomAttribute), true));
		WriteAttributes (Attribute.GetCustomAttributes(typeof(MyDerivedClass), typeof(MyCustomAttribute), false));

		WriteAttributes (Attribute.GetCustomAttributes(typeof(MyDerivedClass)));
		WriteAttributes (Attribute.GetCustomAttributes(typeof(MyDerivedClass), true));
		WriteAttributes (Attribute.GetCustomAttributes(typeof(MyDerivedClass), false));
	}

*/

}

}