|
| 1 | +using System.Collections.Generic; |
| 2 | +using System.Linq; |
| 3 | +using BenchmarkDotNet.Attributes; |
| 4 | +using BenchmarkDotNet.Running; |
| 5 | +using BenchmarkDotNet.Validators; |
| 6 | +using Xunit; |
| 7 | + |
| 8 | +namespace BenchmarkDotNet.Tests.Validators |
| 9 | +{ |
| 10 | + public class ParameterizationValidatorTests |
| 11 | + { |
| 12 | + [Fact] |
| 13 | + public void PublicIsSupported() |
| 14 | + { |
| 15 | + var validationErrors = ParameterizationValidator.FailOnError.Validate(BenchmarkConverter.TypeToBenchmarks(typeof(PublicParameterization))); |
| 16 | + |
| 17 | + Assert.Empty(validationErrors); |
| 18 | + } |
| 19 | + |
| 20 | + public class PublicParameterization |
| 21 | + { |
| 22 | +#pragma warning disable CS0649 |
| 23 | + [Params(1)] |
| 24 | + public int Field; |
| 25 | + |
| 26 | + [Params(1)] |
| 27 | + public int Property { get; set; } |
| 28 | + |
| 29 | + [ParamsSource(nameof(GetValues))] |
| 30 | + public int FieldSource; |
| 31 | + |
| 32 | + [ParamsSource(nameof(GetValues))] |
| 33 | + public int PropertySource { get; set; } |
| 34 | +#pragma warning restore CS0649 |
| 35 | + |
| 36 | + public IEnumerable<object> GetValues() |
| 37 | + { |
| 38 | + yield return 1; |
| 39 | + } |
| 40 | + |
| 41 | + [Benchmark] |
| 42 | + public void Foo() { } |
| 43 | + } |
| 44 | + |
| 45 | + [Fact] |
| 46 | + public void InternalIsNotSupported() |
| 47 | + { |
| 48 | + var validationErrors = ParameterizationValidator.FailOnError.Validate(BenchmarkConverter.TypeToBenchmarks(typeof(InternalParameterization))).ToArray(); |
| 49 | + |
| 50 | + Assert.NotEmpty(validationErrors); |
| 51 | + Assert.StartsWith("Member \"Field\" must be public if it has the [Params]", validationErrors[0].Message); |
| 52 | + Assert.StartsWith("Member \"Property\" must be public if it has the [Params]", validationErrors[1].Message); |
| 53 | + Assert.StartsWith("Member \"FieldSource\" must be public if it has the [ParamsSource]", validationErrors[2].Message); |
| 54 | + Assert.StartsWith("Member \"PropertySource\" must be public if it has the [ParamsSource]", validationErrors[3].Message); |
| 55 | + } |
| 56 | + |
| 57 | + public class InternalParameterization |
| 58 | + { |
| 59 | +#pragma warning disable CS0649 |
| 60 | + [Params(1)] |
| 61 | + internal int Field; |
| 62 | + |
| 63 | + [Params(1)] |
| 64 | + internal int Property { get; set; } |
| 65 | + |
| 66 | + [ParamsSource(nameof(GetValues))] |
| 67 | + internal int FieldSource; |
| 68 | + |
| 69 | + [ParamsSource(nameof(GetValues))] |
| 70 | + internal int PropertySource { get; set; } |
| 71 | +#pragma warning restore CS0649 |
| 72 | + |
| 73 | + public IEnumerable<object> GetValues() |
| 74 | + { |
| 75 | + yield return 0; |
| 76 | + } |
| 77 | + |
| 78 | + [Benchmark] |
| 79 | + public void Foo() { } |
| 80 | + } |
| 81 | + |
| 82 | + [Fact] |
| 83 | + public void PrivateIsNotSupported() |
| 84 | + { |
| 85 | + var validationErrors = ParameterizationValidator.FailOnError.Validate(BenchmarkConverter.TypeToBenchmarks(typeof(PrivateParameterization))).ToArray(); |
| 86 | + |
| 87 | + Assert.NotEmpty(validationErrors); |
| 88 | + Assert.StartsWith("Member \"Field\" must be public if it has the [Params]", validationErrors[0].Message); |
| 89 | + Assert.StartsWith("Member \"Property\" must be public if it has the [Params]", validationErrors[1].Message); |
| 90 | + Assert.StartsWith("Member \"FieldSource\" must be public if it has the [ParamsSource]", validationErrors[2].Message); |
| 91 | + Assert.StartsWith("Member \"PropertySource\" must be public if it has the [ParamsSource]", validationErrors[3].Message); |
| 92 | + } |
| 93 | + |
| 94 | + public class PrivateParameterization |
| 95 | + { |
| 96 | +#pragma warning disable CA1823 |
| 97 | +#pragma warning disable CS0169 |
| 98 | + [Params(1)] |
| 99 | + private int Field; |
| 100 | + |
| 101 | + [Params(1)] |
| 102 | + private int Property { get; set; } |
| 103 | + |
| 104 | + [ParamsSource(nameof(GetValues))] |
| 105 | + private int FieldSource; |
| 106 | + |
| 107 | + [ParamsSource(nameof(GetValues))] |
| 108 | + private int PropertySource { get; set; } |
| 109 | +#pragma warning restore CA1823 |
| 110 | +#pragma warning restore CS0169 |
| 111 | + |
| 112 | + public IEnumerable<object> GetValues() |
| 113 | + { |
| 114 | + yield return 0; |
| 115 | + } |
| 116 | + |
| 117 | + [Benchmark] |
| 118 | + public void Foo() { } |
| 119 | + } |
| 120 | + } |
| 121 | +} |
0 commit comments