diff --git a/.editorconfig b/.editorconfig index 7085553..b7b4094 100644 --- a/.editorconfig +++ b/.editorconfig @@ -6,151 +6,237 @@ indent_size = 4 trim_trailing_whitespace = true insert_final_newline = true -[*.csproj] +[*.{xml,csproj,proj,stylecop,resx,ruleset,props,targets,config,nuspec,builds}] indent_size = 2 -[*.cs] -# Use "var" everywhere -csharp_style_var_for_built_in_types = true : error -csharp_style_var_when_type_is_apparent = true : error -csharp_style_var_elsewhere = true : suggestion - -# Prefer method-like constructs to have a block body -csharp_style_expression_bodied_methods = false : none -csharp_style_expression_bodied_constructors = false : none -csharp_style_expression_bodied_operators = true : none - -# Prefer property-like constructs to have an expression-body -csharp_style_expression_bodied_properties = true : none -csharp_style_expression_bodied_indexers = true : none -csharp_style_expression_bodied_accessors = true : none +[*.json] +indent_size = 2 -# Require fields that are only assigned inline or in a ctor be marked with readonly -dotnet_style_readonly_field = true : error +[*.cs] +# `this` +dotnet_style_qualification_for_field = false:error +dotnet_style_qualification_for_property = false:error +dotnet_style_qualification_for_method = false:error +dotnet_style_qualification_for_event = false:error + +# Keywords +dotnet_style_predefined_type_for_locals_parameters_members = true:error +dotnet_style_predefined_type_for_member_access = true:error + +# Modifiers +dotnet_style_require_accessibility_modifiers = for_non_interface_members:error +csharp_preferred_modifier_order = public, private, protected, internal, static, extern, new, virtual, abstract, sealed, override, readonly, unsafe, volatile, async:error +dotnet_style_readonly_field = true:error + +# Parentheses +dotnet_style_parentheses_in_arithmetic_binary_operators = always_for_clarity:none +dotnet_style_parentheses_in_relational_binary_operators = always_for_clarity:none +dotnet_style_parentheses_in_other_binary_operators = always_for_clarity:none +dotnet_style_parentheses_in_other_operators = never_if_unnecessary:error + +# Expressions +dotnet_style_object_initializer = true:suggestion +dotnet_style_collection_initializer = true:suggestion +dotnet_style_explicit_tuple_names = true:suggestion +dotnet_style_prefer_inferred_tuple_names = true:error +dotnet_style_prefer_inferred_anonymous_type_member_names = true:error +dotnet_style_prefer_auto_properties = true:none +dotnet_style_prefer_is_null_check_over_reference_equality_method = true:error +dotnet_style_prefer_conditional_expression_over_assignment = true:suggestion +dotnet_style_prefer_conditional_expression_over_return = true:silent +dotnet_style_prefer_compound_assignment = true:error +csharp_style_inlined_variable_declaration = true:suggestion +csharp_prefer_simple_default_expression = true:error + +# Null-checking +dotnet_style_coalesce_expression = true:error +dotnet_style_null_propagation = true:error +csharp_style_throw_expression = true:suggestion +csharp_style_conditional_delegate_call = true:error + +# Parameters +dotnet_code_quality_unused_parameters = all:error + +# `var` +csharp_style_var_for_built_in_types = true:error +csharp_style_var_when_type_is_apparent = true:error +csharp_style_var_elsewhere = true:silent + +# Expression-bodied members +csharp_style_expression_bodied_methods = when_on_single_line:silent +csharp_style_expression_bodied_constructors = when_on_single_line:silent +csharp_style_expression_bodied_operators = when_on_single_line:suggestion +csharp_style_expression_bodied_properties = when_on_single_line:suggestion +csharp_style_expression_bodied_indexers = when_on_single_line:suggestion +csharp_style_expression_bodied_accessors = when_on_single_line:suggestion +csharp_style_expression_bodied_lambdas = when_on_single_line:suggestion +csharp_style_expression_bodied_local_functions = when_on_single_line:suggestion + +# Pattern matching +csharp_style_pattern_matching_over_is_with_cast_check = true:error +csharp_style_pattern_matching_over_as_with_null_check = true:suggestion + +# Code block +csharp_prefer_braces = false:none + +# Unused values +csharp_style_unused_value_expression_statement_preference = discard_variable:silent +csharp_style_unused_value_assignment_preference = discard_variable:silent + +# Index and range +csharp_style_prefer_index_operator = true:error +csharp_style_prefer_range_operator = true:error + +# Miscellaneous +csharp_style_deconstructed_variable_declaration = true:suggestion +csharp_style_pattern_local_over_anonymous_function = true:error +csharp_using_directive_placement = outside_namespace:error +csharp_prefer_static_local_function = true:error +csharp_prefer_simple_using_statement = true:suggestion +csharp_style_prefer_switch_expression = true:error + +# Organize usings +dotnet_sort_system_directives_first = true +dotnet_separate_import_directive_groups = false -# Code formatting rules +# New-line csharp_new_line_before_open_brace = all csharp_new_line_before_else = true csharp_new_line_before_catch = true csharp_new_line_before_finally = true csharp_new_line_before_members_in_object_initializers = true csharp_new_line_before_members_in_anonymous_types = true +csharp_new_line_between_query_expression_clauses = true + +# Indentation csharp_indent_case_contents = true csharp_indent_switch_labels = true -csharp_indent_labels = flush_left +csharp_indent_labels = one_less_than_current +csharp_indent_block_contents = true +csharp_indent_braces = false +csharp_indent_case_contents_when_block = true + +# Spacing csharp_space_after_cast = false csharp_space_after_keywords_in_control_flow_statements = true -csharp_space_between_method_declaration_parameter_list_parentheses = false -csharp_space_between_method_call_parameter_list_parentheses = false csharp_space_between_parentheses = false csharp_space_before_colon_in_inheritance_clause = true csharp_space_after_colon_in_inheritance_clause = true csharp_space_around_binary_operators = before_and_after +csharp_space_between_method_declaration_parameter_list_parentheses = false csharp_space_between_method_declaration_empty_parameter_list_parentheses = false -csharp_space_between_method_call_name_and_opening_parenthesis = false +csharp_space_between_method_declaration_name_and_open_parenthesis = false +csharp_space_between_method_call_parameter_list_parentheses = false csharp_space_between_method_call_empty_parameter_list_parentheses = false +csharp_space_between_method_call_name_and_opening_parenthesis = false +csharp_space_after_comma = true +csharp_space_before_comma = false +csharp_space_after_dot = false +csharp_space_before_dot = false +csharp_space_after_semicolon_in_for_statement = true +csharp_space_before_semicolon_in_for_statement = false +csharp_space_around_declaration_statements = do_not_ignore +csharp_space_before_open_square_brackets = false +csharp_space_between_empty_square_brackets = false +csharp_space_between_square_brackets = false + +# Wrap csharp_preserve_single_line_statements = false +csharp_preserve_single_line_blocks = true -# Sort using and Import directives with System.* appearing first -dotnet_sort_system_directives_first = true - -# Avoid "this." and "Me." if not necessary -dotnet_style_qualification_for_field = true : error -dotnet_style_qualification_for_property = true : error -dotnet_style_qualification_for_method = true : error -dotnet_style_qualification_for_event = true : error - -# Use language keywords instead of framework type names for type references -dotnet_style_predefined_type_for_locals_parameters_members = true : error -dotnet_style_predefined_type_for_member_access = true : error - -# Pattern matching rules -csharp_style_pattern_matching_over_is_with_cast_check = true : error -csharp_style_pattern_matching_over_as_with_null_check = true : error - -# Null checking rules -csharp_style_throw_expression = true : suggestion -csharp_style_conditional_delegate_call = true : error - -# Code block rules -csharp_prefer_braces = false : error - -# Use more modern language features when available -dotnet_style_object_initializer = true : error -dotnet_style_collection_initializer = true : error -dotnet_style_coalesce_expression = true : error -dotnet_style_null_propagation = true : error -dotnet_style_explicit_tuple_names = true : error -dotnet_style_prefer_inferred_tuple_names = true : error -dotnet_style_prefer_inferred_anonymous_type_member_names = true : error -dotnet_style_prefer_auto_properties = true : error -dotnet_style_prefer_is_null_check_over_reference_equality_method = true : error -dotnet_style_prefer_conditional_expression_over_assignment = true : error -dotnet_style_prefer_conditional_expression_over_return = false : none -csharp_prefer_simple_default_expression = true : error -csharp_style_deconstructed_variable_declaration = true : error -csharp_style_pattern_local_over_anonymous_function = true : error -csharp_style_inlined_variable_declaration = true : none - -# Require modifiers to be specified in the correct order -dotnet_style_require_accessibility_modifiers = always : error -csharp_preferred_modifier_order = public,protected,internal,private,static,extern,new,virtual,abstract,sealed,override,readonly,unsafe,volatile,async : error - -# Use parentheses for clarity -dotnet_style_parentheses_in_other_binary_operators = always_for_clarity : error - -# Define naming symbols +# Naming symbols dotnet_naming_symbols.namespace_symbols.applicable_kinds = namespace -dotnet_naming_symbols.type_symbols.applicable_kinds = class,struct,enum,delegate + +dotnet_naming_symbols.type_symbols.applicable_kinds = class, struct, enum, delegate + dotnet_naming_symbols.interface_symbols.applicable_kinds = interface + dotnet_naming_symbols.type_parameter_symbols.applicable_kinds = type_parameter -dotnet_naming_symbols.function_symbols.applicable_kinds = property,event,method,local_function -dotnet_naming_symbols.visible_field_symbols.applicable_kinds = field -dotnet_naming_symbols.visible_field_symbols.applicable_accessibilities = public,protected + +dotnet_naming_symbols.function_symbols.applicable_kinds = property, event, method, local_function + +dotnet_naming_symbols.constant_field_symbols.applicable_kinds = field +dotnet_naming_symbols.constant_field_symbols.required_modifiers = const + +dotnet_naming_symbols.public_field_symbols.applicable_kinds = field +dotnet_naming_symbols.public_field_symbols.applicable_accessibilities = public, protected, protected_internal + dotnet_naming_symbols.private_field_symbols.applicable_kinds = field -dotnet_naming_symbols.private_field_symbols.applicable_accessibilities = private,internal,protected_internal -dotnet_naming_symbols.constant_symbols.applicable_kinds = field,local -dotnet_naming_symbols.constant_symbols.required_modifiers = const -dotnet_naming_symbols.static_readonly_symbols.applicable_kinds = field -dotnet_naming_symbols.static_readonly_symbols.required_modifiers = static,readonly -dotnet_naming_symbols.local_symbols.applicable_kinds = parameter,local - -# Define naming styles +dotnet_naming_symbols.private_field_symbols.applicable_accessibilities = private, internal, private_protected + +dotnet_naming_symbols.private_static_field_symbols.applicable_kinds = field +dotnet_naming_symbols.private_static_field_symbols.applicable_accessibilities = private, internal, private_protected +dotnet_naming_symbols.private_static_field_symbols.required_modifiers = static + +dotnet_naming_symbols.static_readonly_field_symbols.applicable_kinds = field +dotnet_naming_symbols.static_readonly_field_symbols.required_modifiers = static, readonly + +dotnet_naming_symbols.local_symbols.applicable_kinds = parameter, local + +# Naming styles dotnet_naming_style.pascal_style.capitalization = pascal_case + dotnet_naming_style.i_pascal_style.capitalization = pascal_case dotnet_naming_style.i_pascal_style.required_prefix = I + dotnet_naming_style.t_pascal_style.capitalization = pascal_case dotnet_naming_style.t_pascal_style.required_prefix = T + dotnet_naming_style.camel_style.capitalization = camel_case -# Define naming rules +dotnet_naming_style.u_camel_style.capitalization = camel_case +dotnet_naming_style.u_camel_style.required_prefix = _ + +dotnet_naming_style.su_camel_style.capitalization = camel_case +dotnet_naming_style.su_camel_style.required_prefix = s_ + +# Naming rules dotnet_naming_rule.namespace_names_must_be_in_pascal_style.symbols = namespace_symbols dotnet_naming_rule.namespace_names_must_be_in_pascal_style.style = pascal_style dotnet_naming_rule.namespace_names_must_be_in_pascal_style.severity = error + dotnet_naming_rule.type_names_must_be_in_pascal_style.symbols = type_symbols dotnet_naming_rule.type_names_must_be_in_pascal_style.style = pascal_style dotnet_naming_rule.type_names_must_be_in_pascal_style.severity = error + dotnet_naming_rule.interface_names_must_have_prefix.symbols = interface_symbols dotnet_naming_rule.interface_names_must_have_prefix.style = i_pascal_style dotnet_naming_rule.interface_names_must_have_prefix.severity = error + dotnet_naming_rule.type_parameter_names_must_have_prefix.symbols = type_parameter_symbols dotnet_naming_rule.type_parameter_names_must_have_prefix.style = t_pascal_style dotnet_naming_rule.type_parameter_names_must_have_prefix.severity = error + dotnet_naming_rule.function_names_must_be_in_pascal_style.symbols = function_symbols dotnet_naming_rule.function_names_must_be_in_pascal_style.style = pascal_style dotnet_naming_rule.function_names_must_be_in_pascal_style.severity = error -dotnet_naming_rule.visible_field_names_must_be_in_pascal_style.symbols = visible_field_symbols -dotnet_naming_rule.visible_field_names_must_be_in_pascal_style.style = pascal_style -dotnet_naming_rule.visible_field_names_must_be_in_pascal_style.severity = error -dotnet_naming_rule.constant_names_must_be_in_pascal_style.symbols = constant_symbols + +dotnet_naming_rule.constant_names_must_be_in_pascal_style.symbols = constant_field_symbols dotnet_naming_rule.constant_names_must_be_in_pascal_style.style = pascal_style dotnet_naming_rule.constant_names_must_be_in_pascal_style.severity = error -dotnet_naming_rule.static_readonly_field_names_must_be_in_pascal_style.symbols = static_readonly_symbols + +dotnet_naming_rule.visible_field_names_must_be_in_pascal_style.symbols = public_field_symbols +dotnet_naming_rule.visible_field_names_must_be_in_pascal_style.style = pascal_style +dotnet_naming_rule.visible_field_names_must_be_in_pascal_style.severity = error + +dotnet_naming_rule.static_readonly_field_names_must_be_in_pascal_style.symbols = static_readonly_field_symbols dotnet_naming_rule.static_readonly_field_names_must_be_in_pascal_style.style = pascal_style dotnet_naming_rule.static_readonly_field_names_must_be_in_pascal_style.severity = error + dotnet_naming_rule.local_names_must_be_in_camel_style.symbols = local_symbols dotnet_naming_rule.local_names_must_be_in_camel_style.style = camel_style dotnet_naming_rule.local_names_must_be_in_camel_style.severity = error -dotnet_naming_rule.private_field_names_must_be_in_camel_style.symbols = private_field_symbols -dotnet_naming_rule.private_field_names_must_be_in_camel_style.style = camel_style -dotnet_naming_rule.private_field_names_must_be_in_camel_style.severity = error + +dotnet_naming_rule.private_field_names_must_have_prefix.symbols = private_field_symbols +dotnet_naming_rule.private_field_names_must_have_prefix.style = u_camel_style +dotnet_naming_rule.private_field_names_must_have_prefix.severity = error + +dotnet_naming_rule.private_static_field_names_must_have_prefix.symbols = private_static_field_symbols +dotnet_naming_rule.private_static_field_names_must_have_prefix.style = su_camel_style +dotnet_naming_rule.private_static_field_names_must_have_prefix.severity = error + +[*.sh] +end_of_line = lf + +[*.{cmd, bat}] +end_of_line = crlf diff --git a/README.md b/README.md index ce613ca..41e945d 100644 --- a/README.md +++ b/README.md @@ -41,6 +41,8 @@ public Person(string name, int age) And this is how we write the same constructor with Guard: ```c# +using Dawn; // Bring Guard into scope. + public Person(string name, int age) { Name = Guard.Argument(name, nameof(name)).NotNull().NotEmpty(); diff --git a/docs/standard-validations.md b/docs/standard-validations.md index d8e9c84..8c0106a 100644 --- a/docs/standard-validations.md +++ b/docs/standard-validations.md @@ -38,6 +38,8 @@ For `ArgumentInfo where T : struct` For `ArgumentInfo where T : IComparable` * `Min(T)` * `Max(T)` +* `GreaterThan(T)` +* `LessThan(T)` * `InRange(T, T)` For `ArgumentInfo where T : struct, IComparable` diff --git a/snippets/guard-cs.vs.snippet b/snippets/guard-cs.vs.snippet index d35a02a..94e720d 100644 --- a/snippets/guard-cs.vs.snippet +++ b/snippets/guard-cs.vs.snippet @@ -944,6 +944,54 @@ + +
+ Guard.GreaterThan(other) + ggt +
+ + + + Dawn + + + + + arg + arg + + + other + other + + + + +
+ +
+ Guard.NotNull().GreaterThan(other) + gxgt +
+ + + + Dawn + + + + + arg + arg + + + other + other + + + + +
Guard.InRange(minValue, maxValue) @@ -1000,6 +1048,54 @@ + +
+ Guard.LessThan(other) + glt +
+ + + + Dawn + + + + + arg + arg + + + other + other + + + + +
+ +
+ Guard.NotNull().LessThan(other) + gxlt +
+ + + + Dawn + + + + + arg + arg + + + other + other + + + + +
Guard.Max(maxValue) diff --git a/src/Guard.Argument.cs b/src/Guard.Argument.cs index db91dbd..cb3f8ec 100644 --- a/src/Guard.Argument.cs +++ b/src/Guard.Argument.cs @@ -1,15 +1,15 @@ #nullable enable +using System; +using System.ComponentModel; +using System.Diagnostics; +using System.Linq.Expressions; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using JetBrains.Annotations; + namespace Dawn { - using System; - using System.ComponentModel; - using System.Diagnostics; - using System.Linq.Expressions; - using System.Runtime.CompilerServices; - using System.Runtime.InteropServices; - using JetBrains.Annotations; - /// Validates argument preconditions. /// Contains the argument initialization methods. public static partial class Guard @@ -98,7 +98,7 @@ public readonly partial struct ArgumentInfo private static readonly string DefaultName = $"The {typeof(T)} argument"; /// The argument name. - private readonly string? name; + private readonly string? _name; /// /// Initializes a new instance of the struct. @@ -120,17 +120,17 @@ public ArgumentInfo( bool modified = false, bool secure = false) { - this.Value = value; - this.name = name; - this.Modified = modified; - this.Secure = secure; + Value = value; + _name = name; + Modified = modified; + Secure = secure; } /// Gets the argument value. public T Value { get; } /// Gets the argument name. - public string Name => this.name ?? DefaultName; + public string Name => _name ?? DefaultName; /// /// Gets a value indicating whether the original method argument is modified before @@ -151,10 +151,10 @@ internal string DebuggerDisplay { get { - var name = this.name; - var value = this.Value?.ToString() ?? "null"; + var name = _name; + var value = Value?.ToString() ?? "null"; var result = name is null ? value : $"{name}: {value}"; - return this.Secure ? $"[SECURE] {result}" : result; + return Secure ? $"[SECURE] {result}" : result; } } @@ -170,7 +170,7 @@ internal string DebuggerDisplay [DebuggerStepThrough] [MethodImpl(MethodImplOptions.AggressiveInlining)] [GuardFunction("Initialization")] - public bool HasValue() => this.Value != null; + public bool HasValue() => Value != null; /// Determines whether the argument value is null. /// @@ -178,12 +178,12 @@ internal string DebuggerDisplay /// [Obsolete("Use the HasValue method to check against null.")] [EditorBrowsable(EditorBrowsableState.Never)] - public bool IsNull() => this.Value is null; + public bool IsNull() => Value is null; /// Returns the string representation of the argument value. /// String representation of the argument value. public override string ToString() - => this.Value?.ToString() ?? string.Empty; + => Value?.ToString() ?? string.Empty; } } } diff --git a/src/Guard.Boolean.cs b/src/Guard.Boolean.cs index 64fc9a1..669c655 100644 --- a/src/Guard.Boolean.cs +++ b/src/Guard.Boolean.cs @@ -1,11 +1,11 @@ #nullable enable +using System; +using System.Diagnostics; +using JetBrains.Annotations; + namespace Dawn { - using System; - using System.Diagnostics; - using JetBrains.Annotations; - /// Provides preconditions for arguments. public static partial class Guard { diff --git a/src/Guard.Double.cs b/src/Guard.Double.cs index 9f535ca..1b51eac 100644 --- a/src/Guard.Double.cs +++ b/src/Guard.Double.cs @@ -1,12 +1,12 @@ #nullable enable +using System; +using System.ComponentModel; +using System.Diagnostics; +using JetBrains.Annotations; + namespace Dawn { - using System; - using System.ComponentModel; - using System.Diagnostics; - using JetBrains.Annotations; - /// Provides preconditions for arguments. public static partial class Guard { diff --git a/src/Guard.Email.cs b/src/Guard.Email.cs index 8b62867..43df28c 100644 --- a/src/Guard.Email.cs +++ b/src/Guard.Email.cs @@ -1,13 +1,13 @@ #if !NETSTANDARD1_0 +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Net.Mail; +using JetBrains.Annotations; + namespace Dawn { - using System; - using System.Collections.Generic; - using System.Diagnostics; - using System.Net.Mail; - using JetBrains.Annotations; - /// Provides preconditions for arguments. public static partial class Guard { diff --git a/src/Guard.Enums.Legacy.cs b/src/Guard.Enums.Legacy.cs index 49754cd..9b74106 100644 --- a/src/Guard.Enums.Legacy.cs +++ b/src/Guard.Enums.Legacy.cs @@ -1,10 +1,10 @@ -namespace Dawn -{ - using System; - using System.Collections.Generic; - using System.Diagnostics; - using JetBrains.Annotations; +using System; +using System.Collections.Generic; +using System.Diagnostics; +using JetBrains.Annotations; +namespace Dawn +{ /// Provides preconditions for arguments. public static partial class Guard { @@ -70,7 +70,7 @@ public readonly struct EnumArgumentInfo /// /// The original argument. internal EnumArgumentInfo(ArgumentInfo argument) - => this.Argument = argument; + => Argument = argument; /// Gets the original argument. public ArgumentInfo Argument { get; } @@ -96,10 +96,10 @@ public static implicit operator T(EnumArgumentInfo argument) [DebuggerStepThrough] public EnumArgumentInfo Defined(Func message = null) { - if (!EnumInfo.Values.Contains(this.Argument.Value)) + if (!EnumInfo.Values.Contains(Argument.Value)) { - var m = message?.Invoke(this.Argument.Value) ?? Messages.EnumDefined(this.Argument); - throw new ArgumentException(m, this.Argument.Name); + var m = message?.Invoke(Argument.Value) ?? Messages.EnumDefined(Argument); + throw new ArgumentException(m, Argument.Name); } return this; @@ -118,10 +118,10 @@ public EnumArgumentInfo Defined(Func message = null) [DebuggerStepThrough] public EnumArgumentInfo None(Func message = null) { - if (!EqualityComparer.Default.Equals(this.Argument.Value, default)) + if (!EqualityComparer.Default.Equals(Argument.Value, default)) { - var m = message?.Invoke(this.Argument.Value) ?? Messages.EnumNone(this.Argument); - throw new ArgumentException(m, this.Argument.Name); + var m = message?.Invoke(Argument.Value) ?? Messages.EnumNone(Argument); + throw new ArgumentException(m, Argument.Name); } return this; @@ -140,10 +140,10 @@ public EnumArgumentInfo None(Func message = null) [DebuggerStepThrough] public EnumArgumentInfo NotNone(Func message = null) { - if (EqualityComparer.Default.Equals(this.Argument.Value, default)) + if (EqualityComparer.Default.Equals(Argument.Value, default)) { - var m = message?.Invoke(this.Argument.Value) ?? Messages.EnumNotNone(this.Argument); - throw new ArgumentException(m, this.Argument.Name); + var m = message?.Invoke(Argument.Value) ?? Messages.EnumNotNone(Argument); + throw new ArgumentException(m, Argument.Name); } return this; @@ -163,10 +163,10 @@ public EnumArgumentInfo NotNone(Func message = null) [DebuggerStepThrough] public EnumArgumentInfo Equal(T other, Func message = null) { - if (!EqualityComparer.Default.Equals(this.Argument.Value, other)) + if (!EqualityComparer.Default.Equals(Argument.Value, other)) { - var m = message?.Invoke(this.Argument.Value, other) ?? Messages.Equal(this.Argument, other); - throw new ArgumentException(m, this.Argument.Name); + var m = message?.Invoke(Argument.Value, other) ?? Messages.Equal(Argument, other); + throw new ArgumentException(m, Argument.Name); } return this; @@ -188,10 +188,10 @@ public EnumArgumentInfo Equal(T other, Func message = null) [DebuggerStepThrough] public EnumArgumentInfo NotEqual(T other, Func message = null) { - if (EqualityComparer.Default.Equals(this.Argument.Value, other)) + if (EqualityComparer.Default.Equals(Argument.Value, other)) { - var m = message?.Invoke(this.Argument.Value) ?? Messages.NotEqual(this.Argument, other); - throw new ArgumentException(m, this.Argument.Name); + var m = message?.Invoke(Argument.Value) ?? Messages.NotEqual(Argument, other); + throw new ArgumentException(m, Argument.Name); } return this; @@ -212,10 +212,10 @@ public EnumArgumentInfo NotEqual(T other, Func message = null) [DebuggerStepThrough] public EnumArgumentInfo HasFlag(T flag, Func message = null) { - if (!EnumInfo.HasFlag(this.Argument.Value, flag)) + if (!EnumInfo.HasFlag(Argument.Value, flag)) { - var m = message?.Invoke(this.Argument.Value, flag) ?? Messages.EnumHasFlag(this.Argument, flag); - throw new ArgumentException(m, this.Argument.Name); + var m = message?.Invoke(Argument.Value, flag) ?? Messages.EnumHasFlag(Argument, flag); + throw new ArgumentException(m, Argument.Name); } return this; @@ -235,10 +235,10 @@ public EnumArgumentInfo HasFlag(T flag, Func message = null) [DebuggerStepThrough] public EnumArgumentInfo DoesNotHaveFlag(T flag, Func message = null) { - if (EnumInfo.HasFlag(this.Argument.Value, flag)) + if (EnumInfo.HasFlag(Argument.Value, flag)) { - var m = message?.Invoke(this.Argument.Value, flag) ?? Messages.EnumDoesNotHaveFlag(this.Argument, flag); - throw new ArgumentException(m, this.Argument.Name); + var m = message?.Invoke(Argument.Value, flag) ?? Messages.EnumDoesNotHaveFlag(Argument, flag); + throw new ArgumentException(m, Argument.Name); } return this; @@ -256,7 +256,7 @@ public readonly struct NullableEnumArgumentInfo /// /// The original argument. internal NullableEnumArgumentInfo(ArgumentInfo argument) - => this.Argument = argument; + => Argument = argument; /// Gets the original argument. public ArgumentInfo Argument { get; } @@ -264,7 +264,7 @@ internal NullableEnumArgumentInfo(ArgumentInfo argument) /// Gets the value of a nullable enum argument. /// The argument whose value to return. /// The value of . - public static implicit operator T? (NullableEnumArgumentInfo argument) + public static implicit operator T?(NullableEnumArgumentInfo argument) => argument.Argument.Value; /// Requires the nullable enum argument to be null. @@ -278,13 +278,13 @@ internal NullableEnumArgumentInfo(ArgumentInfo argument) /// public ArgumentInfo Null(Func message = null) { - if (this.Argument.HasValue()) + if (Argument.HasValue()) { - var m = message?.Invoke(this.Argument.Value.Value) ?? Messages.Null(this.Argument); - throw new ArgumentException(m, this.Argument.Name); + var m = message?.Invoke(Argument.Value.Value) ?? Messages.Null(Argument); + throw new ArgumentException(m, Argument.Name); } - return this.Argument; + return Argument; } /// Requires the nullable enum argument to be not null. @@ -302,7 +302,7 @@ internal NullableEnumArgumentInfo(ArgumentInfo argument) /// public EnumArgumentInfo NotNull(string message = null) #pragma warning disable CS0618 // Type or member is obsolete - => this.Argument.NotNull(message).Enum(); + => Argument.NotNull(message).Enum(); #pragma warning restore CS0618 // Type or member is obsolete @@ -323,7 +323,7 @@ public EnumArgumentInfo NotNull(string message = null) [DebuggerStepThrough] public NullableEnumArgumentInfo Defined(Func message = null) { - if (this.NotNull(out var a)) + if (NotNull(out var a)) a.Defined(message); return this; @@ -344,7 +344,7 @@ public NullableEnumArgumentInfo Defined(Func message = null) [DebuggerStepThrough] public NullableEnumArgumentInfo None(Func message = null) { - if (this.NotNull(out var a)) + if (NotNull(out var a)) a.None(message); return this; @@ -363,7 +363,7 @@ public NullableEnumArgumentInfo None(Func message = null) [DebuggerStepThrough] public NullableEnumArgumentInfo NotNone(Func message = null) { - if (this.NotNull(out var a)) + if (NotNull(out var a)) a.NotNone(message); return this; @@ -385,7 +385,7 @@ public NullableEnumArgumentInfo NotNone(Func message = null) [DebuggerStepThrough] public NullableEnumArgumentInfo Equal(T other, Func message = null) { - if (this.NotNull(out var a)) + if (NotNull(out var a)) a.Equal(other, message); return this; @@ -408,7 +408,7 @@ public NullableEnumArgumentInfo Equal(T other, Func message = n [DebuggerStepThrough] public NullableEnumArgumentInfo NotEqual(T other, Func message = null) { - if (this.NotNull(out var a)) + if (NotNull(out var a)) a.NotEqual(other, message); return this; @@ -432,7 +432,7 @@ public NullableEnumArgumentInfo NotEqual(T other, Func message = n [DebuggerStepThrough] public NullableEnumArgumentInfo HasFlag(T flag, Func message = null) { - if (this.NotNull(out var a)) + if (NotNull(out var a)) a.HasFlag(flag, message); return this; @@ -456,7 +456,7 @@ public NullableEnumArgumentInfo HasFlag(T flag, Func message = [DebuggerStepThrough] public NullableEnumArgumentInfo DoesNotHaveFlag(T flag, Func message = null) { - if (this.NotNull(out var a)) + if (NotNull(out var a)) a.DoesNotHaveFlag(flag, message); return this; @@ -475,14 +475,14 @@ public NullableEnumArgumentInfo DoesNotHaveFlag(T flag, Func me /// private bool NotNull(out EnumArgumentInfo result) { - if (this.Argument.Value.HasValue) + if (Argument.Value.HasValue) { result = new EnumArgumentInfo( new ArgumentInfo( - this.Argument.Value.Value, - this.Argument.Name, - this.Argument.Modified, - this.Argument.Secure)); + Argument.Value.Value, + Argument.Name, + Argument.Modified, + Argument.Secure)); return true; } diff --git a/src/Guard.Enums.cs b/src/Guard.Enums.cs index f265c3f..a2d46ed 100644 --- a/src/Guard.Enums.cs +++ b/src/Guard.Enums.cs @@ -1,13 +1,13 @@ #nullable enable +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq.Expressions; +using JetBrains.Annotations; + namespace Dawn { - using System; - using System.Collections.Generic; - using System.Diagnostics; - using System.Linq.Expressions; - using JetBrains.Annotations; - /// Provides preconditions for arguments. public static partial class Guard { diff --git a/src/Guard.Equality.cs b/src/Guard.Equality.cs index 7f8ab57..e1898f1 100644 --- a/src/Guard.Equality.cs +++ b/src/Guard.Equality.cs @@ -1,14 +1,14 @@ #nullable enable +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Diagnostics; +using System.Runtime.CompilerServices; +using JetBrains.Annotations; + namespace Dawn { - using System; - using System.Collections.Generic; - using System.ComponentModel; - using System.Diagnostics; - using System.Runtime.CompilerServices; - using JetBrains.Annotations; - /// Provides preconditions for arguments. public static partial class Guard { diff --git a/src/Guard.IComparable.cs b/src/Guard.IComparable.cs index 8c35301..69f8b2c 100644 --- a/src/Guard.IComparable.cs +++ b/src/Guard.IComparable.cs @@ -1,13 +1,13 @@ #nullable enable +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Diagnostics; +using JetBrains.Annotations; + namespace Dawn { - using System; - using System.Collections.Generic; - using System.ComponentModel; - using System.Diagnostics; - using JetBrains.Annotations; - /// Provides preconditions for arguments. public static partial class Guard { @@ -90,6 +90,85 @@ public static ref readonly ArgumentInfo Min( return ref argument; } + /// + /// Requires the argument to have a value that is greater than a specified value. + /// + /// The type of the comparable argument. + /// The comparable argument. + /// The value that the argument must be greater than. + /// + /// The factory to initialize the message of the exception that will be thrown if the + /// precondition is not satisfied. + /// + /// . + /// + /// value is less than or equal to + /// and the argument is not modified since it is initialized. + /// + /// + /// value is less than or equal to + /// and the argument is modified after its initialization. + /// + [AssertionMethod] + [DebuggerStepThrough] + [GuardFunction("Comparison", "ggt")] + public static ref readonly ArgumentInfo GreaterThan( + in this ArgumentInfo argument, in T other, Func? message = null) + where T : IComparable? + { + if (argument.HasValue() && Comparer.Default.Compare(argument.Value, other) <= 0) + { + var m = message?.Invoke(argument.Value, other) ?? Messages.GreaterThan(argument, other); + throw Fail(!argument.Modified + ? new ArgumentOutOfRangeException(argument.Name, argument.Secure ? null : argument.Value as object, m) + : new ArgumentException(m, argument.Name)); + } + + return ref argument; + } + + /// + /// Requires the nullable argument to have a value that is null or greater than + /// the specified value. + /// + /// The type of the comparable argument. + /// The comparable argument. + /// The value that the argument must be greater than. + /// + /// The factory to initialize the message of the exception that will be thrown if the + /// precondition is not satisfied. + /// + /// . + /// + /// value is less than or equal to + /// and the argument is not modified since it is initialized. + /// + /// + /// value is less than or equal to + /// and the argument is modified after its initialization. + /// + [AssertionMethod] + [DebuggerStepThrough] + [GuardFunction("Comparison", "ggt")] + public static ref readonly ArgumentInfo GreaterThan( + in this ArgumentInfo argument, in T other, Func? message = null) + where T : struct, IComparable + { + if (argument.HasValue()) + { + var value = argument.GetValueOrDefault(); + if (argument.HasValue() && Comparer.Default.Compare(value, other) <= 0) + { + var m = message?.Invoke(value, other) ?? Messages.GreaterThan(argument, other); + throw Fail(!argument.Modified + ? new ArgumentOutOfRangeException(argument.Name, argument.Secure ? null : value as object, m) + : new ArgumentException(m, argument.Name)); + } + } + + return ref argument; + } + /// /// Requires the argument to have a value that is equal to or lower than a specified value. /// @@ -169,6 +248,85 @@ public static ref readonly ArgumentInfo Max( return ref argument; } + /// + /// Requires the argument to have a value that is less than a specified value. + /// + /// The type of the comparable argument. + /// The comparable argument. + /// The value that the argument must be less than. + /// + /// The factory to initialize the message of the exception that will be thrown if the + /// precondition is not satisfied. + /// + /// . + /// + /// value is greater than or equal to + /// and the argument is not modified since it is initialized. + /// + /// + /// value is greater than or equal to + /// and the argument is modified after its initialization. + /// + [AssertionMethod] + [DebuggerStepThrough] + [GuardFunction("Comparison", "glt")] + public static ref readonly ArgumentInfo LessThan( + in this ArgumentInfo argument, in T other, Func? message = null) + where T : IComparable? + { + if (argument.HasValue() && Comparer.Default.Compare(argument.Value, other) >= 0) + { + var m = message?.Invoke(argument.Value, other) ?? Messages.LessThan(argument, other); + throw Fail(!argument.Modified + ? new ArgumentOutOfRangeException(argument.Name, argument.Secure ? null : argument.Value as object, m) + : new ArgumentException(m, argument.Name)); + } + + return ref argument; + } + + /// + /// Requires the nullable argument to have a value that is null or less than + /// the specified value. + /// + /// The type of the comparable argument. + /// The comparable argument. + /// The value that the argument must be less than. + /// + /// The factory to initialize the message of the exception that will be thrown if the + /// precondition is not satisfied. + /// + /// . + /// + /// value is greater than or equal to + /// and the argument is not modified since it is initialized. + /// + /// + /// value is greater than or equal to + /// and the argument is modified after its initialization. + /// + [AssertionMethod] + [DebuggerStepThrough] + [GuardFunction("Comparison", "glt")] + public static ref readonly ArgumentInfo LessThan( + in this ArgumentInfo argument, in T other, Func? message = null) + where T : struct, IComparable + { + if (argument.HasValue()) + { + var value = argument.GetValueOrDefault(); + if (argument.HasValue() && Comparer.Default.Compare(value, other) >= 0) + { + var m = message?.Invoke(value, other) ?? Messages.GreaterThan(argument, other); + throw Fail(!argument.Modified + ? new ArgumentOutOfRangeException(argument.Name, argument.Secure ? null : value as object, m) + : new ArgumentException(m, argument.Name)); + } + } + + return ref argument; + } + /// /// Requires the argument to have a value that is between the specified minimum and /// maximum values. diff --git a/src/Guard.IEnumerable.cs b/src/Guard.IEnumerable.cs index 2c23c18..c39fb1d 100644 --- a/src/Guard.IEnumerable.cs +++ b/src/Guard.IEnumerable.cs @@ -1,18 +1,18 @@ #nullable enable +using System; +using System.Collections; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Linq.Expressions; +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Threading; +using JetBrains.Annotations; + namespace Dawn { - using System; - using System.Collections; - using System.Collections.Generic; - using System.Diagnostics; - using System.Linq; - using System.Linq.Expressions; - using System.Reflection; - using System.Runtime.CompilerServices; - using System.Threading; - using JetBrains.Annotations; - /// Provides preconditions for arguments. public static partial class Guard { @@ -865,14 +865,14 @@ static int ProxyCount(TCollection collection, int max) /// private static Func InitContainsNull() { - const string Name = "Contains"; + const string name = "Contains"; var collectionType = typeof(TCollection); IEnumerable search; #if NETSTANDARD1_0 - search = collectionType.GetTypeInfo().GetDeclaredMethods(Name).Where(m => m.IsPublic && !m.IsStatic); + search = collectionType.GetTypeInfo().GetDeclaredMethods(name).Where(m => m.IsPublic && !m.IsStatic); #else - search = collectionType.GetMethods(BindingFlags.Public | BindingFlags.Instance).Where(m => m.Name == Name); + search = collectionType.GetMethods(BindingFlags.Public | BindingFlags.Instance).Where(m => m.Name == name); #endif var methods = search.Where(m => m.ReturnType == typeof(bool)).ToList(); @@ -1104,10 +1104,7 @@ private static Func, bool> InitCont { var collectionType = typeof(TCollection); -#pragma warning disable IDE0007 // Use implicit type Type? itemType = typeof(TItem); -#pragma warning restore IDE0007 // Use implicit type - do { var method = collectionType.GetMethod("Contains", new[] { itemType }); diff --git a/src/Guard.Interop.cs b/src/Guard.Interop.cs index ac0448f..ea411cd 100644 --- a/src/Guard.Interop.cs +++ b/src/Guard.Interop.cs @@ -1,13 +1,13 @@ #nullable enable +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using System.Runtime.CompilerServices; + namespace Dawn { - using System; - using System.Collections.Generic; - using System.Linq; - using System.Reflection; - using System.Runtime.CompilerServices; - /// Provides utilities to support legacy frameworks. public static partial class Guard { @@ -150,7 +150,7 @@ namespace System.Runtime.InteropServices { /// Required to use "in" parameters on .NET Standard 1.0. [AttributeUsage(AttributeTargets.Parameter, Inherited = false)] - public sealed class InAttribute : Attribute + internal sealed class InAttribute : Attribute { } } @@ -161,11 +161,11 @@ namespace System.Diagnostics.CodeAnalysis { /// Required for reference nullability annotations. [AttributeUsage(AttributeTargets.Parameter, Inherited = false)] - public sealed class NotNullWhenAttribute : Attribute + internal sealed class NotNullWhenAttribute : Attribute { /// Initializes a new instance of the class. /// If the method returns this value, the associated parameter will not be null. - public NotNullWhenAttribute(bool returnValue) => this.ReturnValue = returnValue; + public NotNullWhenAttribute(bool returnValue) => ReturnValue = returnValue; /// Gets the return value condition. public bool ReturnValue { get; } diff --git a/src/Guard.Member.cs b/src/Guard.Member.cs index b1451ef..edf6cf1 100644 --- a/src/Guard.Member.cs +++ b/src/Guard.Member.cs @@ -1,16 +1,16 @@ #nullable enable +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq.Expressions; +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Threading; +using JetBrains.Annotations; + namespace Dawn { - using System; - using System.Collections.Generic; - using System.Diagnostics; - using System.Linq.Expressions; - using System.Reflection; - using System.Runtime.CompilerServices; - using System.Threading; - using JetBrains.Annotations; - /// Provides routed member preconditions. public static partial class Guard { @@ -298,23 +298,21 @@ public static ArgumentMemberInfo GetMemberInfo(Expressio throw new ArgumentException(m, nameof(lexp)); } -#pragma warning disable IDE0019 // Use pattern matching node.Lock.EnterUpgradeableReadLock(); try { - var info = node.Info as ArgumentMemberInfo; - if (info is null) + if (node.Info is ArgumentMemberInfo info) + return info; + + node.Lock.EnterWriteLock(); + try { - node.Lock.EnterWriteLock(); - try - { - info = new ArgumentMemberInfo((lexp.Body as MemberExpression)!, lexp.Compile()); - node.Info = info; - } - finally - { - node.Lock.ExitWriteLock(); - } + info = new ArgumentMemberInfo((lexp.Body as MemberExpression)!, lexp.Compile()); + node.Info = info; + } + finally + { + node.Lock.ExitWriteLock(); } return info; @@ -323,7 +321,6 @@ public static ArgumentMemberInfo GetMemberInfo(Expressio { node.Lock.ExitUpgradeableReadLock(); } -#pragma warning restore IDE0019 // Use pattern matching } /// Represents a node in a tree of members. @@ -332,8 +329,8 @@ private sealed class Node /// Initializes a new instance of the class. public Node() { - this.Owners = new Dictionary(1); - this.Lock = new ReaderWriterLockSlim(); + Owners = new Dictionary(1); + Lock = new ReaderWriterLockSlim(); } /// Gets the owners of the member that the current node represents. @@ -363,8 +360,8 @@ public ArgumentMemberInfo(MemberExpression mexp, Func getValue) { var memberName = mexp.ToString(); - this.Name = memberName.Substring(memberName.IndexOf('.') + 1); - this.GetValue = getValue; + Name = memberName.Substring(memberName.IndexOf('.') + 1); + GetValue = getValue; } /// Gets the member name. diff --git a/src/Guard.Messages.cs b/src/Guard.Messages.cs index bb2ba40..7ff40d8 100644 --- a/src/Guard.Messages.cs +++ b/src/Guard.Messages.cs @@ -1,18 +1,18 @@ #nullable enable -namespace Dawn -{ - using System; - using System.Collections; - using System.Collections.Generic; - using System.Linq; +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; #if !NETSTANDARD1_0 - using System.Net.Mail; +using System.Net.Mail; #endif +namespace Dawn +{ /// Provides error messages for the common preconditions. public static partial class Guard { @@ -83,9 +83,15 @@ public static string NotCompatible(in ArgumentInfo(in ArgumentInfo argument, in T minValue) => $"{argument.Name} cannot be less than {minValue}."; + public static string GreaterThan(in ArgumentInfo argument, in T other) + => $"{argument.Name} must be greater than {other}."; + public static string Max(in ArgumentInfo argument, in T maxValue) => $"{argument.Name} cannot be greater than {maxValue}."; + public static string LessThan(in ArgumentInfo argument, in T other) + => $"{argument.Name} must be less than {other}."; + public static string Zero(in ArgumentInfo argument) => $"{argument.Name} must be zero."; @@ -283,16 +289,16 @@ public static string EmailDoesNotHaveDisplayName(in ArgumentInfo ar private static string Join(IEnumerable collection) { - const int Max = 5; + const int max = 5; var objects = collection is IEnumerable e ? e.Select(i => $"\"{i}\"") as IEnumerable : collection.Cast(); - var list = objects.Take(Max + 1).ToList(); - var ellipsis = list.Count > Max; + var list = objects.Take(max + 1).ToList(); + var ellipsis = list.Count > max; if (ellipsis) - list.RemoveAt(Max); + list.RemoveAt(max); var result = string.Join(", ", list); if (ellipsis) diff --git a/src/Guard.Modify.cs b/src/Guard.Modify.cs index 412ecd4..70c6c8e 100644 --- a/src/Guard.Modify.cs +++ b/src/Guard.Modify.cs @@ -1,11 +1,11 @@ #nullable enable +using System; +using System.Diagnostics; +using JetBrains.Annotations; + namespace Dawn { - using System; - using System.Diagnostics; - using JetBrains.Annotations; - /// Provides safe modification functions to normalize arguments. public static partial class Guard { diff --git a/src/Guard.Null.cs b/src/Guard.Null.cs index f4a5ce9..3afb7f0 100644 --- a/src/Guard.Null.cs +++ b/src/Guard.Null.cs @@ -1,13 +1,13 @@ #nullable enable +using System; +using System.ComponentModel; +using System.Diagnostics; +using System.Runtime.CompilerServices; +using JetBrains.Annotations; + namespace Dawn { - using System; - using System.ComponentModel; - using System.Diagnostics; - using System.Runtime.CompilerServices; - using JetBrains.Annotations; - /// Nullability preconditions. public static partial class Guard { diff --git a/src/Guard.Require.cs b/src/Guard.Require.cs index 028ceac..838be87 100644 --- a/src/Guard.Require.cs +++ b/src/Guard.Require.cs @@ -1,13 +1,13 @@ #nullable enable +using System; +using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; +using System.Linq.Expressions; +using JetBrains.Annotations; + namespace Dawn { - using System; - using System.Diagnostics; - using System.Diagnostics.CodeAnalysis; - using System.Linq.Expressions; - using JetBrains.Annotations; - /// Provides generic preconditions. public static partial class Guard { @@ -54,10 +54,10 @@ public ArgumentInfo Require( bool condition, Func? message = null) where TException : Exception { - if (this.HasValue() && !condition) + if (HasValue() && !condition) { - var m = message?.Invoke(this.Value) ?? Messages.Require(this); - throw Fail(Exception.Factory(this.Name, m)); + var m = message?.Invoke(Value) ?? Messages.Require(this); + throw Fail(Exception.Factory(Name, m)); } return this; @@ -103,10 +103,10 @@ public ArgumentInfo Require( Func predicate, Func? message = null) where TException : Exception { - if (this.HasValue() && predicate?.Invoke(this.Value) == false) + if (HasValue() && predicate?.Invoke(Value) == false) { - var m = message?.Invoke(this.Value) ?? Messages.Require(this); - throw Fail(Exception.Factory(this.Name, m)); + var m = message?.Invoke(Value) ?? Messages.Require(this); + throw Fail(Exception.Factory(Name, m)); } return this; diff --git a/src/Guard.Scopes.cs b/src/Guard.Scopes.cs index 019d100..47b0bd2 100644 --- a/src/Guard.Scopes.cs +++ b/src/Guard.Scopes.cs @@ -2,12 +2,12 @@ #if !NETSTANDARD1_0 +using System; +using System.Diagnostics; +using System.Threading; + namespace Dawn { - using System; - using System.Diagnostics; - using System.Threading; - /// Provides scoping support. public static partial class Guard { @@ -40,7 +40,7 @@ private sealed class Scope : IDisposable /// /// Contains zero if the instance is not disposed; and one if it is disposed. /// - private int disposed; + private int _disposed; /// Initializes a new instance of the class. /// @@ -52,11 +52,11 @@ private sealed class Scope : IDisposable /// public Scope(Action? exceptionInterceptor, bool propagates) { - this.Parent = Current; + Parent = Current; Current = this; - this.ExceptionInterceptor = exceptionInterceptor; - this.Propagates = propagates; + ExceptionInterceptor = exceptionInterceptor; + Propagates = propagates; } /// Gets the previous scope to restore when the current one is disposed. @@ -83,8 +83,8 @@ public static Scope? Current /// Ends the guarding scope. public void Dispose() { - if (Interlocked.CompareExchange(ref this.disposed, 1, 0) == 0) - Current = this.Parent; + if (Interlocked.CompareExchange(ref _disposed, 1, 0) == 0) + Current = Parent; } } diff --git a/src/Guard.Single.cs b/src/Guard.Single.cs index 99442b9..a99d133 100644 --- a/src/Guard.Single.cs +++ b/src/Guard.Single.cs @@ -1,12 +1,12 @@ #nullable enable +using System; +using System.ComponentModel; +using System.Diagnostics; +using JetBrains.Annotations; + namespace Dawn { - using System; - using System.ComponentModel; - using System.Diagnostics; - using JetBrains.Annotations; - /// Provides preconditions for arguments. public static partial class Guard { diff --git a/src/Guard.State.cs b/src/Guard.State.cs index 78d0cc3..9e31195 100644 --- a/src/Guard.State.cs +++ b/src/Guard.State.cs @@ -1,12 +1,12 @@ #nullable enable +using System; +using System.Diagnostics; +using System.Runtime.CompilerServices; +using JetBrains.Annotations; + namespace Dawn { - using System; - using System.Diagnostics; - using System.Runtime.CompilerServices; - using JetBrains.Annotations; - /// Provides state preconditions. public static partial class Guard { diff --git a/src/Guard.String.cs b/src/Guard.String.cs index f7d91e7..b16ca71 100644 --- a/src/Guard.String.cs +++ b/src/Guard.String.cs @@ -1,12 +1,12 @@ -namespace Dawn +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Runtime.CompilerServices; +using System.Text.RegularExpressions; +using JetBrains.Annotations; + +namespace Dawn { - using System; - using System.Collections.Generic; - using System.Diagnostics; - using System.Runtime.CompilerServices; - using System.Text.RegularExpressions; - using JetBrains.Annotations; - /// Provides preconditions for arguments. public static partial class Guard { diff --git a/src/Guard.Types.cs b/src/Guard.Types.cs index 5952bdd..c4ae172 100644 --- a/src/Guard.Types.cs +++ b/src/Guard.Types.cs @@ -1,14 +1,14 @@ #nullable enable +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq.Expressions; +using System.Threading; +using JetBrains.Annotations; + namespace Dawn { - using System; - using System.Collections.Generic; - using System.Diagnostics; - using System.Linq.Expressions; - using System.Threading; - using JetBrains.Annotations; - /// Provides type preconditions. public static partial class Guard { @@ -155,11 +155,11 @@ public partial struct ArgumentInfo [GuardFunction("Type", "gcomp")] public ArgumentInfo Compatible(Func? message = null) { - if (!this.HasValue() || this.Value is TTarget value) + if (!HasValue() || Value is TTarget value) return this; - var m = message?.Invoke(this.Value) ?? Messages.Compatible(this); - throw Fail(new ArgumentException(m, this.Name)); + var m = message?.Invoke(Value) ?? Messages.Compatible(this); + throw Fail(new ArgumentException(m, Name)); } /// @@ -182,10 +182,10 @@ public ArgumentInfo Compatible(Func? message = null) [GuardFunction("Type", "gncomp")] public ArgumentInfo NotCompatible(Func? message = null) { - if (this.HasValue() && this.Value is TTarget value) + if (HasValue() && Value is TTarget value) { var m = message?.Invoke(value) ?? Messages.NotCompatible(this); - throw Fail(new ArgumentException(m, this.Name)); + throw Fail(new ArgumentException(m, Name)); } return this; @@ -214,11 +214,11 @@ public ArgumentInfo NotCompatible(Func? message = n [GuardFunction("Type", "gcast")] public ArgumentInfo Cast(Func? message = null) { - if (this.Value is TTarget value) + if (Value is TTarget value) return new ArgumentInfo(value, this.Name, this.Modified, this.Secure); - var m = message?.Invoke(this.Value) ?? Messages.Compatible(this); - throw Fail(new ArgumentException(m, this.Name)); + var m = message?.Invoke(Value) ?? Messages.Compatible(this); + throw Fail(new ArgumentException(m, Name)); } } @@ -241,9 +241,7 @@ private static Func InitCanBeInitializedFrom() var isValueType = IsValueType(targetType); var isNullable = !isValueType || targetType.IsGenericType(typeof(Nullable<>)); -#pragma warning disable IDE0007 // Use implicit type Type? type = targetType; -#pragma warning restore IDE0007 // Use implicit type var resultChain = new HashSet(); do { diff --git a/src/Guard.Uri.cs b/src/Guard.Uri.cs index 07ec55c..776b020 100644 --- a/src/Guard.Uri.cs +++ b/src/Guard.Uri.cs @@ -1,10 +1,10 @@ -namespace Dawn -{ - using System; - using System.Diagnostics; - using System.Runtime.CompilerServices; - using JetBrains.Annotations; +using System; +using System.Diagnostics; +using System.Runtime.CompilerServices; +using JetBrains.Annotations; +namespace Dawn +{ /// Provides preconditions for arguments. public static partial class Guard { diff --git a/src/Guard.csproj b/src/Guard.csproj index d988c37..826384d 100644 --- a/src/Guard.csproj +++ b/src/Guard.csproj @@ -5,7 +5,7 @@ netstandard2.0;netstandard1.0 Dawn.Guard Dawn Utils - 1.10.0 + 1.11.0 1.0.0 Dawn true diff --git a/src/GuardFunctionAttribute.cs b/src/GuardFunctionAttribute.cs index dd0bfac..3f8af9f 100644 --- a/src/GuardFunctionAttribute.cs +++ b/src/GuardFunctionAttribute.cs @@ -1,12 +1,12 @@ #nullable enable +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; + namespace Dawn { - using System; - using System.Collections.Generic; - using System.Linq; - using System.Reflection; - /// Marks a target as a function of . [AttributeUsage(AttributeTargets.Method, AllowMultiple = false)] internal sealed class GuardFunctionAttribute : Attribute @@ -25,16 +25,16 @@ internal sealed class GuardFunctionAttribute : Attribute /// public GuardFunctionAttribute(string group, string? shortcut = null, int order = 0) { - this.Group = Guard.Argument(group, nameof(group)) + Group = Guard.Argument(group, nameof(group)) .NotNull() .NotWhiteSpace(); - this.Shortcut = Guard.Argument(shortcut, nameof(shortcut)) + Shortcut = Guard.Argument(shortcut, nameof(shortcut)) .StartsWith("g", StringComparison.Ordinal) .DoesNotStartWith("gx", StringComparison.Ordinal) .MinLength(2); - this.Order = order; + Order = order; } /// The group that the function belongs to. diff --git a/src/NonGuardAttribute.cs b/src/NonGuardAttribute.cs index cc4ad92..a69bfe7 100644 --- a/src/NonGuardAttribute.cs +++ b/src/NonGuardAttribute.cs @@ -1,9 +1,9 @@ #nullable enable +using System; + namespace Dawn { - using System; - /// Marks a method as a non-guarding utility. /// Methods with this attribute are ignored by annotation tests. [AttributeUsage(AttributeTargets.Method)] diff --git a/src/Properties/Annotations.cs b/src/Properties/Annotations.cs index ad023bd..7befbd5 100644 --- a/src/Properties/Annotations.cs +++ b/src/Properties/Annotations.cs @@ -1,7 +1,7 @@ -namespace JetBrains.Annotations -{ - using System; +using System; +namespace JetBrains.Annotations +{ /// /// Indicates that the marked method is an assertion method, i.e. it halts the control flow /// unless a condition is satisfied. @@ -18,7 +18,7 @@ internal sealed class ContractAnnotationAttribute : Attribute /// Initializes a new instance of the class. /// /// The contract string. - public ContractAnnotationAttribute([NotNull] string contract) => this.Contract = contract; + public ContractAnnotationAttribute([NotNull] string contract) => Contract = contract; /// Gets the contract string. [NotNull] diff --git a/tests/BaseTests.cs b/tests/BaseTests.cs index e8b44e8..7a04a0e 100644 --- a/tests/BaseTests.cs +++ b/tests/BaseTests.cs @@ -1,10 +1,10 @@ -namespace Dawn.Tests -{ - using System; - using System.Collections.Generic; - using System.Linq; - using Xunit; +using System; +using System.Collections.Generic; +using System.Linq; +using Xunit; +namespace Dawn.Tests +{ public abstract class BaseTests { private const string Alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; @@ -268,47 +268,47 @@ protected static class RandomUtils private static readonly Random Seeder = new Random(); [ThreadStatic] - private static Random current; + private static Random s_current; public static Random Current { get { - if (current == null) + if (s_current == null) { int seed; lock (Seeder) seed = Seeder.Next(); - current = new Random(seed); + s_current = new Random(seed); } - return current; + return s_current; } } } private sealed class Scope : IDisposable { - private readonly IDisposable scope; + private readonly IDisposable _scope; #pragma warning disable IDE0044 - private Exception lastException; + private Exception _lastException; #pragma warning restore IDE0044 public Scope(bool doNotTestScoping = false) { if (RandomBoolean && !doNotTestScoping) - this.scope = Guard.BeginScope((ex, stackTrace) => this.lastException = ex); + _scope = Guard.BeginScope((ex, stackTrace) => _lastException = ex); } public void CheckException(Exception exception) { - if (this.scope != null && exception != null) - Assert.Same(this.lastException, exception); + if (_scope != null && exception != null) + Assert.Same(_lastException, exception); } - public void Dispose() => this.scope?.Dispose(); + public void Dispose() => _scope?.Dispose(); } } } diff --git a/tests/BooleanTests.cs b/tests/BooleanTests.cs index a2fdab5..3710b69 100644 --- a/tests/BooleanTests.cs +++ b/tests/BooleanTests.cs @@ -1,7 +1,7 @@ -namespace Dawn.Tests -{ - using Xunit; +using Xunit; +namespace Dawn.Tests +{ public sealed class BooleanTests : BaseTests { [Theory(DisplayName = "Boolean: True/False")] diff --git a/tests/ComparableTests.cs b/tests/ComparableTests.cs index 585d4e2..b67c153 100644 --- a/tests/ComparableTests.cs +++ b/tests/ComparableTests.cs @@ -1,40 +1,75 @@ -namespace Dawn.Tests -{ - using Xunit; +using Xunit; +namespace Dawn.Tests +{ public sealed class ComparableTests : BaseTests { [Theory(DisplayName = "Comparable: Min")] [InlineData(null, 3, 4)] [InlineData(3, 3, 4)] [InlineData(3, 2, 5)] - public void Min(int? value, int valueOrLess, int moreThanValue) + public void Min(int? value, int valueOrLess, int greaterThanValue) { var nullableValueArg = Guard.Argument(() => value).Min(valueOrLess); if (!value.HasValue) { - nullableValueArg.Min(moreThanValue); + nullableValueArg.Min(greaterThanValue); return; } ThrowsArgumentOutOfRangeException( nullableValueArg, - arg => arg.Min(moreThanValue), - (arg, message) => arg.Min(moreThanValue, (v, m) => + arg => arg.Min(greaterThanValue), + (arg, message) => arg.Min(greaterThanValue, (v, m) => { Assert.Equal(value, v); - Assert.Equal(moreThanValue, m); + Assert.Equal(greaterThanValue, m); return message; })); var valueArg = Guard.Argument(value.Value, nameof(value)).Min(valueOrLess); ThrowsArgumentOutOfRangeException( valueArg, - arg => arg.Min(moreThanValue), - (arg, message) => arg.Min(moreThanValue, (v, m) => + arg => arg.Min(greaterThanValue), + (arg, message) => arg.Min(greaterThanValue, (v, m) => + { + Assert.Equal(value, v); + Assert.Equal(greaterThanValue, m); + return message; + })); + } + + [Theory(DisplayName = "Comparable: GreaterThan")] + [InlineData(null, 3, 4)] + [InlineData(3, 2, 3)] + [InlineData(3, 1, 4)] + public void GreaterThan(int? value, int lessThanValue, int valueOrGreater) + { + var nullableValueArg = Guard.Argument(() => value).GreaterThan(lessThanValue); + if (!value.HasValue) + { + nullableValueArg.GreaterThan(valueOrGreater); + return; + } + + ThrowsArgumentOutOfRangeException( + nullableValueArg, + arg => arg.GreaterThan(valueOrGreater), + (arg, message) => arg.GreaterThan(valueOrGreater, (v, m) => + { + Assert.Equal(value, v); + Assert.Equal(valueOrGreater, m); + return message; + })); + + var valueArg = Guard.Argument(value.Value, nameof(value)).GreaterThan(lessThanValue); + ThrowsArgumentOutOfRangeException( + valueArg, + arg => arg.GreaterThan(valueOrGreater), + (arg, message) => arg.GreaterThan(valueOrGreater, (v, m) => { Assert.Equal(value, v); - Assert.Equal(moreThanValue, m); + Assert.Equal(valueOrGreater, m); return message; })); } @@ -43,9 +78,9 @@ public void Min(int? value, int valueOrLess, int moreThanValue) [InlineData(null, 3, 2)] [InlineData(3, 3, 2)] [InlineData(3, 4, 1)] - public void Max(int? value, int valueOrMore, int lessThanValue) + public void Max(int? value, int valueOrGreater, int lessThanValue) { - var nullableValueArg = Guard.Argument(() => value).Max(valueOrMore); + var nullableValueArg = Guard.Argument(() => value).Max(valueOrGreater); if (!value.HasValue) { nullableValueArg.Max(lessThanValue); @@ -62,7 +97,7 @@ public void Max(int? value, int valueOrMore, int lessThanValue) return message; })); - var valueArg = Guard.Argument(value.Value, nameof(value)).Max(valueOrMore); + var valueArg = Guard.Argument(value.Value, nameof(value)).Max(valueOrGreater); ThrowsArgumentOutOfRangeException( valueArg, arg => arg.Max(lessThanValue), @@ -74,18 +109,53 @@ public void Max(int? value, int valueOrMore, int lessThanValue) })); } + [Theory(DisplayName = "Comparable: LessThan")] + [InlineData(null, 3, 2)] + [InlineData(3, 4, 3)] + [InlineData(3, 5, 2)] + public void LessThan(int? value, int greaterThanValue, int valueOrLess) + { + var nullableValueArg = Guard.Argument(() => value).LessThan(greaterThanValue); + if (!value.HasValue) + { + nullableValueArg.LessThan(valueOrLess); + return; + } + + ThrowsArgumentOutOfRangeException( + nullableValueArg, + arg => arg.LessThan(valueOrLess), + (arg, message) => arg.LessThan(valueOrLess, (v, m) => + { + Assert.Equal(value, v); + Assert.Equal(valueOrLess, m); + return message; + })); + + var valueArg = Guard.Argument(value.Value, nameof(value)).LessThan(greaterThanValue); + ThrowsArgumentOutOfRangeException( + valueArg, + arg => arg.LessThan(valueOrLess), + (arg, message) => arg.LessThan(valueOrLess, (v, m) => + { + Assert.Equal(value, v); + Assert.Equal(valueOrLess, m); + return message; + })); + } + [Theory(DisplayName = "Comparable: InRange")] [InlineData(null, 2, 4)] [InlineData(3, 2, 4)] [InlineData(3, 1, 5)] - public void InRange(int? value, int lessThanValue, int moreThanValue) + public void InRange(int? value, int lessThanValue, int greaterThanValue) { - var nullableValueArg = Guard.Argument(() => value).InRange(lessThanValue, moreThanValue); + var nullableValueArg = Guard.Argument(() => value).InRange(lessThanValue, greaterThanValue); if (!value.HasValue) { for (var i = 0; i < 2; i++) { - var limit = i == 0 ? lessThanValue : moreThanValue; + var limit = i == 0 ? lessThanValue : greaterThanValue; nullableValueArg.InRange(limit, limit); } @@ -95,17 +165,17 @@ public void InRange(int? value, int lessThanValue, int moreThanValue) nullableValueArg .InRange(lessThanValue, value.Value) .InRange(value.Value, value.Value) - .InRange(value.Value, moreThanValue); + .InRange(value.Value, greaterThanValue); var valueArg = Guard.Argument(value.Value, nameof(value)) .InRange(lessThanValue, value.Value) .InRange(value.Value, value.Value) - .InRange(value.Value, moreThanValue) - .InRange(lessThanValue, moreThanValue); + .InRange(value.Value, greaterThanValue) + .InRange(lessThanValue, greaterThanValue); for (var i = 0; i < 2; i++) { - var limit = i == 0 ? lessThanValue : moreThanValue; + var limit = i == 0 ? lessThanValue : greaterThanValue; ThrowsArgumentOutOfRangeException( nullableValueArg, arg => arg.InRange(limit, limit), diff --git a/tests/DoubleTests.cs b/tests/DoubleTests.cs index 8e0bbf6..9976d45 100644 --- a/tests/DoubleTests.cs +++ b/tests/DoubleTests.cs @@ -1,7 +1,7 @@ -namespace Dawn.Tests -{ - using Xunit; +using Xunit; +namespace Dawn.Tests +{ public sealed class DoubleTests : BaseTests { [Theory(DisplayName = "Double: NaN/NotNaN")] diff --git a/tests/EmailTests.cs b/tests/EmailTests.cs index 3b177c4..6dbf122 100644 --- a/tests/EmailTests.cs +++ b/tests/EmailTests.cs @@ -1,12 +1,12 @@ -namespace Dawn.Tests +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; +using System.Net.Mail; +using Xunit; + +namespace Dawn.Tests { - using System; - using System.Collections; - using System.Collections.Generic; - using System.Linq; - using System.Net.Mail; - using Xunit; - public sealed class EmailTests : BaseTests { [Theory(DisplayName = "Email: HasHost/DoesNotHaveHost")] diff --git a/tests/EnumTests.cs b/tests/EnumTests.cs index cff8381..afba199 100644 --- a/tests/EnumTests.cs +++ b/tests/EnumTests.cs @@ -1,8 +1,8 @@ -namespace Dawn.Tests -{ - using System; - using Xunit; +using System; +using Xunit; +namespace Dawn.Tests +{ public sealed class EnumTests : BaseTests { [Theory(DisplayName = "Enum: Defined")] diff --git a/tests/EnumerableTests.cs b/tests/EnumerableTests.cs index 29af2d9..8faa8b9 100644 --- a/tests/EnumerableTests.cs +++ b/tests/EnumerableTests.cs @@ -1,11 +1,11 @@ -namespace Dawn.Tests -{ - using System; - using System.Collections; - using System.Collections.Generic; - using System.Linq; - using Xunit; +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; +using Xunit; +namespace Dawn.Tests +{ public sealed class EnumerableTests : BaseTests { [Flags] @@ -114,7 +114,7 @@ public void Count(string value, int count, int nonCount) [InlineData(CollectionOptions.HasCount, 3, 3, 4)] [InlineData(CollectionOptions.NotEmpty, 3, 2, 5)] [InlineData(CollectionOptions.HasCount, 3, 2, 5)] - public void MinCount(CollectionOptions options, int count, int countOrLess, int moreThanCount) + public void MinCount(CollectionOptions options, int count, int countOrLess, int greaterThanCount) { var enumerable = GetEnumerable(options, count); var enumerableArg = Guard.Argument(() => enumerable).MinCount(countOrLess); @@ -122,17 +122,17 @@ public void MinCount(CollectionOptions options, int count, int countOrLess, int if (enumerable is null) { - enumerableArg.MinCount(moreThanCount); + enumerableArg.MinCount(greaterThanCount); return; } ThrowsArgumentException( enumerableArg, - arg => arg.MinCount(moreThanCount), - (arg, message) => arg.MinCount(moreThanCount, (e, m) => + arg => arg.MinCount(greaterThanCount), + (arg, message) => arg.MinCount(greaterThanCount, (e, m) => { Assert.Same(enumerable, e); - Assert.Equal(moreThanCount, m); + Assert.Equal(greaterThanCount, m); return message; })); } @@ -176,7 +176,7 @@ public void MaxCount(CollectionOptions options, int count, int countOrMore, int [InlineData(CollectionOptions.HasCount, 3, 2, 4)] [InlineData(CollectionOptions.NotEmpty, 3, 1, 5)] [InlineData(CollectionOptions.HasCount, 3, 1, 5)] - public void CountInRange(CollectionOptions options, int count, int lessThanCount, int moreThanCount) + public void CountInRange(CollectionOptions options, int count, int lessThanCount, int greaterThanCount) { var enumerable = GetEnumerable(options, count); var enumerableArg = Guard.Argument(() => enumerable); @@ -187,17 +187,17 @@ public void CountInRange(CollectionOptions options, int count, int lessThanCount enumerableArg.CountInRange(count, count); CheckAndReset(enumerable, countCalled: true, enumerationCount: count, enumerated: count + 1 != 0); - enumerableArg.CountInRange(count, moreThanCount); - CheckAndReset(enumerable, countCalled: true, enumerationCount: count, enumerated: moreThanCount + 1 != 0); + enumerableArg.CountInRange(count, greaterThanCount); + CheckAndReset(enumerable, countCalled: true, enumerationCount: count, enumerated: greaterThanCount + 1 != 0); - enumerableArg.CountInRange(lessThanCount, moreThanCount); - CheckAndReset(enumerable, countCalled: true, enumerationCount: count, enumerated: moreThanCount + 1 != 0); + enumerableArg.CountInRange(lessThanCount, greaterThanCount); + CheckAndReset(enumerable, countCalled: true, enumerationCount: count, enumerated: greaterThanCount + 1 != 0); if (enumerable is null) { for (var i = 0; i < 2; i++) { - var limit = i == 0 ? lessThanCount : moreThanCount; + var limit = i == 0 ? lessThanCount : greaterThanCount; enumerableArg.CountInRange(limit, limit); } @@ -206,7 +206,7 @@ public void CountInRange(CollectionOptions options, int count, int lessThanCount for (var i = 0; i < 2; i++) { - var limit = i == 0 ? lessThanCount : moreThanCount; + var limit = i == 0 ? lessThanCount : greaterThanCount; ThrowsArgumentException( enumerableArg, arg => arg.CountInRange(limit, limit), @@ -614,8 +614,8 @@ public void DoesNotContainDuplicate( [InlineData(CollectionOptions.Null, 3, 2, -1, false)] [InlineData(CollectionOptions.Null, 3, null, null, false)] [InlineData(CollectionOptions.Null, 3, null, null, true)] - [InlineData(CollectionOptions.NotEmpty, 3, 2, -1, false)] - [InlineData(CollectionOptions.NotEmpty, 3, 2, -1, true)] + [InlineData(CollectionOptions.NotEmpty, 6, 2, -1, false)] + [InlineData(CollectionOptions.NotEmpty, 6, 2, -1, true)] [InlineData(CollectionOptions.NotEmpty, 3, null, -1, false)] [InlineData(CollectionOptions.NotEmpty, 3, null, -1, true)] [InlineData(CollectionOptions.NotEmpty, 3, 2, null, false)] @@ -778,7 +778,7 @@ int GetEnumerationCount(bool found) } bool TestGeneratedMessage(string message) - => secure || enumerable.Items.All(i => message.Contains(i.ToString())); + => secure || enumerable.Items.Take(5).All(i => message.Contains(i.ToString())); } [Theory(DisplayName = "Enumerable: In/NotIn array")] @@ -906,7 +906,7 @@ private static ITestEnumerable GetEnumerable(CollectionOptions options, in public class TestEnumerable : ITestEnumerable { - public TestEnumerable(IEnumerable items) => this.Items = items; + public TestEnumerable(IEnumerable items) => Items = items; public IEnumerable Items { get; } @@ -916,36 +916,36 @@ public class TestEnumerable : ITestEnumerable public IEnumerator GetEnumerator() { - this.Enumerated = true; - foreach (var item in this.Items) + Enumerated = true; + foreach (var item in Items) { - this.EnumerationCount++; + EnumerationCount++; yield return item; } } - IEnumerator IEnumerable.GetEnumerator() => this.GetEnumerator(); + IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); public virtual void Reset() { - this.Enumerated = false; - this.EnumerationCount = 0; + Enumerated = false; + EnumerationCount = 0; } } public class TestEnumerableWithCount : TestEnumerable, ITestEnumerableWithCount { - private readonly int count; + private readonly int _count; public TestEnumerableWithCount(IEnumerable items) - : base(items) => this.count = items.Count(); + : base(items) => _count = items.Count(); public int Count { get { - this.CountCalled = true; - return this.count; + CountCalled = true; + return _count; } } @@ -954,7 +954,7 @@ public int Count public override void Reset() { base.Reset(); - this.CountCalled = false; + CountCalled = false; } } @@ -969,14 +969,14 @@ public TestEnumerableWithContains(IEnumerable items) public bool Contains(T item) { - this.ContainsCalled = true; - return this.Items.Contains(item); + ContainsCalled = true; + return Items.Contains(item); } public override void Reset() { base.Reset(); - this.ContainsCalled = false; + ContainsCalled = false; } } @@ -992,14 +992,14 @@ public TestEnumerableWithCountAndContains(IEnumerable items) public bool Contains(T item) { - this.ContainsCalled = true; - return this.Items.Contains(item); + ContainsCalled = true; + return Items.Contains(item); } public override void Reset() { base.Reset(); - this.ContainsCalled = false; + ContainsCalled = false; } } } diff --git a/tests/EqualityTests.cs b/tests/EqualityTests.cs index a7bc411..bb47f55 100644 --- a/tests/EqualityTests.cs +++ b/tests/EqualityTests.cs @@ -1,8 +1,8 @@ -namespace Dawn.Tests -{ - using System; - using Xunit; +using System; +using Xunit; +namespace Dawn.Tests +{ public sealed class EqualityTests : BaseTests { [Theory(DisplayName = "Equality: Default/NotDefault")] diff --git a/tests/InitializationTests.cs b/tests/InitializationTests.cs index 21e45f3..be5a517 100644 --- a/tests/InitializationTests.cs +++ b/tests/InitializationTests.cs @@ -1,8 +1,8 @@ -namespace Dawn.Tests -{ - using System; - using Xunit; +using System; +using Xunit; +namespace Dawn.Tests +{ public sealed class InitializationTests : BaseTests { [Fact(DisplayName = "Argument: Uninitialized")] diff --git a/tests/MemberTests.cs b/tests/MemberTests.cs index 7490aa9..8aff0bf 100644 --- a/tests/MemberTests.cs +++ b/tests/MemberTests.cs @@ -1,9 +1,9 @@ -namespace Dawn.Tests -{ - using System; - using System.Globalization; - using Xunit; +using System; +using System.Globalization; +using Xunit; +namespace Dawn.Tests +{ public sealed class MemberTests : BaseTests { [Fact(DisplayName = "Member w/o valid expression")] @@ -132,7 +132,7 @@ private sealed class TestObjectWithInaccessibleMember public TestObjectWithInaccessibleMember(Exception accessException) => this.accessException = accessException; - public object InaccessibleMember => throw this.accessException; + public object InaccessibleMember => throw accessException; } } } diff --git a/tests/ModifyTests.cs b/tests/ModifyTests.cs index 1e0e9c8..bed7d37 100644 --- a/tests/ModifyTests.cs +++ b/tests/ModifyTests.cs @@ -1,8 +1,8 @@ -namespace Dawn.Tests -{ - using System; - using Xunit; +using System; +using Xunit; +namespace Dawn.Tests +{ public sealed class ModifyTests : BaseTests { [Fact(DisplayName = "Modify: Modify using value")] @@ -38,7 +38,7 @@ public void ModifyUsingConverter() var exception = new Exception(RandomMessage); Assert.Same(exception, Assert.Throws(() - => stringArg.Modify(s => { throw exception; }))); + => stringArg.Modify(s => throw exception))); } } @@ -59,8 +59,8 @@ public void Wrap() var exception = new Exception(RandomMessage); Assert.DoesNotContain(exception, ThrowsArgumentException( stringArg, - arg => arg.Wrap(s => { throw exception; }), - (arg, message) => arg.Wrap(s => { throw exception; }, s => + arg => arg.Wrap(s => throw exception), + (arg, message) => arg.Wrap(s => throw exception, s => { Assert.Same(stringArg.Value, s); return message; @@ -103,9 +103,7 @@ public TestCloneable() { } -#pragma warning disable IDE0060 // Remove unused parameter - private TestCloneable(bool isClone) => this.IsClone = true; -#pragma warning restore IDE0060 // Remove unused parameter + private TestCloneable(bool isClone) => IsClone = isClone; public bool IsClone { get; } diff --git a/tests/NullTests.cs b/tests/NullTests.cs index 844ec1d..a0777f9 100644 --- a/tests/NullTests.cs +++ b/tests/NullTests.cs @@ -1,8 +1,8 @@ -namespace Dawn.Tests -{ - using System; - using Xunit; +using System; +using Xunit; +namespace Dawn.Tests +{ public sealed class NullTests : BaseTests { [Fact(DisplayName = "Nullable class: Null/NotNull")] diff --git a/tests/RequireTests.cs b/tests/RequireTests.cs index 754e16c..0679e77 100644 --- a/tests/RequireTests.cs +++ b/tests/RequireTests.cs @@ -1,12 +1,12 @@ -namespace Dawn.Tests -{ - using System; - using Xunit; +using System; +using Xunit; +namespace Dawn.Tests +{ public sealed class RequireTests : BaseTests { [ThreadStatic] - private static object currentValue; + private static object s_currentValue; [Theory(DisplayName = "Require: Default exception")] [InlineData(null)] @@ -14,7 +14,7 @@ public sealed class RequireTests : BaseTests [InlineData("A")] public void RequireDefaultException(T value) { - currentValue = value; + s_currentValue = value; var valueArg = Guard.Argument(() => value).Require(true).Require(Success); if (value == null) { @@ -47,7 +47,7 @@ public void RequireDefaultException(T value) [InlineData("A")] public void RequireArgumentExceptionWithMessage(T value) { - currentValue = value; + s_currentValue = value; var valueArg = Guard.Argument(() => value) .Require(true) .Require(Success); @@ -86,7 +86,7 @@ public void RequireArgumentExceptionWithMessage(T value) [InlineData("A")] public void RequireArgumentExceptionWithoutMessage(T value) { - currentValue = value; + s_currentValue = value; var valueArg = Guard.Argument(() => value) .Require(true) .Require(Success); @@ -125,7 +125,7 @@ public void RequireArgumentExceptionWithoutMessage(T value) [InlineData("A")] public void RequireCommonExceptionWithMessage(T value) { - currentValue = value; + s_currentValue = value; var valueArg = Guard.Argument(() => value) .Require(true) .Require(Success); @@ -164,7 +164,7 @@ public void RequireCommonExceptionWithMessage(T value) [InlineData("A")] public void RequireCommonExceptionWithoutMessage(T value) { - currentValue = value; + s_currentValue = value; var valueArg = Guard.Argument(() => value) .Require(true) .Require(Success); @@ -203,7 +203,7 @@ public void RequireCommonExceptionWithoutMessage(T value) [InlineData("A")] public void RequireExceptionWithoutConstructor(T value) { - currentValue = value; + s_currentValue = value; var valueArg = Guard.Argument(() => value) .Require(true) .Require(Success); @@ -288,13 +288,13 @@ public void RequireExceptionWithoutConstructor(T value) private static bool Success(T v) { - Assert.Equal((T)currentValue, v); + Assert.Equal((T)s_currentValue, v); return true; } private static bool Fail(T v) { - Assert.Equal((T)currentValue, v); + Assert.Equal((T)s_currentValue, v); return false; } diff --git a/tests/ScopeTests.cs b/tests/ScopeTests.cs index c29ab9d..466147e 100644 --- a/tests/ScopeTests.cs +++ b/tests/ScopeTests.cs @@ -1,12 +1,12 @@ -namespace Dawn.Tests +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using System.Threading.Tasks; +using Xunit; + +namespace Dawn.Tests { - using System; - using System.Collections.Generic; - using System.Linq; - using System.Reflection; - using System.Threading.Tasks; - using Xunit; - public sealed class ScopeTests : BaseTests { [Fact(DisplayName = "Scopes: BeginScope")] diff --git a/tests/SingleTests.cs b/tests/SingleTests.cs index 1e56387..3851fea 100644 --- a/tests/SingleTests.cs +++ b/tests/SingleTests.cs @@ -1,7 +1,7 @@ -namespace Dawn.Tests -{ - using Xunit; +using Xunit; +namespace Dawn.Tests +{ public sealed class SingleTests : BaseTests { [Theory(DisplayName = "Single: NaN/NotNaN")] diff --git a/tests/StateTests.cs b/tests/StateTests.cs index dab46a4..a4d169f 100644 --- a/tests/StateTests.cs +++ b/tests/StateTests.cs @@ -1,8 +1,8 @@ -namespace Dawn.Tests -{ - using System; - using Xunit; +using System; +using Xunit; +namespace Dawn.Tests +{ public sealed class StateTests : BaseTests { [Fact(DisplayName = "State: Operation")] diff --git a/tests/StringTests.cs b/tests/StringTests.cs index 7b6b90e..7a7c877 100644 --- a/tests/StringTests.cs +++ b/tests/StringTests.cs @@ -1,10 +1,10 @@ -namespace Dawn.Tests -{ - using System; - using System.Text.RegularExpressions; - using System.Threading.Tasks; - using Xunit; +using System; +using System.Text.RegularExpressions; +using System.Threading.Tasks; +using Xunit; +namespace Dawn.Tests +{ public sealed class StringTests : BaseTests { private static readonly TimeSpan MatchTimeout = TimeSpan.FromMilliseconds(10); @@ -118,23 +118,23 @@ public void Length(string value, int length, int nonLength) [InlineData("", 0, 1)] [InlineData("ABC", 3, 4)] [InlineData("DEF", 2, 5)] - public void MinLength(string value, int lengthOrLess, int moreThanLength) + public void MinLength(string value, int lengthOrLess, int greaterThanLength) { var valueArg = Guard.Argument(() => value).MinLength(lengthOrLess); if (value is null) { - valueArg.MinLength(moreThanLength); + valueArg.MinLength(greaterThanLength); return; } ThrowsArgumentException( valueArg, - arg => arg.MinLength(moreThanLength), - (arg, message) => arg.MinLength(moreThanLength, (v, m) => + arg => arg.MinLength(greaterThanLength), + (arg, message) => arg.MinLength(greaterThanLength, (v, m) => { Assert.Same(value, v); - Assert.Equal(moreThanLength, m); + Assert.Equal(greaterThanLength, m); return message; })); } @@ -170,20 +170,20 @@ public void MaxLength(string value, int lengthOrMore, int lessThanLength) [InlineData("", -1, 1)] [InlineData("ABC", 2, 4)] [InlineData("DEF", 1, 5)] - public void LengthInRange(string value, int lessThanLength, int moreThanLength) + public void LengthInRange(string value, int lessThanLength, int greaterThanLength) { var length = value?.Length ?? RandomNumber; var valueArg = Guard.Argument(() => value) .LengthInRange(lessThanLength, length) .LengthInRange(length, length) - .LengthInRange(length, moreThanLength) - .LengthInRange(lessThanLength, moreThanLength); + .LengthInRange(length, greaterThanLength) + .LengthInRange(lessThanLength, greaterThanLength); if (value is null) { for (var i = 0; i < 2; i++) { - var limit = i == 0 ? lessThanLength : moreThanLength; + var limit = i == 0 ? lessThanLength : greaterThanLength; valueArg.LengthInRange(limit, limit); } @@ -192,7 +192,7 @@ public void LengthInRange(string value, int lessThanLength, int moreThanLength) for (var i = 0; i < 2; i++) { - var limit = i == 0 ? lessThanLength : moreThanLength; + var limit = i == 0 ? lessThanLength : greaterThanLength; ThrowsArgumentException( valueArg, arg => arg.LengthInRange(limit, limit), diff --git a/tests/TypeTests.cs b/tests/TypeTests.cs index 5e328c6..934c7b5 100644 --- a/tests/TypeTests.cs +++ b/tests/TypeTests.cs @@ -1,8 +1,8 @@ -namespace Dawn.Tests -{ - using System.IO; - using Xunit; +using System.IO; +using Xunit; +namespace Dawn.Tests +{ public sealed class TypeTests : BaseTests { [Theory(DisplayName = "Type: Type/NotType (generic class)")] diff --git a/tests/UriTests.cs b/tests/UriTests.cs index c8aae4a..576ed61 100644 --- a/tests/UriTests.cs +++ b/tests/UriTests.cs @@ -1,8 +1,8 @@ -namespace Dawn.Tests -{ - using System; - using Xunit; +using System; +using Xunit; +namespace Dawn.Tests +{ public sealed class UriTests : BaseTests { private const string HttpUriScheme = "http"; // Uri.HttpUriScheme diff --git a/utils/Program.cs b/utils/Program.cs index a447e15..8851e9b 100644 --- a/utils/Program.cs +++ b/utils/Program.cs @@ -1,9 +1,9 @@ -namespace Dawn.Utils -{ - using System; - using System.Threading; - using System.Threading.Tasks; +using System; +using System.Threading; +using System.Threading.Tasks; +namespace Dawn.Utils +{ public static class Program { public static async Task Main(string[] args) diff --git a/utils/Snippet.cs b/utils/Snippet.cs index 92a7612..9afca61 100644 --- a/utils/Snippet.cs +++ b/utils/Snippet.cs @@ -1,26 +1,26 @@ -namespace Dawn.Utils +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Text; +using System.Threading; +using System.Threading.Tasks; +using System.Xml.Linq; +using Dawn; + +namespace Dawn.Utils { - using System; - using System.Collections.Generic; - using System.IO; - using System.Linq; - using System.Reflection; - using System.Runtime.CompilerServices; - using System.Text; - using System.Threading; - using System.Threading.Tasks; - using System.Xml.Linq; - using Dawn; - internal abstract class Snippet { private static readonly Encoding Encoding = new UTF8Encoding(false); public Snippet(MethodInfo method, GuardFunctionAttribute attribute, string shortcut) { - this.Method = method; - this.Attribute = attribute; - this.Shortcut = shortcut; + Method = method; + Attribute = attribute; + Shortcut = shortcut; } public MethodInfo Method { get; } @@ -57,12 +57,12 @@ public sealed class VisualStudioSnippet : Snippet private static readonly XNamespace Namespace = "http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet"; - private readonly XElement element; + private readonly XElement _element; private VisualStudioSnippet( MethodInfo method, GuardFunctionAttribute attribute, string shortcut, XElement element) : base(method, attribute, shortcut) - => this.element = element; + => _element = element; public static IEnumerable CreateSnippets(MethodInfo method, GuardFunctionAttribute attribute) { @@ -199,7 +199,7 @@ public static XDocument CreateDocument(IEnumerable snippets { var elements = snippets .GroupBy(s => s.Shortcut) - .Select(g => new XElement(g.First().element)); + .Select(g => new XElement(g.First()._element)); return new XDocument(Declaration, new XElement(Namespace + "CodeSnippets", elements)); }