1
+ // Copyright (c) Microsoft. All rights reserved.
2
+ // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
+
4
+ using System ;
5
+
6
+ using System . Reflection . Emit ;
7
+ using System . Linq ;
8
+ using Xunit ;
9
+
10
+ namespace System . Reflection . Emit . ILGeneration . Tests
11
+ {
12
+ public class CustomAttributeBuilderCtor1
13
+ {
14
+ [ Fact ]
15
+ public void PosTest1 ( )
16
+ {
17
+ string str = "PosTest1" ;
18
+ Type [ ] ctorParams = new Type [ ] { typeof ( string ) } ;
19
+ object [ ] paramValues = new object [ ] { str } ;
20
+ CustomAttributeBuilder cab = new CustomAttributeBuilder (
21
+ typeof ( ObsoleteAttribute ) . GetConstructor ( ctorParams ) , paramValues ) ;
22
+
23
+ Assert . NotNull ( cab ) ;
24
+ }
25
+
26
+ [ Fact ]
27
+ public void PosTest2 ( )
28
+ {
29
+ Type [ ] ctorParams = new Type [ ] { } ;
30
+ object [ ] paramValues = new object [ ] { } ;
31
+
32
+ CustomAttributeBuilder cab = new CustomAttributeBuilder (
33
+ typeof ( ObsoleteAttribute ) . GetConstructor ( ctorParams ) , paramValues ) ;
34
+
35
+ Assert . NotNull ( cab ) ;
36
+ }
37
+
38
+ [ Fact ]
39
+ public void PosTest3 ( )
40
+ {
41
+ string str = "PosTest3" ;
42
+ bool b = TestLibrary . Generator . GetByte ( - 55 ) > Byte . MaxValue / 2 ;
43
+
44
+ Type [ ] ctorParams = new Type [ ] { typeof ( string ) , typeof ( bool ) } ;
45
+ object [ ] paramValues = new object [ ] { str , b } ;
46
+
47
+ CustomAttributeBuilder cab = new CustomAttributeBuilder (
48
+ typeof ( ObsoleteAttribute ) . GetConstructor ( ctorParams ) , paramValues ) ;
49
+
50
+ Assert . NotNull ( cab ) ;
51
+ }
52
+
53
+ [ Fact ]
54
+ public void NegTest1 ( )
55
+ {
56
+ object [ ] paramValues = new object [ ] { } ;
57
+
58
+ Assert . Throws < ArgumentException > ( ( ) =>
59
+ {
60
+ CustomAttributeBuilder cab = new CustomAttributeBuilder (
61
+ typeof ( TestConstructor ) . GetConstructors ( BindingFlags . Static | BindingFlags . Public | BindingFlags . Instance | BindingFlags . NonPublic )
62
+ . Where ( c => c . IsStatic ) . First ( ) , paramValues ) ;
63
+ } ) ;
64
+ }
65
+
66
+ [ Fact ]
67
+ public void NegTest2 ( )
68
+ {
69
+ object [ ] paramValues = new object [ ]
70
+ {
71
+ false
72
+ } ;
73
+
74
+ Assert . Throws < ArgumentException > ( ( ) =>
75
+ {
76
+ CustomAttributeBuilder cab = new CustomAttributeBuilder (
77
+ typeof ( TestConstructor ) . GetConstructors ( BindingFlags . Static | BindingFlags . Public | BindingFlags . Instance | BindingFlags . NonPublic )
78
+ . Where ( c => c . IsPrivate ) . First ( ) , paramValues ) ;
79
+ } ) ;
80
+ }
81
+
82
+ [ Fact ]
83
+ public void NegTest3 ( )
84
+ {
85
+ string str = "NegTest3" ;
86
+ bool b = false ;
87
+
88
+ Type [ ] ctorParams = new Type [ ] { typeof ( string ) } ;
89
+ object [ ] paramValues = new object [ ] { str , b } ;
90
+
91
+ Assert . Throws < ArgumentException > ( ( ) =>
92
+ {
93
+ CustomAttributeBuilder cab = new CustomAttributeBuilder (
94
+ typeof ( ObsoleteAttribute ) . GetConstructor ( ctorParams ) , paramValues ) ;
95
+ } ) ;
96
+ }
97
+
98
+ [ Fact ]
99
+ public void NegTest4 ( )
100
+ {
101
+ string str = "NegTest4" ;
102
+ bool b = true ;
103
+
104
+ Type [ ] ctorParams = new Type [ ] { typeof ( string ) , typeof ( bool ) } ;
105
+ object [ ] paramValues = new object [ ] { b , str } ;
106
+
107
+ Assert . Throws < ArgumentException > ( ( ) =>
108
+ {
109
+ CustomAttributeBuilder cab = new CustomAttributeBuilder (
110
+ typeof ( ObsoleteAttribute ) . GetConstructor ( ctorParams ) , paramValues ) ;
111
+ } ) ;
112
+ }
113
+
114
+ [ Fact ]
115
+ public void NegTest5 ( )
116
+ {
117
+ string str = "NegTest5" ;
118
+ bool b = false ;
119
+
120
+ object [ ] paramValues = new object [ ] { str , b } ;
121
+
122
+ Assert . Throws < ArgumentNullException > ( ( ) =>
123
+ {
124
+ CustomAttributeBuilder cab = new CustomAttributeBuilder (
125
+ null , paramValues ) ;
126
+ } ) ;
127
+ }
128
+
129
+ [ Fact ]
130
+ public void NegTest6 ( )
131
+ {
132
+ Type [ ] ctorParams = new Type [ ] { typeof ( bool ) , typeof ( string ) } ;
133
+
134
+ Assert . Throws < ArgumentNullException > ( ( ) =>
135
+ {
136
+ CustomAttributeBuilder cab = new CustomAttributeBuilder (
137
+ typeof ( ObsoleteAttribute ) . GetConstructor ( ctorParams ) , null ) ;
138
+ } ) ;
139
+ }
140
+ }
141
+
142
+ public class TestConstructor
143
+ {
144
+ static TestConstructor ( ) { }
145
+ internal TestConstructor ( bool b ) { }
146
+ }
147
+ }
0 commit comments