1+ using System ;
2+ using System . Globalization ;
3+ using System . Linq ;
4+ using System . Runtime . CompilerServices ;
5+ using System . Threading ;
6+ using ApprovalTests ;
7+ using ApprovalTests . Namers ;
8+ using ApprovalTests . Reporters ;
9+ using BenchmarkDotNet . Attributes ;
10+ using BenchmarkDotNet . Configs ;
11+ using BenchmarkDotNet . Exporters ;
12+ using BenchmarkDotNet . Loggers ;
13+ using BenchmarkDotNet . Tests . Mocks ;
14+ using BenchmarkDotNet . Validators ;
15+ using JetBrains . Annotations ;
16+ using Xunit ;
17+
18+ namespace BenchmarkDotNet . Tests . Exporters
19+ {
20+ [ UseReporter ( typeof ( XUnit2Reporter ) ) ]
21+ [ UseApprovalSubdirectory ( "ApprovedFiles" ) ]
22+ [ Collection ( "ApprovalTests" ) ]
23+ public class MarkdownExporterMultipleClassApprovalTests : IDisposable
24+ {
25+ private readonly CultureInfo initCulture ;
26+
27+ public MarkdownExporterMultipleClassApprovalTests ( ) => initCulture = Thread . CurrentThread . CurrentCulture ;
28+
29+ [ UsedImplicitly ]
30+ public static TheoryData < Type , BenchmarkLogicalGroupRule ? > GetLogicalGroupRules ( )
31+ {
32+ var data = new TheoryData < Type , BenchmarkLogicalGroupRule ? > ( ) ;
33+
34+ foreach ( var type in typeof ( LogicalGroupBenchmarks ) . GetNestedTypes ( ) )
35+ {
36+ data . Add ( type , null ) ; // no logical rule
37+
38+ foreach ( var rule in Enum . GetValues ( typeof ( BenchmarkLogicalGroupRule ) ) )
39+ data . Add ( type , ( BenchmarkLogicalGroupRule ? ) rule ) ;
40+ }
41+
42+ return data ;
43+ }
44+
45+ [ Theory ]
46+ [ MemberData ( nameof ( GetLogicalGroupRules ) ) ]
47+ [ MethodImpl ( MethodImplOptions . NoInlining ) ]
48+ public void LogicalRuleTest ( Type benchmarkType , BenchmarkLogicalGroupRule ? rule )
49+ {
50+ var config = DefaultConfig . Instance ;
51+ if ( rule is { } logicalRule )
52+ config = config . AddLogicalGroupRules ( logicalRule ) ;
53+
54+ var fileName = rule == null
55+ ? $ "{ benchmarkType . Name } .Default"
56+ : $ "{ benchmarkType . Name } .{ rule } ";
57+
58+ NamerFactory . AdditionalInformation = fileName ;
59+ Thread . CurrentThread . CurrentCulture = CultureInfo . InvariantCulture ;
60+
61+ var logger = new AccumulationLogger ( ) ;
62+ logger . WriteLine ( $ "=== " + fileName + " ===" ) ;
63+
64+ var exporter = MarkdownExporter . Mock ;
65+ var summary = MockFactory . CreateSummary ( config , benchmarkType . GetNestedTypes ( ) ) ;
66+ exporter . ExportToLog ( summary , logger ) ;
67+
68+ var validator = BaselineValidator . FailOnError ;
69+ var errors = validator . Validate ( new ValidationParameters ( summary . BenchmarksCases , summary . BenchmarksCases . First ( ) . Config ) ) . ToList ( ) ;
70+ logger . WriteLine ( ) ;
71+ logger . WriteLine ( "Errors: " + errors . Count ) ;
72+ foreach ( var error in errors )
73+ logger . WriteLineError ( "* " + error . Message ) ;
74+
75+ Approvals . Verify ( logger . GetLog ( ) ) ;
76+ }
77+
78+ public void Dispose ( ) => Thread . CurrentThread . CurrentCulture = initCulture ;
79+
80+ public static class LogicalGroupBenchmarks
81+ {
82+ public static class NoBaseline
83+ {
84+ [ LogicalGroupColumn , CategoriesColumn , BaselineColumn ]
85+ [ BenchmarkCategory ( "A" ) ]
86+ public class Bench1
87+ {
88+ [ Params ( 10 , 20 ) ] public int Param ;
89+ [ Benchmark ] public void Foo ( ) { }
90+ [ Benchmark ] public void Bar ( ) { }
91+ }
92+
93+ [ BenchmarkCategory ( "B" ) ]
94+ public class Bench2
95+ {
96+ [ Params ( 10 , 20 ) ] public int Param ;
97+ [ Benchmark ] public void Foo ( ) { }
98+ [ Benchmark ] public void Bar ( ) { }
99+ }
100+ }
101+
102+ public static class OneBaseline
103+ {
104+ [ LogicalGroupColumn , CategoriesColumn , BaselineColumn ]
105+ [ BenchmarkCategory ( "A" ) ]
106+ public class Bench1
107+ {
108+ [ Params ( 10 , 20 ) ] public int Param ;
109+ [ Benchmark ( Baseline = true ) ] public void Foo ( ) { }
110+ [ Benchmark ] public void Bar ( ) { }
111+ }
112+
113+ [ BenchmarkCategory ( "B" ) ]
114+ public class Bench2
115+ {
116+ [ Params ( 10 , 20 ) ] public int Param ;
117+ [ Benchmark ] public void Foo ( ) { }
118+ [ Benchmark ] public void Bar ( ) { }
119+ }
120+ }
121+
122+ public static class TwoBaselines
123+ {
124+ [ LogicalGroupColumn , CategoriesColumn , BaselineColumn ]
125+ [ BenchmarkCategory ( "A" ) ]
126+ public class Bench1
127+ {
128+ [ Params ( 10 , 20 ) ] public int Param ;
129+ [ Benchmark ( Baseline = true ) ] public void Foo ( ) { }
130+ [ Benchmark ] public void Bar ( ) { }
131+ }
132+
133+ [ BenchmarkCategory ( "B" ) ]
134+ public class Bench2
135+ {
136+ [ Params ( 10 , 20 ) ] public int Param ;
137+ [ Benchmark ( Baseline = true ) ] public void Foo ( ) { }
138+ [ Benchmark ] public void Bar ( ) { }
139+ }
140+ }
141+
142+ public static class TwoBaselinesWithJobs
143+ {
144+ [ LogicalGroupColumn , CategoriesColumn , BaselineColumn ]
145+ [ BenchmarkCategory ( "A" ) ]
146+ [ SimpleJob ( id : "Job1" ) , SimpleJob ( id : "Job2" ) ]
147+ public class Bench1
148+ {
149+ [ Params ( 10 , 20 ) ] public int Param ;
150+ [ Benchmark ( Baseline = true ) ] public void Foo ( ) { }
151+ [ Benchmark ] public void Bar ( ) { }
152+ }
153+
154+ [ BenchmarkCategory ( "B" ) ]
155+ public class Bench2
156+ {
157+ [ Params ( 10 , 20 ) ] public int Param ;
158+ [ Benchmark ( Baseline = true ) ] public void Foo ( ) { }
159+ [ Benchmark ] public void Bar ( ) { }
160+ }
161+ }
162+ }
163+ }
164+ }
0 commit comments