├── .editorconfig ├── .gitignore ├── LICENSE ├── README.md ├── azure-pipelines.yml ├── build-templates ├── build-package.job.yml ├── build-solution.steps.yml ├── publish-nuget-package-artifacts.steps.yml └── run-xunit-unit-tests.steps.yml └── src ├── Pitcher.Benchmarks ├── Benchmarks │ ├── ArgumentNullBenchmarks.cs │ ├── ArgumentOutOfRangeBenchmarks.cs │ ├── ThrowBenchmarks.cs │ └── ThrowTBenchmarks.cs ├── Pitcher.Benchmarks.csproj └── Program.cs ├── Pitcher.Tests ├── ArgumentNullTests.cs ├── ArgumentOutOfRangeTests.cs ├── Fakes │ └── ObjWithIncorrectOverridenEquals.cs ├── Pitcher.Tests.csproj ├── ThrowTTests.cs └── ThrowTests.cs ├── Pitcher.sln └── Pitcher ├── GlobalSuppressions.cs ├── Pitcher.csproj ├── Pitcher.snk ├── Throw.cs └── ThrowT.cs /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | # Enable "this." 4 | dotnet_style_qualification_for_field = true:warning 5 | dotnet_style_qualification_for_property = true:warning 6 | dotnet_style_qualification_for_method = true:warning 7 | dotnet_style_qualification_for_method = true:warning 8 | 9 | # use predefined type ("int") instead of framework names ("Int32") 10 | dotnet_style_predefined_type_for_locals_parameters_members = true:suggestion 11 | dotnet_style_predefined_type_for_member_access = true:suggestion 12 | 13 | # Access modifiers 14 | dotnet_style_require_accessibility_modifiers = always:warning 15 | 16 | # 17 | 18 | [*.cs] 19 | # Enable "this." 20 | dotnet_style_qualification_for_field = true:warning 21 | dotnet_style_qualification_for_property = true:warning 22 | dotnet_style_qualification_for_method = true:warning 23 | dotnet_style_qualification_for_method = true:warning 24 | # use predefined type ("int") instead of framework names ("Int32") 25 | dotnet_style_predefined_type_for_locals_parameters_members = true:suggestion 26 | dotnet_style_predefined_type_for_member_access = true:suggestion 27 | # Access modifiers 28 | dotnet_style_require_accessibility_modifiers = always:error 29 | # Prefer "var" 30 | csharp_style_var_for_built_in_types = true:suggestion 31 | csharp_style_var_when_type_is_apparent = true:suggestion 32 | csharp_style_var_elsewhere = true:suggestion 33 | # Pattern match ("object is int i") over "is..as" dance 34 | csharp_style_pattern_matching_over_is_with_cast_check = true:warning 35 | csharp_style_pattern_matching_over_as_with_null_check = true:warning 36 | # Inline out variable 37 | csharp_style_inlined_variable_declaration = true:warning 38 | # Throw expressions 39 | csharp_style_throw_expression = true:error -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | 4 | # Visual studio files 5 | .vs/ 6 | 7 | # BenchmarkDotNet 8 | BenchmarkDotNet.Artifacts/ 9 | 10 | # Generated documentation 11 | docs/ 12 | 13 | # User-specific files 14 | *.suo 15 | *.user 16 | *.userosscache 17 | *.sln.docstates 18 | 19 | # Build results 20 | [Dd]ebug/ 21 | [Dd]ebugPublic/ 22 | [Rr]elease/ 23 | [Rr]eleases/ 24 | x64/ 25 | x86/ 26 | build/ 27 | bld/ 28 | [Bb]in/ 29 | [Oo]bj/ 30 | 31 | # Roslyn cache directories 32 | *.ide/ 33 | 34 | # MSTest test Results 35 | [Tt]est[Rr]esult*/ 36 | [Bb]uild[Ll]og.* 37 | 38 | #NUNIT 39 | *.VisualState.xml 40 | TestResult.xml 41 | 42 | # Build Results of an ATL Project 43 | [Dd]ebugPS/ 44 | [Rr]eleasePS/ 45 | dlldata.c 46 | 47 | *_i.c 48 | *_p.c 49 | *_i.h 50 | *.ilk 51 | *.meta 52 | *.obj 53 | *.pch 54 | *.pdb 55 | *.pgc 56 | *.pgd 57 | *.rsp 58 | *.sbr 59 | *.tlb 60 | *.tli 61 | *.tlh 62 | *.tmp 63 | *.tmp_proj 64 | *.log 65 | *.vspscc 66 | *.vssscc 67 | .builds 68 | *.pidb 69 | *.svclog 70 | *.scc 71 | 72 | # Chutzpah Test files 73 | _Chutzpah* 74 | 75 | # Visual C++ cache files 76 | ipch/ 77 | *.aps 78 | *.ncb 79 | *.opensdf 80 | *.sdf 81 | *.cachefile 82 | 83 | # Visual Studio profiler 84 | *.psess 85 | *.vsp 86 | *.vspx 87 | 88 | # TFS 2012 Local Workspace 89 | $tf/ 90 | 91 | # Guidance Automation Toolkit 92 | *.gpState 93 | 94 | # ReSharper is a .NET coding add-in 95 | _ReSharper*/ 96 | *.[Rr]e[Ss]harper 97 | *.DotSettings.user 98 | 99 | # JustCode is a .NET coding addin-in 100 | .JustCode 101 | 102 | # TeamCity is a build add-in 103 | _TeamCity* 104 | 105 | # DotCover is a Code Coverage Tool 106 | *.dotCover 107 | 108 | # NCrunch 109 | _NCrunch_* 110 | .*crunch*.local.xml 111 | 112 | # MightyMoose 113 | *.mm.* 114 | AutoTest.Net/ 115 | 116 | # Web workbench (sass) 117 | .sass-cache/ 118 | 119 | # Installshield output folder 120 | [Ee]xpress/ 121 | 122 | # DocProject is a documentation generator add-in 123 | DocProject/buildhelp/ 124 | DocProject/Help/*.HxT 125 | DocProject/Help/*.HxC 126 | DocProject/Help/*.hhc 127 | DocProject/Help/*.hhk 128 | DocProject/Help/*.hhp 129 | DocProject/Help/Html2 130 | DocProject/Help/html 131 | 132 | # Click-Once directory 133 | publish/ 134 | 135 | # Publish Web Output 136 | *.[Pp]ublish.xml 137 | *.azurePubxml 138 | # TODO: Comment the next line if you want to checkin your web deploy settings 139 | # but database connection strings (with potential passwords) will be unencrypted 140 | *.pubxml 141 | *.publishproj 142 | 143 | # NuGet Packages 144 | *.nupkg 145 | # The packages folder can be ignored because of Package Restore 146 | **/packages/* 147 | # except build/, which is used as an MSBuild target. 148 | !**/packages/build/ 149 | # If using the old MSBuild-Integrated Package Restore, uncomment this: 150 | #!**/packages/repositories.config 151 | 152 | # Windows Azure Build Output 153 | csx/ 154 | *.build.csdef 155 | 156 | # Windows Store app package directory 157 | AppPackages/ 158 | 159 | # Others 160 | sql/ 161 | *.Cache 162 | ClientBin/ 163 | [Ss]tyle[Cc]op.* 164 | ~$* 165 | *~ 166 | *.dbmdl 167 | *.dbproj.schemaview 168 | *.pfx 169 | *.publishsettings 170 | node_modules/ 171 | bower_components/ 172 | 173 | # RIA/Silverlight projects 174 | Generated_Code/ 175 | 176 | # Backup & report files from converting an old project file 177 | # to a newer Visual Studio version. Backup files are not needed, 178 | # because we have git ;-) 179 | _UpgradeReport_Files/ 180 | Backup*/ 181 | UpgradeLog*.XML 182 | UpgradeLog*.htm 183 | 184 | # SQL Server files 185 | *.mdf 186 | *.ldf 187 | 188 | # Business Intelligence projects 189 | *.rdl.data 190 | *.bim.layout 191 | *.bim_*.settings 192 | 193 | # Microsoft Fakes 194 | FakesAssemblies/ 195 | 196 | # Coverlet coverage file 197 | coverage.json -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Alex Kamsteeg 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Pitcher 2 | 3 | Pitcher is a utility library to simplify throwing exceptions, especially when checking arguments. It makes methods easier to inline, by reducing code size. 4 | 5 | 6 | [![Build Status](https://interastra.visualstudio.com/OSS%20-%20CI/_apis/build/status/Pitcher%20CI?branchName=master)](https://dev.azure.com/interastra/OSS%20-%20CI/_build?definitionId=14&_a=summary&branchName=master) [![Azure DevOps coverage](https://img.shields.io/azure-devops/coverage/interastra/OSS%20-%20CI/14.svg?cacheSeconds=3600)](https://dev.azure.com/interastra/OSS%20-%20CI/_build?definitionId=14&_a=summary) 7 | 8 | ## Platform support 9 | 10 | | .NET Framework | .NET Core | 11 | |:------------------:|:------------------:| 12 | | :heavy_check_mark: | :heavy_check_mark: | 13 | 14 | ## Installation 15 | 16 | The Pitcher NuGet package is the prefered way to install and use it. 17 | 18 | [![NuGet](https://img.shields.io/nuget/v/Pitcher.svg?cacheSeconds=3600)](https://www.nuget.org/packages/Pitcher/) 19 | 20 | ```powershell 21 | install-package Pitcher 22 | ``` 23 | 24 | ## Usage 25 | 26 | To use Pitcher, add a using to your source file: 27 | 28 | ```csharp 29 | using Pitcher; 30 | ``` 31 | 32 | Then, you can use `Throw` or `Throw` to throw exceptions. 33 | 34 | ### Throw 35 | 36 | Throw an exception directly: 37 | ```csharp 38 | Throw.This(new ArgumentNullException(nameof(args)); 39 | ``` 40 | 41 | Throw an exception based on a condition: 42 | 43 | ```csharp 44 | Throw.When(args == null, new ArgumentNullException(nameof(args)); // This will always allocate the exception 45 | ``` 46 | 47 | Throw an exception based on a condition, with a `Func` to create the exception in a more complex way: 48 | 49 | ```csharp 50 | Throw.When(args == null, () => new ArgumentNullException(nameof(args)); 51 | ``` 52 | 53 | #### ArgumentNullException 54 | 55 | There are helpers for simplifying argument checking and throwing an `ArgumentNullException`. 56 | 57 | ```csharp 58 | // Throw ArgumentNullException for parameter 59 | Throw.ArgumentNull.For(nameof(args)); 60 | 61 | // Throw ArgumentNullException for parameter, with message 62 | Throw.ArgumentNull.For(nameof(args), "Args is a required parameter"); 63 | 64 | // Throw ArgumentNullException for parameter, when a condition is met 65 | Throw.ArgumentNull.When(args == null, nameof(args)); 66 | 67 | // Throw ArgumentNullException for parameter with a message, when a condition is met 68 | Throw.ArgumentNull.When(args == null, nameof(args), "Args is a required parameter"); 69 | 70 | // Throw ArgumentNullException for parameter, when the parameter is null 71 | Throw.ArgumentNull.WhenNull(args, nameof(args)); 72 | 73 | // Throw ArgumentNullException for parameter with a message, when the parameter is null 74 | Throw.ArgumentNull.WhenNull(args, nameof(args), "Args is a required parameter"); 75 | 76 | // Throw ArgumentNullException for a null or empty string 77 | Throw.ArgumentNull.WhenNullOrEmpty(args, nameof(args)); 78 | 79 | // Throw ArgumentNullException with a message for a null or empty string 80 | Throw.ArgumentNull.WhenNullOrEmpty(args, nameof(args), "Args is a required parameter"); 81 | 82 | // Throw ArgumentNullException for a null, empty or whitespace string 83 | Throw.ArgumentNull.WhenNullOrWhitespace(args, nameof(args)); 84 | 85 | // Throw ArgumentNullException with a message for a null, empty or whitespace string 86 | Throw.ArgumentNull.WhenNullOrWhitespace(args, nameof(args), "Args is a required parameter"); 87 | 88 | // Throw ArgumentNullException for a null or empty IEnumerable 89 | Throw.ArgumentNull.WhenNullOrEmpty(new List(), nameof(args)); 90 | 91 | // Throw ArgumentNullException with a message for a null or empty IEnumerable 92 | Throw.ArgumentNull.WhenNullOrEmpty(new List(), nameof(args), "Args is a required parameter"); 93 | ``` 94 | 95 | #### ArgumentOutOfRangeException 96 | 97 | Specific helpers are available for throwing an `ArgumentOutOfRangeException`. 98 | 99 | ```csharp 100 | // Throw ArgumentOutOfRangeException for parameter 101 | Throw.ArgumentOutOfRange.For(nameof(args)); 102 | 103 | // Throw ArgumentOutOfRangeException for parameter, with message 104 | Throw.ArgumentOutOfRange.For(nameof(args), "Args is out of the acceptable range"); 105 | 106 | // Throw ArgumentOutOfRangeException for parameter, when a condition is met 107 | Throw.ArgumentOutOfRange.When(args < 0, nameof(args)); 108 | 109 | // Throw ArgumentOutOfRangeException for parameter with a message, when a condition is met 110 | Throw.ArgumentOutOfRange.When(args < 0, nameof(args), "Args is out of the acceptable range"); 111 | 112 | ``` 113 | 114 | ### Throw<T> 115 | 116 | For exceptions without parameters or when you don't care about the parameters, you can use `Throw`. 117 | 118 | ```csharp 119 | // Throw an exception 120 | Throw.Now(); 121 | 122 | //Throw an exception based on a condition 123 | Throw.When(obj == null); 124 | ``` 125 | 126 | ## License 127 | 128 | Pitcher uses the MIT license, see the LICENSE file. 129 | -------------------------------------------------------------------------------- /azure-pipelines.yml: -------------------------------------------------------------------------------- 1 | jobs: 2 | - template: build-templates/build-package.job.yml 3 | parameters: 4 | name: Windows 5 | vmImage: 'windows-2022' 6 | publishBuildArtifacts: 'true' 7 | publishCodeCoverage: 'true' 8 | 9 | - template: build-templates/build-package.job.yml 10 | parameters: 11 | name: Linux 12 | vmImage: 'ubuntu-latest' 13 | publishBuildArtifacts: 'false' 14 | publishCodeCoverage: 'false' 15 | 16 | - template: build-templates/build-package.job.yml 17 | parameters: 18 | name: MacOS 19 | vmImage: 'macOS-latest' 20 | publishBuildArtifacts: 'false' 21 | publishCodeCoverage: 'false' -------------------------------------------------------------------------------- /build-templates/build-package.job.yml: -------------------------------------------------------------------------------- 1 | # Defines a job that builds a solution with optionally 2 | 3 | parameters: 4 | name: '(Unknown)' 5 | vmImage: '' 6 | buildConfiguration: 'Release' 7 | publishCodeCoverage: 'false' 8 | publishBuildArtifacts: 'false' 9 | 10 | jobs: 11 | - job: ${{ parameters.name }} 12 | pool: 13 | vmImage: ${{ parameters.vmImage }} 14 | steps: 15 | - template: build-solution.steps.yml 16 | parameters: 17 | buildConfiguration: ${{ parameters.buildConfiguration }} 18 | - template: run-xunit-unit-tests.steps.yml 19 | parameters: 20 | buildConfiguration: ${{ parameters.buildConfiguration }} 21 | pathToUnitTestProject: '$(Build.SourcesDirectory)/src/**/*.Tests.csproj' 22 | publishCodeCoverage: ${{ parameters.publishCodeCoverage }} 23 | - ${{ if eq(parameters.publishBuildArtifacts, 'true') }}: 24 | - template: publish-nuget-package-artifacts.steps.yml 25 | -------------------------------------------------------------------------------- /build-templates/build-solution.steps.yml: -------------------------------------------------------------------------------- 1 | # Build a solution 2 | 3 | parameters: 4 | buildConfiguration: 'Release' 5 | 6 | steps: 7 | - task: DotNetCoreCLI@2 8 | displayName: 'Restore packages' 9 | inputs: 10 | command: restore 11 | projects: '**/*.csproj' 12 | arguments: '--configuration ${{ parameters.buildConfiguration }}' 13 | 14 | - task: DotNetCoreCLI@2 15 | displayName: 'Build solution' 16 | inputs: 17 | command: build 18 | projects: '**/*.sln' 19 | arguments: '--configuration ${{ parameters.buildConfiguration }}' 20 | -------------------------------------------------------------------------------- /build-templates/publish-nuget-package-artifacts.steps.yml: -------------------------------------------------------------------------------- 1 | # Publishes NuGet packages as build artifacts 2 | 3 | steps: 4 | - task: CopyFiles@2 5 | displayName: 'Copy NuGet package to: $(build.artifactstagingdirectory)' 6 | inputs: 7 | Contents: '**/*.nupkg' 8 | TargetFolder: '$(build.artifactstagingdirectory)' 9 | flattenFolders: true 10 | 11 | - task: CopyFiles@2 12 | displayName: 'Copy NuGet symbols package to: $(build.artifactstagingdirectory)' 13 | inputs: 14 | Contents: '**/*.snupkg' 15 | TargetFolder: '$(build.artifactstagingdirectory)' 16 | flattenFolders: true 17 | 18 | - task: PublishBuildArtifacts@1 19 | displayName: 'Publish Artifact: NuGet packages' 20 | inputs: 21 | PathtoPublish: '$(build.artifactstagingdirectory)' 22 | ArtifactName: 'NuGet packages' -------------------------------------------------------------------------------- /build-templates/run-xunit-unit-tests.steps.yml: -------------------------------------------------------------------------------- 1 | # Run the XUnit unit tests and optionally publish the Coverlet code coverage results 2 | 3 | parameters: 4 | buildConfiguration: 'Release' 5 | pathToUnitTestProject: '$(Build.SourcesDirectory)/**/*.Tests.csproj' 6 | publishCodeCoverage: 'true' 7 | excludeFromCodeCoverage: '[xunit*]*%2c[*.Tests]*' 8 | 9 | steps: 10 | - task: DotNetCoreCLI@2 11 | displayName: 'Run tests' 12 | inputs: 13 | command: test 14 | projects: '${{ parameters.pathToUnitTestProject }}' 15 | arguments: '--configuration ${{ parameters.buildConfiguration }} /p:CollectCoverage=true /p:CoverletOutputFormat=cobertura /p:Exclude="${{ parameters.excludeFromCodeCoverage }}"' 16 | 17 | - ${{ if eq(parameters.publishCodeCoverage, 'true') }}: 18 | - task: PublishCodeCoverageResults@1 19 | displayName: 'Publish code coverage results' 20 | inputs: 21 | summaryFileLocation: $(Build.SourcesDirectory)/**/*.cobertura.xml 22 | codecoverageTool: cobertura -------------------------------------------------------------------------------- /src/Pitcher.Benchmarks/Benchmarks/ArgumentNullBenchmarks.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using BenchmarkDotNet.Attributes; 3 | 4 | namespace Pitcher.Benchmarks.Benchmarks 5 | { 6 | public class ArgumentNullBenchmarks 7 | { 8 | [Benchmark] 9 | public void IfNullThenThrow() 10 | { 11 | try 12 | { 13 | object obj = null; 14 | 15 | if (obj == null) 16 | { 17 | throw new ArgumentNullException(nameof(obj)); 18 | } 19 | } 20 | catch 21 | { 22 | } 23 | } 24 | 25 | [Benchmark] 26 | public void ThrowArgumentNullFor() 27 | { 28 | try 29 | { 30 | object obj = null; 31 | 32 | Throw.ArgumentNull.For(nameof(obj)); 33 | } 34 | catch 35 | { 36 | } 37 | } 38 | 39 | [Benchmark] 40 | public void ThrowArgumentNullWhen() 41 | { 42 | try 43 | { 44 | object obj = null; 45 | 46 | Throw.ArgumentNull.When(obj == null, nameof(obj)); 47 | } 48 | catch 49 | { 50 | } 51 | } 52 | 53 | [Benchmark] 54 | public void ThrowArgumentNullWhenNull() 55 | { 56 | try 57 | { 58 | object obj = null; 59 | 60 | Throw.ArgumentNull.WhenNull(obj, nameof(obj)); 61 | } 62 | catch 63 | { 64 | } 65 | } 66 | 67 | [Benchmark] 68 | public void ThrowArgumentNullWhenStringNull() 69 | { 70 | try 71 | { 72 | object obj = null; 73 | 74 | Throw.ArgumentNull.WhenNull((string)obj, nameof(obj)); 75 | } 76 | catch 77 | { 78 | } 79 | } 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /src/Pitcher.Benchmarks/Benchmarks/ArgumentOutOfRangeBenchmarks.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | using BenchmarkDotNet.Attributes; 5 | 6 | namespace Pitcher.Benchmarks.Benchmarks 7 | { 8 | public class ArgumentOutOfRangeBenchmarks 9 | { 10 | [Benchmark] 11 | public void IfOutOfRangeThenThrow() 12 | { 13 | try 14 | { 15 | if (1 < 2) 16 | { 17 | throw new ArgumentOutOfRangeException("foo", "bar"); 18 | } 19 | } 20 | catch 21 | { 22 | } 23 | } 24 | 25 | [Benchmark] 26 | public void ThrowArgumentOutOfRangeFor() 27 | { 28 | try 29 | { 30 | Throw.ArgumentOutOfRange.For("foo", "bar"); 31 | } 32 | catch 33 | { 34 | } 35 | } 36 | 37 | [Benchmark] 38 | public void ThrowArgumentOutOfRangeWhen() 39 | { 40 | try 41 | { 42 | Throw.ArgumentOutOfRange.When(1 < 2, "foo", "bar"); 43 | } 44 | catch 45 | { 46 | } 47 | } 48 | 49 | [Benchmark] 50 | public void ThrowArgumentOutOfRangeWhenNegativeNumber() 51 | { 52 | try 53 | { 54 | Throw.ArgumentOutOfRange.WhenNegativeNumber(-1, "foo", "bar"); 55 | } 56 | catch 57 | { 58 | } 59 | } 60 | 61 | [Benchmark] 62 | public void ThrowArgumentOutOfRangeWhenPositiveNumber() 63 | { 64 | try 65 | { 66 | Throw.ArgumentOutOfRange.WhenNegativeNumber(1, "foo", "bar"); 67 | } 68 | catch 69 | { 70 | } 71 | } 72 | 73 | [Benchmark] 74 | public void ThrowArgumentOutOfRangeWhenLessThan() 75 | { 76 | try 77 | { 78 | Throw.ArgumentOutOfRange.WhenLessThan(0, 1, "foo", "bar"); 79 | } 80 | catch 81 | { 82 | } 83 | } 84 | 85 | [Benchmark] 86 | public void ThrowArgumentOutOfRangeWhenMoreThan() 87 | { 88 | try 89 | { 90 | Throw.ArgumentOutOfRange.WhenMoreThan(1, 0, "foo", "bar"); 91 | } 92 | catch 93 | { 94 | } 95 | } 96 | } 97 | } 98 | -------------------------------------------------------------------------------- /src/Pitcher.Benchmarks/Benchmarks/ThrowBenchmarks.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using BenchmarkDotNet.Attributes; 3 | 4 | namespace Pitcher.Benchmarks.Benchmarks 5 | { 6 | public class ThrowBenchmarks 7 | { 8 | [Benchmark] 9 | public void ThrowException() 10 | { 11 | try 12 | { 13 | throw new Exception("foo"); 14 | } 15 | catch 16 | { 17 | 18 | } 19 | } 20 | 21 | [Benchmark] 22 | public void ThrowThis() 23 | { 24 | try 25 | { 26 | Throw.This(new Exception("foo")); 27 | } 28 | catch 29 | { 30 | 31 | } 32 | } 33 | 34 | [Benchmark] 35 | public void ThrowWhen() 36 | { 37 | try 38 | { 39 | Throw.When(1 < 2, new Exception("foo")); 40 | } 41 | catch 42 | { 43 | } 44 | } 45 | 46 | [Benchmark] 47 | public void ThrowWhenFactory() 48 | { 49 | try 50 | { 51 | Throw.When(1 < 2, () => new Exception("foo")); 52 | } 53 | catch 54 | { 55 | } 56 | } 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /src/Pitcher.Benchmarks/Benchmarks/ThrowTBenchmarks.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | using BenchmarkDotNet.Attributes; 5 | 6 | namespace Pitcher.Benchmarks.Benchmarks 7 | { 8 | public class ThrowTBenchmarks 9 | { 10 | [Benchmark] 11 | public void ThrowException() 12 | { 13 | try 14 | { 15 | throw new Exception(); 16 | } 17 | catch 18 | { 19 | 20 | } 21 | } 22 | 23 | [Benchmark] 24 | public void ThrowTNow() 25 | { 26 | try 27 | { 28 | Throw.Now(); 29 | } 30 | catch 31 | { 32 | 33 | } 34 | } 35 | 36 | [Benchmark] 37 | public void ThrowTWhen() 38 | { 39 | try 40 | { 41 | Throw.When(1 < 2); 42 | } 43 | catch 44 | { 45 | 46 | } 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/Pitcher.Benchmarks/Pitcher.Benchmarks.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Exe 5 | net6.0;netcoreapp3.1;netstandard2.0 6 | 1.0.0 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /src/Pitcher.Benchmarks/Program.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using BenchmarkDotNet.Configs; 3 | using BenchmarkDotNet.Jobs; 4 | using BenchmarkDotNet.Running; 5 | using BenchmarkDotNet.Toolchains.CsProj; 6 | 7 | namespace Pitcher.Benchmarks 8 | { 9 | internal static class Program 10 | { 11 | private static void Main(string[] args) 12 | { 13 | var config = GetConfig(); 14 | BenchmarkRunner.Run(Assembly.GetExecutingAssembly(), config); 15 | } 16 | 17 | private static IConfig GetConfig() 18 | { 19 | var config = ManualConfig.Create(DefaultConfig.Instance); 20 | 21 | config 22 | .AddDiagnoser(BenchmarkDotNet.Diagnosers.MemoryDiagnoser.Default); 23 | 24 | config.AddJob( 25 | Job.Default.WithToolchain(CsProjCoreToolchain.NetCoreApp60).AsBaseline(), 26 | Job.Default.WithToolchain(CsProjCoreToolchain.NetCoreApp31), 27 | Job.Default.WithToolchain(CsProjClassicNetToolchain.Net481) 28 | ); 29 | 30 | return config; 31 | } 32 | } 33 | } -------------------------------------------------------------------------------- /src/Pitcher.Tests/ArgumentNullTests.cs: -------------------------------------------------------------------------------- 1 | using Pitcher.Tests.Fakes; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using Xunit; 6 | 7 | namespace Pitcher.Tests 8 | { 9 | public class ArgumentNullTests 10 | { 11 | [Fact] 12 | public void For_Throws() 13 | { 14 | Assert.Throws("TEST", () => Throw.ArgumentNull.For("TEST")); 15 | } 16 | 17 | [Fact] 18 | public void For_ThrowsAndSetsCorrectArgumentName() 19 | { 20 | const string argumentName = "TEST"; 21 | try 22 | { 23 | Throw.ArgumentNull.For(argumentName); 24 | } 25 | catch(ArgumentNullException e) 26 | { 27 | Assert.Equal(argumentName, e.ParamName); 28 | } 29 | } 30 | 31 | [Fact] 32 | public void For_WithMessage_Throws() 33 | { 34 | Assert.Throws("TEST", () => Throw.ArgumentNull.For("TEST", "message")); 35 | } 36 | 37 | [Fact] 38 | public void For_WithMessage_ThrowsAndSetsCorrectArgumentName() 39 | { 40 | const string argumentName = "TEST"; 41 | const string message = "message"; 42 | 43 | try 44 | { 45 | Throw.ArgumentNull.For(argumentName, message); 46 | } 47 | catch (ArgumentNullException e) 48 | { 49 | Assert.Equal(argumentName, e.ParamName); 50 | Assert.Contains(message, e.Message); 51 | } 52 | } 53 | 54 | [Fact] 55 | public void When_ConditionTrue_Throws() 56 | { 57 | Assert.Throws("TEST", () => Throw.ArgumentNull.When(true, "TEST")); 58 | } 59 | 60 | [Fact] 61 | public void When_ConditionFalse_DoesNotThrow() 62 | { 63 | Throw.ArgumentNull.When(false, "TEST"); 64 | } 65 | 66 | [Fact] 67 | public void When_WithMessage_ConditionTrue_Throws() 68 | { 69 | Assert.Throws("TEST", () => Throw.ArgumentNull.When(true, "TEST", "message")); 70 | } 71 | 72 | [Fact] 73 | public void When_WithMessage_ConditionFalse_DoesNotThrow() 74 | { 75 | Throw.ArgumentNull.When(false, "TEST", "message"); 76 | } 77 | 78 | [Fact] 79 | public void WhenNull_WithNull_Throws() 80 | { 81 | Assert.Throws(() => 82 | Throw.ArgumentNull.WhenNull((object)null, "TEST") 83 | ); 84 | } 85 | 86 | [Fact] 87 | public void WhenNull_WithObject_DoesNotThrow() 88 | { 89 | Throw.ArgumentNull.WhenNull(new object(), "TEST"); 90 | } 91 | 92 | [Fact] 93 | public void WhenNull_WithNull_SetCorrectParameterNameOnException() 94 | { 95 | const string parameterName = "TEST"; 96 | 97 | Assert.Throws(parameterName, () => 98 | Throw.ArgumentNull.WhenNull((object)null, parameterName) 99 | ); 100 | } 101 | 102 | [Fact] 103 | public void WhenNull_WithMessage_WithNull_Throws() 104 | { 105 | Assert.Throws(() => 106 | Throw.ArgumentNull.WhenNull((object)null, "TEST", "message") 107 | ); 108 | } 109 | 110 | [Fact] 111 | public void WhenNull_WithMessage_WithObject_DoesNotThrow() 112 | { 113 | Throw.ArgumentNull.WhenNull(new object(), "TEST", "message"); 114 | } 115 | 116 | [Fact] 117 | public void WhenNull_WithMessage_WithNull_SetCorrectParameterNameOnException() 118 | { 119 | const string parameterName = "TEST"; 120 | 121 | Assert.Throws(parameterName, () => 122 | Throw.ArgumentNull.WhenNull((object)null, parameterName, "message") 123 | ); 124 | } 125 | 126 | [Fact] 127 | public void WhenNull_WithMessage_WithNull_SetCorrectMessageOnException() 128 | { 129 | const string parameterName = "TEST"; 130 | const string message = "TESTMESSAGE"; 131 | 132 | try 133 | { 134 | Throw.ArgumentNull.WhenNull((object)null, parameterName, message); 135 | } 136 | catch (ArgumentNullException e) 137 | { 138 | Assert.Contains(message, e.Message); 139 | } 140 | } 141 | 142 | [Fact] 143 | public void WhenNull_WithString_DoesNotThrow() 144 | { 145 | Throw.ArgumentNull.WhenNull("DUMMY", "TEST"); 146 | } 147 | 148 | [Fact] 149 | public void WhenNull_String_WithNull_Throws() 150 | { 151 | Assert.Throws(() => 152 | Throw.ArgumentNull.WhenNull((string)null, "TEST") 153 | ); 154 | } 155 | 156 | [Fact] 157 | public void WhenNull_String_WithMessage_WithNull_Throws() 158 | { 159 | Assert.Throws(() => 160 | Throw.ArgumentNull.WhenNull((string)null, "TEST", "message") 161 | ); 162 | } 163 | 164 | [Fact] 165 | public void WhenNull_String_WithNull_SetCorrectParameterNameOnException() 166 | { 167 | const string parameterName = "TEST"; 168 | 169 | Assert.Throws(parameterName, () => 170 | Throw.ArgumentNull.WhenNull((string)null, parameterName) 171 | ); 172 | } 173 | 174 | [Fact] 175 | public void WhenNull_String_WithMessage_WithNull_SetCorrectParameterNameOnException() 176 | { 177 | const string parameterName = "TEST"; 178 | 179 | Assert.Throws(parameterName, () => 180 | Throw.ArgumentNull.WhenNull((string)null, parameterName, "message") 181 | ); 182 | } 183 | 184 | [Fact] 185 | public void WhenNull_String_WithMessage_WithNull_SetCorrectMessageOnException() 186 | { 187 | const string parameterName = "TEST"; 188 | const string message = "TESTMESSAGE"; 189 | 190 | try 191 | { 192 | Throw.ArgumentNull.WhenNull((string)null, parameterName, message); 193 | } 194 | catch (ArgumentNullException e) 195 | { 196 | Assert.Contains(message, e.Message); 197 | } 198 | } 199 | 200 | [Fact] 201 | public void WhenNull_WithNull_OfObjectWithIncorrectOverridenEquals_DoesNotThrow() 202 | { 203 | var lyingObject = new ObjWithIncorrectOverridenEquals(); // .Equals(null) returns true 204 | 205 | Throw.ArgumentNull.WhenNull(lyingObject, "TEST"); 206 | } 207 | 208 | [Fact] 209 | public void WhenNull_WithNull_WithMessage_OfObjectWithIncorrectOverridenEquals_DoesNotThrow() 210 | { 211 | const string parameterName = "TEST"; 212 | const string message = "TESTMESSAGE"; 213 | 214 | var lyingObject = new ObjWithIncorrectOverridenEquals(); // .Equals(null) returns true 215 | 216 | Throw.ArgumentNull.WhenNull(lyingObject, parameterName, message); 217 | } 218 | 219 | #region Throw.ArgumentNullOrEmpty.WhenNullOrEmpty(string) 220 | 221 | [Fact] 222 | public void WhenNullOrEmpty_WithString_DoesNotThrow() 223 | { 224 | Throw.ArgumentNull.WhenNullOrEmpty("DUMMY", "TEST"); 225 | } 226 | 227 | [Fact] 228 | public void WhenNullOrEmpty_String_WithNull_Throws() 229 | { 230 | Assert.Throws(() => 231 | Throw.ArgumentNull.WhenNullOrEmpty((string)null, "TEST") 232 | ); 233 | } 234 | 235 | [Fact] 236 | public void WhenNullOrEmpty_String_WithMessage_WithNull_Throws() 237 | { 238 | Assert.Throws(() => 239 | Throw.ArgumentNull.WhenNullOrEmpty((string)null, "TEST", "message") 240 | ); 241 | } 242 | 243 | [Fact] 244 | public void WhenNullOrEmpty_String_WithNull_SetCorrectParameterNameOnException() 245 | { 246 | const string parameterName = "TEST"; 247 | 248 | Assert.Throws(parameterName, () => 249 | Throw.ArgumentNull.WhenNullOrEmpty((string)null, parameterName) 250 | ); 251 | } 252 | 253 | [Fact] 254 | public void WhenNullOrEmpty_String_WithMessage_WithNull_SetCorrectParameterNameOnException() 255 | { 256 | const string parameterName = "TEST"; 257 | 258 | Assert.Throws(parameterName, () => 259 | Throw.ArgumentNull.WhenNullOrEmpty((string)null, parameterName, "message") 260 | ); 261 | } 262 | 263 | [Fact] 264 | public void WhenNullOrEmpty_String_WithMessage_WithNull_SetCorrectMessageOnException() 265 | { 266 | const string parameterName = "TEST"; 267 | const string message = "TESTMESSAGE"; 268 | 269 | try 270 | { 271 | Throw.ArgumentNull.WhenNullOrEmpty((string)null, parameterName, message); 272 | } 273 | catch (ArgumentNullException e) 274 | { 275 | Assert.Contains(message, e.Message); 276 | } 277 | } 278 | 279 | #endregion 280 | 281 | #region Throw.ArgumentNullOrEmpty.WhenNullOrWhiteSpace(string) 282 | 283 | [Fact] 284 | public void WhenNullOrWhiteSpace_WithString_DoesNotThrow() 285 | { 286 | Throw.ArgumentNull.WhenNullOrWhiteSpace("DUMMY", "TEST"); 287 | } 288 | 289 | [Fact] 290 | public void WhenNullOrWhiteSpace_String_WithNull_Throws() 291 | { 292 | Assert.Throws(() => 293 | Throw.ArgumentNull.WhenNullOrWhiteSpace((string)null, "TEST") 294 | ); 295 | } 296 | 297 | [Fact] 298 | public void WhenNullOrWhiteSpace_String_WithEmptyString_Throws() 299 | { 300 | Assert.Throws(() => 301 | Throw.ArgumentNull.WhenNullOrWhiteSpace(string.Empty, "TEST") 302 | ); 303 | } 304 | 305 | [Fact] 306 | public void WhenNullOrWhiteSpace_String_WithMessage_WithNull_Throws() 307 | { 308 | Assert.Throws(() => 309 | Throw.ArgumentNull.WhenNullOrWhiteSpace((string)null, "TEST", "message") 310 | ); 311 | } 312 | 313 | [Fact] 314 | public void WhenNullOrWhiteSpace_String_WithMessage_WithEmptyString_Throws() 315 | { 316 | Assert.Throws(() => 317 | Throw.ArgumentNull.WhenNullOrWhiteSpace(string.Empty, "TEST", "message") 318 | ); 319 | } 320 | 321 | [Fact] 322 | public void WhenNullOrWhiteSpace_String_WithNull_SetCorrectParameterNameOnException() 323 | { 324 | const string parameterName = "TEST"; 325 | 326 | Assert.Throws(parameterName, () => 327 | Throw.ArgumentNull.WhenNullOrWhiteSpace((string)null, parameterName) 328 | ); 329 | } 330 | 331 | [Fact] 332 | public void WhenNullOrWhiteSpace_String_WithMessage_WithNull_SetCorrectParameterNameOnException() 333 | { 334 | const string parameterName = "TEST"; 335 | 336 | Assert.Throws(parameterName, () => 337 | Throw.ArgumentNull.WhenNullOrWhiteSpace((string)null, parameterName, "message") 338 | ); 339 | } 340 | 341 | [Fact] 342 | public void WhenNullOrWhiteSpace_String_WithMessage_WithNull_SetCorrectMessageOnException() 343 | { 344 | const string parameterName = "TEST"; 345 | const string message = "TESTMESSAGE"; 346 | 347 | try 348 | { 349 | Throw.ArgumentNull.WhenNullOrWhiteSpace((string)null, parameterName, message); 350 | } 351 | catch (ArgumentNullException e) 352 | { 353 | Assert.Contains(message, e.Message); 354 | } 355 | } 356 | 357 | #endregion 358 | 359 | #region Throw.ArgumentNullOrEmpty(IEnumerable, string) 360 | 361 | [Fact] 362 | public void WhenNullOrEmpty_WithNullIEnumerableT_Throws() 363 | { 364 | IEnumerable obj = null; 365 | Assert.Throws(() => 366 | Throw.ArgumentNull.WhenNullOrEmpty(obj, nameof(obj)) 367 | ); 368 | } 369 | 370 | [Fact] 371 | public void WhenNullOrEmpty_WithEmptyIEnumerableT_Throws() 372 | { 373 | IEnumerable obj = Enumerable.Empty(); 374 | Assert.Throws(() => 375 | Throw.ArgumentNull.WhenNullOrEmpty(obj, nameof(obj)) 376 | ); 377 | } 378 | 379 | [Fact] 380 | public void WhenNullOrEmpty_WithEmptyIEnumerableT_SetCorrectParameterNameOnException() 381 | { 382 | const string parameterName = "TEST"; 383 | 384 | IEnumerable obj = Enumerable.Empty(); 385 | 386 | Assert.Throws(parameterName, () => 387 | Throw.ArgumentNull.WhenNullOrEmpty(obj, parameterName) 388 | ); 389 | } 390 | 391 | [Fact] 392 | public void WhenNullOrEmpty_WithNotEmptyIEnumerableT_DoesNotThrow() 393 | { 394 | IEnumerable obj = new List() { new object() }; 395 | 396 | Throw.ArgumentNull.WhenNullOrEmpty(obj, nameof(obj)); 397 | } 398 | 399 | 400 | #endregion 401 | 402 | #region Throw.ArgumentNull.WhenNullOrEmpty(IEnumerable, string string) 403 | 404 | [Fact] 405 | public void WhenNullOrEmpty_WithMessage_WithNullIEnumerableT_Throws() 406 | { 407 | IEnumerable obj = null; 408 | Assert.Throws(() => 409 | Throw.ArgumentNull.WhenNullOrEmpty(obj, nameof(obj), "message") 410 | ); 411 | } 412 | 413 | [Fact] 414 | public void WhenNullOrEmpty_WithMessage_WithEmptyIEnumerableT_Throws() 415 | { 416 | IEnumerable obj = Enumerable.Empty(); 417 | Assert.Throws(() => 418 | Throw.ArgumentNull.WhenNullOrEmpty(obj, nameof(obj), "message") 419 | ); 420 | } 421 | 422 | [Fact] 423 | public void WhenNullOrEmpty_WithMessage_WithEmptyIEnumerableT_SetCorrectParameterNameOnException() 424 | { 425 | const string parameterName = "TEST"; 426 | 427 | IEnumerable obj = Enumerable.Empty(); 428 | 429 | Assert.Throws(parameterName, () => 430 | Throw.ArgumentNull.WhenNullOrEmpty(obj, parameterName, "message") 431 | ); 432 | } 433 | 434 | [Fact] 435 | public void WhenNullOrEmpty_WithMessage_WithEmptyIEnumerableT_SetCorrectMessageOnException() 436 | { 437 | const string parameterName = "TEST"; 438 | const string message = "TESTMESSAGE"; 439 | 440 | IEnumerable obj = Enumerable.Empty(); 441 | 442 | try 443 | { 444 | Throw.ArgumentNull.WhenNullOrEmpty(obj, parameterName, message); 445 | } 446 | catch (ArgumentNullException e) 447 | { 448 | Assert.Contains(message, e.Message); 449 | } 450 | } 451 | 452 | [Fact] 453 | public void WhenNullOrEmpty_WithMessage_WithNotEmptyIEnumerableT_DoesNotThrow() 454 | { 455 | IEnumerable obj = new List() { new object() }; 456 | 457 | Throw.ArgumentNull.WhenNullOrEmpty(obj, nameof(obj), "message"); 458 | } 459 | 460 | #endregion 461 | } 462 | } 463 | -------------------------------------------------------------------------------- /src/Pitcher.Tests/ArgumentOutOfRangeTests.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Xunit; 3 | 4 | namespace Pitcher.Tests 5 | { 6 | public class ArgumentOutOfRangeTests 7 | { 8 | [Fact] 9 | public void For_Throws() 10 | { 11 | Assert.Throws("TEST", () => Throw.ArgumentOutOfRange.For("TEST")); 12 | } 13 | 14 | [Fact] 15 | public void For_WithMessage_Throws() 16 | { 17 | Assert.Throws("TEST", () => Throw.ArgumentOutOfRange.For("TEST", "message")); 18 | } 19 | 20 | [Fact] 21 | public void When_ConditionTrue_Throws() 22 | { 23 | Assert.Throws("TEST", () => Throw.ArgumentOutOfRange.When(true, "TEST")); 24 | } 25 | 26 | [Fact] 27 | public void When_ConditionFalse_DoesNotThrow() 28 | { 29 | Throw.ArgumentNull.When(false, "TEST"); 30 | } 31 | 32 | [Fact] 33 | public void When_WithMessage_ConditionTrue_Throws() 34 | { 35 | Assert.Throws("TEST", () => Throw.ArgumentOutOfRange.When(true, "TEST", "message")); 36 | } 37 | 38 | [Fact] 39 | public void When_WithMessage_ConditionFalse_DoesNotThrow() 40 | { 41 | Throw.ArgumentNull.When(false, "TEST", "message"); 42 | } 43 | 44 | #region Throw.ArgumentOutOfRange.WhenNegativeNumber 45 | 46 | [Fact] 47 | public void WhenNegativeNumber_Int_ConditionTrue_Throws() 48 | { 49 | Assert.Throws("TEST", () => Throw.ArgumentOutOfRange.WhenNegativeNumber(-1, "TEST")); 50 | } 51 | 52 | [Fact] 53 | public void WhenNegativeNumber_Int_WithMessage_ConditionTrue_Throws() 54 | { 55 | Assert.Throws("TEST", () => Throw.ArgumentOutOfRange.WhenNegativeNumber(-1, "TEST", "message")); 56 | } 57 | 58 | [Fact] 59 | public void WhenZeroNumber_Int_WithZero_DoesNotThrow() 60 | { 61 | Throw.ArgumentOutOfRange.WhenNegativeNumber(0, "TEST"); 62 | } 63 | 64 | [Fact] 65 | public void WhenZeroNumbe_Int_WithZero_WithMesssage_WithZero_DoesNotThrow() 66 | { 67 | Throw.ArgumentOutOfRange.WhenNegativeNumber(0, "TEST", "message"); 68 | } 69 | 70 | [Fact] 71 | public void WhenZeroNumber_Int_WithPositive_DoesNotThrow() 72 | { 73 | Throw.ArgumentOutOfRange.WhenNegativeNumber(1, "TEST"); 74 | } 75 | 76 | [Fact] 77 | public void WhenZeroNumbe_Int_WithPositive__WithMesssage_WithZero_DoesNotThrow() 78 | { 79 | Throw.ArgumentOutOfRange.WhenNegativeNumber(1, "TEST", "message"); 80 | } 81 | 82 | [Fact] 83 | public void WhenNegativeNumber_Double_WithZero_DoesNotThrow() 84 | { 85 | Throw.ArgumentOutOfRange.WhenNegativeNumber(0D, "TEST"); 86 | } 87 | 88 | [Fact] 89 | public void WhenNegativeNumber_Int_WithDoubleZero_WithMesssage_WithZero_DoesNotThrow() 90 | { 91 | Throw.ArgumentOutOfRange.WhenNegativeNumber(0D, "TEST", "message"); 92 | } 93 | 94 | [Fact] 95 | public void WhenNegativeNumber_Int_ConditionFalse_DoesNotThrow() 96 | { 97 | Throw.ArgumentOutOfRange.WhenNegativeNumber(1, "TEST"); 98 | } 99 | 100 | [Fact] 101 | public void WhenNegativeNumber_Int_WithMessage_ConditionFalse_DoesNotThrow() 102 | { 103 | Throw.ArgumentOutOfRange.WhenNegativeNumber(1, "TEST", "message"); 104 | } 105 | 106 | [Fact] 107 | public void WhenNegativeNumber_Double_ConditionTrue_Throws() 108 | { 109 | Assert.Throws("TEST", () => Throw.ArgumentOutOfRange.WhenNegativeNumber(-1D, "TEST")); 110 | } 111 | 112 | [Fact] 113 | public void WhenNegativeNumber_Double_WithMessage_ConditionTrue_Throws() 114 | { 115 | Assert.Throws("TEST", () => Throw.ArgumentOutOfRange.WhenNegativeNumber(-1D, "TEST", "message")); 116 | } 117 | 118 | [Fact] 119 | public void WhenNegativeNumber_Double_ConditionFalse_DoesNotThrow() 120 | { 121 | Throw.ArgumentOutOfRange.WhenNegativeNumber(1D, "TEST"); 122 | } 123 | 124 | [Fact] 125 | public void WhenNegativeNumber_Double_WithMessage_ConditionFalse_DoesNotThrow() 126 | { 127 | Throw.ArgumentOutOfRange.WhenNegativeNumber(1D, "TEST", "message"); 128 | } 129 | 130 | [Fact] 131 | public void WhenNegativeNumber_Float_ConditionTrue_Throws() 132 | { 133 | Assert.Throws("TEST", () => Throw.ArgumentOutOfRange.WhenNegativeNumber(-1f, "TEST")); 134 | } 135 | 136 | [Fact] 137 | public void WhenNegativeNumber_Float_WithMessage_ConditionTrue_Throws() 138 | { 139 | Assert.Throws("TEST", () => Throw.ArgumentOutOfRange.WhenNegativeNumber(-1f, "TEST", "message")); 140 | } 141 | 142 | [Fact] 143 | public void WhenNegativeNumber_Float_ConditionFalse_DoesNotThrow() 144 | { 145 | Throw.ArgumentOutOfRange.WhenNegativeNumber(1f, "TEST"); 146 | } 147 | 148 | [Fact] 149 | public void WhenNegativeNumber_Float_WithMessage_ConditionFalse_DoesNotThrow() 150 | { 151 | Throw.ArgumentOutOfRange.WhenNegativeNumber(1f, "TEST", "message"); 152 | } 153 | 154 | [Fact] 155 | public void WhenNegativeNumber_Decimal_ConditionTrue_Throws() 156 | { 157 | Assert.Throws("TEST", () => Throw.ArgumentOutOfRange.WhenNegativeNumber(-1m, "TEST")); 158 | } 159 | 160 | [Fact] 161 | public void WhenNegativeNumber_Decima_WithMessage_ConditionTrue_Throws() 162 | { 163 | Assert.Throws("TEST", () => Throw.ArgumentOutOfRange.WhenNegativeNumber(-1m, "TEST", "message")); 164 | } 165 | 166 | [Fact] 167 | public void WhenNegativeNumber_Decimal_ConditionFalse_DoesNotThrow() 168 | { 169 | Throw.ArgumentOutOfRange.WhenNegativeNumber(1m, "TEST"); 170 | } 171 | 172 | [Fact] 173 | public void WhenNegativeNumber_Decimal_WithMessage_ConditionFalse_DoesNotThrow() 174 | { 175 | Throw.ArgumentOutOfRange.WhenNegativeNumber(1m, "TEST", "message"); 176 | } 177 | 178 | #endregion 179 | 180 | #region Throw.ArgumentOutOfRange.WhenPostiveNumber 181 | 182 | [Theory] 183 | [InlineData(0)] 184 | [InlineData(1)] 185 | public void WhenPositiveNumbere_Int_ConditionTrue_Throws(int testValue) 186 | { 187 | Assert.Throws("TEST", () => Throw.ArgumentOutOfRange.WhenPositiveNumber(testValue, "TEST")); 188 | } 189 | 190 | [Theory] 191 | [InlineData(0)] 192 | [InlineData(1)] 193 | public void WhenPositiveNumber_Int_WithMessage_ConditionTrue_Throws(int testValue) 194 | { 195 | Assert.Throws("TEST", () => Throw.ArgumentOutOfRange.WhenPositiveNumber(testValue, "TEST", "message")); 196 | } 197 | 198 | [Fact] 199 | public void WhenPositiveNumber_Int_ConditionFalse_DoesNotThrow() 200 | { 201 | Throw.ArgumentOutOfRange.WhenPositiveNumber(-1, "TEST"); 202 | } 203 | 204 | [Fact] 205 | public void WhenPositiveNumber_Int_WithMessage_ConditionFalse_DoesNotThrow() 206 | { 207 | Throw.ArgumentOutOfRange.WhenPositiveNumber(-1, "TEST", "message"); 208 | } 209 | 210 | [Theory] 211 | [InlineData(0D)] 212 | [InlineData(1D)] 213 | public void WhenPositiveNumber_Double_ConditionTrue_Throws(double testValue) 214 | { 215 | Assert.Throws("TEST", () => Throw.ArgumentOutOfRange.WhenPositiveNumber(testValue, "TEST")); 216 | } 217 | 218 | [Theory] 219 | [InlineData(0D)] 220 | [InlineData(1D)] 221 | public void WhenPositiveNumber_Double_WithMessage_ConditionTrue_Throws(double testValue) 222 | { 223 | Assert.Throws("TEST", () => Throw.ArgumentOutOfRange.WhenPositiveNumber(testValue, "TEST", "message")); 224 | } 225 | 226 | [Fact] 227 | public void WhenPositiveNumber_Double_ConditionFalse_DoesNotThrow() 228 | { 229 | Throw.ArgumentOutOfRange.WhenPositiveNumber(-1D, "TEST"); 230 | } 231 | 232 | [Fact] 233 | public void WhenPositiveNumber_Double_WithMessage_ConditionFalse_DoesNotThrow() 234 | { 235 | Throw.ArgumentOutOfRange.WhenPositiveNumber(-1D, "TEST", "message"); 236 | } 237 | 238 | 239 | [Theory] 240 | [InlineData(0f)] 241 | [InlineData(1f)] 242 | public void WhenPositiveNumber_Float_ConditionTrue_Throws(float testValue) 243 | { 244 | Assert.Throws("TEST", () => Throw.ArgumentOutOfRange.WhenPositiveNumber(testValue, "TEST")); 245 | } 246 | 247 | [Theory] 248 | [InlineData(0f)] 249 | [InlineData(1f)] 250 | public void WhenPositiveNumber_Float_WithMessage_ConditionTrue_Throws(float testValue) 251 | { 252 | Assert.Throws("TEST", () => Throw.ArgumentOutOfRange.WhenPositiveNumber(testValue, "TEST", "message")); 253 | } 254 | 255 | [Fact] 256 | public void WhenPositiveNumber_Float_ConditionFalse_DoesNotThrow() 257 | { 258 | Throw.ArgumentOutOfRange.WhenPositiveNumber(-1f, "TEST"); 259 | } 260 | 261 | [Fact] 262 | public void WhenPositiveNumber_Float_WithMessage_ConditionFalse_DoesNotThrow() 263 | { 264 | Throw.ArgumentOutOfRange.WhenPositiveNumber(-1f, "TEST", "message"); 265 | } 266 | 267 | [Theory] 268 | [InlineData(0)] 269 | [InlineData(1)] 270 | public void WhenPositiveNumber_Decimal_ConditionTrue_Throws(decimal testValue) 271 | { 272 | Assert.Throws("TEST", () => Throw.ArgumentOutOfRange.WhenPositiveNumber(testValue, "TEST")); 273 | } 274 | 275 | [Theory] 276 | [InlineData(0)] 277 | [InlineData(1)] 278 | public void WhenPositiveNumber_Decimal_WithMessage_ConditionTrue_Throws(decimal testValue) 279 | { 280 | Assert.Throws("TEST", () => Throw.ArgumentOutOfRange.WhenPositiveNumber(testValue, "TEST", "message")); 281 | } 282 | 283 | [Fact] 284 | public void WhenPositiveNumber_Decimal_ConditionFalse_DoesNotThrow() 285 | { 286 | Throw.ArgumentOutOfRange.WhenPositiveNumber(-1m, "TEST"); 287 | } 288 | 289 | [Fact] 290 | public void WhenPositiveNumber_Decimal_WithMessage_ConditionFalse_DoesNotThrow() 291 | { 292 | Throw.ArgumentOutOfRange.WhenPositiveNumber(-1m, "TEST", "message"); 293 | } 294 | 295 | #endregion 296 | 297 | #region #region Throw.ArgumentOutOfRange.WhenMoreThan 298 | 299 | [Fact] 300 | public void WhenMoreThan_ConditionTrue_Throws() 301 | { 302 | Assert.Throws("TEST", () => Throw.ArgumentOutOfRange.WhenMoreThan(1, 0, "TEST")); 303 | } 304 | 305 | [Fact] 306 | public void WhenMoreThan_ConditionFalse_DoesNotThrow() 307 | { 308 | Throw.ArgumentOutOfRange.WhenMoreThan(0, 1, "TEST"); 309 | } 310 | 311 | [Fact] 312 | public void WhenMoreThan_WithMessage_ConditionTrue_Throws() 313 | { 314 | Assert.Throws("TEST", () => Throw.ArgumentOutOfRange.WhenMoreThan(1, 0, "TEST", "message")); 315 | } 316 | 317 | [Fact] 318 | public void WhenMoreThan_WithMessage_ConditionFalse_DoesNotThrow() 319 | { 320 | Throw.ArgumentOutOfRange.WhenMoreThan(0, 1, "TEST", "message"); 321 | } 322 | 323 | #endregion 324 | 325 | #region #region Throw.ArgumentOutOfRange.WhenLessThan 326 | 327 | [Fact] 328 | public void WhenLessThan_ConditionTrue_Throws() 329 | { 330 | Assert.Throws("TEST", () => Throw.ArgumentOutOfRange.WhenLessThan(0, 1, "TEST")); 331 | } 332 | 333 | [Fact] 334 | public void WhenLessThan_ConditionFalse_DoesNotThrow() 335 | { 336 | Throw.ArgumentOutOfRange.WhenLessThan(1, 0, "TEST"); 337 | } 338 | 339 | [Fact] 340 | public void WhenLessThan_WithMessage_ConditionTrue_Throws() 341 | { 342 | Assert.Throws("TEST", () => Throw.ArgumentOutOfRange.WhenLessThan(0, 1, "TEST", "message")); 343 | } 344 | 345 | [Fact] 346 | public void WhenLessThan_WithMessage_ConditionFalse_DoesNotThrow() 347 | { 348 | Throw.ArgumentOutOfRange.WhenLessThan(1, 0, "TEST", "message"); 349 | } 350 | 351 | #endregion 352 | } 353 | } 354 | -------------------------------------------------------------------------------- /src/Pitcher.Tests/Fakes/ObjWithIncorrectOverridenEquals.cs: -------------------------------------------------------------------------------- 1 | namespace Pitcher.Tests.Fakes 2 | { 3 | internal sealed class ObjWithIncorrectOverridenEquals 4 | { 5 | public override bool Equals(object obj) 6 | { 7 | var result = false; 8 | 9 | if (obj == null) 10 | { 11 | result = true; // INCORRECT 12 | } 13 | else 14 | { 15 | result = base.Equals(obj); 16 | } 17 | 18 | return result; 19 | } 20 | 21 | public override int GetHashCode() 22 | { 23 | return base.GetHashCode(); 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/Pitcher.Tests/Pitcher.Tests.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | net481;net6.0;netcoreapp3.1 5 | net6.0 6 | false 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | all 16 | runtime; build; native; contentfiles; analyzers 17 | 18 | 19 | 20 | all 21 | runtime; build; native; contentfiles; analyzers; buildtransitive 22 | 23 | 24 | 25 | all 26 | runtime; build; native; contentfiles; analyzers 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /src/Pitcher.Tests/ThrowTTests.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Xunit; 3 | 4 | namespace Pitcher.Tests 5 | { 6 | public class ThrowTTests 7 | { 8 | [Fact] 9 | public void Now_Throws() 10 | { 11 | Assert.ThrowsAny(() => Throw.Now()); 12 | } 13 | 14 | [Fact] 15 | public void When_ConditionTrue_Throws() 16 | { 17 | Assert.ThrowsAny(() => Throw.When(true)); 18 | } 19 | 20 | [Fact] 21 | public void When_ConditionFalse_DoesNotThrow() 22 | { 23 | Throw.When(false); 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/Pitcher.Tests/ThrowTests.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Xunit; 3 | 4 | namespace Pitcher.Tests 5 | { 6 | public class ThrowTests 7 | { 8 | [Fact] 9 | public void This_WithException_Throws() 10 | { 11 | Assert.ThrowsAny(() => Throw.This(new Exception())); 12 | } 13 | 14 | [Fact] 15 | public void When_ConditionTrue_Throws() 16 | { 17 | Assert.ThrowsAny(() => Throw.When(true, new Exception())); 18 | } 19 | 20 | [Fact] 21 | public void When_ConditionFalse_DoesNotThrow() 22 | { 23 | Throw.When(false, new Exception()); 24 | } 25 | 26 | [Fact] 27 | public void WhenFactory_ConditionTrue_Throws() 28 | { 29 | Assert.ThrowsAny(() => Throw.When(true, () => new Exception())); 30 | } 31 | 32 | [Fact] 33 | public void WhenFactory_ConditionFalse_DoesNotThrow() 34 | { 35 | Throw.When(false, () => new Exception()); 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/Pitcher.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 16 4 | VisualStudioVersion = 16.0.28803.352 5 | MinimumVisualStudioVersion = 15.0.26124.0 6 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Pitcher", "Pitcher\Pitcher.csproj", "{129F683E-8470-4E0D-9971-726F78B3A607}" 7 | EndProject 8 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Pitcher.Tests", "Pitcher.Tests\Pitcher.Tests.csproj", "{E11962C4-62A3-4187-B868-D04F0F14A7AB}" 9 | EndProject 10 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Pitcher.Benchmarks", "Pitcher.Benchmarks\Pitcher.Benchmarks.csproj", "{74E5499E-492D-4F21-A3D3-8341527DEA8B}" 11 | EndProject 12 | Global 13 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 14 | Debug|Any CPU = Debug|Any CPU 15 | Debug|x64 = Debug|x64 16 | Debug|x86 = Debug|x86 17 | Release|Any CPU = Release|Any CPU 18 | Release|x64 = Release|x64 19 | Release|x86 = Release|x86 20 | EndGlobalSection 21 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 22 | {129F683E-8470-4E0D-9971-726F78B3A607}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 23 | {129F683E-8470-4E0D-9971-726F78B3A607}.Debug|Any CPU.Build.0 = Debug|Any CPU 24 | {129F683E-8470-4E0D-9971-726F78B3A607}.Debug|x64.ActiveCfg = Debug|Any CPU 25 | {129F683E-8470-4E0D-9971-726F78B3A607}.Debug|x64.Build.0 = Debug|Any CPU 26 | {129F683E-8470-4E0D-9971-726F78B3A607}.Debug|x86.ActiveCfg = Debug|Any CPU 27 | {129F683E-8470-4E0D-9971-726F78B3A607}.Debug|x86.Build.0 = Debug|Any CPU 28 | {129F683E-8470-4E0D-9971-726F78B3A607}.Release|Any CPU.ActiveCfg = Release|Any CPU 29 | {129F683E-8470-4E0D-9971-726F78B3A607}.Release|Any CPU.Build.0 = Release|Any CPU 30 | {129F683E-8470-4E0D-9971-726F78B3A607}.Release|x64.ActiveCfg = Release|Any CPU 31 | {129F683E-8470-4E0D-9971-726F78B3A607}.Release|x64.Build.0 = Release|Any CPU 32 | {129F683E-8470-4E0D-9971-726F78B3A607}.Release|x86.ActiveCfg = Release|Any CPU 33 | {129F683E-8470-4E0D-9971-726F78B3A607}.Release|x86.Build.0 = Release|Any CPU 34 | {E11962C4-62A3-4187-B868-D04F0F14A7AB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 35 | {E11962C4-62A3-4187-B868-D04F0F14A7AB}.Debug|Any CPU.Build.0 = Debug|Any CPU 36 | {E11962C4-62A3-4187-B868-D04F0F14A7AB}.Debug|x64.ActiveCfg = Debug|Any CPU 37 | {E11962C4-62A3-4187-B868-D04F0F14A7AB}.Debug|x64.Build.0 = Debug|Any CPU 38 | {E11962C4-62A3-4187-B868-D04F0F14A7AB}.Debug|x86.ActiveCfg = Debug|Any CPU 39 | {E11962C4-62A3-4187-B868-D04F0F14A7AB}.Debug|x86.Build.0 = Debug|Any CPU 40 | {E11962C4-62A3-4187-B868-D04F0F14A7AB}.Release|Any CPU.ActiveCfg = Release|Any CPU 41 | {E11962C4-62A3-4187-B868-D04F0F14A7AB}.Release|Any CPU.Build.0 = Release|Any CPU 42 | {E11962C4-62A3-4187-B868-D04F0F14A7AB}.Release|x64.ActiveCfg = Release|Any CPU 43 | {E11962C4-62A3-4187-B868-D04F0F14A7AB}.Release|x64.Build.0 = Release|Any CPU 44 | {E11962C4-62A3-4187-B868-D04F0F14A7AB}.Release|x86.ActiveCfg = Release|Any CPU 45 | {E11962C4-62A3-4187-B868-D04F0F14A7AB}.Release|x86.Build.0 = Release|Any CPU 46 | {74E5499E-492D-4F21-A3D3-8341527DEA8B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 47 | {74E5499E-492D-4F21-A3D3-8341527DEA8B}.Debug|Any CPU.Build.0 = Debug|Any CPU 48 | {74E5499E-492D-4F21-A3D3-8341527DEA8B}.Debug|x64.ActiveCfg = Debug|Any CPU 49 | {74E5499E-492D-4F21-A3D3-8341527DEA8B}.Debug|x64.Build.0 = Debug|Any CPU 50 | {74E5499E-492D-4F21-A3D3-8341527DEA8B}.Debug|x86.ActiveCfg = Debug|Any CPU 51 | {74E5499E-492D-4F21-A3D3-8341527DEA8B}.Debug|x86.Build.0 = Debug|Any CPU 52 | {74E5499E-492D-4F21-A3D3-8341527DEA8B}.Release|Any CPU.ActiveCfg = Release|Any CPU 53 | {74E5499E-492D-4F21-A3D3-8341527DEA8B}.Release|Any CPU.Build.0 = Release|Any CPU 54 | {74E5499E-492D-4F21-A3D3-8341527DEA8B}.Release|x64.ActiveCfg = Release|Any CPU 55 | {74E5499E-492D-4F21-A3D3-8341527DEA8B}.Release|x64.Build.0 = Release|Any CPU 56 | {74E5499E-492D-4F21-A3D3-8341527DEA8B}.Release|x86.ActiveCfg = Release|Any CPU 57 | {74E5499E-492D-4F21-A3D3-8341527DEA8B}.Release|x86.Build.0 = Release|Any CPU 58 | EndGlobalSection 59 | GlobalSection(SolutionProperties) = preSolution 60 | HideSolutionNode = FALSE 61 | EndGlobalSection 62 | GlobalSection(ExtensibilityGlobals) = postSolution 63 | SolutionGuid = {EF9AFFC7-2413-43BA-A3A7-CD6F08484668} 64 | EndGlobalSection 65 | EndGlobal 66 | -------------------------------------------------------------------------------- /src/Pitcher/GlobalSuppressions.cs: -------------------------------------------------------------------------------- 1 | // This file is used by Code Analysis to maintain SuppressMessage 2 | // attributes that are applied to this project. 3 | // Project-level suppressions either have no target or are given 4 | // a specific target and scoped to a namespace, type, member, etc. 5 | 6 | using System.Diagnostics.CodeAnalysis; 7 | 8 | [assembly: SuppressMessage("Naming", "CA1716:Identifiers should not match keywords", Justification = "", Scope = "type", Target = "~T:Pitcher.Throw")] 9 | -------------------------------------------------------------------------------- /src/Pitcher/Pitcher.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | netstandard2.0 5 | True 6 | 10.0 7 | true 8 | true 9 | true 10 | enable 11 | 12 | 2.1.1 13 | 14 | true 15 | Pitcher is a helper library to simplify throwing exceptions and make methods easier to inline by reducing code size. 16 | © Alex Kamsteeg 17 | Alex Kamsteeg (https://atlex.nl/) 18 | 19 | https://github.com/akamsteeg/Pitcher 20 | exception throw helper 21 | MIT 22 | 23 | 2.1.1: 24 | - [Feature] Enabled deterministic builds (#15) 25 | 2.1.0: 26 | - [Feature] Use "is null" instead of "== null" for null checks (#10) 27 | - [Feature] Add Throw.ArgumentOutOfRange.WhenNegativeNumber() to throw an ArgumentOutOfRangeException when a number is less than zero (#11) 28 | - [Feature] Add Throw.ArgumentOutOfRange.WhenPositiveNumber() to throw an ArgumentOutOfRangeException when a number is zero or more (#12) 29 | - [Feature]] Add Throw.ArgumentOutOfRange.WhenLessThan<T>() where T is IComparable<T> to throw an ArgumentOutOfRangeException when the argument is less than the specified value (#13) 30 | - [Feature]] Add Throw.ArgumentOutOfRange.WhenMoreThan<T>() where T is IComparable<T> to throw an ArgumentOutOfRangeException when the argument is more than the specified value (#14) 31 | 2.0.0: 32 | - [Breaking] Strong-named the assembly (#9) 33 | - [Breaking] Moved to a single NETSTANDARD2.0 TFM (#5) 34 | - [Deprecated] Throw.ArgumentNull.WhenNull(string, string) and Throw.ArgumentNull.WhenNull(string, string, string) are marked as obsolete (#7) 35 | - [Feature] Added Throw.ArgumentNull.WhenNullOrEmpty(string, string) and Throw.ArgumentNull.WhenNullOrEmpty(string, string, string) to throw an ArgumentNullException on null or empty strings (#7) 36 | - [Feature] Added Throw.ArgumentNull.WhenNullOrEmpty<T>(IEnumerable<T>, string) and Throw.ArgumentNull.WhenNullOrEmpty<T>(IEnumerable<T>, string, string) to throw an ArgumentNullException on null or empty enumerables (#6) 37 | 1.4.0: 38 | - [Feature] Moved from SourceLink with embedded PDB to a .snupkg on NuGet.org for debugging (#3) 39 | - [Feature] Added overloads for Throw.ArgumentNull.WhenNull() for strings (#4) 40 | 1.3.0: 41 | - [Feature] Added Throw.When(bool, Func<Exception>) 42 | 1.2.0: 43 | - [Feature] Enabled SourceLink support (#1) 44 | 1.1.0: 45 | - [Fix] Removed expensive null check on arguments in Throw.This() and Throw.When() 46 | 47 | 48 | 49 | true 50 | https://github.com/akamsteeg/Pitcher.git 51 | true 52 | $(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb 53 | 54 | 55 | true 56 | false 57 | Pitcher.snk 58 | 59 | 60 | 61 | 62 | all 63 | runtime; build; native; contentfiles; analyzers 64 | 65 | 66 | 67 | 68 | true 69 | true 70 | 71 | 72 | -------------------------------------------------------------------------------- /src/Pitcher/Pitcher.snk: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/akamsteeg/Pitcher/1e1462468558f6cd321c7e9febcb6486d288556b/src/Pitcher/Pitcher.snk -------------------------------------------------------------------------------- /src/Pitcher/Throw.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | 5 | namespace Pitcher 6 | { 7 | /// 8 | /// Represents a throw helper for exceptions 9 | /// 10 | public static class Throw 11 | { 12 | /// 13 | /// Throw the specified 14 | /// 15 | /// 16 | /// The to throw 17 | /// 18 | public static void This(Exception exceptionToThrow) 19 | { 20 | throw exceptionToThrow; 21 | } 22 | 23 | /// 24 | /// Throw the specified when the specified condition 25 | /// is true 26 | /// 27 | /// 28 | /// The condition to determine whether to throw the 29 | /// or not 30 | /// 31 | /// 32 | /// The to throw 33 | /// 34 | public static void When(bool condition, Exception exceptionToThrow) 35 | { 36 | if (condition) 37 | { 38 | throw exceptionToThrow; 39 | } 40 | } 41 | 42 | /// 43 | /// Throw the specified when the specified condition 44 | /// is true 45 | /// 46 | /// 47 | /// The condition to determine whether to throw the 48 | /// or not 49 | /// 50 | /// 51 | /// The that creates the 52 | /// to throw 53 | /// 54 | public static void When(bool condition, Func exceptionFactory) 55 | { 56 | if (condition) 57 | { 58 | throw exceptionFactory.Invoke(); 59 | } 60 | } 61 | 62 | /// 63 | /// Represents a throw helper for 64 | /// 65 | public static class ArgumentNull 66 | { 67 | /// 68 | /// Throw an for the specified 69 | /// parameter name 70 | /// 71 | /// 72 | /// The name of the parameter to throw the for 74 | /// 75 | public static void For(string parameterName) => 76 | Throw.This(new ArgumentNullException(parameterName)); 77 | 78 | /// 79 | /// Throw an for the specified 80 | /// parameter name 81 | /// 82 | /// 83 | /// The name of the parameter to throw the for 85 | /// 86 | /// 87 | /// The message to include in the 88 | /// 89 | public static void For(string parameterName, string message) => 90 | Throw.This(new ArgumentNullException(parameterName, message)); 91 | 92 | /// 93 | /// Throw an for the specified 94 | /// parameter name when the specified condition is true 95 | /// 96 | /// 97 | /// The condition to determine whether to throw the or not 98 | /// 99 | /// 100 | /// The name of the parameter to throw the for 102 | /// 103 | public static void When(bool condition, string parameterName) 104 | { 105 | if (condition) 106 | { 107 | Throw.This(new ArgumentNullException(parameterName)); 108 | } 109 | } 110 | 111 | /// 112 | /// Throw an for the specified 113 | /// parameter name when the specified condition is true 114 | /// 115 | /// 116 | /// The condition to determine whether to throw the or not 118 | /// 119 | /// 120 | /// The name of the parameter to throw the for 122 | /// 123 | /// 124 | /// The message to include in the 125 | /// 126 | public static void When(bool condition, string parameterName, string message) 127 | { 128 | if (condition) 129 | { 130 | Throw.This(new ArgumentNullException(parameterName, message)); 131 | } 132 | } 133 | 134 | /// 135 | /// Throw an for the specified 136 | /// parameter when the specified is null 137 | /// 138 | /// 139 | /// The to check for null 140 | /// 141 | /// 142 | /// The name of the parameter to throw the for 144 | /// 145 | public static void WhenNull(object obj, string parameterName) => 146 | Throw.ArgumentNull.When(obj is null, parameterName); 147 | 148 | /// 149 | /// Throw an for the specified 150 | /// parameter when the specified is null 151 | /// 152 | /// 153 | /// The to check for null 154 | /// 155 | /// 156 | /// The name of the parameter to throw the for 158 | /// 159 | /// 160 | /// The message to include in the 161 | /// 162 | public static void WhenNull(object obj, string parameterName, string message) => 163 | Throw.ArgumentNull.When(obj is null, parameterName, message); 164 | 165 | /// 166 | /// Throw an for the specified 167 | /// parameter when the specified is null 168 | /// 169 | /// 170 | /// The to check for null 171 | /// 172 | /// 173 | /// The name of the parameter to throw the for 175 | /// 176 | [Obsolete("Use WhenNullOrEmpty(string, string)")] 177 | public static void WhenNull(string obj, string parameterName) => 178 | Throw.ArgumentNull.When(string.IsNullOrEmpty(obj), parameterName); 179 | 180 | /// 181 | /// Throw an for the specified 182 | /// parameter when the specified is null 183 | /// 184 | /// 185 | /// The to check for null 186 | /// 187 | /// 188 | /// The name of the parameter to throw the for 190 | /// 191 | /// 192 | /// The message to include in the 193 | /// 194 | [Obsolete("Use WhenNullOrEmpty(string, string, string)")] 195 | public static void WhenNull(string obj, string parameterName, string message) => 196 | Throw.ArgumentNull.When(string.IsNullOrEmpty(obj), parameterName, message); 197 | 198 | /// 199 | /// Throw an for the specified 200 | /// parameter when the specified is null or empty 201 | /// 202 | /// 203 | /// The to check for null or empty string 204 | /// 205 | /// 206 | /// The name of the parameter to throw the for 208 | /// 209 | public static void WhenNullOrEmpty(string obj, string parameterName) => 210 | Throw.ArgumentNull.When(string.IsNullOrEmpty(obj), parameterName); 211 | 212 | /// 213 | /// Throw an for the specified 214 | /// parameter when the specified is null or empty 215 | /// 216 | /// 217 | /// The to check for null or empty string 218 | /// 219 | /// 220 | /// The name of the parameter to throw the for 222 | /// 223 | /// 224 | /// The message to include in the 225 | /// 226 | public static void WhenNullOrEmpty(string obj, string parameterName, string message) => 227 | Throw.ArgumentNull.When(string.IsNullOrEmpty(obj), parameterName, message); 228 | 229 | /// 230 | /// Throw an for the specified 231 | /// parameter when the specified is null, empty 232 | /// or whitespace 233 | /// 234 | /// 235 | /// The to check for null, empty or whitespace 236 | /// 237 | /// 238 | /// The name of the parameter to throw the for 240 | /// 241 | public static void WhenNullOrWhiteSpace(string obj, string parameterName) => 242 | Throw.ArgumentNull.When(string.IsNullOrWhiteSpace(obj), parameterName); 243 | 244 | /// 245 | /// Throw an for the specified 246 | /// parameter when the specified is null, empty 247 | /// or whitespace 248 | /// 249 | /// 250 | /// The to check for null, empty or whitespace 251 | /// 252 | /// 253 | /// The name of the parameter to throw the for 255 | /// 256 | /// 257 | /// The message to include in the 258 | /// 259 | public static void WhenNullOrWhiteSpace(string obj, string parameterName, string message) => 260 | Throw.ArgumentNull.When(string.IsNullOrWhiteSpace(obj), parameterName, message); 261 | 262 | /// 263 | /// Throw an for the specified 264 | /// parameter when the specified is null 265 | /// or empty 266 | /// 267 | /// 268 | /// The to check for null or emptiness 269 | /// 270 | /// 271 | /// The name of the parameter to throw the for 273 | /// 274 | public static void WhenNullOrEmpty(IEnumerable obj, string parameterName) => 275 | Throw.ArgumentNull.When(obj is null || !obj.Any(), parameterName); 276 | 277 | /// 278 | /// Throw an for the specified 279 | /// parameter when the specified is null 280 | /// or empty 281 | /// 282 | /// 283 | /// The to check for null or emptiness 284 | /// 285 | /// 286 | /// The name of the parameter to throw the for 288 | /// 289 | /// 290 | /// The message to include in the 291 | /// 292 | public static void WhenNullOrEmpty(IEnumerable obj, string parameterName, string message) => 293 | Throw.ArgumentNull.When(obj is null || !obj.Any(), parameterName, message); 294 | } 295 | 296 | /// 297 | /// Represents a throw helper for see 298 | /// 299 | public static class ArgumentOutOfRange 300 | { 301 | /// 302 | /// Throw an for the specified 303 | /// parameter name 304 | /// 305 | /// 306 | /// The name of the parameter to throw the for 308 | /// 309 | public static void For(string parameterName) => 310 | Throw.This(new ArgumentOutOfRangeException(parameterName)); 311 | 312 | /// 313 | /// Throw an for the specified 314 | /// parameter name 315 | /// 316 | /// 317 | /// The name of the parameter to throw the for 319 | /// 320 | /// 321 | /// The message to include in the 322 | /// 323 | public static void For(string parameterName, string message) => 324 | Throw.This(new ArgumentOutOfRangeException(parameterName, message)); 325 | 326 | /// 327 | /// Throw an for the specified 328 | /// parameter name when the specified condition is true 329 | /// 330 | /// 331 | /// The condition to determine whether to throw the or not 332 | /// 333 | /// 334 | /// The name of the parameter to throw the for 336 | /// 337 | public static void When(bool condition, string parameterName) 338 | { 339 | if (condition) 340 | { 341 | Throw.This(new ArgumentOutOfRangeException(parameterName)); 342 | } 343 | } 344 | 345 | /// 346 | /// Throw an for the specified 347 | /// parameter name when the specified condition is true 348 | /// 349 | /// 350 | /// The condition to determine whether to throw the or not 352 | /// 353 | /// 354 | /// The name of the parameter to throw the for 356 | /// 357 | /// 358 | /// The message to include in the 359 | /// 360 | public static void When(bool condition, string parameterName, string message) 361 | { 362 | if (condition) 363 | { 364 | Throw.This(new ArgumentOutOfRangeException(parameterName, message)); 365 | } 366 | } 367 | 368 | /// 369 | /// Throw an when the specified value of 370 | /// the specified parameter name is a negative number 371 | /// 372 | /// 373 | /// The value of the parameter 374 | /// 375 | /// 376 | /// The name of the parameter to throw the for 378 | /// 379 | public static void WhenNegativeNumber(int value, string argumentName) 380 | => When(value < 0, argumentName); 381 | 382 | /// 383 | /// Throw an when the specified value of 384 | /// the specified parameter name is a negative number 385 | /// 386 | /// 387 | /// The value of the parameter 388 | /// 389 | /// 390 | /// The name of the parameter to throw the for 392 | /// 393 | /// 394 | /// The message to include in the 395 | /// 396 | public static void WhenNegativeNumber(int value, string argumentName, string message) 397 | => When(value < 0, argumentName, message); 398 | 399 | /// 400 | /// Throw an when the specified value of 401 | /// the specified parameter name is a negative number 402 | /// 403 | /// 404 | /// The value of the parameter 405 | /// 406 | /// 407 | /// The name of the parameter to throw the for 409 | /// 410 | public static void WhenNegativeNumber(double value, string argumentName) 411 | => When(value < 0.0D, argumentName); 412 | 413 | /// 414 | /// Throw an when the specified value of 415 | /// the specified parameter name is a negative number 416 | /// 417 | /// 418 | /// The value of the parameter 419 | /// 420 | /// 421 | /// The name of the parameter to throw the for 423 | /// 424 | /// 425 | /// The message to include in the 426 | /// 427 | public static void WhenNegativeNumber(double value, string argumentName, string message) 428 | => When(value < 0.0D, argumentName, message); 429 | 430 | /// 431 | /// Throw an when the specified value of 432 | /// the specified parameter name is a negative number 433 | /// 434 | /// 435 | /// The value of the parameter 436 | /// 437 | /// 438 | /// The name of the parameter to throw the for 440 | /// 441 | public static void WhenNegativeNumber(float value, string argumentName) 442 | => When(value < 0.0f, argumentName); 443 | 444 | /// 445 | /// Throw an when the specified value of 446 | /// the specified parameter name is a negative number 447 | /// 448 | /// 449 | /// The value of the parameter 450 | /// 451 | /// 452 | /// The name of the parameter to throw the for 454 | /// 455 | /// 456 | /// The message to include in the 457 | /// 458 | public static void WhenNegativeNumber(float value, string argumentName, string message) 459 | => When(value < 0.0f, argumentName, message); 460 | 461 | /// 462 | /// Throw an when the specified value of 463 | /// the specified parameter name is a negative number 464 | /// 465 | /// 466 | /// The value of the parameter 467 | /// 468 | /// 469 | /// The name of the parameter to throw the for 471 | /// 472 | public static void WhenNegativeNumber(decimal value, string argumentName) 473 | => When(value < 0.0m, argumentName); 474 | 475 | /// 476 | /// Throw an when the specified value of 477 | /// the specified parameter name is a negative number 478 | /// 479 | /// 480 | /// The value of the parameter 481 | /// 482 | /// 483 | /// The name of the parameter to throw the for 485 | /// 486 | /// 487 | /// The message to include in the 488 | /// 489 | public static void WhenNegativeNumber(decimal value, string argumentName, string message) 490 | => When(value < 0.0m, argumentName, message); 491 | 492 | /// 493 | /// Throw an when the specified value of 494 | /// the specified parameter name is a positive number 495 | /// 496 | /// 497 | /// The value of the parameter 498 | /// 499 | /// 500 | /// The name of the parameter to throw the for 502 | /// ' 503 | public static void WhenPositiveNumber(int value, string argumentName) 504 | => When(value >= 0, argumentName); 505 | 506 | /// 507 | /// Throw an when the specified value of 508 | /// the specified parameter name is a positive number 509 | /// 510 | /// 511 | /// The value of the parameter 512 | /// 513 | /// 514 | /// The name of the parameter to throw the for 516 | /// 517 | /// 518 | /// The message to include in the 519 | /// 520 | public static void WhenPositiveNumber(int value, string argumentName, string message) 521 | => When(value >= 0, argumentName, message); 522 | 523 | /// 524 | /// Throw an when the specified value of 525 | /// the specified parameter name is a positive number 526 | /// 527 | /// 528 | /// The value of the parameter 529 | /// 530 | /// 531 | /// The name of the parameter to throw the for 533 | /// ' 534 | public static void WhenPositiveNumber(double value, string argumentName) 535 | => When(value >= 0.0D, argumentName); 536 | 537 | /// 538 | /// Throw an when the specified value of 539 | /// the specified parameter name is a positive number 540 | /// 541 | /// 542 | /// The value of the parameter 543 | /// 544 | /// 545 | /// The name of the parameter to throw the for 547 | /// 548 | /// 549 | /// The message to include in the 550 | /// 551 | public static void WhenPositiveNumber(double value, string argumentName, string message) 552 | => When(value >= 0.0D, argumentName, message); 553 | 554 | /// 555 | /// Throw an when the specified value of 556 | /// the specified parameter name is a positive number 557 | /// 558 | /// 559 | /// The value of the parameter 560 | /// 561 | /// 562 | /// The name of the parameter to throw the for 564 | /// ' 565 | public static void WhenPositiveNumber(float value, string argumentName) 566 | => When(value >= 0.0f, argumentName); 567 | 568 | /// 569 | /// Throw an when the specified value of 570 | /// the specified parameter name is a positive number 571 | /// 572 | /// 573 | /// The value of the parameter 574 | /// 575 | /// 576 | /// The name of the parameter to throw the for 578 | /// 579 | /// 580 | /// The message to include in the 581 | /// 582 | public static void WhenPositiveNumber(float value, string argumentName, string message) 583 | => When(value >= 0.0f, argumentName, message); 584 | 585 | /// 586 | /// Throw an when the specified value of 587 | /// the specified parameter name is a positive number 588 | /// 589 | /// 590 | /// The value of the parameter 591 | /// 592 | /// 593 | /// The name of the parameter to throw the for 595 | /// ' 596 | public static void WhenPositiveNumber(decimal value, string argumentName) 597 | => When(value >= 0.0m, argumentName); 598 | 599 | /// 600 | /// Throw an when the specified value of 601 | /// the specified parameter name is a positive number 602 | /// 603 | /// 604 | /// The value of the parameter 605 | /// 606 | /// 607 | /// The name of the parameter to throw the for 609 | /// 610 | /// 611 | /// The message to include in the 612 | /// 613 | public static void WhenPositiveNumber(decimal value, string argumentName, string message) 614 | => When(value >= 0.0m, argumentName, message); 615 | 616 | /// 617 | /// Throw an when the specified value of 618 | /// the specified parameter name is lower than the given value 619 | /// 620 | /// 621 | /// The value of the parameter 622 | /// 623 | /// 624 | /// The value must be more than this 625 | /// 626 | /// 627 | /// The name of the parameter to throw the for 629 | /// ' 630 | public static void WhenLessThan(T value, T mustBeMoreThan, string argumentName) 631 | where T : IComparable 632 | => When(value.CompareTo(mustBeMoreThan) < 0, argumentName); 633 | 634 | /// 635 | /// Throw an when the specified value of 636 | /// the specified parameter name is lower than the given value 637 | /// 638 | /// 639 | /// The value of the parameter 640 | /// 641 | /// 642 | /// The value must be more than this 643 | /// 644 | /// 645 | /// The name of the parameter to throw the for 647 | /// ' 648 | /// 649 | /// The message to include in the 650 | /// 651 | public static void WhenLessThan(T value, T mustBeMoreThan, string argumentName, string message) 652 | where T : IComparable 653 | => When(value.CompareTo(mustBeMoreThan) < 0, argumentName, message); 654 | 655 | /// 656 | /// Throw an when the specified value of 657 | /// the specified parameter name is greater than the given value 658 | /// 659 | /// 660 | /// The value of the parameter 661 | /// 662 | /// 663 | /// The value must be less than this 664 | /// 665 | /// 666 | /// The name of the parameter to throw the for 668 | /// 669 | public static void WhenMoreThan(T value, T mustBeLessThan, string argumentName) 670 | where T : IComparable 671 | => When(value.CompareTo(mustBeLessThan) > 0, argumentName); 672 | 673 | /// 674 | /// Throw an when the specified value of 675 | /// the specified parameter name is greater than the given value 676 | /// 677 | /// 678 | /// The value of the parameter 679 | /// 680 | /// 681 | /// The value must be less than this 682 | /// 683 | /// 684 | /// The name of the parameter to throw the for 686 | /// 687 | /// 688 | /// The message to include in the 689 | /// 690 | public static void WhenMoreThan(T value, T mustBeLessThan, string argumentName, string message) 691 | where T : IComparable 692 | => When(value.CompareTo(mustBeLessThan) > 0, argumentName, message); 693 | } 694 | } 695 | } 696 | -------------------------------------------------------------------------------- /src/Pitcher/ThrowT.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Pitcher 4 | { 5 | /// 6 | /// Represents a throw helper for exceptions 7 | /// 8 | /// 9 | /// The type of to throw 10 | /// 11 | public static class Throw 12 | where TException : Exception, new() 13 | { 14 | /// 15 | /// Throw the of the specified type 16 | /// 17 | public static void Now() 18 | { 19 | var ex = Create(); 20 | 21 | throw ex; 22 | } 23 | 24 | /// 25 | /// Throw the of the specified type when the 26 | /// specified condition is true 27 | /// 28 | /// 29 | /// The condition to determine whether to throw the 30 | /// or not 31 | /// 32 | public static void When(bool condition) 33 | { 34 | if (condition) 35 | { 36 | Now(); 37 | } 38 | } 39 | 40 | /// 41 | /// Create a new of the specified type 42 | /// 43 | /// 44 | /// A new of the specified type 45 | /// 46 | private static TException Create() 47 | { 48 | var result = Activator.CreateInstance(); 49 | return result; 50 | } 51 | } 52 | } 53 | --------------------------------------------------------------------------------