├── .editorconfig ├── .gitignore ├── Build ├── getUnitTestCodeCoverage.ps1 └── publishNuGetPackage.ps1 ├── Doc ├── Screenshot_1.png ├── icon.ico └── icon.png ├── LICENSE ├── README.md ├── Source ├── DefensiveProgrammingFramework.sln └── DefensiveProgrammingFramework │ ├── CollectionCannotExtensions.cs │ ├── CollectionIsExtensions.cs │ ├── CollectionIsNotExtensions.cs │ ├── CollectionMustExtensions.cs │ ├── CollectionWhenExtensions.cs │ ├── CollectionWhenNotExtensions.cs │ ├── DefensiveProgrammingFramework.csproj │ ├── FileCannotExtensions.cs │ ├── FileIsExtensions.cs │ ├── FileIsNotExtensions.cs │ ├── FileMustExtensions.cs │ ├── FileWhenExtensions.cs │ ├── FileWhenNotExtensions.cs │ ├── HelperExtensions.cs │ ├── ObjectCannotExtensions.cs │ ├── ObjectIsExtensions.cs │ ├── ObjectIsNotExtensions.cs │ ├── ObjectMustExtensions.cs │ ├── ObjectWhenExtensions.cs │ ├── ObjectWhenNotExtensions.cs │ ├── ThenExtensions.cs │ ├── icon.ico │ └── key.pfx ├── Test └── DefensiveProgrammingFramework.Test │ ├── CollectionCannotExtensionsTest.cs │ ├── CollectionIsExtensionsTest.cs │ ├── CollectionIsNotTestExtensionsTest.cs │ ├── CollectionMustExtensionsTest.cs │ ├── CollectionThenExtensionsTest.cs │ ├── CollectionWhenExtensionsTest.cs │ ├── CollectionWhenNotExtensionsTest.cs │ ├── DefensiveProgrammingFramework.Test.csproj │ ├── FileCannotExtensionsTest.cs │ ├── FileIsExtensionsTest.cs │ ├── FileIsNotExtensionsTest.cs │ ├── FileMustExtensionsTest.cs │ ├── FileWhenExtensionsTest.cs │ ├── FileWhenNotExtensionsTest.cs │ ├── FrameworkTest.cs │ ├── ObjectCannotExtensionsTest.cs │ ├── ObjectIsExtensionsTest.cs │ ├── ObjectIsNotExtensionsTest.cs │ ├── ObjectMustExtensionsTest.cs │ ├── ObjectWhenExtensionsTest.cs │ └── ObjectWhenNotExtensionsTest.cs └── _config.yml /.editorconfig: -------------------------------------------------------------------------------- 1 | # Remove the line below if you want to inherit .editorconfig settings from higher directories 2 | root = true 3 | 4 | # C# files 5 | [*.cs] 6 | 7 | #### Core EditorConfig Options #### 8 | 9 | # Indentation and spacing 10 | indent_size = 4 11 | indent_style = space 12 | tab_width = 4 13 | 14 | # New line preferences 15 | end_of_line = crlf 16 | insert_final_newline = false 17 | 18 | #### .NET Coding Conventions #### 19 | 20 | # Organize usings 21 | dotnet_separate_import_directive_groups = false 22 | dotnet_sort_system_directives_first = true 23 | file_header_template = unset 24 | 25 | # this. and Me. preferences 26 | dotnet_style_qualification_for_event = true:error 27 | dotnet_style_qualification_for_field = true 28 | dotnet_style_qualification_for_method = true:error 29 | dotnet_style_qualification_for_property = true:error 30 | 31 | # Language keywords vs BCL types preferences 32 | dotnet_style_predefined_type_for_locals_parameters_members = true:error 33 | dotnet_style_predefined_type_for_member_access = true:error 34 | 35 | # Parentheses preferences 36 | dotnet_style_parentheses_in_arithmetic_binary_operators = never_if_unnecessary:error 37 | dotnet_style_parentheses_in_other_binary_operators = never_if_unnecessary:error 38 | dotnet_style_parentheses_in_other_operators = never_if_unnecessary:error 39 | dotnet_style_parentheses_in_relational_binary_operators = never_if_unnecessary:error 40 | 41 | # Modifier preferences 42 | dotnet_style_require_accessibility_modifiers = for_non_interface_members 43 | 44 | # Expression-level preferences 45 | dotnet_style_coalesce_expression = true:error 46 | dotnet_style_collection_initializer = true:error 47 | dotnet_style_explicit_tuple_names = true:error 48 | dotnet_style_namespace_match_folder = true 49 | dotnet_style_null_propagation = true:error 50 | dotnet_style_object_initializer = true:error 51 | dotnet_style_operator_placement_when_wrapping = beginning_of_line 52 | dotnet_style_prefer_auto_properties = true:error 53 | dotnet_style_prefer_collection_expression = true 54 | dotnet_style_prefer_compound_assignment = true:error 55 | dotnet_style_prefer_conditional_expression_over_assignment = true:error 56 | dotnet_style_prefer_conditional_expression_over_return = false 57 | dotnet_style_prefer_foreach_explicit_cast_in_source = when_strongly_typed 58 | dotnet_style_prefer_inferred_anonymous_type_member_names = true:error 59 | dotnet_style_prefer_inferred_tuple_names = true:error 60 | dotnet_style_prefer_is_null_check_over_reference_equality_method = true:error 61 | dotnet_style_prefer_simplified_boolean_expressions = true:error 62 | dotnet_style_prefer_simplified_interpolation = true 63 | 64 | # Field preferences 65 | dotnet_style_readonly_field = true:error 66 | 67 | # Parameter preferences 68 | dotnet_code_quality_unused_parameters = all:warning 69 | 70 | # Suppression preferences 71 | dotnet_remove_unnecessary_suppression_exclusions = none 72 | 73 | # New line preferences 74 | dotnet_style_allow_multiple_blank_lines_experimental = false:error 75 | dotnet_style_allow_statement_immediately_after_block_experimental = false:error 76 | 77 | #### C# Coding Conventions #### 78 | 79 | # var preferences 80 | csharp_style_var_elsewhere = true:error 81 | csharp_style_var_for_built_in_types = true:error 82 | csharp_style_var_when_type_is_apparent = true:error 83 | 84 | # Expression-bodied members 85 | csharp_style_expression_bodied_accessors = when_on_single_line:error 86 | csharp_style_expression_bodied_constructors = false:error 87 | csharp_style_expression_bodied_indexers = when_on_single_line:error 88 | csharp_style_expression_bodied_lambdas = when_on_single_line:error 89 | csharp_style_expression_bodied_local_functions = false:error 90 | csharp_style_expression_bodied_methods = false:error 91 | csharp_style_expression_bodied_operators = false:error 92 | csharp_style_expression_bodied_properties = when_on_single_line:error 93 | 94 | # Pattern matching preferences 95 | csharp_style_pattern_matching_over_as_with_null_check = true:warning 96 | csharp_style_pattern_matching_over_is_with_cast_check = true:warning 97 | csharp_style_prefer_extended_property_pattern = true 98 | csharp_style_prefer_not_pattern = true:warning 99 | csharp_style_prefer_pattern_matching = true:warning 100 | csharp_style_prefer_switch_expression = true:error 101 | 102 | # Null-checking preferences 103 | csharp_style_conditional_delegate_call = true:error 104 | 105 | # Modifier preferences 106 | csharp_prefer_static_local_function = true:error 107 | csharp_preferred_modifier_order = public,private,protected,internal,static,extern,new,virtual,abstract,sealed,override,readonly,unsafe,required,volatile,async 108 | csharp_style_prefer_readonly_struct = true:error 109 | csharp_style_prefer_readonly_struct_member = true 110 | 111 | # Code-block preferences 112 | csharp_prefer_braces = true:error 113 | csharp_prefer_simple_using_statement = true:error 114 | csharp_style_namespace_declarations = file_scoped:error 115 | csharp_style_prefer_method_group_conversion = false:error 116 | csharp_style_prefer_primary_constructors = true 117 | csharp_style_prefer_top_level_statements = true 118 | 119 | # Expression-level preferences 120 | csharp_prefer_simple_default_expression = true:error 121 | csharp_style_deconstructed_variable_declaration = true:warning 122 | csharp_style_implicit_object_creation_when_type_is_apparent = true:error 123 | csharp_style_inlined_variable_declaration = true:warning 124 | csharp_style_prefer_index_operator = true 125 | csharp_style_prefer_local_over_anonymous_function = true 126 | csharp_style_prefer_null_check_over_type_check = true:error 127 | csharp_style_prefer_range_operator = true 128 | csharp_style_prefer_tuple_swap = true 129 | csharp_style_prefer_utf8_string_literals = true 130 | csharp_style_throw_expression = false:silent 131 | csharp_style_unused_value_assignment_preference = discard_variable:never 132 | csharp_style_unused_value_expression_statement_preference = discard_variable:never 133 | 134 | # 'using' directive preferences 135 | csharp_using_directive_placement = outside_namespace:error 136 | 137 | # New line preferences 138 | csharp_style_allow_blank_line_after_colon_in_constructor_initializer_experimental = true:error 139 | csharp_style_allow_blank_line_after_token_in_arrow_expression_clause_experimental = true:error 140 | csharp_style_allow_blank_line_after_token_in_conditional_expression_experimental = true:error 141 | csharp_style_allow_blank_lines_between_consecutive_braces_experimental = false:error 142 | csharp_style_allow_embedded_statements_on_same_line_experimental = false:error 143 | 144 | #### C# Formatting Rules #### 145 | 146 | # New line preferences 147 | csharp_new_line_before_catch = true 148 | csharp_new_line_before_else = true 149 | csharp_new_line_before_finally = true 150 | csharp_new_line_before_members_in_anonymous_types = true 151 | csharp_new_line_before_members_in_object_initializers = true 152 | csharp_new_line_before_open_brace = all 153 | csharp_new_line_between_query_expression_clauses = true 154 | 155 | # Indentation preferences 156 | csharp_indent_block_contents = true 157 | csharp_indent_braces = false 158 | csharp_indent_case_contents = true 159 | csharp_indent_case_contents_when_block = false 160 | csharp_indent_labels = flush_left 161 | csharp_indent_switch_labels = true 162 | 163 | # Space preferences 164 | csharp_space_after_cast = false 165 | csharp_space_after_colon_in_inheritance_clause = true 166 | csharp_space_after_comma = true 167 | csharp_space_after_dot = false 168 | csharp_space_after_keywords_in_control_flow_statements = true 169 | csharp_space_after_semicolon_in_for_statement = true 170 | csharp_space_around_binary_operators = before_and_after 171 | csharp_space_around_declaration_statements = false 172 | csharp_space_before_colon_in_inheritance_clause = true 173 | csharp_space_before_comma = false 174 | csharp_space_before_dot = false 175 | csharp_space_before_open_square_brackets = false 176 | csharp_space_before_semicolon_in_for_statement = false 177 | csharp_space_between_empty_square_brackets = false 178 | csharp_space_between_method_call_empty_parameter_list_parentheses = false 179 | csharp_space_between_method_call_name_and_opening_parenthesis = false 180 | csharp_space_between_method_call_parameter_list_parentheses = false 181 | csharp_space_between_method_declaration_empty_parameter_list_parentheses = false 182 | csharp_space_between_method_declaration_name_and_open_parenthesis = false 183 | csharp_space_between_method_declaration_parameter_list_parentheses = false 184 | csharp_space_between_parentheses = false 185 | csharp_space_between_square_brackets = false 186 | 187 | # Wrapping preferences 188 | csharp_preserve_single_line_blocks = true 189 | csharp_preserve_single_line_statements = false 190 | 191 | #### Naming styles #### 192 | 193 | # Naming rules 194 | 195 | dotnet_naming_rule.interface_should_be_begins_with_i.severity = error 196 | dotnet_naming_rule.interface_should_be_begins_with_i.symbols = interface 197 | dotnet_naming_rule.interface_should_be_begins_with_i.style = begins_with_i 198 | 199 | dotnet_naming_rule.types_should_be_pascal_case.severity = error 200 | dotnet_naming_rule.types_should_be_pascal_case.symbols = types 201 | dotnet_naming_rule.types_should_be_pascal_case.style = pascal_case 202 | 203 | dotnet_naming_rule.non_field_members_should_be_pascal_case.severity = error 204 | dotnet_naming_rule.non_field_members_should_be_pascal_case.symbols = non_field_members 205 | dotnet_naming_rule.non_field_members_should_be_pascal_case.style = pascal_case 206 | 207 | dotnet_naming_rule.async_methods_should_be_ends_with_async.severity = error 208 | dotnet_naming_rule.async_methods_should_be_ends_with_async.symbols = async_methods 209 | dotnet_naming_rule.async_methods_should_be_ends_with_async.style = ends_with_async 210 | 211 | dotnet_naming_rule.private_or_internal_field_should_be_begins_with__.severity = error 212 | dotnet_naming_rule.private_or_internal_field_should_be_begins_with__.symbols = private_or_internal_field 213 | dotnet_naming_rule.private_or_internal_field_should_be_begins_with__.style = begins_with__ 214 | 215 | # Symbol specifications 216 | 217 | dotnet_naming_symbols.interface.applicable_kinds = interface 218 | dotnet_naming_symbols.interface.applicable_accessibilities = public, internal, private, protected, protected_internal 219 | dotnet_naming_symbols.interface.required_modifiers = 220 | 221 | dotnet_naming_symbols.private_or_internal_field.applicable_kinds = field 222 | dotnet_naming_symbols.private_or_internal_field.applicable_accessibilities = internal, private 223 | dotnet_naming_symbols.private_or_internal_field.required_modifiers = 224 | 225 | dotnet_naming_symbols.types.applicable_kinds = class, struct, interface, enum 226 | dotnet_naming_symbols.types.applicable_accessibilities = public, internal, private, protected, protected_internal 227 | dotnet_naming_symbols.types.required_modifiers = 228 | 229 | dotnet_naming_symbols.non_field_members.applicable_kinds = property, event, method 230 | dotnet_naming_symbols.non_field_members.applicable_accessibilities = public, internal, private, protected, protected_internal 231 | dotnet_naming_symbols.non_field_members.required_modifiers = 232 | 233 | dotnet_naming_symbols.async_methods.applicable_kinds = method 234 | dotnet_naming_symbols.async_methods.applicable_accessibilities = * 235 | dotnet_naming_symbols.async_methods.required_modifiers = async 236 | 237 | # Naming styles 238 | 239 | dotnet_naming_style.pascal_case.required_prefix = 240 | dotnet_naming_style.pascal_case.required_suffix = 241 | dotnet_naming_style.pascal_case.word_separator = 242 | dotnet_naming_style.pascal_case.capitalization = pascal_case 243 | 244 | dotnet_naming_style.begins_with_i.required_prefix = I 245 | dotnet_naming_style.begins_with_i.required_suffix = 246 | dotnet_naming_style.begins_with_i.word_separator = 247 | dotnet_naming_style.begins_with_i.capitalization = pascal_case 248 | 249 | dotnet_naming_style.begins_with__.required_prefix = _ 250 | dotnet_naming_style.begins_with__.required_suffix = 251 | dotnet_naming_style.begins_with__.word_separator = 252 | dotnet_naming_style.begins_with__.capitalization = camel_case 253 | 254 | dotnet_naming_style.ends_with_async.required_prefix = 255 | dotnet_naming_style.ends_with_async.required_suffix = Async 256 | dotnet_naming_style.ends_with_async.word_separator = 257 | dotnet_naming_style.ends_with_async.capitalization = pascal_case 258 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | TestResults 2 | 3 | .vs 4 | bin 5 | obj 6 | 7 | GeneratedReports 8 | Packages 9 | 10 | *.bak 11 | -------------------------------------------------------------------------------- /Build/getUnitTestCodeCoverage.ps1: -------------------------------------------------------------------------------- 1 | $ErrorPreference = 'Stop' 2 | 3 | $vsTestConsole = "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\Extensions\TestPlatform\vstest.console.exe" 4 | $openCoverConsole = "..\Packages\OpenCover.4.6.519\tools\OpenCover.Console.exe" 5 | $reportGenerator = "..\Packages\ReportGenerator.4.0.0\tools\net47\ReportGenerator.exe" 6 | $msbuild = "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\15.0\Bin\msbuild.exe" 7 | 8 | $solutionFilePath = "..\Source\DefensiveProgrammingFramework.sln"; 9 | $unitTestDll = "..\Test\DefensiveProgrammingFramework.Test\bin\Debug\DefensiveProgrammingFramework.Test.dll" 10 | $outputDirectory = ".\GeneratedReports" 11 | $testResultDirectory = ".\TestResults" 12 | $openCoverReport = "$outputDirectory\openCoverReport.xml" 13 | $codeCoverageReport = "$outputDirectory\CodeCoverageReport\index.htm" 14 | 15 | Write-Output "------------------------------------------------------- removing old reports -------------------------------------------------------" 16 | 17 | if (Test-Path "$outputDirectory") 18 | { 19 | Remove-Item -Path "$outputDirectory" -Recurse -Force 20 | } 21 | 22 | if (Test-Path "$testResultDirectory") 23 | { 24 | Remove-Item -Path "$testResultDirectory" -Recurse -Force 25 | } 26 | 27 | if (Test-Path "$openCoverReport") 28 | { 29 | Remove-Item -Path "$openCoverReport" 30 | } 31 | 32 | New-Item -ItemType Directory "$outputDirectory" 33 | 34 | Write-Output "`n`n" 35 | Write-Output "------------------------------------------------------- compiling solution -------------------------------------------------------" 36 | Write-Output "`n" 37 | 38 | & "$msbuild" "$solutionFilePath" /property:Configuration=Release 39 | 40 | Write-Output "------------------------------------------------------- running unit tests -------------------------------------------------------" 41 | Write-Output "`n" 42 | 43 | & "$openCoverConsole" -register:user -target:$vsTestConsole -targetargs:"$unitTestDll /Logger:trx" -filter:"+[*]* -[*.Test]*" -mergebyhash -skipautoprops -output:"$openCoverReport" 44 | 45 | Write-Output "`n`n" 46 | Write-Output "------------------------------------------------------- generating code coverage report -------------------------------------------------------" 47 | Write-Output "`n" 48 | 49 | & "$reportGenerator" -reports:"$openCoverReport" -targetdir:"$([System.IO.Path]::GetDirectoryName("$codeCoverageReport"))" 50 | 51 | & "$codeCoverageReport" 52 | 53 | Write-Output "`n`n" 54 | Write-Output "------------------------------------------------------- cleaning up -------------------------------------------------------" 55 | Write-Output "`n" 56 | 57 | Remove-Item -Path "$testResultDirectory" -Recurse -Force 58 | Remove-Item -Path "$openCoverReport" -------------------------------------------------------------------------------- /Build/publishNuGetPackage.ps1: -------------------------------------------------------------------------------- 1 | param($nugetKey="") 2 | 3 | $ErrorPreference = "Stop" 4 | 5 | $msbuild = "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\15.0\Bin\msbuild.exe" 6 | $nuget = "C:\Program Files (x86)\NuGet\nuget.exe" 7 | 8 | $projectFilePath = "..\Source\DefensiveProgrammingFramework\DefensiveProgrammingFramework.csproj"; 9 | $nugetPackageFilePath = "..\Source\DefensiveProgrammingFramework\bin\Release\DefensiveProgrammingFramework.1.0.6.nupkg" 10 | 11 | # remove references to stylecop and fxcop 12 | (Get-Content $projectFilePath) -replace ".*StyleCop.*", "" | Out-File $projectFilePath 13 | (Get-Content $projectFilePath) -replace ".*FxCop.*", "" | Out-File $projectFilePath 14 | 15 | # build package 16 | & "$msbuild" "$projectFilePath" /t:pack /p:Configuration=Release 17 | 18 | # publish package 19 | & "$nuget" push "$nugetPackageFilePath" $nugetKey -Source https://api.nuget.org/v3/index.json -------------------------------------------------------------------------------- /Doc/Screenshot_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aljazsim/defensive-programming-framework-for-net/1eced1a4aad87dd9eb45fac22dd1e516c65e489c/Doc/Screenshot_1.png -------------------------------------------------------------------------------- /Doc/icon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aljazsim/defensive-programming-framework-for-net/1eced1a4aad87dd9eb45fac22dd1e516c65e489c/Doc/icon.ico -------------------------------------------------------------------------------- /Doc/icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aljazsim/defensive-programming-framework-for-net/1eced1a4aad87dd9eb45fac22dd1e516c65e489c/Doc/icon.png -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 aljazsim 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 | -------------------------------------------------------------------------------- /Source/DefensiveProgrammingFramework.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 15 4 | VisualStudioVersion = 15.0.27130.2027 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DefensiveProgrammingFramework", "DefensiveProgrammingFramework\DefensiveProgrammingFramework.csproj", "{FB0EB106-55F9-4C59-8C9A-92FB7771B90E}" 7 | EndProject 8 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DefensiveProgrammingFramework.Test", "..\Test\DefensiveProgrammingFramework.Test\DefensiveProgrammingFramework.Test.csproj", "{C0F5F509-24CD-4E7F-9EC6-A6E93A661F75}" 9 | EndProject 10 | Global 11 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 12 | Debug|Any CPU = Debug|Any CPU 13 | Release|Any CPU = Release|Any CPU 14 | EndGlobalSection 15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 16 | {FB0EB106-55F9-4C59-8C9A-92FB7771B90E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 17 | {FB0EB106-55F9-4C59-8C9A-92FB7771B90E}.Debug|Any CPU.Build.0 = Debug|Any CPU 18 | {FB0EB106-55F9-4C59-8C9A-92FB7771B90E}.Release|Any CPU.ActiveCfg = Release|Any CPU 19 | {FB0EB106-55F9-4C59-8C9A-92FB7771B90E}.Release|Any CPU.Build.0 = Release|Any CPU 20 | {C0F5F509-24CD-4E7F-9EC6-A6E93A661F75}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 21 | {C0F5F509-24CD-4E7F-9EC6-A6E93A661F75}.Debug|Any CPU.Build.0 = Debug|Any CPU 22 | {C0F5F509-24CD-4E7F-9EC6-A6E93A661F75}.Release|Any CPU.ActiveCfg = Release|Any CPU 23 | {C0F5F509-24CD-4E7F-9EC6-A6E93A661F75}.Release|Any CPU.Build.0 = Release|Any CPU 24 | EndGlobalSection 25 | GlobalSection(SolutionProperties) = preSolution 26 | HideSolutionNode = FALSE 27 | EndGlobalSection 28 | GlobalSection(ExtensibilityGlobals) = postSolution 29 | SolutionGuid = {2AF71991-7F70-459B-BA03-69604E42D457} 30 | EndGlobalSection 31 | EndGlobal 32 | -------------------------------------------------------------------------------- /Source/DefensiveProgrammingFramework/CollectionCannotExtensions.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Diagnostics; 3 | 4 | namespace DefensiveProgrammingFramework; 5 | 6 | /// 7 | /// The collection cannot extensions methods. 8 | /// 9 | [DebuggerNonUserCode] 10 | [DebuggerStepThrough] 11 | public static class CollectionCannotExtensions 12 | { 13 | #region Public Methods 14 | 15 | /// 16 | /// Returns original value if the specified value is not empty; otherwise throws a new ArgumentException. 17 | /// 18 | /// The value type. 19 | /// The value. 20 | /// The error handler. 21 | /// 22 | /// The original value if the specified value is not empty; otherwise throws a new ArgumentException. 23 | /// 24 | public static IEnumerable CannotBeEmpty(this IEnumerable value, Action errorHandler = null) 25 | { 26 | if (value.IsEmpty()) 27 | { 28 | if (errorHandler.IsNull()) 29 | { 30 | throw new ArgumentException("Value cannot be empty."); 31 | } 32 | else 33 | { 34 | errorHandler(); 35 | } 36 | } 37 | 38 | return value; 39 | } 40 | 41 | /// 42 | /// Returns original value if the specified value is not equal to the compared value; otherwise throws a new ArgumentException. 43 | /// 44 | /// The value type. 45 | /// The value1. 46 | /// The value2. 47 | /// If set to true ignore item order. 48 | /// The error handler. 49 | /// 50 | /// The original value if the specified value is not equal to the compared value; otherwise throws a new ArgumentException. 51 | /// 52 | public static IEnumerable CannotBeEqualTo(this IEnumerable value1, IEnumerable value2, bool ignoreOrder = false, Action errorHandler = null) 53 | where T : IComparable 54 | { 55 | if (value1.IsEqualTo(value2, ignoreOrder)) 56 | { 57 | if (errorHandler.IsNull()) 58 | { 59 | throw new ArgumentException($"Value cannot be equal to {value2.Format()}."); 60 | } 61 | else 62 | { 63 | errorHandler(); 64 | } 65 | } 66 | 67 | return value1; 68 | } 69 | 70 | /// 71 | /// Returns original value if the specified value is not null or empty; otherwise throws a new ArgumentException. 72 | /// 73 | /// The value type. 74 | /// The value. 75 | /// The error handler. 76 | /// 77 | /// The original value if the specified value is not null or empty; otherwise throws a new ArgumentException. 78 | /// 79 | public static T CannotBeNullOrEmpty(this T value, Action errorHandler = null) 80 | where T : IEnumerable 81 | { 82 | if (value.IsNullOrEmpty()) 83 | { 84 | if (errorHandler.IsNull()) 85 | { 86 | throw new ArgumentException("Value cannot be null or empty."); 87 | } 88 | else 89 | { 90 | errorHandler(); 91 | } 92 | } 93 | 94 | return value; 95 | } 96 | 97 | /// 98 | /// Returns original value if the specified value doesn't belong to the specified set; otherwise throws a new ArgumentException. 99 | /// 100 | /// The value type. 101 | /// The value. 102 | /// The set. 103 | /// The error handler. 104 | /// 105 | /// The original value if the specified value doesn't belong to the specified set; otherwise throws a new ArgumentException. 106 | /// 107 | public static T CannotBeOneOf(this T value, IEnumerable set, Action errorHandler = null) 108 | where T : IComparable 109 | { 110 | if (value.IsOneOf(set)) 111 | { 112 | if (errorHandler.IsNull()) 113 | { 114 | throw new ArgumentException($"Value cannot be one of {set.Format()}."); 115 | } 116 | else 117 | { 118 | errorHandler(); 119 | } 120 | } 121 | 122 | return value; 123 | } 124 | 125 | /// 126 | /// Returns original value if the specified value doesn't contain any items coresponding to the selector function; otherwise throws a new ArgumentException. 127 | /// 128 | /// The value type. 129 | /// The value. 130 | /// The selector function. 131 | /// The error handler. 132 | /// 133 | /// The original value if the specified value doesn't contain any items coresponding to the selector function; otherwise throws a new ArgumentException. 134 | /// 135 | public static IEnumerable CannotContain(this IEnumerable value, Func func, Action errorHandler = null) 136 | { 137 | if (value.Contains(func)) 138 | { 139 | if (errorHandler.IsNull()) 140 | { 141 | throw new ArgumentException("Value cannot contain specified expression."); 142 | } 143 | else 144 | { 145 | errorHandler(); 146 | } 147 | } 148 | 149 | return value; 150 | } 151 | 152 | /// 153 | /// Returns original value if the specified value doesn't contain duplicates; otherwise throws a new ArgumentException. 154 | /// 155 | /// The value type. 156 | /// The value. 157 | /// The error handler. 158 | /// 159 | /// The original value if the specified value doesn't contain duplicates; otherwise throws a new ArgumentException. 160 | /// 161 | public static IEnumerable CannotContainDuplicates(this IEnumerable value, Action errorHandler = null) 162 | where T : IComparable 163 | { 164 | if (value.ContainsDuplicates()) 165 | { 166 | if (errorHandler.IsNull()) 167 | { 168 | throw new ArgumentException("Value cannot contain duplicates."); 169 | } 170 | else 171 | { 172 | errorHandler(); 173 | } 174 | } 175 | 176 | return value; 177 | } 178 | 179 | /// 180 | /// Returns original value if the specified value doesn't contain null values; otherwise throws a new ArgumentException. 181 | /// 182 | /// The value type. 183 | /// The value. 184 | /// The error handler. 185 | /// 186 | /// The original value if the specified value doesn't contain null values; otherwise throws a new ArgumentException. 187 | /// 188 | public static IEnumerable CannotContainNull(this IEnumerable value, Action errorHandler = null) 189 | where T : class 190 | { 191 | if (value.ContainsNull()) 192 | { 193 | if (errorHandler.IsNull()) 194 | { 195 | throw new ArgumentException("Value cannot contain null."); 196 | } 197 | else 198 | { 199 | errorHandler(); 200 | } 201 | } 202 | 203 | return value; 204 | } 205 | 206 | /// 207 | /// Returns original value if the specified value doesn't contain only null values; otherwise throws a new ArgumentException. 208 | /// 209 | /// The value type. 210 | /// The value. 211 | /// The error handler. 212 | /// 213 | /// The original value if the specified value doesn't contain only null values; otherwise throws a new ArgumentException. 214 | /// 215 | public static IEnumerable CannotContainOnlyNull(this IEnumerable value, Action errorHandler = null) 216 | where T : class 217 | { 218 | if (value.ContainsOnlyNull()) 219 | { 220 | if (errorHandler.IsNull()) 221 | { 222 | throw new ArgumentException("Value cannot contain only null."); 223 | } 224 | else 225 | { 226 | errorHandler(); 227 | } 228 | } 229 | 230 | return value; 231 | } 232 | 233 | #endregion Public Methods 234 | } 235 | -------------------------------------------------------------------------------- /Source/DefensiveProgrammingFramework/CollectionIsExtensions.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | 3 | namespace DefensiveProgrammingFramework; 4 | 5 | /// 6 | /// The collection is extension method. 7 | /// 8 | public static class CollectionIsExtensions 9 | { 10 | #region Public Methods 11 | 12 | /// 13 | /// Determines whether the specified collection contains any items coresponding to the selector function. 14 | /// 15 | /// The value type. 16 | /// The value. 17 | /// The function. 18 | /// 19 | /// true if the specified collection contains any items coresponding to the selector function; otherwise, false. 20 | /// 21 | public static bool Contains(this IEnumerable value, Func func) 22 | { 23 | func.CannotBeNull(); 24 | 25 | if (value != null) 26 | { 27 | foreach (var item in value) 28 | { 29 | if (func(item)) 30 | { 31 | return true; 32 | } 33 | } 34 | } 35 | 36 | return false; 37 | } 38 | 39 | /// 40 | /// Determines whether the specified collection contains duplicates. 41 | /// 42 | /// The value type. 43 | /// The value. 44 | /// 45 | /// true if the specified collection contains duplicates; otherwise, false. 46 | /// 47 | public static bool ContainsDuplicates(this IEnumerable value) where T : IComparable 48 | { 49 | if (value != null) 50 | { 51 | return value.GroupBy(x => x).Any(x => x.Count() > 1); 52 | } 53 | else 54 | { 55 | return false; 56 | } 57 | } 58 | 59 | /// 60 | /// Determines whether the specified collection contains a null value. 61 | /// 62 | /// The value type. 63 | /// The value. 64 | /// 65 | /// true if the specified collection contains a null value; otherwise, false. 66 | /// 67 | public static bool ContainsNull(this IEnumerable value) where T : class 68 | { 69 | if (value != null) 70 | { 71 | foreach (var item in value) 72 | { 73 | if (item == null) 74 | { 75 | return true; 76 | } 77 | } 78 | } 79 | 80 | return false; 81 | } 82 | 83 | /// 84 | /// Determines whether the specified collection contains only null values. 85 | /// 86 | /// The value type. 87 | /// The value. 88 | /// 89 | /// true if the specified collection contains only null values; otherwise, false. 90 | /// 91 | public static bool ContainsOnlyNull(this IEnumerable value) where T : class 92 | { 93 | if (value != null) 94 | { 95 | if (value.IsEmpty()) 96 | { 97 | return false; 98 | } 99 | else 100 | { 101 | foreach (var item in value) 102 | { 103 | if (item != null) 104 | { 105 | return false; 106 | } 107 | } 108 | 109 | return true; 110 | } 111 | } 112 | else 113 | { 114 | return false; 115 | } 116 | } 117 | 118 | /// 119 | /// Determines whether the specified collection is empty. 120 | /// 121 | /// The value type. 122 | /// The value. 123 | /// 124 | /// true if the specified collection is empty; otherwise, false. 125 | /// 126 | public static bool IsEmpty(this IEnumerable value) 127 | { 128 | if (value == null) 129 | { 130 | return false; 131 | } 132 | else 133 | { 134 | return !value.GetEnumerator().MoveNext(); 135 | } 136 | } 137 | 138 | /// 139 | /// Determines whether the specified collection is equal to compared collection. 140 | /// 141 | /// The value type. 142 | /// The value1. 143 | /// The value2. 144 | /// If set to true ignore order of the items. 145 | /// 146 | /// true if the specified collection is equal to compared collection; otherwise, false. 147 | /// 148 | public static bool IsEqualTo(this IEnumerable value1, IEnumerable value2, bool ignoreOrder = false) where T : IComparable 149 | { 150 | if (value1 == null && 151 | value2 == null) 152 | { 153 | return true; 154 | } 155 | else if (value1 != null && 156 | value2 != null) 157 | { 158 | if (ignoreOrder) 159 | { 160 | value1 = value1.OrderBy(x => x).ToList(); 161 | value2 = value2.OrderBy(x => x).ToList(); 162 | } 163 | 164 | return value1.SequenceEqual(value2); 165 | } 166 | else 167 | { 168 | return false; 169 | } 170 | } 171 | 172 | /// 173 | /// Determines whether the specified collection is null or empty. 174 | /// 175 | /// The value type. 176 | /// The value. 177 | /// 178 | /// true if the specified collection is null or empty; otherwise, false. 179 | /// 180 | public static bool IsNullOrEmpty(this T value) where T : IEnumerable 181 | { 182 | if (value == null) 183 | { 184 | return true; 185 | } 186 | else 187 | { 188 | return !value.GetEnumerator().MoveNext(); 189 | } 190 | } 191 | 192 | /// 193 | /// Determines whether the specified value belongs to the specified set. 194 | /// 195 | /// The value type. 196 | /// The value. 197 | /// The set. 198 | /// 199 | /// true if the specified value belongs to the specified set; otherwise, false. 200 | /// 201 | public static bool IsOneOf(this T value, IEnumerable set) where T : IComparable 202 | { 203 | set.CannotBeNull(); 204 | 205 | if (typeof(T).IsValueType) 206 | { 207 | return set.Any(x => value.IsEqualTo(x)); 208 | } 209 | else 210 | { 211 | if (value == null) 212 | { 213 | return set.Any(x => x == null); 214 | } 215 | else 216 | { 217 | return set.Any(x => value.IsEqualTo(x)); 218 | } 219 | } 220 | } 221 | 222 | #endregion Public Methods 223 | } 224 | -------------------------------------------------------------------------------- /Source/DefensiveProgrammingFramework/CollectionIsNotExtensions.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | 3 | namespace DefensiveProgrammingFramework; 4 | 5 | /// 6 | /// The collection is not extension methods. 7 | /// 8 | public static class CollectionIsNotExtensions 9 | { 10 | #region Public Methods 11 | 12 | /// 13 | /// Determines whether the specified collection doesn't contain any items coresponding to the selector function. 14 | /// 15 | /// The value type. 16 | /// The value. 17 | /// The function. 18 | /// 19 | /// true if the specified collection doesn't contain any items coresponding to the selector function; otherwise, false. 20 | /// 21 | public static bool ContainsNot(this IEnumerable value, Func func) 22 | { 23 | return !value.Contains(func); 24 | } 25 | 26 | /// 27 | /// Determines whether the specified collection doesn't contain duplicates. 28 | /// 29 | /// The value type. 30 | /// The value. 31 | /// 32 | /// true if the specified collection doesn't contain duplicates; otherwise, false. 33 | /// 34 | public static bool ContainsNotDuplicates(this IEnumerable value) where T : IComparable 35 | { 36 | return !value.ContainsDuplicates(); 37 | } 38 | 39 | /// 40 | /// Determines whether the specified collection doesn't contain a null value. 41 | /// 42 | /// The value type. 43 | /// The value. 44 | /// 45 | /// true if the specified collection doesn't contain a null value; otherwise, false. 46 | /// 47 | public static bool ContainsNotNull(this IEnumerable value) where T : class 48 | { 49 | return !value.ContainsNull(); 50 | } 51 | 52 | /// 53 | /// Determines whether the specified collection doesn't contain only null values. 54 | /// 55 | /// The value type. 56 | /// The value. 57 | /// 58 | /// true if the specified collection doesn't contain only null values; otherwise, false. 59 | /// 60 | public static bool ContainsNotOnlyNull(this IEnumerable value) where T : class 61 | { 62 | return !value.ContainsOnlyNull(); 63 | } 64 | 65 | /// 66 | /// Determines whether the specified collection is not empty. 67 | /// 68 | /// The value type. 69 | /// The value. 70 | /// 71 | /// true if the specified collection is not empty; otherwise, false. 72 | /// 73 | public static bool IsNotEmpty(this IEnumerable value) 74 | { 75 | return !value.IsEmpty(); 76 | } 77 | 78 | /// 79 | /// Determines whether the specified collection is not equal to compared collection. 80 | /// 81 | /// The value type. 82 | /// The value1. 83 | /// The value2. 84 | /// If set to true ignore order of the items. 85 | /// 86 | /// true if the specified collection is not equal to compared collection; otherwise, false. 87 | /// 88 | public static bool IsNotEqualTo(this IEnumerable value1, IEnumerable value2, bool ignoreOrder = false) where T : IComparable 89 | { 90 | return !value1.IsEqualTo(value2, ignoreOrder); 91 | } 92 | 93 | /// 94 | /// Determines whether the specified collection is not null or empty. 95 | /// 96 | /// The value type. 97 | /// The value. 98 | /// 99 | /// true if the specified collection is not null or empty; otherwise, false. 100 | /// 101 | public static bool IsNotNullOrEmpty(this T value) where T : IEnumerable 102 | { 103 | return !value.IsNullOrEmpty(); 104 | } 105 | 106 | /// 107 | /// Determines whether the specified value doesn't belong to the specified set. 108 | /// 109 | /// The value type. 110 | /// The value. 111 | /// The set. 112 | /// 113 | /// true if the specified value doesn't belong to the specified set; otherwise, false. 114 | /// 115 | public static bool IsNotOneOf(this T value, IEnumerable set) where T : IComparable 116 | { 117 | return !value.IsOneOf(set); 118 | } 119 | 120 | #endregion Public Methods 121 | } 122 | -------------------------------------------------------------------------------- /Source/DefensiveProgrammingFramework/CollectionMustExtensions.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Diagnostics; 3 | 4 | namespace DefensiveProgrammingFramework; 5 | 6 | /// 7 | /// The collection must extensions methods. 8 | /// 9 | [DebuggerNonUserCode] 10 | [DebuggerStepThrough] 11 | public static class CollectionMustExtensions 12 | { 13 | #region Public Methods 14 | 15 | /// 16 | /// Returns original value if the specified value is empty; otherwise throws a new ArgumentException. 17 | /// 18 | /// The value type. 19 | /// The value. 20 | /// The error handler. 21 | /// 22 | /// The original value if the specified value is empty; otherwise throws a new ArgumentException. 23 | /// 24 | public static IEnumerable MustBeEmpty(this IEnumerable value, Action errorHandler = null) 25 | { 26 | if (value.IsNotEmpty()) 27 | { 28 | if (errorHandler.IsNull()) 29 | { 30 | throw new ArgumentException("Value must be empty."); 31 | } 32 | else 33 | { 34 | errorHandler(); 35 | } 36 | } 37 | 38 | return value; 39 | } 40 | 41 | /// 42 | /// Returns original value if the specified value is equal to the compared value; otherwise throws a new ArgumentException. 43 | /// 44 | /// The value type. 45 | /// The value1. 46 | /// The value2. 47 | /// If set to true ignore item order. 48 | /// The error handler. 49 | /// 50 | /// The original value if the specified value is equal to the compared value; otherwise throws a new ArgumentException. 51 | /// 52 | public static IEnumerable MustBeEqualTo(this IEnumerable value1, IEnumerable value2, bool ignoreOrder = false, Action errorHandler = null) 53 | where T : IComparable 54 | { 55 | if (value1.IsNotEqualTo(value2, ignoreOrder)) 56 | { 57 | if (errorHandler.IsNull()) 58 | { 59 | throw new ArgumentException($"Value must be equal to {value2.Format()}."); 60 | } 61 | else 62 | { 63 | errorHandler(); 64 | } 65 | } 66 | 67 | return value1; 68 | } 69 | 70 | /// 71 | /// Returns original value if the specified value is null or empty; otherwise throws a new ArgumentException. 72 | /// 73 | /// The value type. 74 | /// The value. 75 | /// The error handler. 76 | /// 77 | /// The original value if the specified value is null or empty; otherwise throws a new ArgumentException. 78 | /// 79 | public static T MustBeNullOrEmpty(this T value, Action errorHandler = null) 80 | where T : IEnumerable 81 | { 82 | if (value.IsNotNullOrEmpty()) 83 | { 84 | if (errorHandler.IsNull()) 85 | { 86 | throw new ArgumentException("Value must be null or empty."); 87 | } 88 | else 89 | { 90 | errorHandler(); 91 | } 92 | } 93 | 94 | return value; 95 | } 96 | 97 | /// 98 | /// Returns original value if the specified value belongs to the specified set; otherwise throws a new ArgumentException. 99 | /// 100 | /// The value type. 101 | /// The value. 102 | /// The set. 103 | /// The error handler. 104 | /// 105 | /// The original value if the specified value belongs to the specified set; otherwise throws a new ArgumentException. 106 | /// 107 | public static T MustBeOneOf(this T value, IEnumerable set, Action errorHandler = null) 108 | where T : IComparable 109 | { 110 | if (value.IsNotOneOf(set)) 111 | { 112 | if (errorHandler.IsNull()) 113 | { 114 | throw new ArgumentException($"Value must be one of {set.Format()}."); 115 | } 116 | else 117 | { 118 | errorHandler(); 119 | } 120 | } 121 | 122 | return value; 123 | } 124 | 125 | /// 126 | /// Returns original value if the specified value contains any items coresponding to the selector function; otherwise throws a new ArgumentException. 127 | /// 128 | /// The value type. 129 | /// The value. 130 | /// The selector function. 131 | /// The error handler. 132 | /// 133 | /// The original value if the specified value contains any items coresponding to the selector function; otherwise throws a new ArgumentException. 134 | /// 135 | public static IEnumerable MustContain(this IEnumerable value, Func func, Action errorHandler = null) 136 | { 137 | if (value.ContainsNot(func)) 138 | { 139 | if (errorHandler.IsNull()) 140 | { 141 | throw new ArgumentException("Value must contain specified expression."); 142 | } 143 | else 144 | { 145 | errorHandler(); 146 | } 147 | } 148 | 149 | return value; 150 | } 151 | 152 | /// 153 | /// Returns original value if the specified value contains duplicates; otherwise throws a new ArgumentException. 154 | /// 155 | /// The value type. 156 | /// The value. 157 | /// The error handler. 158 | /// 159 | /// The original value if the specified value contains duplicates; otherwise throws a new ArgumentException. 160 | /// 161 | public static IEnumerable MustContainDuplicates(this IEnumerable value, Action errorHandler = null) 162 | where T : IComparable 163 | { 164 | if (value.ContainsNotDuplicates()) 165 | { 166 | if (errorHandler.IsNull()) 167 | { 168 | throw new ArgumentException("Value must contain duplicates."); 169 | } 170 | else 171 | { 172 | errorHandler(); 173 | } 174 | } 175 | 176 | return value; 177 | } 178 | 179 | /// 180 | /// Returns original value if the specified value contains null values; otherwise throws a new ArgumentException. 181 | /// 182 | /// The value type. 183 | /// The value. 184 | /// The error handler. 185 | /// 186 | /// The original value if the specified value contains null values; otherwise throws a new ArgumentException. 187 | /// 188 | public static IEnumerable MustContainNull(this IEnumerable value, Action errorHandler = null) 189 | where T : class 190 | { 191 | if (value.ContainsNotNull()) 192 | { 193 | if (errorHandler.IsNull()) 194 | { 195 | throw new ArgumentException("Value must contain null."); 196 | } 197 | else 198 | { 199 | errorHandler(); 200 | } 201 | } 202 | 203 | return value; 204 | } 205 | 206 | /// 207 | /// Returns original value if the specified value contains only null values; otherwise throws a new ArgumentException. 208 | /// 209 | /// The value type. 210 | /// The value. 211 | /// The error handler. 212 | /// 213 | /// The original value if the specified value contains only null values; otherwise throws a new ArgumentException. 214 | /// 215 | public static IEnumerable MustContainOnlyNull(this IEnumerable value, Action errorHandler = null) 216 | where T : class 217 | { 218 | if (value.ContainsNotOnlyNull()) 219 | { 220 | if (errorHandler.IsNull()) 221 | { 222 | throw new ArgumentException("Value must contain only null."); 223 | } 224 | else 225 | { 226 | errorHandler(); 227 | } 228 | } 229 | 230 | return value; 231 | } 232 | 233 | #endregion Public Methods 234 | } 235 | -------------------------------------------------------------------------------- /Source/DefensiveProgrammingFramework/CollectionWhenExtensions.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | 3 | namespace DefensiveProgrammingFramework; 4 | 5 | /// 6 | /// The collection when extension methods. 7 | /// 8 | public static class CollectionWhenExtensions 9 | { 10 | #region Public Methods 11 | 12 | /// 13 | /// Returns original value if the specified value contains any items coresponding to the selector function; otherwise throws a new ArgumentException; otherwise returns the original value. 14 | /// 15 | /// The value type. 16 | /// The value. 17 | /// The function. 18 | /// The default value. 19 | /// 20 | /// The original value if the specified value contains any items coresponding to the selector function; otherwise returns the original value. 21 | /// 22 | public static IEnumerable WhenContains(this IEnumerable value, Func func, IEnumerable defaultValue) where T : class 23 | { 24 | if (value.Contains(func)) 25 | { 26 | return defaultValue; 27 | } 28 | else 29 | { 30 | return value; 31 | } 32 | } 33 | 34 | /// 35 | /// Returns original value if the specified value contains duplicates; otherwise returns the original value. 36 | /// 37 | /// The value type. 38 | /// The value. 39 | /// The default value. 40 | /// 41 | /// The original value if the specified value contains duplicates; otherwise returns the original value. 42 | /// 43 | public static IEnumerable WhenContainsDuplicates(this IEnumerable value, IEnumerable defaultValue) where T : IComparable 44 | { 45 | if (value.ContainsDuplicates()) 46 | { 47 | return defaultValue; 48 | } 49 | else 50 | { 51 | return value; 52 | } 53 | } 54 | 55 | /// 56 | /// Returns original value if the specified value contains null values; otherwise returns the original value. 57 | /// 58 | /// The value type. 59 | /// The value. 60 | /// The default value. 61 | /// 62 | /// The original value if the specified value contains null values; otherwise returns the original value. 63 | /// 64 | public static IEnumerable WhenContainsNull(this IEnumerable value, IEnumerable defaultValue) where T : class 65 | { 66 | if (value.ContainsNull()) 67 | { 68 | return defaultValue; 69 | } 70 | else 71 | { 72 | return value; 73 | } 74 | } 75 | 76 | /// 77 | /// Returns original value if the specified value contains only null values; otherwise returns the original value. 78 | /// 79 | /// The value type. 80 | /// The value. 81 | /// The default value. 82 | /// 83 | /// The original value if the specified value contains only null values; otherwise returns the original value. 84 | /// 85 | public static IEnumerable WhenContainsOnlyNull(this IEnumerable value, IEnumerable defaultValue) where T : class 86 | { 87 | if (value.ContainsOnlyNull()) 88 | { 89 | return defaultValue; 90 | } 91 | else 92 | { 93 | return value; 94 | } 95 | } 96 | 97 | /// 98 | /// Returns original value if the specified value is empty; otherwise returns the original value. 99 | /// 100 | /// The value type. 101 | /// The value. 102 | /// The default value. 103 | /// 104 | /// The original value if the specified value is empty; otherwise returns the original value. 105 | /// 106 | public static IEnumerable WhenIsEmpty(this IEnumerable value, IEnumerable defaultValue) 107 | { 108 | if (value.IsEmpty()) 109 | { 110 | return defaultValue; 111 | } 112 | else 113 | { 114 | return value; 115 | } 116 | } 117 | 118 | /// 119 | /// Returns original value if the specified value is equal to the compared value; otherwise returns the original value. 120 | /// 121 | /// The value type. 122 | /// The value1. 123 | /// The value2. 124 | /// If set to true ignore item order. 125 | /// The default value. 126 | /// 127 | /// The original value if the specified value is equal to the compared value; otherwise returns the original value. 128 | /// 129 | public static IEnumerable WhenIsEqualTo(this IEnumerable value1, IEnumerable value2, bool ignoreOrder, IEnumerable defaultValue) where T : IComparable 130 | { 131 | if (value1.IsEqualTo(value2, ignoreOrder)) 132 | { 133 | return defaultValue; 134 | } 135 | else 136 | { 137 | return value1; 138 | } 139 | } 140 | 141 | /// 142 | /// Returns original value if the specified value is null or empty; otherwise returns the original value. 143 | /// 144 | /// The value type. 145 | /// The value. 146 | /// The default value. 147 | /// 148 | /// The original value if the specified value is null or empty; otherwise returns the original value. 149 | /// 150 | public static T WhenIsNullOrEmpty(this T value, T defaultValue) where T : IEnumerable 151 | { 152 | if (value.IsNullOrEmpty()) 153 | { 154 | return defaultValue; 155 | } 156 | else 157 | { 158 | return value; 159 | } 160 | } 161 | 162 | /// 163 | /// Returns original value if the specified value belongs to the specified set; otherwise returns the original value. 164 | /// 165 | /// The value type. 166 | /// The value. 167 | /// The set. 168 | /// The default value. 169 | /// 170 | /// The original value if the specified value belongs to the specified set; otherwise returns the original value. 171 | /// 172 | public static T WhenIsOneOf(this T value, IEnumerable set, T defaultValue) where T : IComparable 173 | { 174 | if (value.IsOneOf(set)) 175 | { 176 | return defaultValue; 177 | } 178 | else 179 | { 180 | return value; 181 | } 182 | } 183 | 184 | #endregion Public Methods 185 | } 186 | -------------------------------------------------------------------------------- /Source/DefensiveProgrammingFramework/CollectionWhenNotExtensions.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | 3 | namespace DefensiveProgrammingFramework; 4 | 5 | /// 6 | /// The collection when not extension method. 7 | /// 8 | public static class CollectionWhenNotExtensions 9 | { 10 | #region Public Methods 11 | 12 | /// 13 | /// Returns original value if the specified value doesn't contain any items coresponding to the selector function; otherwise throws a new ArgumentException; otherwise returns the original value. 14 | /// 15 | /// The value type. 16 | /// The value. 17 | /// The function. 18 | /// The default value. 19 | /// 20 | /// The original value if the specified value doesn't contain any items coresponding to the selector function; otherwise returns the original value. 21 | /// 22 | public static IEnumerable WhenContainsNot(this IEnumerable value, Func func, IEnumerable defaultValue) 23 | { 24 | if (value.ContainsNot(func)) 25 | { 26 | return defaultValue; 27 | } 28 | else 29 | { 30 | return value; 31 | } 32 | } 33 | 34 | /// 35 | /// Returns original value if the specified value doesn't contain duplicates; otherwise returns the original value. 36 | /// 37 | /// The value type. 38 | /// The value. 39 | /// The default value. 40 | /// 41 | /// The original value if the specified value doesn't contain; otherwise returns the original value. 42 | /// 43 | public static IEnumerable WhenContainsNotDuplicates(this IEnumerable value, IEnumerable defaultValue) 44 | where T : IComparable 45 | { 46 | if (value.ContainsNotDuplicates()) 47 | { 48 | return defaultValue; 49 | } 50 | else 51 | { 52 | return value; 53 | } 54 | } 55 | 56 | /// 57 | /// Returns original value if the specified value doesn't contain null values; otherwise returns the original value. 58 | /// 59 | /// The value type. 60 | /// The value. 61 | /// The default value. 62 | /// 63 | /// The original value if the specified value doesn't contain null values; otherwise returns the original value. 64 | /// 65 | public static IEnumerable WhenContainsNotNull(this IEnumerable value, IEnumerable defaultValue) 66 | where T : class 67 | { 68 | if (value.ContainsNotNull()) 69 | { 70 | return defaultValue; 71 | } 72 | else 73 | { 74 | return value; 75 | } 76 | } 77 | 78 | /// 79 | /// Returns original value if the specified value doesn't contain only null values; otherwise returns the original value. 80 | /// 81 | /// The value type. 82 | /// The value. 83 | /// The default value. 84 | /// 85 | /// The original value if the specified value doesn't contain only null values; otherwise returns the original value. 86 | /// 87 | public static IEnumerable WhenContainsNotOnlyNull(this IEnumerable value, IEnumerable defaultValue) 88 | where T : class 89 | { 90 | if (value.ContainsNotOnlyNull()) 91 | { 92 | return defaultValue; 93 | } 94 | else 95 | { 96 | return value; 97 | } 98 | } 99 | 100 | /// 101 | /// Returns original value if the specified value is not empty; otherwise returns the original value. 102 | /// 103 | /// The value type. 104 | /// The value. 105 | /// The default value. 106 | /// 107 | /// The original value if the specified value is not empty; otherwise returns the original value. 108 | /// 109 | public static IEnumerable WhenIsNotEmpty(this IEnumerable value, IEnumerable defaultValue) 110 | { 111 | if (value.IsNotEmpty()) 112 | { 113 | return defaultValue; 114 | } 115 | else 116 | { 117 | return value; 118 | } 119 | } 120 | 121 | /// 122 | /// Returns original value if the specified value is not equal to the compared value; otherwise returns the original value. 123 | /// 124 | /// The value type. 125 | /// The value1. 126 | /// The value2. 127 | /// If set to true ignore item order. 128 | /// The default value. 129 | /// 130 | /// The original value if the specified value is not equal to the compared value; otherwise returns the original value. 131 | /// 132 | public static IEnumerable WhenIsNotEqualTo(this IEnumerable value1, IEnumerable value2, bool ignoreOrder, IEnumerable defaultValue) 133 | where T : IComparable 134 | { 135 | if (value1.IsNotEqualTo(value2, ignoreOrder)) 136 | { 137 | return defaultValue; 138 | } 139 | else 140 | { 141 | return value1; 142 | } 143 | } 144 | 145 | /// 146 | /// Returns original value if the specified value is not null or empty; otherwise returns the original value. 147 | /// 148 | /// The value type. 149 | /// The value. 150 | /// The default value. 151 | /// 152 | /// The original value if the specified value is not null or empty; otherwise returns the original value. 153 | /// 154 | public static T WhenIsNotNullOrEmpty(this T value, T defaultValue) where T : IEnumerable 155 | { 156 | if (value.IsNotNullOrEmpty()) 157 | { 158 | return defaultValue; 159 | } 160 | else 161 | { 162 | return value; 163 | } 164 | } 165 | 166 | /// 167 | /// Returns original value if the specified value doesn't belong to the specified set; otherwise returns the original value. 168 | /// 169 | /// The value type. 170 | /// The value. 171 | /// The set. 172 | /// The default value. 173 | /// 174 | /// The original value if the specified value doesn't belong to the specified set; otherwise returns the original value. 175 | /// 176 | public static T WhenIsNotOneOf(this T value, IEnumerable set, T defaultValue) 177 | where T : IComparable 178 | { 179 | if (value.IsNotOneOf(set)) 180 | { 181 | return defaultValue; 182 | } 183 | else 184 | { 185 | return value; 186 | } 187 | } 188 | 189 | #endregion Public Methods 190 | } 191 | -------------------------------------------------------------------------------- /Source/DefensiveProgrammingFramework/DefensiveProgrammingFramework.csproj: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aljazsim/defensive-programming-framework-for-net/1eced1a4aad87dd9eb45fac22dd1e516c65e489c/Source/DefensiveProgrammingFramework/DefensiveProgrammingFramework.csproj -------------------------------------------------------------------------------- /Source/DefensiveProgrammingFramework/FileCannotExtensions.cs: -------------------------------------------------------------------------------- 1 | using System.Diagnostics; 2 | 3 | namespace DefensiveProgrammingFramework; 4 | 5 | /// 6 | /// The file cannot extension methods. 7 | /// 8 | [DebuggerNonUserCode] 9 | [DebuggerStepThrough] 10 | public static class FileCannotExtensions 11 | { 12 | #region Public Methods 13 | 14 | /// 15 | /// Returns original value if the specified value is not an absolute directory path; otherwise throws a new ArgumentException. 16 | /// 17 | /// The value. 18 | /// The error handler. 19 | /// 20 | /// The original value if the specified value is not an absolute directory path; otherwise throws a new ArgumentException. 21 | /// 22 | public static string CannotBeAbsoluteDirectoryPath(this string value, Action errorHandler = null) 23 | { 24 | if (value.IsAbsoluteDirectoryPath()) 25 | { 26 | if (errorHandler.IsNull()) 27 | { 28 | throw new ArgumentException("Value cannot be an absolute directory path."); 29 | } 30 | else 31 | { 32 | errorHandler(); 33 | } 34 | } 35 | 36 | return value; 37 | } 38 | 39 | /// 40 | /// Returns original value if the specified value not is an absolute file path; otherwise throws a new ArgumentException. 41 | /// 42 | /// The value. 43 | /// The error handler. 44 | /// 45 | /// The original value if the specified value not is an absolute file path; otherwise throws a new ArgumentException. 46 | /// 47 | public static string CannotBeAbsoluteFilePath(this string value, Action errorHandler = null) 48 | { 49 | if (value.IsAbsoluteFilePath()) 50 | { 51 | if (errorHandler.IsNull()) 52 | { 53 | throw new ArgumentException("Value cannot be an absolute file path."); 54 | } 55 | else 56 | { 57 | errorHandler(); 58 | } 59 | } 60 | 61 | return value; 62 | } 63 | 64 | /// 65 | /// Returns original value if the specified value is not an empty directory path; otherwise throws a new ArgumentException. 66 | /// 67 | /// The value. 68 | /// The error handler. 69 | /// 70 | /// The original value if the specified value is not an empty directory path; otherwise throws a new ArgumentException. 71 | /// 72 | public static string CannotBeEmptyDirectory(this string value, Action errorHandler = null) 73 | { 74 | if (value.IsEmptyDirectory()) 75 | { 76 | if (errorHandler.IsNull()) 77 | { 78 | throw new ArgumentException("Value cannot be an empty directory."); 79 | } 80 | else 81 | { 82 | errorHandler(); 83 | } 84 | } 85 | 86 | return value; 87 | } 88 | 89 | /// 90 | /// Returns original value if the specified value is not a valid directory path; otherwise throws a new ArgumentException. 91 | /// 92 | /// The value. 93 | /// The error handler. 94 | /// 95 | /// The original value if the specified value is not a valid directory path; otherwise throws a new ArgumentException. 96 | /// 97 | public static string CannotBeValidDirectoryPath(this string value, Action errorHandler = null) 98 | { 99 | if (value.IsValidDirectoryPath()) 100 | { 101 | if (errorHandler.IsNull()) 102 | { 103 | throw new ArgumentException("Value cannot be a valid directory path."); 104 | } 105 | else 106 | { 107 | errorHandler(); 108 | } 109 | } 110 | 111 | return value; 112 | } 113 | 114 | /// 115 | /// Returns original value if the specified value is not a valid file name; otherwise throws a new ArgumentException. 116 | /// 117 | /// The value. 118 | /// The error handler. 119 | /// 120 | /// The original value if the specified value is not a valid file name; otherwise throws a new ArgumentException. 121 | /// 122 | public static string CannotBeValidFileName(this string value, Action errorHandler = null) 123 | { 124 | if (value.IsValidFileName()) 125 | { 126 | if (errorHandler.IsNull()) 127 | { 128 | throw new ArgumentException("Value cannot be a valid file name."); 129 | } 130 | else 131 | { 132 | errorHandler(); 133 | } 134 | } 135 | 136 | return value; 137 | } 138 | 139 | /// 140 | /// Returns original value if the specified value is not a valid file path; otherwise throws a new ArgumentException. 141 | /// 142 | /// The value. 143 | /// The error handler. 144 | /// 145 | /// The original value if the specified value is not a valid file path; otherwise throws a new ArgumentException. 146 | /// 147 | public static string CannotBeValidFilePath(this string value, Action errorHandler = null) 148 | { 149 | if (value.IsValidFilePath()) 150 | { 151 | if (errorHandler.IsNull()) 152 | { 153 | throw new ArgumentException("Value cannot be a valid file path."); 154 | } 155 | else 156 | { 157 | errorHandler(); 158 | } 159 | } 160 | 161 | return value; 162 | } 163 | 164 | /// 165 | /// Returns original value if the specified directory does not exist; otherwise throws a new ArgumentException. 166 | /// 167 | /// The value. 168 | /// The error handler. 169 | /// 170 | /// The original value if the specified directory does not exist; otherwise throws a new ArgumentException. 171 | /// 172 | public static string CannotDirectoryExist(this string value, Action errorHandler = null) 173 | { 174 | if (value.DoesDirectoryExist()) 175 | { 176 | if (errorHandler.IsNull()) 177 | { 178 | throw new ArgumentException("Directory cannot exist."); 179 | } 180 | else 181 | { 182 | errorHandler(); 183 | } 184 | } 185 | 186 | return value; 187 | } 188 | 189 | /// 190 | /// Returns original value if the specified file does not exist; otherwise throws a new ArgumentException. 191 | /// 192 | /// The value. 193 | /// The error handler. 194 | /// 195 | /// The original value if the specified file does not exist; otherwise throws a new ArgumentException. 196 | /// 197 | public static string CannotFileExist(this string value, Action errorHandler = null) 198 | { 199 | if (value.DoesFileExist()) 200 | { 201 | if (errorHandler.IsNull()) 202 | { 203 | throw new ArgumentException("File cannot exist."); 204 | } 205 | else 206 | { 207 | errorHandler(); 208 | } 209 | } 210 | 211 | return value; 212 | } 213 | 214 | #endregion Public Methods 215 | } 216 | -------------------------------------------------------------------------------- /Source/DefensiveProgrammingFramework/FileIsExtensions.cs: -------------------------------------------------------------------------------- 1 | namespace DefensiveProgrammingFramework; 2 | 3 | /// 4 | /// The file is extension methods. 5 | /// 6 | ////[DebuggerNonUserCode] 7 | ////[DebuggerStepThrough] 8 | public static class FileIsExtensions 9 | { 10 | #region Public Methods 11 | 12 | /// 13 | /// Determines whether the specified directory exists. 14 | /// 15 | /// The value. 16 | /// 17 | /// true if the specified directory exists; otherwise, false. 18 | /// 19 | public static bool DoesDirectoryExist(this string value) 20 | { 21 | value.MustBeValidDirectoryPath(); 22 | 23 | return Directory.Exists(value); 24 | } 25 | 26 | /// 27 | /// Determines whether the specified file exists. 28 | /// 29 | /// The value. 30 | /// 31 | /// true if the specified file exists; otherwise, false. 32 | /// 33 | public static bool DoesFileExist(this string value) 34 | { 35 | value.MustBeValidFilePath(); 36 | 37 | return File.Exists(value); 38 | } 39 | 40 | /// 41 | /// Determines whether the specified value is an absolute directory path. 42 | /// 43 | /// The value. 44 | /// 45 | /// true if the specified value is an absolute directory path; otherwise, false. 46 | /// 47 | public static bool IsAbsoluteDirectoryPath(this string value) 48 | { 49 | if (value == null) 50 | { 51 | return true; 52 | } 53 | else 54 | { 55 | value.MustBeValidDirectoryPath(); 56 | 57 | return value == Path.GetFullPath(value); 58 | } 59 | } 60 | 61 | /// 62 | /// Determines whether the specified value is an absolute file path. 63 | /// 64 | /// The value. 65 | /// 66 | /// true if the specified value is an absolute file path; otherwise, false. 67 | /// 68 | public static bool IsAbsoluteFilePath(this string value) 69 | { 70 | if (value == null) 71 | { 72 | return true; 73 | } 74 | else 75 | { 76 | value.MustBeValidFilePath(); 77 | 78 | return value == Path.GetFullPath(value); 79 | } 80 | } 81 | 82 | /// 83 | /// Determines whether specified direcory is empty. 84 | /// 85 | /// The value. 86 | /// 87 | /// true if specified direcory is empty; otherwise, false. 88 | /// 89 | public static bool IsEmptyDirectory(this string value) 90 | { 91 | if (value == null) 92 | { 93 | return true; 94 | } 95 | else if (string.IsNullOrEmpty(value)) 96 | { 97 | return false; 98 | } 99 | else 100 | { 101 | value.MustBeValidDirectoryPath(); 102 | 103 | if (value.DoesDirectoryExist()) 104 | { 105 | return !Directory.GetDirectories(value, "*", SearchOption.AllDirectories).Any() && 106 | !Directory.GetFiles(value, "*", SearchOption.AllDirectories).Any(); 107 | } 108 | else 109 | { 110 | return true; 111 | } 112 | } 113 | } 114 | 115 | /// 116 | /// Determines whether the specified value is a valid directory path. 117 | /// 118 | /// The value. 119 | /// 120 | /// true if the specified value is a valid directory path; otherwise, false. 121 | /// 122 | public static bool IsValidDirectoryPath(this string value) 123 | { 124 | if (value == null) 125 | { 126 | return true; 127 | } 128 | else if (string.IsNullOrWhiteSpace(value)) 129 | { 130 | return false; 131 | } 132 | else 133 | { 134 | return !value.ToCharArray().Intersect(Path.GetInvalidPathChars()).Any(); 135 | } 136 | } 137 | 138 | /// 139 | /// Determines whether the specified value is a valid file name. 140 | /// 141 | /// The value. 142 | /// 143 | /// true if the specified value is a valid file name; otherwise, false. 144 | /// 145 | public static bool IsValidFileName(this string value) 146 | { 147 | if (value == null) 148 | { 149 | return true; 150 | } 151 | else if (string.IsNullOrWhiteSpace(value)) 152 | { 153 | return false; 154 | } 155 | else 156 | { 157 | return !value.ToCharArray().Intersect(Path.GetInvalidFileNameChars()).Any(); 158 | } 159 | } 160 | 161 | /// 162 | /// Determines whether the specified value is a valid file path. 163 | /// 164 | /// The value. 165 | /// 166 | /// true if the specified value is a valid file path; otherwise, false. 167 | /// 168 | public static bool IsValidFilePath(this string value) 169 | { 170 | if (value == null) 171 | { 172 | return true; 173 | } 174 | else if (string.IsNullOrWhiteSpace(value)) 175 | { 176 | return false; 177 | } 178 | else 179 | { 180 | return !value.ToCharArray().Intersect(Path.GetInvalidPathChars()).Any(); 181 | } 182 | } 183 | 184 | #endregion Public Methods 185 | } 186 | -------------------------------------------------------------------------------- /Source/DefensiveProgrammingFramework/FileIsNotExtensions.cs: -------------------------------------------------------------------------------- 1 | using System.Diagnostics; 2 | 3 | namespace DefensiveProgrammingFramework; 4 | 5 | /// 6 | /// The file is not extension methods. 7 | /// 8 | [DebuggerNonUserCode] 9 | [DebuggerStepThrough] 10 | public static class FileIsNotExtensions 11 | { 12 | #region Public Methods 13 | 14 | /// 15 | /// Determines whether the specified directory does not exist. 16 | /// 17 | /// The value. 18 | /// 19 | /// true if the specified directory does not exist; otherwise, false. 20 | /// 21 | public static bool DoesNotDirectoryExist(this string value) 22 | { 23 | value.MustBeValidDirectoryPath(); 24 | 25 | return !value.DoesDirectoryExist(); 26 | } 27 | 28 | /// 29 | /// Determines whether the specified file does not exist. 30 | /// 31 | /// The value. 32 | /// 33 | /// true if the specified file does not exist; otherwise, false. 34 | /// 35 | public static bool DoesNotFileExist(this string value) 36 | { 37 | value.MustBeValidFilePath(); 38 | 39 | return !value.DoesFileExist(); 40 | } 41 | 42 | /// 43 | /// Determines whether specified direcory is not empty. 44 | /// 45 | /// The value. 46 | /// 47 | /// true if specified direcory is not empty; otherwise, false. 48 | /// 49 | public static bool IsNotEmptyDirectory(this string value) 50 | { 51 | return !value.IsEmptyDirectory(); 52 | } 53 | 54 | /// 55 | /// Determines whether the specified value is an absolute directory path. 56 | /// 57 | /// The value. 58 | /// 59 | /// true if the specified value is an absolute directory path; otherwise, false. 60 | /// 61 | public static bool IsNotAbsoluteDirectoryPath(this string value) 62 | { 63 | return !value.IsAbsoluteDirectoryPath(); 64 | } 65 | 66 | /// 67 | /// Determines whether the specified value is an absolute file path. 68 | /// 69 | /// The value. 70 | /// 71 | /// true if the specified value is an absolute file path; otherwise, false. 72 | /// 73 | public static bool IsNotAbsoluteFilePath(this string value) 74 | { 75 | return !value.IsAbsoluteFilePath(); 76 | } 77 | 78 | /// 79 | /// Determines whether the specified value is not a valid directory path. 80 | /// 81 | /// The value. 82 | /// 83 | /// true if the specified value is not a valid directory path; otherwise, false. 84 | /// 85 | public static bool IsNotValidDirectoryPath(this string value) 86 | { 87 | return !value.IsValidDirectoryPath(); 88 | } 89 | 90 | /// 91 | /// Determines whether the specified value is not a valid file name. 92 | /// 93 | /// The value. 94 | /// 95 | /// true if the specified value is not a valid file name; otherwise, false. 96 | /// 97 | public static bool IsNotValidFileName(this string value) 98 | { 99 | return !value.IsValidFileName(); 100 | } 101 | 102 | /// 103 | /// Determines whether the specified value is not a valid file path. 104 | /// 105 | /// The value. 106 | /// 107 | /// true if the specified value is not a valid file path; otherwise, false. 108 | /// 109 | public static bool IsNotValidFilePath(this string value) 110 | { 111 | return !value.IsValidFilePath(); 112 | } 113 | 114 | #endregion Public Methods 115 | } 116 | -------------------------------------------------------------------------------- /Source/DefensiveProgrammingFramework/FileMustExtensions.cs: -------------------------------------------------------------------------------- 1 | using System.Diagnostics; 2 | 3 | namespace DefensiveProgrammingFramework; 4 | 5 | /// 6 | /// The object must extension methods. 7 | /// 8 | [DebuggerNonUserCode] 9 | [DebuggerStepThrough] 10 | public static class FileMustExtensions 11 | { 12 | #region Public Methods 13 | 14 | /// 15 | /// Returns original value if the specified value is an absolute directory path; otherwise throws a new ArgumentException. 16 | /// 17 | /// The value. 18 | /// The error handler. 19 | /// 20 | /// The original value if the specified value is an absolute directory path; otherwise throws a new ArgumentException. 21 | /// 22 | public static string MustBeAbsoluteDirectoryPath(this string value, Action errorHandler = null) 23 | { 24 | if (value.IsNotAbsoluteDirectoryPath()) 25 | { 26 | if (errorHandler.IsNull()) 27 | { 28 | throw new ArgumentException("Value must be an absolute directory path."); 29 | } 30 | else 31 | { 32 | errorHandler(); 33 | } 34 | } 35 | 36 | return value; 37 | } 38 | 39 | /// 40 | /// Returns original value if the specified value is an absolute file path; otherwise throws a new ArgumentException. 41 | /// 42 | /// The value. 43 | /// The error handler. 44 | /// 45 | /// The original value if the specified value is an absolute file path; otherwise throws a new ArgumentException. 46 | /// 47 | public static string MustBeAbsoluteFilePath(this string value, Action errorHandler = null) 48 | { 49 | if (value.IsNotAbsoluteFilePath()) 50 | { 51 | if (errorHandler.IsNull()) 52 | { 53 | throw new ArgumentException("Value must be an absolute file path."); 54 | } 55 | else 56 | { 57 | errorHandler(); 58 | } 59 | } 60 | 61 | return value; 62 | } 63 | 64 | /// 65 | /// Returns original value if the specified value is an empty directory path; otherwise throws a new ArgumentException. 66 | /// 67 | /// The value. 68 | /// The error handler. 69 | /// 70 | /// The original value if the specified value is an empty directory path; otherwise throws a new ArgumentException. 71 | /// 72 | public static string MustBeEmptyDirectory(this string value, Action errorHandler = null) 73 | { 74 | if (value.IsNotEmptyDirectory()) 75 | { 76 | if (errorHandler.IsNull()) 77 | { 78 | throw new ArgumentException("Value must be an empty directory."); 79 | } 80 | else 81 | { 82 | errorHandler(); 83 | } 84 | } 85 | 86 | return value; 87 | } 88 | 89 | /// 90 | /// Returns original value if the specified value is a valid directory path; otherwise throws a new ArgumentException. 91 | /// 92 | /// The value. 93 | /// The error handler. 94 | /// 95 | /// The original value if the specified value is a valid directory path; otherwise throws a new ArgumentException. 96 | /// 97 | public static string MustBeValidDirectoryPath(this string value, Action errorHandler = null) 98 | { 99 | if (value.IsNotValidFilePath()) 100 | { 101 | if (errorHandler.IsNull()) 102 | { 103 | throw new ArgumentException("Value must be a valid directory path."); 104 | } 105 | else 106 | { 107 | errorHandler(); 108 | } 109 | } 110 | 111 | return value; 112 | } 113 | 114 | /// 115 | /// Returns original value if the specified value is a valid file name; otherwise throws a new ArgumentException. 116 | /// 117 | /// The value. 118 | /// The error handler. 119 | /// 120 | /// The original value if the specified value is a valid file name; otherwise throws a new ArgumentException. 121 | /// 122 | public static string MustBeValidFileName(this string value, Action errorHandler = null) 123 | { 124 | if (value.IsNotValidFileName()) 125 | { 126 | if (errorHandler.IsNull()) 127 | { 128 | throw new ArgumentException("Value must be a valid file name."); 129 | } 130 | else 131 | { 132 | errorHandler(); 133 | } 134 | } 135 | 136 | return value; 137 | } 138 | 139 | /// 140 | /// Returns original value if the specified value is a valid file path; otherwise throws a new ArgumentException. 141 | /// 142 | /// The value. 143 | /// The error handler. 144 | /// 145 | /// The original value if the specified value is a valid file path; otherwise throws a new ArgumentException. 146 | /// 147 | public static string MustBeValidFilePath(this string value, Action errorHandler = null) 148 | { 149 | if (value.IsNotValidFilePath()) 150 | { 151 | if (errorHandler.IsNull()) 152 | { 153 | throw new ArgumentException("Value must be a valid file path."); 154 | } 155 | else 156 | { 157 | errorHandler(); 158 | } 159 | } 160 | 161 | return value; 162 | } 163 | 164 | /// 165 | /// Returns original value if the specified directory exists; otherwise throws a new ArgumentException. 166 | /// 167 | /// The value. 168 | /// The error handler. 169 | /// 170 | /// The original value if the specified directory exists; otherwise throws a new ArgumentException. 171 | /// 172 | public static string MustDirectoryExist(this string value, Action errorHandler = null) 173 | { 174 | if (value.DoesNotDirectoryExist()) 175 | { 176 | if (errorHandler.IsNull()) 177 | { 178 | throw new ArgumentException("Directory must exist."); 179 | } 180 | else 181 | { 182 | errorHandler(); 183 | } 184 | } 185 | 186 | return value; 187 | } 188 | 189 | /// 190 | /// Returns original value if the specified file exists; otherwise throws a new ArgumentException. 191 | /// 192 | /// The value. 193 | /// The error handler. 194 | /// 195 | /// The original value if the specified file exists; otherwise throws a new ArgumentException. 196 | /// 197 | public static string MustFileExist(this string value, Action errorHandler = null) 198 | { 199 | if (value.DoesNotFileExist()) 200 | { 201 | if (errorHandler.IsNull()) 202 | { 203 | throw new ArgumentException("File must exist."); 204 | } 205 | else 206 | { 207 | errorHandler(); 208 | } 209 | } 210 | 211 | return value; 212 | } 213 | 214 | #endregion Public Methods 215 | } 216 | -------------------------------------------------------------------------------- /Source/DefensiveProgrammingFramework/FileWhenExtensions.cs: -------------------------------------------------------------------------------- 1 | using System.Diagnostics; 2 | 3 | namespace DefensiveProgrammingFramework; 4 | 5 | /// 6 | /// The file when extension methods. 7 | /// 8 | [DebuggerNonUserCode] 9 | [DebuggerStepThrough] 10 | public static class FileWhenExtensions 11 | { 12 | #region Public Methods 13 | 14 | /// 15 | /// Returns default value when the specified directory exist; otherwise returns the original value. 16 | /// 17 | /// The value. 18 | /// The default value. 19 | /// 20 | /// The default value when the directory exist; otherwise returns the original value. 21 | /// 22 | public static string WhenDoesDirectoryExist(this string value, string defaultValue) 23 | { 24 | if (value.DoesDirectoryExist()) 25 | { 26 | return defaultValue; 27 | } 28 | else 29 | { 30 | return value; 31 | } 32 | } 33 | 34 | /// 35 | /// Returns default value when the specified file exist; otherwise returns the original value. 36 | /// 37 | /// The value. 38 | /// The default value. 39 | /// 40 | /// The default value when the file exist; otherwise returns the original value. 41 | /// 42 | public static string WhenDoesFileExist(this string value, string defaultValue) 43 | { 44 | if (value.DoesFileExist()) 45 | { 46 | return defaultValue; 47 | } 48 | else 49 | { 50 | return value; 51 | } 52 | } 53 | 54 | /// 55 | /// Returns default value when the specified directory path is an absolute directory path; otherwise returns the original value. 56 | /// 57 | /// The value. 58 | /// The default value. 59 | /// 60 | /// The default value when the directory path is an absolute directory path; otherwise returns the original value. 61 | /// 62 | public static string WhenIsAbsoluteDirectoryPath(this string value, string defaultValue) 63 | { 64 | if (value.IsAbsoluteDirectoryPath()) 65 | { 66 | return defaultValue; 67 | } 68 | else 69 | { 70 | return value; 71 | } 72 | } 73 | 74 | /// 75 | /// Returns default value when the specified file path is an absolute file path; otherwise returns the original value. 76 | /// 77 | /// The value. 78 | /// The default value. 79 | /// 80 | /// The default value when the file path is an absolute file path; otherwise returns the original value. 81 | /// 82 | public static string WhenIsAbsoluteFilePath(this string value, string defaultValue) 83 | { 84 | if (value.IsAbsoluteFilePath()) 85 | { 86 | return defaultValue; 87 | } 88 | else 89 | { 90 | return value; 91 | } 92 | } 93 | 94 | /// 95 | /// Returns default value when the specified directory path is empty; otherwise returns the original value. 96 | /// 97 | /// The value. 98 | /// The default value. 99 | /// 100 | /// The default value when the directory path is empty; otherwise returns the original value. 101 | /// 102 | public static string WhenIsEmptyDirectory(this string value, string defaultValue) 103 | { 104 | if (value.IsEmptyDirectory()) 105 | { 106 | return defaultValue; 107 | } 108 | else 109 | { 110 | return value; 111 | } 112 | } 113 | 114 | /// 115 | /// Returns default value when the original value is a valid directory path; otherwise returns the original value. 116 | /// 117 | /// The value. 118 | /// The default value. 119 | /// 120 | /// The default value when the original value is a valid directory path; otherwise returns the original value. 121 | /// 122 | public static string WhenIsValidDirectoryPath(this string value, string defaultValue) 123 | { 124 | if (value.IsValidDirectoryPath()) 125 | { 126 | return defaultValue; 127 | } 128 | else 129 | { 130 | return value; 131 | } 132 | } 133 | 134 | /// 135 | /// Returns default value when the original value is a valid file name; otherwise returns the original value. 136 | /// 137 | /// The value. 138 | /// The default value. 139 | /// 140 | /// The default value when the original value is a valid file name; otherwise returns the original value. 141 | /// 142 | public static string WhenIsValidFileName(this string value, string defaultValue) 143 | { 144 | if (value.IsValidFileName()) 145 | { 146 | return defaultValue; 147 | } 148 | else 149 | { 150 | return value; 151 | } 152 | } 153 | 154 | /// 155 | /// Returns default value when the original value is a valid file path; otherwise returns the original value. 156 | /// 157 | /// The value. 158 | /// The default value. 159 | /// 160 | /// The default value when the original value is a valid file path; otherwise returns the original value. 161 | /// 162 | public static string WhenIsValidFilePath(this string value, string defaultValue) 163 | { 164 | if (value.IsValidFilePath()) 165 | { 166 | return defaultValue; 167 | } 168 | else 169 | { 170 | return value; 171 | } 172 | } 173 | 174 | #endregion Public Methods 175 | } 176 | -------------------------------------------------------------------------------- /Source/DefensiveProgrammingFramework/FileWhenNotExtensions.cs: -------------------------------------------------------------------------------- 1 | using System.Diagnostics; 2 | 3 | namespace DefensiveProgrammingFramework; 4 | 5 | /// 6 | /// The file when not extension methods. 7 | /// 8 | [DebuggerNonUserCode] 9 | [DebuggerStepThrough] 10 | public static class FileWhenNotExtensions 11 | { 12 | #region Public Methods 13 | 14 | /// 15 | /// Returns default value when the specified directory does not exist; otherwise returns the original value. 16 | /// 17 | /// The value. 18 | /// The default value. 19 | /// 20 | /// The default value when the directory does not exist; otherwise returns the original value. 21 | /// 22 | public static string WhenDoesNotDirectoryExist(this string value, string defaultValue) 23 | { 24 | if (value.DoesNotDirectoryExist()) 25 | { 26 | return defaultValue; 27 | } 28 | else 29 | { 30 | return value; 31 | } 32 | } 33 | 34 | /// 35 | /// Returns default value when the specified directory path is not empty; otherwise returns the original value. 36 | /// 37 | /// The value. 38 | /// The default value. 39 | /// 40 | /// The default value when the directory path is not empty; otherwise returns the original value. 41 | /// 42 | public static string WhenIsNotEmptyDirectory(this string value, string defaultValue) 43 | { 44 | if (value.IsNotEmptyDirectory()) 45 | { 46 | return defaultValue; 47 | } 48 | else 49 | { 50 | return value; 51 | } 52 | } 53 | 54 | /// 55 | /// Returns default value when the specified file does not exist; otherwise returns the original value. 56 | /// 57 | /// The value. 58 | /// The default value. 59 | /// 60 | /// The default value when the file does not exist; otherwise returns the original value. 61 | /// 62 | public static string WhenDoesNotFileExist(this string value, string defaultValue) 63 | { 64 | if (value.DoesNotFileExist()) 65 | { 66 | return defaultValue; 67 | } 68 | else 69 | { 70 | return value; 71 | } 72 | } 73 | 74 | /// 75 | /// Returns default value when the specified directory path is not an absolute directory path; otherwise returns the original value. 76 | /// 77 | /// The value. 78 | /// The default value. 79 | /// 80 | /// The default value when the directory path is not an absolute directory path; otherwise returns the original value. 81 | /// 82 | public static string WhenIsNotAbsoluteDirectoryPath(this string value, string defaultValue) 83 | { 84 | if (value.IsNotAbsoluteDirectoryPath()) 85 | { 86 | return defaultValue; 87 | } 88 | else 89 | { 90 | return value; 91 | } 92 | } 93 | 94 | /// 95 | /// Returns default value when the specified file path is not an absolute file path; otherwise returns the original value. 96 | /// 97 | /// The value. 98 | /// The default value. 99 | /// 100 | /// The default value when the file path is not an absolute file path; otherwise returns the original value. 101 | /// 102 | public static string WhenIsNotAbsoluteFilePath(this string value, string defaultValue) 103 | { 104 | if (value.IsNotAbsoluteFilePath()) 105 | { 106 | return defaultValue; 107 | } 108 | else 109 | { 110 | return value; 111 | } 112 | } 113 | 114 | /// 115 | /// Returns default value when the original value is not a valid directory path; otherwise returns the original value. 116 | /// 117 | /// The value. 118 | /// The default value. 119 | /// 120 | /// The default value when the original value is not a valid directory path; otherwise returns the original value. 121 | /// 122 | public static string WhenIsNotValidDirectoryPath(this string value, string defaultValue) 123 | { 124 | if (value.IsNotValidDirectoryPath()) 125 | { 126 | return defaultValue; 127 | } 128 | else 129 | { 130 | return value; 131 | } 132 | } 133 | 134 | /// 135 | /// Returns default value when the original value is not a valid file name; otherwise returns the original value. 136 | /// 137 | /// The value. 138 | /// The default value. 139 | /// 140 | /// The default value when the original value is not a valid file name; otherwise returns the original value. 141 | /// 142 | public static string WhenIsNotValidFileName(this string value, string defaultValue) 143 | { 144 | if (value.IsNotValidFileName()) 145 | { 146 | return defaultValue; 147 | } 148 | else 149 | { 150 | return value; 151 | } 152 | } 153 | 154 | /// 155 | /// Returns default value when the original value is not a valid file path; otherwise returns the original value. 156 | /// 157 | /// The value. 158 | /// The default value. 159 | /// 160 | /// The default value when the original value is not a valid file path; otherwise returns the original value. 161 | /// 162 | public static string WhenIsNotValidFilePath(this string value, string defaultValue) 163 | { 164 | if (value.IsNotValidFilePath()) 165 | { 166 | return defaultValue; 167 | } 168 | else 169 | { 170 | return value; 171 | } 172 | } 173 | 174 | #endregion Public Methods 175 | } 176 | -------------------------------------------------------------------------------- /Source/DefensiveProgrammingFramework/HelperExtensions.cs: -------------------------------------------------------------------------------- 1 | namespace DefensiveProgrammingFramework; 2 | 3 | /// 4 | /// The helper extension methods. 5 | /// 6 | internal static class HelperExtensions 7 | { 8 | #region Internal Methods 9 | 10 | /// 11 | /// Formats the specified list. 12 | /// 13 | /// The list item type. 14 | /// The list. 15 | /// The formatted list. 16 | internal static string Format(this IEnumerable list) 17 | { 18 | var max = 10; 19 | 20 | if (list.Count() > max) 21 | { 22 | return $"[{string.Join(", ", list.Take(max).Select(x => x.ToString()))}, ...]"; 23 | } 24 | else 25 | { 26 | return $"[{string.Join(", ", list.Select(x => x.ToString()))}]"; 27 | } 28 | } 29 | 30 | #endregion Internal Methods 31 | } 32 | -------------------------------------------------------------------------------- /Source/DefensiveProgrammingFramework/ObjectIsExtensions.cs: -------------------------------------------------------------------------------- 1 | using System.Diagnostics; 2 | using System.Text.RegularExpressions; 3 | 4 | namespace DefensiveProgrammingFramework; 5 | 6 | /// 7 | /// The object is extension methods. 8 | /// 9 | [DebuggerNonUserCode] 10 | [DebuggerStepThrough] 11 | public static class ObjectIsExtensions 12 | { 13 | #region Public Methods 14 | 15 | /// 16 | /// Determines whether the specified value matches the specified regular expression. 17 | /// 18 | /// The value. 19 | /// The regular expression. 20 | /// 21 | /// true if specified value matches the specified regular expression; otherwise, false. 22 | /// 23 | public static bool DoesMatch(this string value, Regex regex) 24 | { 25 | regex.CannotBeNull(); 26 | 27 | if (value == null) 28 | { 29 | return false; 30 | } 31 | else 32 | { 33 | return regex.IsMatch(value); 34 | } 35 | } 36 | 37 | /// 38 | /// Determines whether the specified function returns true. 39 | /// 40 | /// The value type. 41 | /// The value. 42 | /// The function. 43 | /// 44 | /// true if the specified function returns true; otherwise, false. 45 | /// 46 | public static bool Is(this T value, Func func) 47 | { 48 | func.CannotBeNull(); 49 | 50 | return func(value); 51 | } 52 | 53 | /// 54 | /// Determines whether the specified value is between the specified limits. 55 | /// 56 | /// The value type. 57 | /// The value. 58 | /// The minimum value. 59 | /// The maximum value. 60 | /// if set to true include the limits in the range. 61 | /// 62 | /// true if the specified value is between the specified limits; otherwise, false. 63 | /// 64 | public static bool IsBetween(this T value, T minValue, T maxValue, bool inclusive = true) where T : IComparable 65 | { 66 | if (!typeof(T).IsValueType) 67 | { 68 | ((object)value).CannotBeNull(); 69 | ((object)minValue).CannotBeNull(); 70 | ((object)maxValue).CannotBeNull(); 71 | } 72 | 73 | if (!minValue.IsLessThanOrEqualTo(maxValue)) 74 | { 75 | throw new ArgumentException($"Min value must be less than or equal to max value (min: {minValue}, max: {maxValue})."); 76 | } 77 | 78 | if (inclusive) 79 | { 80 | return value.IsGreaterThanOrEqualTo(minValue) && 81 | value.IsLessThanOrEqualTo(maxValue); 82 | } 83 | else 84 | { 85 | return value.IsGreaterThan(minValue) && 86 | value.IsLessThan(maxValue); 87 | } 88 | } 89 | 90 | /// 91 | /// Determines whether the specified value is the default value for the specified type. 92 | /// 93 | /// The value type. 94 | /// The value. 95 | /// 96 | /// true if the specified value is the default value for the specified type; otherwise, false. 97 | /// 98 | public static bool IsDefault(this T value) where T : struct 99 | { 100 | return default(T).Equals(value); 101 | } 102 | 103 | /// 104 | /// Determines whether the specified value is equal to to the compared value. 105 | /// 106 | /// The value type. 107 | /// The value1. 108 | /// The value2. 109 | /// 110 | /// true if the specified value is equal to to the compared value; otherwise, false. 111 | /// 112 | public static bool IsEqualTo(this T value1, T value2) where T : IComparable 113 | { 114 | if (!typeof(T).IsValueType) 115 | { 116 | if (value1 == null && 117 | value2 == null) 118 | { 119 | return true; 120 | } 121 | else if (value1 != null && 122 | value2 != null) 123 | { 124 | return value1.CompareTo(value2) == 0; 125 | } 126 | else 127 | { 128 | return false; 129 | } 130 | } 131 | else 132 | { 133 | return value1.CompareTo(value2) == 0; 134 | } 135 | } 136 | 137 | /// 138 | /// Determines whether the specified value is greater than the specified limit. 139 | /// 140 | /// The value type. 141 | /// The value. 142 | /// The minimum value. 143 | /// 144 | /// true if the specified value is greater than the specified limit; otherwise, false. 145 | /// 146 | public static bool IsGreaterThan(this T value, T minValue) where T : IComparable 147 | { 148 | if (!typeof(T).IsValueType) 149 | { 150 | ((object)value).CannotBeNull(); 151 | ((object)minValue).CannotBeNull(); 152 | } 153 | 154 | return value.CompareTo(minValue) > 0; 155 | } 156 | 157 | /// 158 | /// Determines whether the specified value is greater than or equal to the specified limit. 159 | /// 160 | /// The value type. 161 | /// The value. 162 | /// The minimum value. 163 | /// 164 | /// true if the specified value is greater than or equal to the specified limit; otherwise, false. 165 | /// 166 | public static bool IsGreaterThanOrEqualTo(this T value, T minValue) where T : IComparable 167 | { 168 | if (!typeof(T).IsValueType) 169 | { 170 | ((object)value).CannotBeNull(); 171 | ((object)minValue).CannotBeNull(); 172 | } 173 | 174 | return value.CompareTo(minValue) >= 0; 175 | } 176 | 177 | /// 178 | /// Determines whether the specified value is less than the specified limit. 179 | /// 180 | /// The value type. 181 | /// The value. 182 | /// The maximum value. 183 | /// 184 | /// true if the specified value is less than the specified limit; otherwise, false. 185 | /// 186 | public static bool IsLessThan(this T value, T maxValue) where T : IComparable 187 | { 188 | if (!typeof(T).IsValueType) 189 | { 190 | ((object)value).CannotBeNull(); 191 | ((object)maxValue).CannotBeNull(); 192 | } 193 | 194 | return value.CompareTo(maxValue) < 0; 195 | } 196 | 197 | /// 198 | /// Determines whether the specified value is less than or equal to the specified limit. 199 | /// 200 | /// The value type. 201 | /// The value. 202 | /// The maximum value. 203 | /// 204 | /// true if the specified value is less than or equal to the specified limit; otherwise, false. 205 | /// 206 | public static bool IsLessThanOrEqualTo(this T value, T maxValue) where T : IComparable 207 | { 208 | if (!typeof(T).IsValueType) 209 | { 210 | ((object)value).CannotBeNull(); 211 | ((object)maxValue).CannotBeNull(); 212 | } 213 | 214 | return value.CompareTo(maxValue) <= 0; 215 | } 216 | 217 | /// 218 | /// Determines whether the specified value is null. 219 | /// 220 | /// The value type. 221 | /// The value. 222 | /// 223 | /// true if the specified value is null; otherwise, false. 224 | /// 225 | public static bool IsNull(this T value) where T : class 226 | { 227 | return value == null; 228 | } 229 | 230 | /// 231 | /// Determines whether the specified value is one of the specified set. 232 | /// 233 | /// The value type. 234 | /// The value. 235 | /// The set. 236 | /// 237 | /// true if the specified value is one of the specified set; otherwise, false. 238 | /// 239 | public static bool IsOneOf(this T value, params T[] set) where T : IComparable 240 | { 241 | set.CannotBeNull(); 242 | 243 | if (!typeof(T).IsValueType) 244 | { 245 | return set.Any(x => value != null && value.IsEqualTo(x) || 246 | value == null && x == null); 247 | } 248 | else 249 | { 250 | return set.Any(x => value.IsEqualTo(x)); 251 | } 252 | } 253 | 254 | /// 255 | /// Determines whether the specified value is subtype of the specified type. 256 | /// 257 | /// The value type. 258 | /// The value. 259 | /// The type. 260 | /// 261 | /// true if the specified value is subtype of the specified type; otherwise, false. 262 | /// 263 | public static bool IsSubTypeOf(this T value, Type type) 264 | { 265 | type.CannotBeNull(); 266 | 267 | if (!typeof(T).IsValueType) 268 | { 269 | if (value != null) 270 | { 271 | return type.IsAssignableFrom(value.GetType()); 272 | } 273 | else 274 | { 275 | return false; 276 | } 277 | } 278 | else 279 | { 280 | return type.IsAssignableFrom(value.GetType()); 281 | } 282 | } 283 | 284 | /// 285 | /// Determines whether the specified value is of the specified type. 286 | /// 287 | /// The value type. 288 | /// The value. 289 | /// The type. 290 | /// 291 | /// true if the specified value is of the specified type; otherwise, false. 292 | /// 293 | public static bool IsTypeOf(this T value, Type type) 294 | { 295 | type.CannotBeNull(); 296 | 297 | if (typeof(T).IsValueType) 298 | { 299 | return value.GetType() == type; 300 | } 301 | else 302 | { 303 | if (value == null) 304 | { 305 | return false; 306 | } 307 | else 308 | { 309 | return value.GetType() == type; 310 | } 311 | } 312 | } 313 | 314 | #endregion Public Methods 315 | } 316 | -------------------------------------------------------------------------------- /Source/DefensiveProgrammingFramework/ObjectIsNotExtensions.cs: -------------------------------------------------------------------------------- 1 | using System.Diagnostics; 2 | using System.Text.RegularExpressions; 3 | 4 | namespace DefensiveProgrammingFramework; 5 | 6 | /// 7 | /// The object is not extension methods. 8 | /// 9 | [DebuggerNonUserCode] 10 | [DebuggerStepThrough] 11 | public static class ObjectIsNotExtensions 12 | { 13 | #region Public Methods 14 | 15 | /// 16 | /// Determines whether the specified value does not match the specified regular expression. 17 | /// 18 | /// The value. 19 | /// The regular expression. 20 | /// 21 | /// true if specified value does not match the specified regular expression; otherwise, false. 22 | /// 23 | public static bool DoesNotMatch(this string value, Regex regex) 24 | { 25 | return !value.DoesMatch(regex); 26 | } 27 | 28 | /// 29 | /// Determines whether the specified function does not return true. 30 | /// 31 | /// The value type. 32 | /// The value. 33 | /// The function. 34 | /// 35 | /// true if the specified function does not return true; otherwise, false. 36 | /// 37 | public static bool IsNot(this T value, Func func) 38 | { 39 | return !value.Is(func); 40 | } 41 | 42 | /// 43 | /// Determines whether the specified value is not between the specified limits. 44 | /// 45 | /// The value type. 46 | /// The value. 47 | /// The minimum value. 48 | /// The maximum value. 49 | /// if set to true include the limits in the range. 50 | /// 51 | /// true if the specified value is not between the specified limits; otherwise, false. 52 | /// 53 | public static bool IsNotBetween(this T value, T minValue, T maxValue, bool inclusive = true) 54 | where T : IComparable 55 | { 56 | return !value.IsBetween(minValue, maxValue, inclusive); 57 | } 58 | 59 | /// 60 | /// Determines whether the specified value is not the default value for the specified type. 61 | /// 62 | /// The value type. 63 | /// The value. 64 | /// 65 | /// true if the specified value is not the default value for the specified type; otherwise, false. 66 | /// 67 | public static bool IsNotDefault(this T value) 68 | where T : struct 69 | { 70 | return !value.IsDefault(); 71 | } 72 | 73 | /// 74 | /// Determines whether the specified value is not equal to to the compared value. 75 | /// 76 | /// The value type. 77 | /// The value1. 78 | /// The value2. 79 | /// 80 | /// true if the specified value is not equal to to the compared value; otherwise, false. 81 | /// 82 | public static bool IsNotEqualTo(this T value1, T value2) 83 | where T : IComparable 84 | { 85 | return !value1.IsEqualTo(value2); 86 | } 87 | 88 | /// 89 | /// Determines whether the specified value is not greater than the specified limit. 90 | /// 91 | /// The value type. 92 | /// The value. 93 | /// The minimum value. 94 | /// 95 | /// true if the specified value is not greater than the specified limit; otherwise, false. 96 | /// 97 | public static bool IsNotGreaterThan(this T value, T minValue) 98 | 99 | where T : IComparable 100 | { 101 | return !value.IsGreaterThan(minValue); 102 | } 103 | 104 | /// 105 | /// Determines whether the specified value is not greater than or equal to the specified limit. 106 | /// 107 | /// The value type. 108 | /// The value. 109 | /// The minimum value. 110 | /// 111 | /// true if the specified value is not greater than or equal to the specified limit; otherwise, false. 112 | /// 113 | public static bool IsNotGreaterThanOrEqualTo(this T value, T minValue) 114 | where T : IComparable 115 | { 116 | return !value.IsGreaterThanOrEqualTo(minValue); 117 | } 118 | 119 | /// 120 | /// Determines whether the specified value is not less than the specified limit. 121 | /// 122 | /// The value type. 123 | /// The value. 124 | /// The maximum value. 125 | /// 126 | /// true if the specified value is not less than the specified limit; otherwise, false. 127 | /// 128 | public static bool IsNotLessThan(this T value, T maxValue) 129 | where T : IComparable 130 | { 131 | return !value.IsLessThan(maxValue); 132 | } 133 | 134 | /// 135 | /// Determines whether the specified value is not less than or equal to the specified limit. 136 | /// 137 | /// The value type. 138 | /// The value. 139 | /// The maximum value. 140 | /// 141 | /// true if the specified value is not less than or equal to the specified limit; otherwise, false. 142 | /// 143 | public static bool IsNotLessThanOrEqualTo(this T value, T maxValue) 144 | where T : IComparable 145 | { 146 | return !value.IsLessThanOrEqualTo(maxValue); 147 | } 148 | 149 | /// 150 | /// Determines whether the specified value is not null. 151 | /// 152 | /// The value type. 153 | /// The value. 154 | /// 155 | /// true if the specified value is not null; otherwise, false. 156 | /// 157 | public static bool IsNotNull(this T value) 158 | where T : class 159 | { 160 | return !value.IsNull(); 161 | } 162 | 163 | /// 164 | /// Determines whether the specified value is not one of the specified set. 165 | /// 166 | /// The value type. 167 | /// The value. 168 | /// The set. 169 | /// 170 | /// true if the specified value is not one of the specified set; otherwise, false. 171 | /// 172 | public static bool IsNotOneOf(this T value, params T[] set) 173 | where T : IComparable 174 | { 175 | return !value.IsOneOf(set); 176 | } 177 | 178 | /// 179 | /// Determines whether the specified value is not subtype of the specified type. 180 | /// 181 | /// The value type. 182 | /// The value. 183 | /// The type. 184 | /// 185 | /// true if the specified value is not subtype of the specified type; otherwise, false. 186 | /// 187 | public static bool IsNotSubTypeOf(this T value, Type type) 188 | { 189 | return !value.IsSubTypeOf(type); 190 | } 191 | 192 | /// 193 | /// Determines whether the specified value is not of the specified type. 194 | /// 195 | /// The value type. 196 | /// The value. 197 | /// The type. 198 | /// 199 | /// true if the specified value is not of the specified type; otherwise, false. 200 | /// 201 | public static bool IsNotTypeOf(this T value, Type type) 202 | { 203 | return !value.IsTypeOf(type); 204 | } 205 | 206 | #endregion Public Methods 207 | } 208 | -------------------------------------------------------------------------------- /Source/DefensiveProgrammingFramework/ObjectWhenExtensions.cs: -------------------------------------------------------------------------------- 1 | using System.Diagnostics; 2 | using System.Text.RegularExpressions; 3 | 4 | namespace DefensiveProgrammingFramework; 5 | 6 | /// 7 | /// The object when extension methods. 8 | /// 9 | [DebuggerNonUserCode] 10 | [DebuggerStepThrough] 11 | public static class ObjectWhenExtensions 12 | { 13 | #region Public Methods 14 | 15 | /// 16 | /// Returns default value when the original value does match the specified regular expression; otherwise returns the original value. 17 | /// 18 | /// The value. 19 | /// The regular expression. 20 | /// The default value. 21 | /// The default value when the original value does match the specified regular expression; otherwise returns the original value. 22 | public static string WhenDoesMatch(this string value, Regex regex, string defaultValue) 23 | { 24 | if (value.DoesMatch(regex)) 25 | { 26 | return defaultValue; 27 | } 28 | else 29 | { 30 | return value; 31 | } 32 | } 33 | 34 | /// 35 | /// Returns default value when the specified function returns true; otherwise returns the original value. 36 | /// 37 | /// The value type. 38 | /// The value. 39 | /// The function. 40 | /// The default value. 41 | /// The default value when the specified function returns true; otherwise returns the original value. 42 | public static T WhenIs(this T value, Func func, T defaultValue) 43 | { 44 | if (value.Is(func)) 45 | { 46 | return defaultValue; 47 | } 48 | else 49 | { 50 | return value; 51 | } 52 | } 53 | 54 | /// 55 | /// Returns default value when the original value is between the specified limits; otherwise returns the original value. 56 | /// 57 | /// The value type. 58 | /// The value. 59 | /// The minimum value. 60 | /// The maximum value. 61 | /// If set to true include limits in the range. 62 | /// The default value. 63 | /// The default value when the original value is between the specified limits; otherwise returns the original value. 64 | public static T WhenIsBetween(this T value, T minValue, T maxValue, bool inclusive, T defaultValue) where T : IComparable 65 | { 66 | if (value.IsBetween(minValue, maxValue, inclusive)) 67 | { 68 | return defaultValue; 69 | } 70 | else 71 | { 72 | return value; 73 | } 74 | } 75 | 76 | /// 77 | /// Returns default value when the original value is default value for the specified type; otherwise returns the original value. 78 | /// 79 | /// The value type. 80 | /// The value. 81 | /// The default value. 82 | /// The default value when the original value is default value for the specified type; otherwise returns the original value. 83 | public static T WhenIsDefault(this T value, T defaultValue) where T : struct 84 | { 85 | if (value.IsDefault()) 86 | { 87 | return defaultValue; 88 | } 89 | else 90 | { 91 | return value; 92 | } 93 | } 94 | 95 | /// 96 | /// Returns default value when the original value is equal to the compared value; otherwise returns the original value. 97 | /// 98 | /// The value type. 99 | /// The value1. 100 | /// The value2. 101 | /// The default value. 102 | /// The default value when the original value is equal to the compared value; otherwise returns the original value. 103 | public static T WhenIsEqualTo(this T value1, T value2, T defaultValue) where T : IComparable 104 | { 105 | if (value1.IsEqualTo(value2)) 106 | { 107 | return defaultValue; 108 | } 109 | else 110 | { 111 | return value1; 112 | } 113 | } 114 | 115 | /// 116 | /// Returns default value when the original value is greater than the specified limit; otherwise returns the original value. 117 | /// 118 | /// The value type. 119 | /// The value. 120 | /// The minimum value. 121 | /// The default value. 122 | /// The default value when the original value is greater than the specified limit; otherwise returns the original value. 123 | public static T WhenIsGreaterThan(this T value, T minValue, T defaultValue) where T : IComparable 124 | { 125 | if (value.IsGreaterThan(minValue)) 126 | { 127 | return defaultValue; 128 | } 129 | else 130 | { 131 | return value; 132 | } 133 | } 134 | 135 | /// 136 | /// Returns default value when the original value is greater than or equal to the specified limit; otherwise returns the original value. 137 | /// 138 | /// The value type. 139 | /// The value. 140 | /// The minimum value. 141 | /// The default value. 142 | /// The default value when the original value is greater than or equal to the specified limit; otherwise returns the original value. 143 | public static T WhenIsGreaterThanOrEqualTo(this T value, T minValue, T defaultValue) where T : IComparable 144 | { 145 | if (value.IsGreaterThanOrEqualTo(minValue)) 146 | { 147 | return defaultValue; 148 | } 149 | else 150 | { 151 | return value; 152 | } 153 | } 154 | 155 | /// 156 | /// Returns default value when the original value is less than the specified limit; otherwise returns the original value. 157 | /// 158 | /// The value type. 159 | /// The value. 160 | /// The maximum value. 161 | /// The default value. 162 | /// the default value when the original value is less than the specified limit; otherwise returns the original value. 163 | public static T WhenIsLessThan(this T value, T maxValue, T defaultValue) where T : IComparable 164 | { 165 | if (value.IsLessThan(maxValue)) 166 | { 167 | return defaultValue; 168 | } 169 | else 170 | { 171 | return value; 172 | } 173 | } 174 | 175 | /// 176 | /// Returns default value when the original value is less than or equal to the specified limit; otherwise returns the original value. 177 | /// 178 | /// The value type. 179 | /// The value. 180 | /// The maximum value. 181 | /// The default value. 182 | /// The default value when the original value is less than or equal to the specified limit; otherwise returns the original value. 183 | public static T WhenIsLessThanOrEqualTo(this T value, T maxValue, T defaultValue) where T : IComparable 184 | { 185 | if (value.IsLessThanOrEqualTo(maxValue)) 186 | { 187 | return defaultValue; 188 | } 189 | else 190 | { 191 | return value; 192 | } 193 | } 194 | 195 | /// 196 | /// Returns default value when the original value is null; otherwise returns the original value. 197 | /// 198 | /// The value type. 199 | /// The value. 200 | /// The default value. 201 | /// The default value when the original value is null; otherwise returns the original value. 202 | public static T WhenIsNull(this T value, T defaultValue) where T : class 203 | { 204 | if (value.IsNull()) 205 | { 206 | return defaultValue; 207 | } 208 | else 209 | { 210 | return value; 211 | } 212 | } 213 | 214 | /// 215 | /// Returns default value when the original value does belong to the specified set; otherwise returns the original value. 216 | /// 217 | /// The value type. 218 | /// The value. 219 | /// The set. 220 | /// The default value. 221 | /// 222 | /// The default value when the original value does belong to the specified set; otherwise returns the original value. 223 | /// 224 | public static T WhenIsOneOf(this T value, T[] set, T defaultValue) where T : IComparable 225 | { 226 | if (value.IsOneOf(set)) 227 | { 228 | return defaultValue; 229 | } 230 | else 231 | { 232 | return value; 233 | } 234 | } 235 | 236 | /// 237 | /// Returns default value when the original value is subtype of the specified type; otherwise returns the original value. 238 | /// 239 | /// The value type. 240 | /// The value. 241 | /// The type. 242 | /// The default value. 243 | /// The default value when the original value is subtype of the specified type; otherwise returns the original value. 244 | public static T WhenIsSubTypeOf(this T value, Type type, T defaultValue) 245 | { 246 | if (value.IsSubTypeOf(type)) 247 | { 248 | return defaultValue; 249 | } 250 | else 251 | { 252 | return value; 253 | } 254 | } 255 | 256 | /// 257 | /// Returns default value when the original value is of the specified type; otherwise returns the original value. 258 | /// 259 | /// The value type. 260 | /// The value. 261 | /// The type. 262 | /// The default value. 263 | /// The default value when the original value is of the specified type; otherwise returns the original value. 264 | public static T WhenIsTypeOf(this T value, Type type, T defaultValue) 265 | { 266 | if (value.IsTypeOf(type)) 267 | { 268 | return defaultValue; 269 | } 270 | else 271 | { 272 | return value; 273 | } 274 | } 275 | 276 | #endregion Public Methods 277 | } 278 | -------------------------------------------------------------------------------- /Source/DefensiveProgrammingFramework/ObjectWhenNotExtensions.cs: -------------------------------------------------------------------------------- 1 | using System.Diagnostics; 2 | using System.Text.RegularExpressions; 3 | 4 | namespace DefensiveProgrammingFramework; 5 | 6 | /// 7 | /// The object when not extension methods. 8 | /// 9 | [DebuggerNonUserCode] 10 | [DebuggerStepThrough] 11 | public static class ObjectWhenNotExtensions 12 | { 13 | #region Public Methods 14 | 15 | /// 16 | /// Returns default value when the original value does not match the specified regular expression; otherwise returns the original value. 17 | /// 18 | /// The value. 19 | /// The regular expression. 20 | /// The default value. 21 | /// The default value when the original value does not match the specified regular expression; otherwise returns the original value. 22 | public static string WhenDoesNotMatch(this string value, Regex regex, string defaultValue) 23 | { 24 | if (value.DoesNotMatch(regex)) 25 | { 26 | return defaultValue; 27 | } 28 | else 29 | { 30 | return value; 31 | } 32 | } 33 | 34 | /// 35 | /// Returns default value when the specified function returns true; otherwise returns the original value. 36 | /// 37 | /// The value type. 38 | /// The value. 39 | /// The function. 40 | /// The default value. 41 | /// The default value when the specified function returns true; otherwise returns the original value 42 | public static T WhenIsNot(this T value, Func func, T defaultValue) 43 | { 44 | if (value.IsNot(func)) 45 | { 46 | return defaultValue; 47 | } 48 | else 49 | { 50 | return value; 51 | } 52 | } 53 | 54 | /// 55 | /// Returns default value when the original value is not between the specified limits; otherwise returns the original value. 56 | /// 57 | /// The value type. 58 | /// The value. 59 | /// The minimum value. 60 | /// The maximum value. 61 | /// If set to true include limits in the range. 62 | /// The default value. 63 | /// The default value when the original value is not between the specified limits; otherwise returns the original value. 64 | public static T WhenIsNotBetween(this T value, T minValue, T maxValue, bool inclusive, T defaultValue) where T : IComparable 65 | { 66 | if (value.IsNotBetween(minValue, maxValue, inclusive)) 67 | { 68 | return defaultValue; 69 | } 70 | else 71 | { 72 | return value; 73 | } 74 | } 75 | 76 | /// 77 | /// Returns default value when the original value is not default value for the specified type; otherwise returns the original value. 78 | /// 79 | /// The value type. 80 | /// The value. 81 | /// The default value. 82 | /// The default value when the original value is not default value for the specified type; otherwise returns the original value. 83 | public static T WhenIsNotDefault(this T value, T defaultValue) where T : struct 84 | { 85 | if (value.IsNotDefault()) 86 | { 87 | return defaultValue; 88 | } 89 | else 90 | { 91 | return value; 92 | } 93 | } 94 | 95 | /// 96 | /// Returns default value when the original value is not equal to the compared value; otherwise returns the original value. 97 | /// 98 | /// The value type. 99 | /// The value1. 100 | /// The value2. 101 | /// The default value. 102 | /// The default value when the original value is not equal to the compared value; otherwise returns the original value. 103 | public static T WhenIsNotEqualTo(this T value1, T value2, T defaultValue) where T : IComparable 104 | { 105 | if (value1.IsNotEqualTo(value2)) 106 | { 107 | return defaultValue; 108 | } 109 | else 110 | { 111 | return value1; 112 | } 113 | } 114 | 115 | /// 116 | /// Returns default value when the original value is not greater than the specified limit; otherwise returns the original value. 117 | /// 118 | /// The value type. 119 | /// The value. 120 | /// The minimum value. 121 | /// The default value. 122 | /// The default value when the original value is not greater than the specified limit; otherwise returns the original value. 123 | public static T WhenIsNotGreaterThan(this T value, T minValue, T defaultValue) where T : IComparable 124 | { 125 | if (value.IsNotGreaterThan(minValue)) 126 | { 127 | return defaultValue; 128 | } 129 | else 130 | { 131 | return value; 132 | } 133 | } 134 | 135 | /// 136 | /// Returns default value when the original value is not greater than or equal to the specified limit; otherwise returns the original value. 137 | /// 138 | /// The value type. 139 | /// The value. 140 | /// The minimum value. 141 | /// The default value. 142 | /// The default value when the original value is not greater than or equal to the specified limit; otherwise returns the original value. 143 | public static T WhenIsNotGreaterThanOrEqualTo(this T value, T minValue, T defaultValue) where T : IComparable 144 | { 145 | if (value.IsNotGreaterThanOrEqualTo(minValue)) 146 | { 147 | return defaultValue; 148 | } 149 | else 150 | { 151 | return value; 152 | } 153 | } 154 | 155 | /// 156 | /// Returns default value when the original value is not less than the specified limit; otherwise returns the original value. 157 | /// 158 | /// The value type. 159 | /// The value. 160 | /// The maximum value. 161 | /// The default value. 162 | /// the default value when the original value is not less than the specified limit; otherwise returns the original value. 163 | public static T WhenIsNotLessThan(this T value, T maxValue, T defaultValue) where T : IComparable 164 | { 165 | if (value.IsNotLessThan(maxValue)) 166 | { 167 | return defaultValue; 168 | } 169 | else 170 | { 171 | return value; 172 | } 173 | } 174 | 175 | /// 176 | /// Returns default value when the original value is not less than or equal to the specified limit; otherwise returns the original value. 177 | /// 178 | /// The value type. 179 | /// The value. 180 | /// The maximum value. 181 | /// The default value. 182 | /// The default value when the original value is not less than or equal to the specified limit; otherwise returns the original value. 183 | public static T WhenIsNotLessThanOrEqualTo(this T value, T maxValue, T defaultValue) where T : IComparable 184 | { 185 | if (value.IsNotLessThanOrEqualTo(maxValue)) 186 | { 187 | return defaultValue; 188 | } 189 | else 190 | { 191 | return value; 192 | } 193 | } 194 | 195 | /// 196 | /// Returns default value when the original value is not null; otherwise returns the original value. 197 | /// 198 | /// The value type. 199 | /// The value. 200 | /// The default value. 201 | /// The default value when the original value is not null; otherwise returns the original value. 202 | public static T WhenIsNotNull(this T value, T defaultValue) where T : class 203 | { 204 | if (value.IsNotNull()) 205 | { 206 | return defaultValue; 207 | } 208 | else 209 | { 210 | return value; 211 | } 212 | } 213 | 214 | /// 215 | /// Returns default value when the original value does not belong to the specified set; otherwise returns the original value. 216 | /// 217 | /// The value type. 218 | /// The value. 219 | /// The set. 220 | /// The default value. 221 | /// 222 | /// The default value when the original value does not belong to the specified set; otherwise returns the original value. 223 | /// 224 | public static T WhenIsNotOneOf(this T value, T[] set, T defaultValue) where T : IComparable 225 | { 226 | if (value.IsNotOneOf(set)) 227 | { 228 | return defaultValue; 229 | } 230 | else 231 | { 232 | return value; 233 | } 234 | } 235 | 236 | /// 237 | /// Returns default value when the original value is not subtype of the specified type; otherwise returns the original value. 238 | /// 239 | /// The value type. 240 | /// The value. 241 | /// The type. 242 | /// The default value. 243 | /// The default value when the original value is not subtype of the specified type; otherwise returns the original value. 244 | public static T WhenIsNotSubTypeOf(this T value, Type type, T defaultValue) 245 | { 246 | if (value.IsNotSubTypeOf(type)) 247 | { 248 | return defaultValue; 249 | } 250 | else 251 | { 252 | return value; 253 | } 254 | } 255 | 256 | /// 257 | /// Returns default value when the original value is not of the specified type; otherwise returns the original value. 258 | /// 259 | /// The value type. 260 | /// The value. 261 | /// The type. 262 | /// The default value. 263 | /// The default value when the original value is not of the specified type; otherwise returns the original value. 264 | public static T WhenIsNotTypeOf(this T value, Type type, T defaultValue) 265 | { 266 | if (value.IsNotTypeOf(type)) 267 | { 268 | return defaultValue; 269 | } 270 | else 271 | { 272 | return value; 273 | } 274 | } 275 | 276 | #endregion Public Methods 277 | } 278 | -------------------------------------------------------------------------------- /Source/DefensiveProgrammingFramework/ThenExtensions.cs: -------------------------------------------------------------------------------- 1 | namespace DefensiveProgrammingFramework; 2 | 3 | /// 4 | /// The then extensions methods. 5 | /// 6 | public static class ThenExtensions 7 | { 8 | #region Public Methods 9 | 10 | /// 11 | /// Executes the specified action when the condition result is true. 12 | /// 13 | /// If set to true the specified action will execute. 14 | /// The action that will execute if the condition result is true.. 15 | public static void Then(this bool conditionResult, Action onTrue) 16 | { 17 | onTrue.CannotBeNull(); 18 | 19 | if (conditionResult) 20 | { 21 | onTrue(); 22 | } 23 | } 24 | 25 | #endregion Public Methods 26 | } 27 | -------------------------------------------------------------------------------- /Source/DefensiveProgrammingFramework/icon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aljazsim/defensive-programming-framework-for-net/1eced1a4aad87dd9eb45fac22dd1e516c65e489c/Source/DefensiveProgrammingFramework/icon.ico -------------------------------------------------------------------------------- /Source/DefensiveProgrammingFramework/key.pfx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aljazsim/defensive-programming-framework-for-net/1eced1a4aad87dd9eb45fac22dd1e516c65e489c/Source/DefensiveProgrammingFramework/key.pfx -------------------------------------------------------------------------------- /Test/DefensiveProgrammingFramework.Test/CollectionIsExtensionsTest.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using Microsoft.VisualStudio.TestTools.UnitTesting; 3 | 4 | namespace DefensiveProgrammingFramework.Test; 5 | 6 | [TestClass] 7 | public class CollectionIsExtensionsTest 8 | { 9 | #region Public Methods 10 | 11 | [TestMethod] 12 | public void Contains() 13 | { 14 | Assert.AreEqual(false, (null as string[]).Contains(x => x == null)); 15 | Assert.AreEqual(false, new string[] { }.Contains(x => x == null)); 16 | Assert.AreEqual(true, new string[] { null }.Contains(x => x == null)); 17 | Assert.AreEqual(false, new string[] { "aaa" }.Contains(x => x == null)); 18 | Assert.AreEqual(false, new string[] { "aaa", "bb", "c" }.Contains(x => x == null)); 19 | Assert.AreEqual(true, new string[] { null, "aaa", "bb", "c" }.Contains(x => x == null)); 20 | Assert.AreEqual(true, new string[] { "aaa", null, "bb", "c" }.Contains(x => x == null)); 21 | Assert.AreEqual(true, new string[] { "aaa", "bb", null, "c" }.Contains(x => x == null)); 22 | Assert.AreEqual(true, new string[] { "aaa", "bb", "c", null }.Contains(x => x == null)); 23 | Assert.AreEqual(true, new string[] { null, null, null }.Contains(x => x == null)); 24 | } 25 | 26 | [TestMethod] 27 | public void ContainsDuplicates() 28 | { 29 | Assert.AreEqual(false, (null as string[]).ContainsDuplicates()); 30 | Assert.AreEqual(false, new string[] { }.ContainsDuplicates()); 31 | Assert.AreEqual(false, new string[] { null }.ContainsDuplicates()); 32 | Assert.AreEqual(true, new string[] { null, null }.ContainsDuplicates()); 33 | Assert.AreEqual(false, new string[] { "aaa" }.ContainsDuplicates()); 34 | Assert.AreEqual(false, new string[] { "aaa", "bb", "c" }.ContainsDuplicates()); 35 | Assert.AreEqual(true, new string[] { "aaa", "bb", "aaa" }.ContainsDuplicates()); 36 | Assert.AreEqual(true, new string[] { null, "aaa", null, "c" }.ContainsDuplicates()); 37 | Assert.AreEqual(true, new string[] { null, null, null }.ContainsDuplicates()); 38 | } 39 | 40 | [TestMethod] 41 | public void ContainsNull() 42 | { 43 | Assert.AreEqual(false, (null as string[]).ContainsNull()); 44 | Assert.AreEqual(false, new string[] { }.ContainsNull()); 45 | Assert.AreEqual(true, new string[] { null }.ContainsNull()); 46 | Assert.AreEqual(false, new string[] { "aaa" }.ContainsNull()); 47 | Assert.AreEqual(false, new string[] { "aaa", "bb", "c" }.ContainsNull()); 48 | Assert.AreEqual(true, new string[] { null, "aaa", "bb", "c" }.ContainsNull()); 49 | Assert.AreEqual(true, new string[] { "aaa", null, "bb", "c" }.ContainsNull()); 50 | Assert.AreEqual(true, new string[] { "aaa", "bb", null, "c" }.ContainsNull()); 51 | Assert.AreEqual(true, new string[] { "aaa", "bb", "c", null }.ContainsNull()); 52 | Assert.AreEqual(true, new string[] { null, null, null }.ContainsNull()); 53 | } 54 | 55 | [TestMethod] 56 | public void ContainsOnlyNull() 57 | { 58 | Assert.AreEqual(false, (null as IEnumerable).ContainsOnlyNull()); 59 | Assert.AreEqual(false, new string[] { }.ContainsOnlyNull()); 60 | Assert.AreEqual(true, new string[] { null }.ContainsOnlyNull()); 61 | Assert.AreEqual(false, new string[] { "aaa" }.ContainsOnlyNull()); 62 | Assert.AreEqual(false, new string[] { "aaa", "bb", "c" }.ContainsOnlyNull()); 63 | Assert.AreEqual(false, new string[] { null, "aaa", "bb", "c" }.ContainsOnlyNull()); 64 | Assert.AreEqual(false, new string[] { "aaa", null, "bb", "c" }.ContainsOnlyNull()); 65 | Assert.AreEqual(false, new string[] { "aaa", "bb", null, "c" }.ContainsOnlyNull()); 66 | Assert.AreEqual(false, new string[] { "aaa", "bb", "c", null }.ContainsOnlyNull()); 67 | Assert.AreEqual(true, new string[] { null, null, null }.ContainsOnlyNull()); 68 | } 69 | 70 | [TestMethod] 71 | public void IsEmpty() 72 | { 73 | Assert.AreEqual(true, "".IsEmpty()); 74 | Assert.AreEqual(false, "a".IsEmpty()); 75 | Assert.AreEqual(false, "ab".IsEmpty()); 76 | Assert.AreEqual(false, "abc".IsEmpty()); 77 | Assert.AreEqual(true, new int[] { }.IsEmpty()); 78 | Assert.AreEqual(false, new int[] { 1 }.IsEmpty()); 79 | Assert.AreEqual(false, (null as IEnumerable).IsEmpty()); 80 | } 81 | 82 | [DataRow("", "", false, true)] 83 | [DataRow(" ", " ", false, true)] 84 | [DataRow(" ", " ", false, false)] 85 | [DataRow(" ", " \\t", false, false)] 86 | [DataRow("a", "a", false, true)] 87 | [DataRow("aa", "aa", false, true)] 88 | [DataRow("aa", "a", false, false)] 89 | [DataRow("ab", "ab", false, true)] 90 | [DataRow("ab", "ba", true, true)] 91 | [DataRow("ab", "ba", false, false)] 92 | [DataRow("abc", "abc", true, true)] 93 | [DataRow("abc", "abc", false, true)] 94 | [DataRow("abc", "bac", true, true)] 95 | [DataRow("abc", "acb", true, true)] 96 | [DataRow("abd", "cba", true, false)] 97 | [DataRow("abc", "bcad", true, false)] 98 | [DataRow("", null, false, false)] 99 | [DataRow(null, "", false, false)] 100 | [DataRow(null, null, false, true)] 101 | [DataRow(null, null, true, true)] 102 | [DataTestMethod] 103 | public void IsEqualTo(IEnumerable value1, IEnumerable value2, bool ignoreOrder, bool expected) 104 | { 105 | Assert.AreEqual(expected, value1.IsEqualTo(value2, ignoreOrder)); 106 | } 107 | 108 | [DataRow(new string[] { null }, new string[] { null }, true, true)] 109 | [DataRow(new string[] { null }, new string[] { null }, false, true)] 110 | [DataRow(new string[] { null, null }, new string[] { null, null }, true, true)] 111 | [DataRow(new string[] { null, null }, new string[] { null, null }, false, true)] 112 | [DataRow(new string[] { null, "aaa" }, new string[] { null, "aaa" }, false, true)] 113 | [DataRow(new string[] { null, "aaa" }, new string[] { "aaa", null }, true, true)] 114 | [DataRow(new string[] { "bbb", "aaa" }, new string[] { "bbb", "aaa" }, true, true)] 115 | [DataRow(new string[] { "bbb", "aaa" }, new string[] { "aaa", "bbb" }, true, true)] 116 | [DataTestMethod] 117 | public void IsEqualTo3(IEnumerable value1, IEnumerable value2, bool ignoreOrder, bool expected) 118 | { 119 | Assert.AreEqual(expected, value1.IsEqualTo(value2, ignoreOrder)); 120 | } 121 | 122 | [DataRow(new int[0], new int[0], false, true)] 123 | [DataRow(new int[] { 1 }, new int[] { 1 }, true, true)] 124 | [DataRow(new int[] { 1, 1 }, new int[] { 1, 1 }, false, true)] 125 | [DataRow(new int[] { 1 }, new int[] { 1, 1 }, true, false)] 126 | [DataRow(new int[] { 0, 9, 8, 7, 6, 5, 4, 3, 2, 1 }, new int[] { 7, 8, 9, 6, 5, 4, 1, 0, 2, 3 }, true, true)] 127 | [DataTestMethod] 128 | public void IsEqualTo4(IEnumerable value1, IEnumerable value2, bool ignoreOrder, bool expected) 129 | { 130 | Assert.AreEqual(expected, value1.IsEqualTo(value2, ignoreOrder)); 131 | } 132 | 133 | [DataRow(null, true)] 134 | [DataRow("", true)] 135 | [DataRow("a", false)] 136 | [DataRow(new int[] { }, true)] 137 | [DataRow(new int[] { 1 }, false)] 138 | [DataRow(new int[] { 1, 2 }, false)] 139 | [DataRow(new int[] { 1, 2, 3 }, false)] 140 | [DataTestMethod] 141 | public void IsNullOrEmpty(IEnumerable value, bool expected) 142 | { 143 | Assert.AreEqual(expected, value.IsNullOrEmpty()); 144 | } 145 | 146 | [DataRow(3, new int[] { }, false)] 147 | [DataRow(3, new int[] { 3 }, true)] 148 | [DataRow(3, new int[] { 2 }, false)] 149 | [DataRow(3, new int[] { 2, 3 }, true)] 150 | [DataRow(3, new int[] { 1, 2, 3, 4 }, true)] 151 | [DataRow(3, new int[] { 4, 3, 2, 1 }, true)] 152 | [DataRow(3, new int[] { 1, 1, 1, 1, 3, 1, 1, 1 }, true)] 153 | [DataRow(3, new int[] { 3, 3, 3, 3, 3, 3, 3 }, true)] 154 | [DataRow(3, new int[] { 4, 4, 4, 4, 4, 4, 4, 4 }, false)] 155 | [DataTestMethod] 156 | public void IsOneOf(int value, IEnumerable set, bool expected) 157 | { 158 | Assert.AreEqual(expected, value.IsOneOf(set)); 159 | Assert.AreEqual(expected, value.IsOneOf(set.ToArray())); 160 | } 161 | 162 | [DataRow("", new string[] { }, false)] 163 | [DataRow("", new string[] { "" }, true)] 164 | [DataRow("a", new string[] { "a" }, true)] 165 | [DataRow("a", new string[] { "a", "b" }, true)] 166 | [DataRow("a", new string[] { "ab", "ba" }, false)] 167 | [DataRow("a", new string[] { "ab", "A" }, false)] 168 | [DataRow("a", new string[] { "ab", "a" }, true)] 169 | [DataRow("a", new string[] { "a", "a", "a", "a", "a", "a", "a" }, true)] 170 | [DataRow("a", new string[] { "b", "b", "b", "b", "b", "b", "b" }, false)] 171 | [DataRow(null, new string[] { "" }, false)] 172 | [DataRow(null, new string[] { null }, true)] 173 | [DataTestMethod] 174 | public void IsOneOf2(string value, IEnumerable set, bool expected) 175 | { 176 | Assert.AreEqual(expected, value.IsOneOf(set)); 177 | } 178 | 179 | [DataRow(null, null)] 180 | [DataRow("a", null)] 181 | [DataTestMethod] 182 | public void IsOneOfFail(string value, IEnumerable set) 183 | { 184 | try 185 | { 186 | value.IsOneOf(set); 187 | 188 | Assert.Fail(); 189 | } 190 | catch (ArgumentException ex) 191 | { 192 | Assert.AreEqual("Value cannot be null.", ex.Message); 193 | } 194 | } 195 | 196 | #endregion Public Methods 197 | } 198 | -------------------------------------------------------------------------------- /Test/DefensiveProgrammingFramework.Test/CollectionIsNotTestExtensionsTest.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using Microsoft.VisualStudio.TestTools.UnitTesting; 3 | 4 | namespace DefensiveProgrammingFramework.Test; 5 | 6 | [TestClass] 7 | public class CollectionIsNotNotTestExtensionsTest 8 | { 9 | #region Public Methods 10 | 11 | [TestMethod] 12 | public void ContainsNot() 13 | { 14 | Assert.AreEqual(true, (null as string[]).ContainsNot(x => x == null)); 15 | Assert.AreEqual(true, new string[] { }.ContainsNot(x => x == null)); 16 | Assert.AreEqual(false, new string[] { null }.ContainsNot(x => x == null)); 17 | Assert.AreEqual(true, new string[] { "aaa" }.ContainsNot(x => x == null)); 18 | Assert.AreEqual(true, new string[] { "aaa", "bb", "c" }.ContainsNot(x => x == null)); 19 | Assert.AreEqual(false, new string[] { null, "aaa", "bb", "c" }.ContainsNot(x => x == null)); 20 | Assert.AreEqual(false, new string[] { "aaa", null, "bb", "c" }.ContainsNot(x => x == null)); 21 | Assert.AreEqual(false, new string[] { "aaa", "bb", null, "c" }.ContainsNot(x => x == null)); 22 | Assert.AreEqual(false, new string[] { "aaa", "bb", "c", null }.ContainsNot(x => x == null)); 23 | Assert.AreEqual(false, new string[] { null, null, null }.ContainsNot(x => x == null)); 24 | } 25 | 26 | [TestMethod] 27 | public void ContainsNotDuplicates() 28 | { 29 | Assert.AreEqual(true, (null as string[]).ContainsNotDuplicates()); 30 | Assert.AreEqual(true, new string[] { }.ContainsNotDuplicates()); 31 | Assert.AreEqual(true, new string[] { null }.ContainsNotDuplicates()); 32 | Assert.AreEqual(false, new string[] { null, null }.ContainsNotDuplicates()); 33 | Assert.AreEqual(true, new string[] { "aaa" }.ContainsNotDuplicates()); 34 | Assert.AreEqual(true, new string[] { "aaa", "bb", "c" }.ContainsNotDuplicates()); 35 | Assert.AreEqual(false, new string[] { "aaa", "bb", "aaa" }.ContainsNotDuplicates()); 36 | Assert.AreEqual(false, new string[] { null, "aaa", null, "c" }.ContainsNotDuplicates()); 37 | Assert.AreEqual(false, new string[] { null, null, null }.ContainsNotDuplicates()); 38 | } 39 | 40 | [TestMethod] 41 | public void ContainsNotNull() 42 | { 43 | Assert.AreEqual(true, (null as string[]).ContainsNotNull()); 44 | Assert.AreEqual(true, new string[] { }.ContainsNotNull()); 45 | Assert.AreEqual(false, new string[] { null }.ContainsNotNull()); 46 | Assert.AreEqual(true, new string[] { "aaa" }.ContainsNotNull()); 47 | Assert.AreEqual(true, new string[] { "aaa", "bb", "c" }.ContainsNotNull()); 48 | Assert.AreEqual(false, new string[] { null, "aaa", "bb", "c" }.ContainsNotNull()); 49 | Assert.AreEqual(false, new string[] { "aaa", null, "bb", "c" }.ContainsNotNull()); 50 | Assert.AreEqual(false, new string[] { "aaa", "bb", null, "c" }.ContainsNotNull()); 51 | Assert.AreEqual(false, new string[] { "aaa", "bb", "c", null }.ContainsNotNull()); 52 | Assert.AreEqual(false, new string[] { null, null, null }.ContainsNotNull()); 53 | } 54 | 55 | [TestMethod] 56 | public void ContainsNotOnlyNull() 57 | { 58 | Assert.AreEqual(true, (null as IEnumerable).ContainsNotOnlyNull()); 59 | Assert.AreEqual(true, new string[] { }.ContainsNotOnlyNull()); 60 | Assert.AreEqual(false, new string[] { null }.ContainsNotOnlyNull()); 61 | Assert.AreEqual(true, new string[] { "aaa" }.ContainsNotOnlyNull()); 62 | Assert.AreEqual(true, new string[] { "aaa", "bb", "c" }.ContainsNotOnlyNull()); 63 | Assert.AreEqual(true, new string[] { null, "aaa", "bb", "c" }.ContainsNotOnlyNull()); 64 | Assert.AreEqual(true, new string[] { "aaa", null, "bb", "c" }.ContainsNotOnlyNull()); 65 | Assert.AreEqual(true, new string[] { "aaa", "bb", null, "c" }.ContainsNotOnlyNull()); 66 | Assert.AreEqual(true, new string[] { "aaa", "bb", "c", null }.ContainsNotOnlyNull()); 67 | Assert.AreEqual(false, new string[] { null, null, null }.ContainsNotOnlyNull()); 68 | } 69 | 70 | [TestMethod] 71 | public void IsNotEmpty() 72 | { 73 | Assert.AreEqual(false, "".IsNotEmpty()); 74 | Assert.AreEqual(true, "a".IsNotEmpty()); 75 | Assert.AreEqual(true, "ab".IsNotEmpty()); 76 | Assert.AreEqual(true, "abc".IsNotEmpty()); 77 | Assert.AreEqual(false, new int[] { }.IsNotEmpty()); 78 | Assert.AreEqual(true, new int[] { 1 }.IsNotEmpty()); 79 | Assert.AreEqual(true, (null as IEnumerable).IsNotEmpty()); 80 | } 81 | 82 | [DataRow("", "", false, false)] 83 | [DataRow(" ", " ", false, false)] 84 | [DataRow(" ", " ", false, true)] 85 | [DataRow(" ", " \\t", false, true)] 86 | [DataRow("a", "a", false, false)] 87 | [DataRow("aa", "aa", false, false)] 88 | [DataRow("aa", "a", false, true)] 89 | [DataRow("ab", "ab", false, false)] 90 | [DataRow("ab", "ba", true, false)] 91 | [DataRow("ab", "ba", false, true)] 92 | [DataRow("abc", "abc", true, false)] 93 | [DataRow("abc", "abc", false, false)] 94 | [DataRow("abc", "bac", true, false)] 95 | [DataRow("abc", "acb", true, false)] 96 | [DataRow("abd", "cba", true, true)] 97 | [DataRow("abc", "bcad", true, true)] 98 | [DataRow("", null, false, true)] 99 | [DataRow(null, "", false, true)] 100 | [DataRow(null, null, false, false)] 101 | [DataRow(null, null, true, false)] 102 | [DataTestMethod] 103 | public void IsNotEqualTo(IEnumerable value1, IEnumerable value2, bool ignoreOrder, bool expected) 104 | { 105 | Assert.AreEqual(expected, value1.IsNotEqualTo(value2, ignoreOrder)); 106 | } 107 | 108 | [DataRow(new string[] { null }, new string[] { null }, true, false)] 109 | [DataRow(new string[] { null }, new string[] { null }, false, false)] 110 | [DataRow(new string[] { null, null }, new string[] { null, null }, true, false)] 111 | [DataRow(new string[] { null, null }, new string[] { null, null }, false, false)] 112 | [DataRow(new string[] { null, "aaa" }, new string[] { null, "aaa" }, false, false)] 113 | [DataRow(new string[] { null, "aaa" }, new string[] { "aaa", null }, true, false)] 114 | [DataRow(new string[] { "bbb", "aaa" }, new string[] { "bbb", "aaa" }, true, false)] 115 | [DataRow(new string[] { "bbb", "aaa" }, new string[] { "aaa", "bbb" }, true, false)] 116 | [DataTestMethod] 117 | public void IsNotEqualTo3(IEnumerable value1, IEnumerable value2, bool ignoreOrder, bool expected) 118 | { 119 | Assert.AreEqual(expected, value1.IsNotEqualTo(value2, ignoreOrder)); 120 | } 121 | 122 | [DataRow(new int[0], new int[0], false, false)] 123 | [DataRow(new int[] { 1 }, new int[] { 1 }, true, false)] 124 | [DataRow(new int[] { 1, 1 }, new int[] { 1, 1 }, false, false)] 125 | [DataRow(new int[] { 1 }, new int[] { 1, 1 }, true, true)] 126 | [DataRow(new int[] { 0, 9, 8, 7, 6, 5, 4, 3, 2, 1 }, new int[] { 7, 8, 9, 6, 5, 4, 1, 0, 2, 3 }, true, false)] 127 | [DataTestMethod] 128 | public void IsNotEqualTo4(IEnumerable value1, IEnumerable value2, bool ignoreOrder, bool expected) 129 | { 130 | Assert.AreEqual(expected, value1.IsNotEqualTo(value2, ignoreOrder)); 131 | } 132 | 133 | [DataRow(null, false)] 134 | [DataRow("", false)] 135 | [DataRow("a", true)] 136 | [DataRow(new int[] { }, false)] 137 | [DataRow(new int[] { 1 }, true)] 138 | [DataRow(new int[] { 1, 2 }, true)] 139 | [DataRow(new int[] { 1, 2, 3 }, true)] 140 | [DataTestMethod] 141 | public void IsNotNullOrEmpty(IEnumerable value, bool expected) 142 | { 143 | Assert.AreEqual(expected, value.IsNotNullOrEmpty()); 144 | } 145 | 146 | [DataRow(3, new int[] { }, true)] 147 | [DataRow(3, new int[] { 3 }, false)] 148 | [DataRow(3, new int[] { 2 }, true)] 149 | [DataRow(3, new int[] { 2, 3 }, false)] 150 | [DataRow(3, new int[] { 1, 2, 3, 4 }, false)] 151 | [DataRow(3, new int[] { 4, 3, 2, 1 }, false)] 152 | [DataRow(3, new int[] { 1, 1, 1, 1, 3, 1, 1, 1 }, false)] 153 | [DataRow(3, new int[] { 3, 3, 3, 3, 3, 3, 3 }, false)] 154 | [DataRow(3, new int[] { 4, 4, 4, 4, 4, 4, 4, 4 }, true)] 155 | [DataTestMethod] 156 | public void IsNotOneOf(int value, IEnumerable set, bool expected) 157 | { 158 | Assert.AreEqual(expected, value.IsNotOneOf(set)); 159 | Assert.AreEqual(expected, value.IsNotOneOf(set.ToArray())); 160 | } 161 | 162 | [DataRow("", new string[] { }, true)] 163 | [DataRow("", new string[] { "" }, false)] 164 | [DataRow("a", new string[] { "a" }, false)] 165 | [DataRow("a", new string[] { "a", "b" }, false)] 166 | [DataRow("a", new string[] { "ab", "ba" }, true)] 167 | [DataRow("a", new string[] { "ab", "A" }, true)] 168 | [DataRow("a", new string[] { "ab", "a" }, false)] 169 | [DataRow("a", new string[] { "a", "a", "a", "a", "a", "a", "a" }, false)] 170 | [DataRow("a", new string[] { "b", "b", "b", "b", "b", "b", "b" }, true)] 171 | [DataRow(null, new string[] { "" }, true)] 172 | [DataRow(null, new string[] { null }, false)] 173 | [DataTestMethod] 174 | public void IsNotOneOf2(string value, IEnumerable set, bool expected) 175 | { 176 | Assert.AreEqual(expected, value.IsNotOneOf(set)); 177 | } 178 | 179 | [DataRow(null, null)] 180 | [DataRow("a", null)] 181 | [DataTestMethod] 182 | public void IsNotOneOfFail(string value, IEnumerable set) 183 | { 184 | try 185 | { 186 | value.IsNotOneOf(set); 187 | 188 | Assert.Fail(); 189 | } 190 | catch (ArgumentException ex) 191 | { 192 | Assert.AreEqual("Value cannot be null.", ex.Message); 193 | } 194 | } 195 | 196 | #endregion Public Methods 197 | } 198 | -------------------------------------------------------------------------------- /Test/DefensiveProgrammingFramework.Test/CollectionThenExtensionsTest.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace DefensiveProgrammingFramework.Test; 4 | 5 | [TestClass] 6 | public class CollectionThenExtensionsTest 7 | { 8 | #region Public Methods 9 | 10 | [TestMethod] 11 | public void Then() 12 | { 13 | true.Then(() => Assert.AreEqual(true, true)); 14 | false.Then(Assert.Fail); 15 | } 16 | 17 | [TestMethod] 18 | public void ThenFail() 19 | { 20 | try 21 | { 22 | true.Then(null); 23 | 24 | Assert.Fail(); 25 | } 26 | catch (ArgumentException ex) 27 | { 28 | Assert.AreEqual("Value cannot be null.", ex.Message); 29 | } 30 | } 31 | 32 | #endregion Public Methods 33 | } 34 | -------------------------------------------------------------------------------- /Test/DefensiveProgrammingFramework.Test/DefensiveProgrammingFramework.Test.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | net8.0 5 | enable 6 | disable 7 | 8 | false 9 | true 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /Test/DefensiveProgrammingFramework.Test/FileCannotExtensionsTest.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace DefensiveProgrammingFramework.Test; 4 | 5 | [TestClass] 6 | public class CannotExtensionsTest 7 | { 8 | #region Public Methods 9 | 10 | [DataRow(".")] 11 | [DataRow(@".\bin\Debug")] 12 | [DataTestMethod] 13 | public void CannotAbsoluteDirectoryPath(string filePath) 14 | { 15 | Assert.AreEqual(filePath, filePath.CannotBeAbsoluteDirectoryPath()); 16 | } 17 | 18 | [DataRow(@"c:\")] 19 | [DataRow(@"c:\apps\MyApp\bin\Debug")] 20 | [DataRow(@"d:\apps\MyApp\bin\Debug")] 21 | [DataRow(@"e:\apps\MyApp\bin\Debug")] 22 | [DataTestMethod] 23 | public void CannotAbsoluteDirectoryPathFail(string directoryPath) 24 | { 25 | try 26 | { 27 | directoryPath.CannotBeAbsoluteDirectoryPath(); 28 | 29 | Assert.Fail(); 30 | } 31 | catch (ArgumentException ex) 32 | { 33 | Assert.AreEqual("Value cannot be an absolute directory path.", ex.Message); 34 | } 35 | 36 | try 37 | { 38 | directoryPath.CannotBeAbsoluteDirectoryPath(() => throw new InvalidOperationException("Test.")); 39 | 40 | Assert.Fail(); 41 | } 42 | catch (InvalidOperationException ex) 43 | { 44 | Assert.AreEqual("Test.", ex.Message); 45 | } 46 | } 47 | 48 | [DataRow(@".\Tem|p")] 49 | [DataTestMethod] 50 | public void CannotAbsoluteDirectoryPathFail2(string directoryPath) 51 | { 52 | try 53 | { 54 | directoryPath.CannotBeAbsoluteDirectoryPath(); 55 | 56 | Assert.Fail(); 57 | } 58 | catch (ArgumentException ex) 59 | { 60 | Assert.AreEqual("Value must be a valid directory path.", ex.Message); 61 | } 62 | } 63 | 64 | [DataRow(@".\file.exe")] 65 | [DataRow(@".\bin\Debug\file.exe")] 66 | [DataTestMethod] 67 | public void CannotBeAbsoluteFilePath(string filePath) 68 | { 69 | Assert.AreEqual(filePath, filePath.CannotBeAbsoluteFilePath()); 70 | } 71 | 72 | [DataRow(@"c:\file.exe")] 73 | [DataRow(@"c:\apps\MyApp\bin\Debug\file.exe")] 74 | [DataRow(@"d:\apps\MyApp\bin\Debug\file.exe")] 75 | [DataRow(@"e:\apps\MyApp\bin\Debug\file.exe")] 76 | [DataTestMethod] 77 | public void CannotBeAbsoluteFilePathFail(string filePath) 78 | { 79 | try 80 | { 81 | filePath.CannotBeAbsoluteFilePath(); 82 | 83 | Assert.Fail(); 84 | } 85 | catch (ArgumentException ex) 86 | { 87 | Assert.AreEqual("Value cannot be an absolute file path.", ex.Message); 88 | } 89 | 90 | try 91 | { 92 | filePath.CannotBeAbsoluteFilePath(() => throw new InvalidOperationException("Test.")); 93 | 94 | Assert.Fail(); 95 | } 96 | catch (InvalidOperationException ex) 97 | { 98 | Assert.AreEqual("Test.", ex.Message); 99 | } 100 | } 101 | 102 | [DataRow(@"c:\fil|e.exe")] 103 | [DataRow(@"e:\apps\M|yApp\bin\Debug\file.exe")] 104 | [DataTestMethod] 105 | public void CannotBeAbsoluteFilePathFail2(string filePath) 106 | { 107 | try 108 | { 109 | filePath.CannotBeAbsoluteFilePath(); 110 | 111 | Assert.Fail(); 112 | } 113 | catch (ArgumentException ex) 114 | { 115 | Assert.AreEqual("Value must be a valid file path.", ex.Message); 116 | } 117 | } 118 | 119 | [TestMethod] 120 | public void CannotBeEmptyDirectory() 121 | { 122 | var directoryPath = @".\Tmp5"; 123 | var filePath = Path.Combine(directoryPath, "tmp.txt"); 124 | 125 | if (!Directory.Exists(directoryPath)) 126 | { 127 | Directory.CreateDirectory(directoryPath); 128 | File.WriteAllText(filePath, "text"); 129 | } 130 | 131 | Assert.AreEqual(directoryPath, directoryPath.CannotBeEmptyDirectory()); 132 | 133 | File.Delete(filePath); 134 | Directory.Delete(directoryPath); 135 | } 136 | 137 | [TestMethod] 138 | public void CannotBeEmptyDirectoryFail() 139 | { 140 | var directoryPath = @".\Tmp5"; 141 | 142 | if (!Directory.Exists(directoryPath)) 143 | { 144 | Directory.CreateDirectory(directoryPath); 145 | } 146 | 147 | try 148 | { 149 | directoryPath.CannotBeEmptyDirectory(); 150 | 151 | Assert.Fail(); 152 | } 153 | catch (ArgumentException ex) 154 | { 155 | Assert.AreEqual("Value cannot be an empty directory.", ex.Message); 156 | } 157 | 158 | try 159 | { 160 | directoryPath.CannotBeEmptyDirectory(() => throw new InvalidOperationException("Test.")); 161 | 162 | Assert.Fail(); 163 | } 164 | catch (InvalidOperationException ex) 165 | { 166 | Assert.AreEqual("Test.", ex.Message); 167 | } 168 | 169 | Directory.Delete(directoryPath); 170 | } 171 | 172 | [TestMethod] 173 | public void CannotBeEmptyDirectoryFail2() 174 | { 175 | var directoryPath = @".\Tmp5<"; 176 | 177 | try 178 | { 179 | directoryPath.CannotBeEmptyDirectory(); 180 | 181 | Assert.Fail(); 182 | } 183 | catch (ArgumentException ex) 184 | { 185 | Assert.AreEqual("Value cannot be an empty directory.", ex.Message); 186 | } 187 | } 188 | 189 | [DataRow("")] 190 | [DataRow("file\t1.exe")] 191 | [DataRow("file|1.ex'")] 192 | [DataTestMethod] 193 | public void CannotBeValidDirectoryPath(string filePath) 194 | { 195 | Assert.AreEqual(filePath, filePath.CannotBeValidDirectoryPath()); 196 | } 197 | 198 | [DataRow(null)] 199 | [DataRow("file")] 200 | [DataRow("file.exe")] 201 | [DataRow(@"C:\Users\User1\source\repos\WpfApp1\WpfApp1\bin\Debug")] 202 | [DataRow(@"C:\Users\User1\source\repos\WpfApp1\WpfApp1\bin\Debug\")] 203 | [DataRow(@"C:\Users\User1\source\repos\WpfApp1\WpfApp1\bin\Debug\file.exe")] 204 | [DataRow("exe.file")] 205 | [DataRow("exe.sdfsfsdfsdfsdf")] 206 | [DataTestMethod] 207 | public void CannotBeValidDirectoryPathFail(string directoryPath) 208 | { 209 | try 210 | { 211 | directoryPath.CannotBeValidDirectoryPath(); 212 | 213 | Assert.Fail(); 214 | } 215 | catch (ArgumentException ex) 216 | { 217 | Assert.AreEqual("Value cannot be a valid directory path.", ex.Message); 218 | } 219 | 220 | try 221 | { 222 | directoryPath.CannotBeValidDirectoryPath(() => throw new InvalidOperationException("Test.")); 223 | 224 | Assert.Fail(); 225 | } 226 | catch (InvalidOperationException ex) 227 | { 228 | Assert.AreEqual("Test.", ex.Message); 229 | } 230 | } 231 | 232 | [DataRow("")] 233 | [DataRow("file>1.exe")] 234 | [DataRow("file?1.ex'")] 235 | [DataTestMethod] 236 | public void CannotBeValidFileName(string filePath) 237 | { 238 | Assert.AreEqual(filePath, filePath.CannotBeValidFileName()); 239 | } 240 | 241 | [DataRow(null)] 242 | [DataRow("f")] 243 | [DataRow("file")] 244 | [DataRow("file.exe")] 245 | [DataRow("exe.file")] 246 | [DataRow("exe.sdfsfsdfsdfsdf")] 247 | [DataTestMethod] 248 | public void CannotBeValidFileNameFail(string fileNme) 249 | { 250 | try 251 | { 252 | fileNme.CannotBeValidFileName(); 253 | 254 | Assert.Fail(); 255 | } 256 | catch (ArgumentException ex) 257 | { 258 | Assert.AreEqual("Value cannot be a valid file name.", ex.Message); 259 | } 260 | 261 | try 262 | { 263 | fileNme.CannotBeValidFileName(() => throw new InvalidOperationException("Test.")); 264 | 265 | Assert.Fail(); 266 | } 267 | catch (InvalidOperationException ex) 268 | { 269 | Assert.AreEqual("Test.", ex.Message); 270 | } 271 | } 272 | 273 | [DataRow("")] 274 | [DataRow("file\t1.exe")] 275 | [DataRow("file|1.ex'")] 276 | [DataTestMethod] 277 | public void CannotBeValidFilePath(string filePath) 278 | { 279 | Assert.AreEqual(filePath, filePath.CannotBeValidFilePath()); 280 | } 281 | 282 | [DataRow(null)] 283 | [DataRow("f")] 284 | [DataRow("file")] 285 | [DataRow("file.exe")] 286 | [DataRow("exe.file")] 287 | [DataRow("exe.sdfsfsdfsdfsdf")] 288 | [DataTestMethod] 289 | public void CannotBeValidFilePathFail(string filePath) 290 | { 291 | try 292 | { 293 | filePath.CannotBeValidFilePath(); 294 | 295 | Assert.Fail(); 296 | } 297 | catch (ArgumentException ex) 298 | { 299 | Assert.AreEqual("Value cannot be a valid file path.", ex.Message); 300 | } 301 | 302 | try 303 | { 304 | filePath.CannotBeValidFilePath(() => throw new InvalidOperationException("Test.")); 305 | 306 | Assert.Fail(); 307 | } 308 | catch (InvalidOperationException ex) 309 | { 310 | Assert.AreEqual("Test.", ex.Message); 311 | } 312 | } 313 | 314 | [DataTestMethod] 315 | public void CannotDirectoryExist() 316 | { 317 | if (!Directory.Exists(@".\Temp")) 318 | { 319 | Directory.CreateDirectory(@".\Temp"); 320 | } 321 | 322 | try 323 | { 324 | @".\Temp".CannotDirectoryExist(); 325 | 326 | Assert.Fail(); 327 | } 328 | catch (ArgumentException ex) 329 | { 330 | Assert.AreEqual("Directory cannot exist.", ex.Message); 331 | } 332 | 333 | try 334 | { 335 | @".\Temp".CannotDirectoryExist(() => throw new InvalidOperationException("Test.")); 336 | 337 | Assert.Fail(); 338 | } 339 | catch (InvalidOperationException ex) 340 | { 341 | Assert.AreEqual("Test.", ex.Message); 342 | } 343 | 344 | if (Directory.Exists(@".\Temp")) 345 | { 346 | Directory.Delete(@".\Temp"); 347 | } 348 | 349 | Assert.AreEqual(@".\Temp", @".\Temp".CannotDirectoryExist()); 350 | } 351 | 352 | [TestMethod] 353 | public void CannotFileExist() 354 | { 355 | if (!File.Exists(@".\Temp.txt")) 356 | { 357 | File.WriteAllText(@".\Temp.txt", "tmp"); 358 | } 359 | 360 | try 361 | { 362 | @".\Temp.txt".CannotFileExist(); 363 | 364 | Assert.Fail(); 365 | } 366 | catch (ArgumentException ex) 367 | { 368 | Assert.AreEqual("File cannot exist.", ex.Message); 369 | } 370 | 371 | try 372 | { 373 | @".\Temp.txt".CannotFileExist(() => throw new InvalidOperationException("Test.")); 374 | 375 | Assert.Fail(); 376 | } 377 | catch (InvalidOperationException ex) 378 | { 379 | Assert.AreEqual("Test.", ex.Message); 380 | } 381 | 382 | if (File.Exists(@".\Temp.txt")) 383 | { 384 | File.Delete(@".\Temp.txt"); 385 | } 386 | 387 | Assert.AreEqual(@".\Temp.txt", @".\Temp.txt".CannotFileExist()); 388 | } 389 | 390 | #endregion Public Methods 391 | } -------------------------------------------------------------------------------- /Test/DefensiveProgrammingFramework.Test/FileIsExtensionsTest.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace DefensiveProgrammingFramework.Test; 4 | 5 | [TestClass] 6 | public class FileIsExtensionsTest 7 | { 8 | #region Public Methods 9 | 10 | [TestMethod] 11 | public void DoesDirectoryExist() 12 | { 13 | if (!Directory.Exists(@".\Temp")) 14 | { 15 | Directory.CreateDirectory(@".\Temp"); 16 | } 17 | 18 | Assert.AreEqual(true, @".\Temp".DoesDirectoryExist()); 19 | 20 | if (Directory.Exists(@".\Temp")) 21 | { 22 | Directory.Delete(@".\Temp"); 23 | } 24 | 25 | Assert.AreEqual(false, @".\Temp".DoesDirectoryExist()); 26 | } 27 | 28 | [TestMethod] 29 | public void DoesFileExist() 30 | { 31 | if (!File.Exists(@".\Temp.txt")) 32 | { 33 | File.WriteAllText(@".\Temp.txt", "tmp"); 34 | } 35 | 36 | Assert.AreEqual(true, @".\Temp.txt".DoesFileExist()); 37 | 38 | if (File.Exists(@".\Temp.txt")) 39 | { 40 | File.Delete(@".\Temp.txt"); 41 | } 42 | 43 | Assert.AreEqual(false, @".\Temp.txt".DoesFileExist()); 44 | } 45 | 46 | [DataRow(null, true)] 47 | [DataRow(".", false)] 48 | [DataRow(@".\bin\Debug", false)] 49 | [DataRow(@"c:\", true)] 50 | [DataRow(@"c:", false)] 51 | [DataRow(@"c:\apps\MyApp\bin\Debug", true)] 52 | [DataRow(@"d:\apps\MyApp\bin\Debug", true)] 53 | [DataRow(@"e:\apps\MyApp\bin\Debug", true)] 54 | [DataTestMethod] 55 | public void IsAbsoluteDirectoryPath(string filePath, bool expected) 56 | { 57 | Assert.AreEqual(expected, filePath.IsAbsoluteDirectoryPath()); 58 | } 59 | 60 | [DataRow("")] 61 | [DataRow(@"e:\apps\M|yApp\bin\Debug")] 62 | [DataTestMethod] 63 | public void IsAbsoluteDirectoryPathFail(string filePath) 64 | { 65 | try 66 | { 67 | filePath.IsAbsoluteDirectoryPath(); 68 | 69 | Assert.Fail(); 70 | } 71 | catch (ArgumentException ex) 72 | { 73 | Assert.AreEqual("Value must be a valid directory path.", ex.Message); 74 | } 75 | } 76 | 77 | [DataRow(null, true)] 78 | [DataRow(@".\file.exe", false)] 79 | [DataRow(@".\bin\Debug\file.exe", false)] 80 | [DataRow(@"c:\file.exe", true)] 81 | [DataRow(@"c:\apps\MyApp\bin\Debug\file.exe", true)] 82 | [DataRow(@"d:\apps\MyApp\bin\Debug\file.exe", true)] 83 | [DataRow(@"e:\apps\MyApp\bin\Debug\file.exe", true)] 84 | [DataTestMethod] 85 | public void IsAbsoluteFilePath(string filePath, bool expected) 86 | { 87 | Assert.AreEqual(expected, filePath.IsAbsoluteFilePath()); 88 | } 89 | 90 | [DataRow(@"")] 91 | [DataRow(@"e:\apps\M|yApp\bin\Debug\file.exe")] 92 | [DataTestMethod] 93 | public void IsAbsoluteFilePathFail(string filePath) 94 | { 95 | try 96 | { 97 | filePath.IsAbsoluteFilePath(); 98 | 99 | Assert.Fail(); 100 | } 101 | catch (ArgumentException ex) 102 | { 103 | Assert.AreEqual("Value must be a valid file path.", ex.Message); 104 | } 105 | } 106 | 107 | [TestMethod] 108 | public void IsEmptyDirectory() 109 | { 110 | var directoryPath = @".\Tmp5"; 111 | var filePath = Path.Combine(directoryPath, "tmp.txt"); 112 | 113 | if (!Directory.Exists(directoryPath)) 114 | { 115 | Directory.CreateDirectory(directoryPath); 116 | } 117 | 118 | Assert.IsTrue((null as string).IsEmptyDirectory()); 119 | Assert.IsFalse(string.Empty.IsEmptyDirectory()); 120 | Assert.IsTrue(@".\aaa".IsEmptyDirectory()); 121 | Assert.IsTrue(directoryPath.IsEmptyDirectory()); 122 | 123 | File.WriteAllText(filePath, "text"); 124 | 125 | Assert.IsFalse(directoryPath.IsEmptyDirectory()); 126 | 127 | File.Delete(filePath); 128 | Directory.Delete(directoryPath); 129 | } 130 | 131 | [DataRow(null, true)] 132 | [DataRow("", false)] 133 | [DataRow("f", true)] 134 | [DataRow("file", true)] 135 | [DataRow("file.exe", true)] 136 | [DataRow(@"C:\Users\User1\source\repos\WpfApp1\WpfApp1\bin\Debug", true)] 137 | [DataRow(@"C:\Users\User1\source\repos\WpfApp1\WpfApp1\bin\Debug\", true)] 138 | [DataRow(@"C:\Users\User1\source\repos\WpfApp1\WpfApp1\bin\Debug\file.exe", true)] 139 | [DataRow("exe.file", true)] 140 | [DataRow("exe.sdfsfsdfsdfsdf", true)] 141 | [DataRow("file\t1.exe", false)] 142 | [DataRow("file|1.ex'", false)] 143 | [DataTestMethod] 144 | public void IsValidDirectoryPath(string filePath, bool expected) 145 | { 146 | Assert.AreEqual(expected, filePath.IsValidDirectoryPath()); 147 | } 148 | 149 | [DataRow(null, true)] 150 | [DataRow("", false)] 151 | [DataRow("f", true)] 152 | [DataRow("file", true)] 153 | [DataRow("file.exe", true)] 154 | [DataRow("exe.file", true)] 155 | [DataRow("exe.sdfsfsdfsdfsdf", true)] 156 | [DataRow("file>1.exe", false)] 157 | [DataRow("file?1.ex'", false)] 158 | [DataTestMethod] 159 | public void IsValidFileName(string filePath, bool expected) 160 | { 161 | Assert.AreEqual(expected, filePath.IsValidFileName()); 162 | } 163 | 164 | [DataRow(null, true)] 165 | [DataRow("", false)] 166 | [DataRow("f", true)] 167 | [DataRow("file", true)] 168 | [DataRow("file.exe", true)] 169 | [DataRow("exe.file", true)] 170 | [DataRow(@"C:\Users\User1\source\repos\WpfApp1\WpfApp1\bin\Debug\file.exe", true)] 171 | [DataRow("exe.sdfsfsdfsdfsdf", true)] 172 | [DataRow("file\t1.exe", false)] 173 | [DataRow("file|1.ex'", false)] 174 | [DataTestMethod] 175 | public void IsValidFilePath(string filePath, bool expected) 176 | { 177 | Assert.AreEqual(expected, filePath.IsValidFilePath()); 178 | } 179 | 180 | #endregion Public Methods 181 | } -------------------------------------------------------------------------------- /Test/DefensiveProgrammingFramework.Test/FileIsNotExtensionsTest.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace DefensiveProgrammingFramework.Test; 4 | 5 | [TestClass] 6 | public class FileIsNotExtensionsTest 7 | { 8 | #region Public Methods 9 | 10 | [TestMethod] 11 | public void IsNotEmptyDirectory() 12 | { 13 | var directoryPath = @".\Tmp5"; 14 | var filePath = Path.Combine(directoryPath, "tmp.txt"); 15 | 16 | if (!Directory.Exists(directoryPath)) 17 | { 18 | Directory.CreateDirectory(directoryPath); 19 | } 20 | 21 | Assert.IsFalse(directoryPath.IsNotEmptyDirectory()); 22 | 23 | File.WriteAllText(filePath, "text"); 24 | 25 | Assert.IsTrue(directoryPath.IsNotEmptyDirectory()); 26 | 27 | File.Delete(filePath); 28 | Directory.Delete(directoryPath); 29 | } 30 | 31 | [DataRow(null, !true)] 32 | [DataRow("", !false)] 33 | [DataRow("f", !true)] 34 | [DataRow("file", !true)] 35 | [DataRow("file.exe", !true)] 36 | [DataRow("exe.file", !true)] 37 | [DataRow("exe.sdfsfsdfsdfsdf", !true)] 38 | [DataRow("file>1.exe", !false)] 39 | [DataRow("file?1.ex'", !false)] 40 | [DataTestMethod] 41 | public void IsNotValidFileName(string filePath, bool expected) 42 | { 43 | Assert.AreEqual(expected, filePath.IsNotValidFileName()); 44 | } 45 | 46 | [DataRow(null, !true)] 47 | [DataRow("", !false)] 48 | [DataRow("f", !true)] 49 | [DataRow("file", !true)] 50 | [DataRow("file.exe", !true)] 51 | [DataRow("exe.file", !true)] 52 | [DataRow(@"C:\Users\User1\source\repos\WpfApp1\WpfApp1\bin\Debug\file.exe", !true)] 53 | [DataRow("exe.sdfsfsdfsdfsdf", !true)] 54 | [DataRow("file\t1.exe", !false)] 55 | [DataRow("file|1.ex'", !false)] 56 | [DataTestMethod] 57 | public void IsNotValidFilePath(string filePath, bool expected) 58 | { 59 | Assert.AreEqual(expected, filePath.IsNotValidFilePath()); 60 | } 61 | 62 | [DataRow(null, !true)] 63 | [DataRow("", !false)] 64 | [DataRow("f", !true)] 65 | [DataRow("file", !true)] 66 | [DataRow("file.exe", !true)] 67 | [DataRow(@"C:\Users\User1\source\repos\WpfApp1\WpfApp1\bin\Debug", !true)] 68 | [DataRow(@"C:\Users\User1\source\repos\WpfApp1\WpfApp1\bin\Debug\", !true)] 69 | [DataRow(@"C:\Users\User1\source\repos\WpfApp1\WpfApp1\bin\Debug\file.exe", !true)] 70 | [DataRow("exe.file", !true)] 71 | [DataRow("exe.sdfsfsdfsdfsdf", !true)] 72 | [DataRow("file\t1.exe", !false)] 73 | [DataRow("file|1.ex'", !false)] 74 | [DataTestMethod] 75 | public void IsNotValidDirectoryPath(string filePath, bool expected) 76 | { 77 | Assert.AreEqual(expected, filePath.IsNotValidDirectoryPath()); 78 | } 79 | 80 | [DataRow(".", !false)] 81 | [DataRow(@".\bin\Debug", !false)] 82 | [DataRow(@"c:\", !true)] 83 | [DataRow(@"c:", !false)] 84 | [DataRow(@"c:\apps\MyApp\bin\Debug", !true)] 85 | [DataRow(@"d:\apps\MyApp\bin\Debug", !true)] 86 | [DataRow(@"e:\apps\MyApp\bin\Debug", !true)] 87 | [DataTestMethod] 88 | public void IsNotAbsoluteDirectoryPath(string filePath, bool expected) 89 | { 90 | Assert.AreEqual(expected, filePath.IsNotAbsoluteDirectoryPath()); 91 | } 92 | 93 | [DataRow(@"e:\apps\M|yApp\bin\Debug\file.exe")] 94 | [DataTestMethod] 95 | public void IsNotAbsoluteFilePathFail(string filePath) 96 | { 97 | try 98 | { 99 | filePath.IsNotAbsoluteFilePath(); 100 | 101 | Assert.Fail(); 102 | } 103 | catch (ArgumentException ex) 104 | { 105 | Assert.AreEqual("Value must be a valid file path.", ex.Message); 106 | } 107 | } 108 | 109 | [DataRow(@"e:\apps\M|yApp\bin\Debug")] 110 | [DataTestMethod] 111 | public void IsNotAbsoluteDirectoryPathFail(string filePath) 112 | { 113 | try 114 | { 115 | filePath.IsNotAbsoluteDirectoryPath(); 116 | 117 | Assert.Fail(); 118 | } 119 | catch (ArgumentException ex) 120 | { 121 | Assert.AreEqual("Value must be a valid directory path.", ex.Message); 122 | } 123 | } 124 | 125 | [DataRow(@".\file.exe", !false)] 126 | [DataRow(@".\bin\Debug\file.exe", !false)] 127 | [DataRow(@"c:\file.exe", !true)] 128 | [DataRow(@"c:\apps\MyApp\bin\Debug\file.exe", !true)] 129 | [DataRow(@"d:\apps\MyApp\bin\Debug\file.exe", !true)] 130 | [DataRow(@"e:\apps\MyApp\bin\Debug\file.exe", !true)] 131 | [DataTestMethod] 132 | public void IsNotAbsoluteFilePath(string filePath, bool expected) 133 | { 134 | Assert.AreEqual(expected, filePath.IsNotAbsoluteFilePath()); 135 | } 136 | 137 | [DataTestMethod] 138 | public void DoesNotDirectoryExist() 139 | { 140 | if (!Directory.Exists(@".\Temp")) 141 | { 142 | Directory.CreateDirectory(@".\Temp"); 143 | } 144 | 145 | Assert.AreEqual(!true, @".\Temp".DoesNotDirectoryExist()); 146 | 147 | if (Directory.Exists(@".\Temp")) 148 | { 149 | Directory.Delete(@".\Temp"); 150 | } 151 | 152 | Assert.AreEqual(!false, @".\Temp".DoesNotDirectoryExist()); 153 | } 154 | 155 | [DataTestMethod] 156 | public void DoesNotFileExist() 157 | { 158 | if (!File.Exists(@".\Temp.txt")) 159 | { 160 | File.WriteAllText(@".\Temp.txt", "tmp"); 161 | } 162 | 163 | Assert.AreEqual(!true, @".\Temp.txt".DoesNotFileExist()); 164 | 165 | if (File.Exists(@".\Temp.txt")) 166 | { 167 | File.Delete(@".\Temp.txt"); 168 | } 169 | 170 | Assert.AreEqual(!false, @".\Temp.txt".DoesNotFileExist()); 171 | } 172 | 173 | #endregion Public Methods 174 | } -------------------------------------------------------------------------------- /Test/DefensiveProgrammingFramework.Test/FileMustExtensionsTest.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace DefensiveProgrammingFramework.Test; 4 | 5 | [TestClass] 6 | public class MustExtensionsTest 7 | { 8 | #region Public Methods 9 | 10 | [TestMethod] 11 | public void MustBeEmptyDirectory() 12 | { 13 | var directoryPath = @".\Tmp5"; 14 | 15 | if (!Directory.Exists(directoryPath)) 16 | { 17 | Directory.CreateDirectory(directoryPath); 18 | } 19 | 20 | Assert.AreEqual(directoryPath, directoryPath.MustBeEmptyDirectory()); 21 | 22 | Directory.Delete(directoryPath); 23 | } 24 | 25 | [TestMethod] 26 | public void MustBeEmptyDirectoryFail() 27 | { 28 | var directoryPath = @".\Tmp5"; 29 | var filePath = Path.Combine(directoryPath, "tmp.txt"); 30 | 31 | if (!Directory.Exists(directoryPath)) 32 | { 33 | Directory.CreateDirectory(directoryPath); 34 | File.WriteAllText(filePath, "text"); 35 | } 36 | 37 | try 38 | { 39 | directoryPath.MustBeEmptyDirectory(); 40 | 41 | Assert.Fail(); 42 | } 43 | catch (ArgumentException ex) 44 | { 45 | Assert.AreEqual("Value must be an empty directory.", ex.Message); 46 | } 47 | 48 | try 49 | { 50 | directoryPath.MustBeEmptyDirectory(() => throw new InvalidOperationException("Test.")); 51 | 52 | Assert.Fail(); 53 | } 54 | catch (InvalidOperationException ex) 55 | { 56 | Assert.AreEqual("Test.", ex.Message); 57 | } 58 | 59 | File.Delete(filePath); 60 | Directory.Delete(directoryPath); 61 | } 62 | 63 | [TestMethod] 64 | public void MustBeEmptyDirectoryFail2() 65 | { 66 | var directoryPath = @".\Tmp5|"; 67 | 68 | try 69 | { 70 | directoryPath.MustBeEmptyDirectory(); 71 | 72 | Assert.Fail(); 73 | } 74 | catch (ArgumentException ex) 75 | { 76 | Assert.AreEqual("Value must be a valid directory path.", ex.Message); 77 | } 78 | } 79 | 80 | [DataRow(@"c:\")] 81 | [DataRow(@"c:\apps\MyApp\bin\Debug")] 82 | [DataRow(@"d:\apps\MyApp\bin\Debug")] 83 | [DataRow(@"e:\apps\MyApp\bin\Debug")] 84 | [DataTestMethod] 85 | public void MustAbsoluteDirectoryPath(string directoryPath) 86 | { 87 | Assert.AreEqual(directoryPath, directoryPath.MustBeAbsoluteDirectoryPath()); 88 | } 89 | 90 | [DataRow(".")] 91 | [DataRow(@".\bin\Debug")] 92 | [DataTestMethod] 93 | public void MustAbsoluteDirectoryPathFail(string directoryPath) 94 | { 95 | try 96 | { 97 | directoryPath.MustBeAbsoluteDirectoryPath(); 98 | 99 | Assert.Fail(); 100 | } 101 | catch (ArgumentException ex) 102 | { 103 | Assert.AreEqual("Value must be an absolute directory path.", ex.Message); 104 | } 105 | 106 | try 107 | { 108 | directoryPath.MustBeAbsoluteDirectoryPath(() => throw new InvalidOperationException("Test.")); 109 | 110 | Assert.Fail(); 111 | } 112 | catch (InvalidOperationException ex) 113 | { 114 | Assert.AreEqual("Test.", ex.Message); 115 | } 116 | } 117 | 118 | [DataRow(@".\Tem|p")] 119 | [DataTestMethod] 120 | public void MustAbsoluteDirectoryPathFail2(string directoryPath) 121 | { 122 | try 123 | { 124 | directoryPath.MustBeAbsoluteDirectoryPath(); 125 | 126 | Assert.Fail(); 127 | } 128 | catch (ArgumentException ex) 129 | { 130 | Assert.AreEqual("Value must be a valid directory path.", ex.Message); 131 | } 132 | } 133 | 134 | [DataRow(@"c:\file.exe")] 135 | [DataRow(@"c:\apps\MyApp\bin\Debug\file.exe")] 136 | [DataRow(@"d:\apps\MyApp\bin\Debug\file.exe")] 137 | [DataRow(@"e:\apps\MyApp\bin\Debug\file.exe")] 138 | [DataTestMethod] 139 | public void MustBeAbsoluteFilePath(string filePath) 140 | { 141 | Assert.AreEqual(filePath, filePath.MustBeAbsoluteFilePath()); 142 | } 143 | 144 | [DataRow(@".\file.exe")] 145 | [DataRow(@".\bin\Debug\file.exe")] 146 | [DataTestMethod] 147 | public void MustBeAbsoluteFilePathFail(string filePath) 148 | { 149 | try 150 | { 151 | filePath.MustBeAbsoluteFilePath(); 152 | 153 | Assert.Fail(); 154 | } 155 | catch (ArgumentException ex) 156 | { 157 | Assert.AreEqual("Value must be an absolute file path.", ex.Message); 158 | } 159 | 160 | try 161 | { 162 | filePath.MustBeAbsoluteFilePath(() => throw new InvalidOperationException("Test.")); 163 | 164 | Assert.Fail(); 165 | } 166 | catch (InvalidOperationException ex) 167 | { 168 | Assert.AreEqual("Test.", ex.Message); 169 | } 170 | } 171 | 172 | [DataRow(@"c:\fil|e.exe")] 173 | [DataRow(@"e:\apps\M|yApp\bin\Debug\file.exe")] 174 | [DataTestMethod] 175 | public void MustBeAbsoluteFilePathFail2(string filePath) 176 | { 177 | try 178 | { 179 | filePath.MustBeAbsoluteFilePath(); 180 | 181 | Assert.Fail(); 182 | } 183 | catch (ArgumentException ex) 184 | { 185 | Assert.AreEqual("Value must be a valid file path.", ex.Message); 186 | } 187 | } 188 | 189 | [DataRow(null)] 190 | [DataRow("file")] 191 | [DataRow("file.exe")] 192 | [DataRow(@"C:\Users\User1\source\repos\WpfApp1\WpfApp1\bin\Debug")] 193 | [DataRow(@"C:\Users\User1\source\repos\WpfApp1\WpfApp1\bin\Debug\")] 194 | [DataRow(@"C:\Users\User1\source\repos\WpfApp1\WpfApp1\bin\Debug\file.exe")] 195 | [DataRow("exe.file")] 196 | [DataRow("exe.sdfsfsdfsdfsdf")] 197 | [DataTestMethod] 198 | public void MustBeValidDirectoryPath(string filePath) 199 | { 200 | Assert.AreEqual(filePath, filePath.MustBeValidDirectoryPath()); 201 | } 202 | 203 | [DataRow("")] 204 | [DataRow("file\t1.exe")] 205 | [DataRow("file|1.ex'")] 206 | [DataTestMethod] 207 | public void MustBeValidDirectoryPathFail(string directoryPath) 208 | { 209 | try 210 | { 211 | directoryPath.MustBeValidDirectoryPath(); 212 | 213 | Assert.Fail(); 214 | } 215 | catch (ArgumentException ex) 216 | { 217 | Assert.AreEqual("Value must be a valid directory path.", ex.Message); 218 | } 219 | 220 | try 221 | { 222 | directoryPath.MustBeValidDirectoryPath(() => throw new InvalidOperationException("Test.")); 223 | 224 | Assert.Fail(); 225 | } 226 | catch (InvalidOperationException ex) 227 | { 228 | Assert.AreEqual("Test.", ex.Message); 229 | } 230 | } 231 | 232 | [DataRow(null)] 233 | [DataRow("f")] 234 | [DataRow("file")] 235 | [DataRow("file.exe")] 236 | [DataRow("exe.file")] 237 | [DataRow("exe.sdfsfsdfsdfsdf")] 238 | [DataTestMethod] 239 | public void MustBeValidFileName(string filePath) 240 | { 241 | Assert.AreEqual(filePath, filePath.MustBeValidFileName()); 242 | } 243 | 244 | [DataRow("")] 245 | [DataRow("file>1.exe")] 246 | [DataRow("file?1.ex'")] 247 | [DataTestMethod] 248 | public void MustBeValidFileNameFail(string fileName) 249 | { 250 | try 251 | { 252 | fileName.MustBeValidFileName(); 253 | 254 | Assert.Fail(); 255 | } 256 | catch (ArgumentException ex) 257 | { 258 | Assert.AreEqual("Value must be a valid file name.", ex.Message); 259 | } 260 | 261 | try 262 | { 263 | fileName.MustBeValidFileName(() => throw new InvalidOperationException("Test.")); 264 | 265 | Assert.Fail(); 266 | } 267 | catch (InvalidOperationException ex) 268 | { 269 | Assert.AreEqual("Test.", ex.Message); 270 | } 271 | } 272 | 273 | [DataRow(null)] 274 | [DataRow("f")] 275 | [DataRow("file")] 276 | [DataRow("file.exe")] 277 | [DataRow("exe.file")] 278 | [DataRow(@"C:\Users\User1\source\repos\WpfApp1\WpfApp1\bin\Debug\file.exe")] 279 | [DataRow("exe.sdfsfsdfsdfsdf")] 280 | [DataTestMethod] 281 | public void MustBeValidFilePath(string filePath) 282 | { 283 | Assert.AreEqual(filePath, filePath.MustBeValidFilePath()); 284 | } 285 | 286 | [DataRow("")] 287 | [DataRow("file>1.exe")] 288 | [DataRow("file|1.ex'")] 289 | public void MustBeValidFilePathFail(string fileName) 290 | { 291 | try 292 | { 293 | fileName.MustBeValidFileName(); 294 | 295 | Assert.Fail(); 296 | } 297 | catch (ArgumentException ex) 298 | { 299 | Assert.AreEqual("Value must be a valid file name.", ex.Message); 300 | } 301 | 302 | try 303 | { 304 | fileName.MustBeValidFileName(() => throw new InvalidOperationException("Test.")); 305 | 306 | Assert.Fail(); 307 | } 308 | catch (InvalidOperationException ex) 309 | { 310 | Assert.AreEqual("Test.", ex.Message); 311 | } 312 | } 313 | 314 | [DataTestMethod] 315 | public void MustDirectoryExist() 316 | { 317 | if (!Directory.Exists(@".\Temp")) 318 | { 319 | Directory.CreateDirectory(@".\Temp"); 320 | } 321 | 322 | Assert.AreEqual(@".\Temp", @".\Temp".MustDirectoryExist()); 323 | 324 | if (Directory.Exists(@".\Temp")) 325 | { 326 | Directory.Delete(@".\Temp"); 327 | } 328 | 329 | try 330 | { 331 | @".\Temp".MustDirectoryExist(); 332 | 333 | Assert.Fail(); 334 | } 335 | catch (ArgumentException ex) 336 | { 337 | Assert.AreEqual("Directory must exist.", ex.Message); 338 | } 339 | 340 | try 341 | { 342 | @".\Temp".MustDirectoryExist(() => throw new InvalidOperationException("Test.")); 343 | 344 | Assert.Fail(); 345 | } 346 | catch (InvalidOperationException ex) 347 | { 348 | Assert.AreEqual("Test.", ex.Message); 349 | } 350 | } 351 | 352 | [TestMethod] 353 | public void MustFileExist() 354 | { 355 | if (!File.Exists(@".\Temp.txt")) 356 | { 357 | File.WriteAllText(@".\Temp.txt", "tmp"); 358 | } 359 | 360 | Assert.AreEqual(@".\Temp.txt", @".\Temp.txt".MustFileExist()); 361 | 362 | if (File.Exists(@".\Temp.txt")) 363 | { 364 | File.Delete(@".\Temp.txt"); 365 | } 366 | 367 | try 368 | { 369 | @".\Temp.txt".MustFileExist(); 370 | 371 | Assert.Fail(); 372 | } 373 | catch (ArgumentException ex) 374 | { 375 | Assert.AreEqual("File must exist.", ex.Message); 376 | } 377 | 378 | try 379 | { 380 | @".\Temp".MustFileExist(() => throw new InvalidOperationException("Test.")); 381 | 382 | Assert.Fail(); 383 | } 384 | catch (InvalidOperationException ex) 385 | { 386 | Assert.AreEqual("Test.", ex.Message); 387 | } 388 | } 389 | 390 | #endregion Public Methods 391 | } -------------------------------------------------------------------------------- /Test/DefensiveProgrammingFramework.Test/FileWhenExtensionsTest.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace DefensiveProgrammingFramework.Test; 4 | 5 | [TestClass] 6 | public class FileWhenExtensionsTest 7 | { 8 | #region Public Methods 9 | 10 | [DataTestMethod] 11 | public void WhenDoesDirectoryExist() 12 | { 13 | if (!Directory.Exists(@".\Temp")) 14 | { 15 | Directory.CreateDirectory(@".\Temp"); 16 | } 17 | 18 | Assert.AreEqual("aaa", @".\Temp".WhenDoesDirectoryExist("aaa")); 19 | 20 | if (Directory.Exists(@".\Temp")) 21 | { 22 | Directory.Delete(@".\Temp"); 23 | } 24 | 25 | Assert.AreEqual(@".\Temp", @".\Temp".WhenDoesDirectoryExist("aaa")); 26 | } 27 | 28 | [DataTestMethod] 29 | public void WhenDoesFileExist() 30 | { 31 | if (!File.Exists(@".\Tmp.txt")) 32 | { 33 | File.WriteAllText(@".\Tmp.txt", "tmp"); 34 | } 35 | 36 | Assert.AreEqual("aaa", @".\Tmp.txt".WhenDoesFileExist("aaa")); 37 | 38 | if (File.Exists(@".\Tmp.txt")) 39 | { 40 | File.Delete(@".\Tmp.txt"); 41 | } 42 | 43 | Assert.AreEqual(@".\Tmp.txt", @".\Tmp.txt".WhenDoesFileExist("aaa")); 44 | } 45 | 46 | [DataRow(null, "aaa", true)] 47 | [DataRow(".", "aaa", false)] 48 | [DataRow(@".\bin\Debug", "aaa", false)] 49 | [DataRow(@"c:\", "aaa", true)] 50 | [DataRow(@"c:", "aaa", false)] 51 | [DataRow(@"c:\apps\MyApp\bin\Debug", "aaa", true)] 52 | [DataRow(@"d:\apps\MyApp\bin\Debug", "aaa", true)] 53 | [DataRow(@"e:\apps\MyApp\bin\Debug", "aaa", true)] 54 | [DataTestMethod] 55 | public void WhenIsAbsoluteDirectoryPath(string filePath, string defaultValue, bool expected) 56 | { 57 | Assert.AreEqual(expected ? defaultValue : filePath, filePath.WhenIsAbsoluteDirectoryPath(defaultValue)); 58 | } 59 | 60 | [DataRow("")] 61 | [DataRow(@"e:\apps\M|yApp\bin\Debug")] 62 | [DataTestMethod] 63 | public void WhenIsAbsoluteDirectoryPathFail(string filePath) 64 | { 65 | try 66 | { 67 | filePath.WhenIsAbsoluteDirectoryPath("aaa"); 68 | 69 | Assert.Fail(); 70 | } 71 | catch (ArgumentException ex) 72 | { 73 | Assert.AreEqual("Value must be a valid directory path.", ex.Message); 74 | } 75 | } 76 | 77 | [DataRow(null, "aaa", true)] 78 | [DataRow(@".\file.exe", "aaa", false)] 79 | [DataRow(@".\bin\Debug\file.exe", "aaa", false)] 80 | [DataRow(@"c:\file.exe", "aaa", true)] 81 | [DataRow(@"c:\apps\MyApp\bin\Debug\file.exe", "aaa", true)] 82 | [DataRow(@"d:\apps\MyApp\bin\Debug\file.exe", "aaa", true)] 83 | [DataRow(@"e:\apps\MyApp\bin\Debug\file.exe", "aaa", true)] 84 | [DataTestMethod] 85 | public void WhenIsAbsoluteFilePath(string filePath, string defaultValue, bool expected) 86 | { 87 | Assert.AreEqual(expected ? defaultValue : filePath, filePath.WhenIsAbsoluteFilePath(defaultValue)); 88 | } 89 | 90 | [DataRow(@"")] 91 | [DataRow(@"e:\apps\M|yApp\bin\Debug\file.exe")] 92 | [DataTestMethod] 93 | public void WhenIsAbsoluteFilePathFail(string filePath) 94 | { 95 | try 96 | { 97 | filePath.WhenIsAbsoluteFilePath("aaa"); 98 | 99 | Assert.Fail(); 100 | } 101 | catch (ArgumentException ex) 102 | { 103 | Assert.AreEqual("Value must be a valid file path.", ex.Message); 104 | } 105 | } 106 | 107 | [TestMethod] 108 | public void WhenIsEmptyDirectory() 109 | { 110 | var directoryPath = @".\Tmp5"; 111 | var filePath = Path.Combine(directoryPath, "tmp.txt"); 112 | 113 | if (!Directory.Exists(directoryPath)) 114 | { 115 | Directory.CreateDirectory(directoryPath); 116 | } 117 | 118 | Assert.AreEqual("aaa", directoryPath.WhenIsEmptyDirectory("aaa")); 119 | 120 | File.WriteAllText(filePath, "text"); 121 | 122 | Assert.AreEqual(directoryPath, directoryPath.WhenIsEmptyDirectory("aaa")); 123 | 124 | File.Delete(filePath); 125 | Directory.Delete(directoryPath); 126 | } 127 | 128 | [DataRow(null, "aaa", true)] 129 | [DataRow("", "aaa", false)] 130 | [DataRow("f", "aaa", true)] 131 | [DataRow("file", "aaa", true)] 132 | [DataRow("file.exe", "aaa", true)] 133 | [DataRow(@"C:\Users\User1\source\repos\WpfApp1\WpfApp1\bin\Debug", "aaa", true)] 134 | [DataRow(@"C:\Users\User1\source\repos\WpfApp1\WpfApp1\bin\Debug\", "aaa", true)] 135 | [DataRow(@"C:\Users\User1\source\repos\WpfApp1\WpfApp1\bin\Debug\file.exe", "aaa", true)] 136 | [DataRow("exe.file", "aaa", true)] 137 | [DataRow("exe.sdfsfsdfsdfsdf", "aaa", true)] 138 | [DataRow("file\t1.exe", "aaa", false)] 139 | [DataRow("file|1.ex'", "aaa", false)] 140 | [DataTestMethod] 141 | public void WhenIsValidDirectoryPath(string filePath, string defaultValue, bool expected) 142 | { 143 | Assert.AreEqual(expected ? defaultValue : filePath, filePath.WhenIsValidDirectoryPath(defaultValue)); 144 | } 145 | 146 | [DataRow(null, "aaa", true)] 147 | [DataRow("", "aaa", false)] 148 | [DataRow("f", "aaa", true)] 149 | [DataRow("file", "aaa", true)] 150 | [DataRow("file.exe", "aaa", true)] 151 | [DataRow("exe.file", "aaa", true)] 152 | [DataRow("exe.sdfsfsdfsdfsdf", "aaa", true)] 153 | [DataRow("file>1.exe", "aaa", false)] 154 | [DataRow("file?1.ex'", "aaa", false)] 155 | [DataTestMethod] 156 | public void WhenIsValidFileName(string filePath, string defaultValue, bool expected) 157 | { 158 | Assert.AreEqual(expected ? defaultValue : filePath, filePath.WhenIsValidFileName(defaultValue)); 159 | } 160 | 161 | [DataRow(null, "aaa", true)] 162 | [DataRow("", "aaa", false)] 163 | [DataRow("f", "aaa", true)] 164 | [DataRow("file", "aaa", true)] 165 | [DataRow("file.exe", "aaa", true)] 166 | [DataRow("exe.file", "aaa", true)] 167 | [DataRow(@"C:\Users\User1\source\repos\WpfApp1\WpfApp1\bin\Debug\file.exe", "aaa", true)] 168 | [DataRow("exe.sdfsfsdfsdfsdf", "aaa", true)] 169 | [DataRow("file\t1.exe", "aaa", false)] 170 | [DataRow("file|1.ex'", "aaa", false)] 171 | [DataTestMethod] 172 | public void WhenIsValidFilePath(string filePath, string defaultValue, bool expected) 173 | { 174 | Assert.AreEqual(expected ? defaultValue : filePath, filePath.WhenIsValidFilePath(defaultValue)); 175 | } 176 | 177 | #endregion Public Methods 178 | } -------------------------------------------------------------------------------- /Test/DefensiveProgrammingFramework.Test/FileWhenNotExtensionsTest.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.VisualStudio.TestTools.UnitTesting; 2 | 3 | namespace DefensiveProgrammingFramework.Test; 4 | 5 | [TestClass] 6 | public class FileWhenNotExtensionsTest 7 | { 8 | #region Public Methods 9 | 10 | [DataTestMethod] 11 | public void WhenDoesNotDirectoryExist() 12 | { 13 | if (!Directory.Exists(@".\Temp")) 14 | { 15 | Directory.CreateDirectory(@".\Temp"); 16 | } 17 | 18 | Assert.AreEqual(@".\Temp", @".\Temp".WhenDoesNotDirectoryExist("aaa")); 19 | 20 | if (Directory.Exists(@".\Temp")) 21 | { 22 | Directory.Delete(@".\Temp"); 23 | } 24 | 25 | Assert.AreEqual("aaa", @".\Temp".WhenDoesNotDirectoryExist("aaa")); 26 | } 27 | 28 | [TestMethod] 29 | public void WhenIsNotEmptyDirectory() 30 | { 31 | var directoryPath = @".\Tmp5"; 32 | var filePath = Path.Combine(directoryPath, "tmp.txt"); 33 | 34 | if (!Directory.Exists(directoryPath)) 35 | { 36 | Directory.CreateDirectory(directoryPath); 37 | } 38 | 39 | Assert.AreEqual(directoryPath, directoryPath.WhenIsNotEmptyDirectory("aaa")); 40 | 41 | File.WriteAllText(filePath, "text"); 42 | 43 | Assert.AreEqual("aaa", directoryPath.WhenIsNotEmptyDirectory("aaa")); 44 | 45 | File.Delete(filePath); 46 | Directory.Delete(directoryPath); 47 | } 48 | 49 | [DataTestMethod] 50 | public void WhenDoesNotFileExist() 51 | { 52 | if (!File.Exists(@".\Tmp.txt")) 53 | { 54 | File.WriteAllText(@".\Tmp.txt", "tmp"); 55 | } 56 | 57 | Assert.AreEqual(@".\Tmp.txt", @".\Tmp.txt".WhenDoesNotFileExist("aaa")); 58 | 59 | if (File.Exists(@".\Tmp.txt")) 60 | { 61 | File.Delete(@".\Tmp.txt"); 62 | } 63 | 64 | Assert.AreEqual("aaa", @".\Tmp.txt".WhenDoesNotFileExist("aaa")); 65 | } 66 | 67 | [DataRow(null, "aaa", !true)] 68 | [DataRow(".", "aaa", !false)] 69 | [DataRow(@".\bin\Debug", "aaa", !false)] 70 | [DataRow(@"c:\", "aaa", !true)] 71 | [DataRow(@"c:", "aaa", !false)] 72 | [DataRow(@"c:\apps\MyApp\bin\Debug", "aaa", !true)] 73 | [DataRow(@"d:\apps\MyApp\bin\Debug", "aaa", !true)] 74 | [DataRow(@"e:\apps\MyApp\bin\Debug", "aaa", !true)] 75 | [DataTestMethod] 76 | public void WhenIsNotAbsoluteDirectoryPath(string filePath, string defaultValue, bool expected) 77 | { 78 | Assert.AreEqual(expected ? defaultValue : filePath, filePath.WhenIsNotAbsoluteDirectoryPath(defaultValue)); 79 | } 80 | 81 | [DataRow("")] 82 | [DataRow(@"e:\apps\M|yApp\bin\Debug")] 83 | [DataTestMethod] 84 | public void WhenIsNotAbsoluteDirectoryPath(string filePath) 85 | { 86 | try 87 | { 88 | filePath.WhenIsNotAbsoluteDirectoryPath("aaa"); 89 | 90 | Assert.Fail(); 91 | } 92 | catch (ArgumentException ex) 93 | { 94 | Assert.AreEqual("Value must be a valid directory path.", ex.Message); 95 | } 96 | } 97 | 98 | [DataRow(null, "aaa", !true)] 99 | [DataRow(@".\file.exe", "aaa", !false)] 100 | [DataRow(@".\bin\Debug\file.exe", "aaa", !false)] 101 | [DataRow(@"c:\file.exe", "aaa", !true)] 102 | [DataRow(@"c:\apps\MyApp\bin\Debug\file.exe", "aaa", !true)] 103 | [DataRow(@"d:\apps\MyApp\bin\Debug\file.exe", "aaa", !true)] 104 | [DataRow(@"e:\apps\MyApp\bin\Debug\file.exe", "aaa", !true)] 105 | [DataTestMethod] 106 | public void WhenIsNotAbsoluteFilePath(string filePath, string defaultValue, bool expected) 107 | { 108 | Assert.AreEqual(expected ? defaultValue : filePath, filePath.WhenIsNotAbsoluteFilePath(defaultValue)); 109 | } 110 | 111 | [DataRow(@"")] 112 | [DataRow(@"e:\apps\M|yApp\bin\Debug\file.exe")] 113 | [DataTestMethod] 114 | public void WhenIsNotAbsoluteFilePathFail(string filePath) 115 | { 116 | try 117 | { 118 | filePath.WhenIsNotAbsoluteFilePath("aaa"); 119 | 120 | Assert.Fail(); 121 | } 122 | catch (ArgumentException ex) 123 | { 124 | Assert.AreEqual("Value must be a valid file path.", ex.Message); 125 | } 126 | } 127 | 128 | [DataRow(null, "aaa", !true)] 129 | [DataRow("", "aaa", !false)] 130 | [DataRow("f", "aaa", !true)] 131 | [DataRow("file", "aaa", !true)] 132 | [DataRow("file.exe", "aaa", !true)] 133 | [DataRow(@"C:\Users\User1\source\repos\WpfApp1\WpfApp1\bin\Debug", "aaa", !true)] 134 | [DataRow(@"C:\Users\User1\source\repos\WpfApp1\WpfApp1\bin\Debug\", "aaa", !true)] 135 | [DataRow(@"C:\Users\User1\source\repos\WpfApp1\WpfApp1\bin\Debug\file.exe", "aaa", !true)] 136 | [DataRow("exe.file", "aaa", !true)] 137 | [DataRow("exe.sdfsfsdfsdfsdf", "aaa", !true)] 138 | [DataRow("file\t1.exe", "aaa", !false)] 139 | [DataRow("file|1.ex'", "aaa", !false)] 140 | [DataTestMethod] 141 | public void WhenIsNotValidDirectoryPath(string filePath, string defaultValue, bool expected) 142 | { 143 | Assert.AreEqual(expected ? defaultValue : filePath, filePath.WhenIsNotValidDirectoryPath(defaultValue)); 144 | } 145 | 146 | [DataRow(null, "aaa", !true)] 147 | [DataRow("", "aaa", !false)] 148 | [DataRow("f", "aaa", !true)] 149 | [DataRow("file", "aaa", !true)] 150 | [DataRow("file.exe", "aaa", !true)] 151 | [DataRow("exe.file", "aaa", !true)] 152 | [DataRow("exe.sdfsfsdfsdfsdf", "aaa", !true)] 153 | [DataRow("file>1.exe", "aaa", !false)] 154 | [DataRow("file?1.ex'", "aaa", !false)] 155 | [DataTestMethod] 156 | public void WhenNotIsValidFileName(string filePath, string defaultValue, bool expected) 157 | { 158 | Assert.AreEqual(expected ? defaultValue : filePath, filePath.WhenIsNotValidFileName(defaultValue)); 159 | } 160 | 161 | [DataRow(null, "aaa", !true)] 162 | [DataRow("", "aaa", !false)] 163 | [DataRow("f", "aaa", !true)] 164 | [DataRow("file", "aaa", !true)] 165 | [DataRow("file.exe", "aaa", !true)] 166 | [DataRow("exe.file", "aaa", !true)] 167 | [DataRow(@"C:\Users\User1\source\repos\WpfApp1\WpfApp1\bin\Debug\file.exe", "aaa", !true)] 168 | [DataRow("exe.sdfsfsdfsdfsdf", "aaa", !true)] 169 | [DataRow("file\t1.exe", "aaa", !false)] 170 | [DataRow("file|1.ex'", "aaa", !false)] 171 | [DataTestMethod] 172 | public void WhenNotIsValidFilePath(string filePath, string defaultValue, bool expected) 173 | { 174 | Assert.AreEqual(expected ? defaultValue : filePath, filePath.WhenIsNotValidFilePath(defaultValue)); 175 | } 176 | 177 | #endregion Public Methods 178 | } -------------------------------------------------------------------------------- /Test/DefensiveProgrammingFramework.Test/FrameworkTest.cs: -------------------------------------------------------------------------------- 1 | using System.Diagnostics; 2 | using System.Reflection; 3 | using System.Runtime.CompilerServices; 4 | using Microsoft.VisualStudio.TestTools.UnitTesting; 5 | 6 | namespace DefensiveProgrammingFramework.Test; 7 | 8 | [TestClass] 9 | public class FrameworkTest 10 | { 11 | #region Public Methods 12 | 13 | [DataRow("Object extension methods", typeof(ObjectIsExtensions), typeof(ObjectIsNotExtensions), typeof(ObjectCannotExtensions), typeof(ObjectMustExtensions), typeof(ObjectWhenExtensions), typeof(ObjectWhenNotExtensions), new string[] { "Is=>Be", "Does=>" })] 14 | [DataRow("Collection extension methods", typeof(CollectionIsExtensions), typeof(CollectionIsNotExtensions), typeof(CollectionCannotExtensions), typeof(CollectionMustExtensions), typeof(CollectionWhenExtensions), typeof(CollectionWhenNotExtensions), new string[] { "Is=>Be", "Contains=>Contain" })] 15 | [DataRow("File system extension methods", typeof(FileIsExtensions), typeof(FileIsNotExtensions), typeof(FileCannotExtensions), typeof(FileMustExtensions), typeof(FileWhenExtensions), typeof(FileWhenNotExtensions), new string[] { "Is=>Be", "Does=>" })] 16 | [DataTestMethod] 17 | public void TestNamingConvention(string name, Type isExtensions, Type isNotExtensions, Type cannotExtensions, Type mustExtensions, Type whenExtensions, Type whenNotExtensions, string[] prefixes) 18 | { 19 | var prefixes2 = prefixes.ToDictionary(x => x.Split(new string[] { "=>" }, StringSplitOptions.None)[0], x => x.Split(new string[] { "=>" }, StringSplitOptions.None)[1]); 20 | 21 | // check if classes are static 22 | this.TestClass(isExtensions); 23 | this.TestClass(isNotExtensions); 24 | this.TestClass(cannotExtensions); 25 | this.TestClass(mustExtensions); 26 | this.TestClass(whenExtensions); 27 | this.TestClass(whenNotExtensions); 28 | 29 | Debug.WriteLine(string.Empty); 30 | Debug.WriteLine($"### {name}:"); 31 | Debug.WriteLine($"|is|is not|must|cannot|when|when not|"); 32 | Debug.WriteLine($"|--|--|--|--|--|--|"); 33 | 34 | foreach (var method in isExtensions.GetMethods(BindingFlags.Public | 35 | BindingFlags.Static | 36 | BindingFlags.DeclaredOnly) 37 | .OrderBy(x => x.Name)) 38 | { 39 | var prefix = prefixes2.First(x => method.Name.StartsWith(x.Key)).Key; 40 | var suffix = method.Name[prefix.Length..]; 41 | var substitute = prefixes2[prefix]; 42 | 43 | var isNotMethodName = $"{prefix}Not{suffix}"; 44 | var cannotMethodName = $"Cannot{substitute}{suffix}"; 45 | var mustMethodName = $"Must{substitute}{suffix}"; 46 | var whenMethodName = $"When{prefix}{suffix}"; 47 | var whenNotMethodName = $"When{prefix}Not{suffix}"; 48 | 49 | if (method.Name == "IsMatch") 50 | { 51 | cannotMethodName = nameof(ObjectCannotExtensions.CannotMatch); 52 | mustMethodName = nameof(ObjectMustExtensions.MustMatch); 53 | whenMethodName = nameof(ObjectWhenExtensions.WhenDoesMatch); 54 | whenNotMethodName = nameof(ObjectWhenNotExtensions.WhenDoesNotMatch); 55 | } 56 | 57 | // check if naming convention 58 | this.TestMethodName(isExtensions, method.Name); 59 | this.TestMethodName(isNotExtensions, isNotMethodName); 60 | this.TestMethodName(cannotExtensions, cannotMethodName); 61 | this.TestMethodName(mustExtensions, mustMethodName); 62 | this.TestMethodName(whenExtensions, whenMethodName); 63 | this.TestMethodName(whenNotExtensions, whenNotMethodName); 64 | 65 | // check parameters 66 | this.TestParameters(method, isNotExtensions.GetMethod(isNotMethodName), false, false); 67 | this.TestParameters(method, cannotExtensions.GetMethod(cannotMethodName), false, name != "Object extension methods" || method.Name != nameof(ObjectIsExtensions.IsOneOf)); 68 | this.TestParameters(method, mustExtensions.GetMethod(mustMethodName), false, name != "Object extension methods" || method.Name != nameof(ObjectIsExtensions.IsOneOf)); 69 | this.TestParameters(method, whenExtensions.GetMethod(whenMethodName), true, false); 70 | this.TestParameters(method, whenNotExtensions.GetMethod(whenNotMethodName), true, false); 71 | 72 | // check return types 73 | this.TestReturnType(method, typeof(bool)); 74 | this.TestReturnType(isNotExtensions.GetMethod(isNotMethodName), method.ReturnType); 75 | this.TestReturnType(cannotExtensions.GetMethod(cannotMethodName), method.GetParameters()[0].ParameterType); 76 | this.TestReturnType(mustExtensions.GetMethod(mustMethodName), method.GetParameters()[0].ParameterType); 77 | this.TestReturnType(whenExtensions.GetMethod(whenMethodName), method.GetParameters()[0].ParameterType); 78 | this.TestReturnType(whenNotExtensions.GetMethod(whenNotMethodName), method.GetParameters()[0].ParameterType); 79 | 80 | Debug.WriteLine($"|{method.Name}|{isNotMethodName}|{mustMethodName}|{cannotMethodName}|{whenMethodName}|{whenNotMethodName}|"); 81 | } 82 | 83 | // check if method count equals 84 | this.TestMethodCount(isExtensions, isNotExtensions); 85 | this.TestMethodCount(isExtensions, cannotExtensions); 86 | this.TestMethodCount(isExtensions, mustExtensions); 87 | this.TestMethodCount(isExtensions, whenExtensions); 88 | this.TestMethodCount(isExtensions, whenNotExtensions); 89 | } 90 | 91 | #endregion Public Methods 92 | 93 | #region Private Methods 94 | 95 | private void TestClass(Type type) 96 | { 97 | if (!type.IsAbstract || 98 | !type.IsSealed) 99 | { 100 | Assert.Fail($"Type {type.Name} must be static."); 101 | } 102 | } 103 | 104 | private void TestMethodCount(Type type1, Type type2) 105 | { 106 | if (type1.GetMethods(BindingFlags.DeclaredOnly).Length != type2.GetMethods(BindingFlags.DeclaredOnly).Length) 107 | { 108 | Assert.Fail($"Method count in {type1} does not match method count in {type2}"); 109 | } 110 | } 111 | 112 | private void TestMethodModifiers(MethodInfo method) 113 | { 114 | if (!method.IsStatic) 115 | { 116 | Assert.Fail($"Method {method.Name} should be static."); 117 | } 118 | else if (!method.IsPublic) 119 | { 120 | Assert.Fail($"Method {method.Name} should be public."); 121 | } 122 | else if (!method.IsDefined(typeof(ExtensionAttribute), false)) 123 | { 124 | Assert.Fail($"Method {method.Name} should be an extension method."); 125 | } 126 | } 127 | 128 | private void TestMethodName(Type type, string methodName) 129 | { 130 | var methods = type.GetMethods(); 131 | 132 | if (!methods.Any(x => x.Name == methodName)) 133 | { 134 | Assert.Fail($"Missing method {methodName} in {type}"); 135 | } 136 | else 137 | { 138 | methods.Where(x => x.Name == methodName).ToList().ForEach(this.TestMethodModifiers); 139 | } 140 | } 141 | 142 | private void TestParameters(MethodInfo methodInfo1, MethodInfo methodInfo2, bool containsDefaultValue, bool containsErrorHandler) 143 | { 144 | var parameters1 = methodInfo1.GetParameters(); 145 | var parameters2 = methodInfo2.GetParameters(); 146 | 147 | if (parameters1.Length != parameters2.Length + (containsDefaultValue ? -1 : 0) + (containsErrorHandler ? -1 : 0)) 148 | { 149 | Assert.Fail($"Method {methodInfo1.Name} parameters do not match {methodInfo2.Name}."); 150 | } 151 | else 152 | { 153 | if (methodInfo1.IsGenericMethod != methodInfo2.IsGenericMethod) 154 | { 155 | Assert.Fail($"Method {methodInfo1.Name} generic parameters do not match {methodInfo2.Name}."); 156 | } 157 | 158 | for (var i = 0; i < parameters1.Length; i++) 159 | { 160 | if (parameters1[i].Name != parameters2[i].Name) 161 | { 162 | Assert.Fail($"Method {methodInfo1.Name} parameters names do not match {methodInfo2.Name} ({parameters2[i].Name}))."); 163 | } 164 | else if (parameters1[i].ParameterType.Name != parameters2[i].ParameterType.Name) 165 | { 166 | Assert.Fail($"Method {methodInfo1.Name} parameters types do not match {methodInfo2.Name} ({parameters2[i].ParameterType})."); 167 | } 168 | } 169 | 170 | if (containsDefaultValue) 171 | { 172 | if (parameters2.Last().Name != "defaultValue") 173 | { 174 | Assert.Fail($"Method {methodInfo2.Name} does not have last parameter named defaultValue."); 175 | } 176 | } 177 | } 178 | } 179 | 180 | private void TestReturnType(MethodInfo methodInfo1, Type returnType) 181 | { 182 | if (methodInfo1.ReturnType.Name != returnType.Name) 183 | { 184 | Assert.Fail($"Method {methodInfo1.Name} returns incorrect return type."); 185 | } 186 | } 187 | 188 | #endregion Private Methods 189 | } -------------------------------------------------------------------------------- /_config.yml: -------------------------------------------------------------------------------- 1 | theme: jekyll-theme-slate --------------------------------------------------------------------------------