1
+ using System . Collections . Generic ;
2
+ using System . Diagnostics . CodeAnalysis ;
3
+ using System . Linq ;
4
+ using BenchmarkDotNet . Attributes ;
5
+ using BenchmarkDotNet . Running ;
6
+ using BenchmarkDotNet . Validators ;
7
+ using Xunit ;
8
+ using Xunit . Abstractions ;
9
+
10
+ namespace BenchmarkDotNet . Tests . Validators
11
+ {
12
+ [ SuppressMessage ( "ReSharper" , "ClassNeverInstantiated.Global" ) ]
13
+ [ SuppressMessage ( "ReSharper" , "MemberCanBePrivate.Global" ) ]
14
+ public class ParamsValidatorTests
15
+ {
16
+ private readonly ITestOutputHelper output ;
17
+
18
+ public ParamsValidatorTests ( ITestOutputHelper output )
19
+ {
20
+ this . output = output ;
21
+ }
22
+
23
+ private void Check < T > ( params string [ ] messageParts )
24
+ {
25
+ var typeToBenchmarks = BenchmarkConverter . TypeToBenchmarks ( typeof ( T ) ) ;
26
+ Assert . NotEmpty ( typeToBenchmarks . BenchmarksCases ) ;
27
+
28
+ var validationErrors = ParamsValidator . FailOnError . Validate ( typeToBenchmarks ) . ToList ( ) ;
29
+ output . WriteLine ( "Number of validation errors: " + validationErrors . Count ) ;
30
+ foreach ( var error in validationErrors )
31
+ output . WriteLine ( "* " + error . Message ) ;
32
+
33
+ Assert . Single ( validationErrors ) ;
34
+ foreach ( string messagePart in messageParts )
35
+ Assert . Contains ( messagePart , validationErrors . Single ( ) . Message ) ;
36
+ }
37
+
38
+ private const string P = "[Params]" ;
39
+ private const string Pa = "[ParamsAllValues]" ;
40
+ private const string Ps = "[ParamsSource]" ;
41
+
42
+ [ Fact ] public void Const1Test ( ) => Check < Const1 > ( nameof ( Const1 . Input ) , "constant" , P ) ;
43
+ [ Fact ] public void Const2Test ( ) => Check < Const2 > ( nameof ( Const2 . Input ) , "constant" , Pa ) ;
44
+ [ Fact ] public void Const3Test ( ) => Check < Const3 > ( nameof ( Const3 . Input ) , "constant" , Ps ) ;
45
+ [ Fact ] public void StaticReadonly1Test ( ) => Check < StaticReadonly1 > ( nameof ( StaticReadonly1 . Input ) , "readonly" , P ) ;
46
+ [ Fact ] public void StaticReadonly2Test ( ) => Check < StaticReadonly2 > ( nameof ( StaticReadonly2 . Input ) , "readonly" , Pa ) ;
47
+ [ Fact ] public void StaticReadonly3Test ( ) => Check < StaticReadonly3 > ( nameof ( StaticReadonly3 . Input ) , "readonly" , Ps ) ;
48
+ [ Fact ] public void NonStaticReadonly1Test ( ) => Check < NonStaticReadonly1 > ( nameof ( NonStaticReadonly1 . Input ) , "readonly" , P ) ;
49
+ [ Fact ] public void NonStaticReadonly2Test ( ) => Check < NonStaticReadonly2 > ( nameof ( NonStaticReadonly2 . Input ) , "readonly" , Pa ) ;
50
+ [ Fact ] public void NonStaticReadonly3Test ( ) => Check < NonStaticReadonly3 > ( nameof ( NonStaticReadonly3 . Input ) , "readonly" , Ps ) ;
51
+
52
+ public class Base
53
+ {
54
+ [ Benchmark ]
55
+ public void Foo ( ) { }
56
+
57
+ public static IEnumerable < bool > Source ( ) => new [ ] { false , true } ;
58
+ }
59
+
60
+ public class Const1 : Base
61
+ {
62
+ [ Params ( false , true ) ]
63
+ public const bool Input = false ;
64
+ }
65
+
66
+ public class Const2 : Base
67
+ {
68
+ [ ParamsAllValues ]
69
+ public const bool Input = false ;
70
+ }
71
+
72
+ public class Const3 : Base
73
+ {
74
+ [ ParamsSource ( nameof ( Source ) ) ]
75
+ public const bool Input = false ;
76
+ }
77
+
78
+ public class StaticReadonly1 : Base
79
+ {
80
+ [ Params ( false , true ) ]
81
+ public static readonly bool Input = false ;
82
+ }
83
+
84
+ public class StaticReadonly2 : Base
85
+ {
86
+ [ ParamsAllValues ]
87
+ public static readonly bool Input = false ;
88
+ }
89
+
90
+ public class StaticReadonly3 : Base
91
+ {
92
+ [ ParamsSource ( nameof ( Source ) ) ]
93
+ public static readonly bool Input = false ;
94
+ }
95
+
96
+ public class NonStaticReadonly1 : Base
97
+ {
98
+ [ Params ( false , true ) ]
99
+ public readonly bool Input = false ;
100
+ }
101
+
102
+ public class NonStaticReadonly2 : Base
103
+ {
104
+ [ ParamsAllValues ]
105
+ public readonly bool Input = false ;
106
+ }
107
+
108
+ public class NonStaticReadonly3 : Base
109
+ {
110
+ [ ParamsSource ( nameof ( Source ) ) ]
111
+ public readonly bool Input = false ;
112
+ }
113
+ }
114
+ }
0 commit comments