From 676d42b6332e41fc6a7e67a315d55fe00362c402 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Pyrzyk?= Date: Wed, 2 Aug 2017 10:55:08 +0200 Subject: [PATCH 1/2] #495 - Unit test for reading attributes from the base class --- .../Running/BenchmarkConverterTests.cs | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 tests/BenchmarkDotNet.Tests/Running/BenchmarkConverterTests.cs diff --git a/tests/BenchmarkDotNet.Tests/Running/BenchmarkConverterTests.cs b/tests/BenchmarkDotNet.Tests/Running/BenchmarkConverterTests.cs new file mode 100644 index 0000000000..f999fc425f --- /dev/null +++ b/tests/BenchmarkDotNet.Tests/Running/BenchmarkConverterTests.cs @@ -0,0 +1,65 @@ +using System.Linq; +using BenchmarkDotNet.Attributes; +using BenchmarkDotNet.Running; +using Xunit; + +namespace BenchmarkDotNet.Tests.Running +{ + public class BenchmarkConverterTests + { + /// + /// https://github.com/dotnet/BenchmarkDotNet/issues/495 + /// + [Fact] + public void ReadsAttributesFromBaseClass() + { + Benchmark benchmark = BenchmarkConverter.TypeToBenchmarks(typeof(Derived)).Single(); + + Assert.NotNull(benchmark); + Assert.NotNull(benchmark.Target); + Assert.NotNull(benchmark.Target.IterationSetupMethod); + Assert.NotNull(benchmark.Target.IterationCleanupMethod); + Assert.NotNull(benchmark.Target.GlobalCleanupMethod); + Assert.NotNull(benchmark.Target.GlobalSetupMethod); + } + + public abstract class Base + { + [GlobalSetup] + public abstract void GlobalSetup(); + + [GlobalCleanup] + public abstract void GlobalCleanup(); + + [IterationSetup] + public abstract void Setup(); + + [IterationCleanup] + public abstract void Cleanup(); + + [Benchmark] + public void Test() + { + } + } + + public class Derived : Base + { + public override void GlobalSetup() + { + } + + public override void GlobalCleanup() + { + } + + public override void Setup() + { + } + + public override void Cleanup() + { + } + } + } +} From 90dea26b6037353314795d564dcf47c6fc216b6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Pyrzyk?= Date: Wed, 2 Aug 2017 11:28:26 +0200 Subject: [PATCH 2/2] Assertion for method declaring type --- .../Running/BenchmarkConverterTests.cs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/tests/BenchmarkDotNet.Tests/Running/BenchmarkConverterTests.cs b/tests/BenchmarkDotNet.Tests/Running/BenchmarkConverterTests.cs index f999fc425f..61978cfa04 100644 --- a/tests/BenchmarkDotNet.Tests/Running/BenchmarkConverterTests.cs +++ b/tests/BenchmarkDotNet.Tests/Running/BenchmarkConverterTests.cs @@ -13,14 +13,23 @@ public class BenchmarkConverterTests [Fact] public void ReadsAttributesFromBaseClass() { - Benchmark benchmark = BenchmarkConverter.TypeToBenchmarks(typeof(Derived)).Single(); + var derivedType = typeof(Derived); + Benchmark benchmark = BenchmarkConverter.TypeToBenchmarks(derivedType).Single(); Assert.NotNull(benchmark); Assert.NotNull(benchmark.Target); + Assert.NotNull(benchmark.Target.IterationSetupMethod); + Assert.Equal(benchmark.Target.IterationSetupMethod.DeclaringType, derivedType); + Assert.NotNull(benchmark.Target.IterationCleanupMethod); + Assert.Equal(benchmark.Target.IterationCleanupMethod.DeclaringType, derivedType); + Assert.NotNull(benchmark.Target.GlobalCleanupMethod); + Assert.Equal(benchmark.Target.GlobalCleanupMethod.DeclaringType, derivedType); + Assert.NotNull(benchmark.Target.GlobalSetupMethod); + Assert.Equal(benchmark.Target.GlobalSetupMethod.DeclaringType, derivedType); } public abstract class Base