Skip to content
This repository has been archived by the owner on Jul 18, 2023. It is now read-only.

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
safakgur committed Mar 31, 2020
2 parents 64564ce + 2a79a90 commit ac05452
Show file tree
Hide file tree
Showing 8 changed files with 313 additions and 18 deletions.
8 changes: 4 additions & 4 deletions .azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@ jobs:
- template: .azure-pipelines-build.yml
parameters:
name: Windows
image: windows-2019
image: windows-latest
artifacts: true

- template: .azure-pipelines-build.yml
parameters:
name: macOS
image: macOS-10.13
image: macOS-latest

- template: .azure-pipelines-build.yml
parameters:
name: Linux
image: Ubuntu-16.04
image: ubuntu-latest

- job: Deploy
pool:
vmImage: macOS-10.13
vmImage: ubuntu-latest

dependsOn:
- Windows
Expand Down
19 changes: 13 additions & 6 deletions docs/standard-validations.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ For `ArgumentInfo<T|T?> where T : struct, IComparable<T>`
* `Negative()`
* `NotNegative()`

### Boolean Guards

For `ArgumentInfo<bool|bool?>`
* `True()`
* `False()`

### Collection Guards

For `ArgumentInfo<T> where T : IEnumerable`
Expand Down Expand Up @@ -104,6 +110,13 @@ For `ArgumentInfo<string>`
* `DoesNotMatch(string, TimeSpan)`
* `DoesNotMatch(Regex)`

### Time Guards

For `ArgumentInfo<DateTime|DateTime?>`

* `KindSpecified()`
* `KindUnspecified()`

### Floating-Point Number Guards

For `ArgumentInfo<float|float?|double|double?>`
Expand All @@ -118,12 +131,6 @@ For `ArgumentInfo<float|float?|double|double?>`
* `Equal(T, T)` - Approx. equality.
* `NotEqual(T, T)` - Approx. unequality.

### Boolean Guards

For `ArgumentInfo<bool|bool?>`
* `True()`
* `False()`

### URI Guards

For `ArgumentInfo<Uri>`
Expand Down
80 changes: 80 additions & 0 deletions snippets/guard-cs.vs.snippet
Original file line number Diff line number Diff line change
Expand Up @@ -1432,6 +1432,86 @@
<Code Language="csharp"><![CDATA[Guard.Argument($arg$, nameof($arg$)).NotNull().Zero()$end$;]]></Code>
</Snippet>
</CodeSnippet>
<CodeSnippet Format="1.0.0">
<Header>
<Title>Guard.KindSpecified()</Title>
<Shortcut>gtks</Shortcut>
</Header>
<Snippet>
<Imports>
<Import>
<Namespace>Dawn</Namespace>
</Import>
</Imports>
<Declarations>
<Literal>
<ID>arg</ID>
<Default>arg</Default>
</Literal>
</Declarations>
<Code Language="csharp"><![CDATA[Guard.Argument($arg$, nameof($arg$)).KindSpecified()$end$;]]></Code>
</Snippet>
</CodeSnippet>
<CodeSnippet Format="1.0.0">
<Header>
<Title>Guard.NotNull().KindSpecified()</Title>
<Shortcut>gxtks</Shortcut>
</Header>
<Snippet>
<Imports>
<Import>
<Namespace>Dawn</Namespace>
</Import>
</Imports>
<Declarations>
<Literal>
<ID>arg</ID>
<Default>arg</Default>
</Literal>
</Declarations>
<Code Language="csharp"><![CDATA[Guard.Argument($arg$, nameof($arg$)).NotNull().KindSpecified()$end$;]]></Code>
</Snippet>
</CodeSnippet>
<CodeSnippet Format="1.0.0">
<Header>
<Title>Guard.KindUnspecified()</Title>
<Shortcut>gtku</Shortcut>
</Header>
<Snippet>
<Imports>
<Import>
<Namespace>Dawn</Namespace>
</Import>
</Imports>
<Declarations>
<Literal>
<ID>arg</ID>
<Default>arg</Default>
</Literal>
</Declarations>
<Code Language="csharp"><![CDATA[Guard.Argument($arg$, nameof($arg$)).KindUnspecified()$end$;]]></Code>
</Snippet>
</CodeSnippet>
<CodeSnippet Format="1.0.0">
<Header>
<Title>Guard.NotNull().KindUnspecified()</Title>
<Shortcut>gxtku</Shortcut>
</Header>
<Snippet>
<Imports>
<Import>
<Namespace>Dawn</Namespace>
</Import>
</Imports>
<Declarations>
<Literal>
<ID>arg</ID>
<Default>arg</Default>
</Literal>
</Declarations>
<Code Language="csharp"><![CDATA[Guard.Argument($arg$, nameof($arg$)).NotNull().KindUnspecified()$end$;]]></Code>
</Snippet>
</CodeSnippet>
<CodeSnippet Format="1.0.0">
<Header>
<Title>Guard.Equal(other, delta)</Title>
Expand Down
134 changes: 134 additions & 0 deletions src/Guard.DateTime.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
#nullable enable

using System;
using System.Diagnostics;
using JetBrains.Annotations;

namespace Dawn
{
/// <content>Provides preconditions for <see cref="DateTime" /> arguments.</content>
public static partial class Guard
{
/// <summary>
/// Requires the date-time argument to have its <see cref="DateTime.Kind" /> specified,
/// i.e. not to have it as <see cref="DateTimeKind.Unspecified" />.
/// </summary>
/// <param name="argument">The argument.</param>
/// <param name="message">
/// The message of the exception that will be thrown if the precondition is not satisfied.
/// </param>
/// <returns><paramref name="argument" />.</returns>
/// <exception cref="ArgumentException">
/// <paramref name="argument" />'s <see cref="DateTime.Kind" /> property is
/// <see cref="DateTimeKind.Unspecified" />.
/// </exception>
[AssertionMethod]
[DebuggerStepThrough]
[GuardFunction("DateTime", "gtks")]
public static ref readonly ArgumentInfo<DateTime> KindSpecified(
in this ArgumentInfo<DateTime> argument, Func<DateTime, string>? message = null)
{
if (argument.Value.Kind == DateTimeKind.Unspecified)
{
var m = message?.Invoke(argument.Value) ?? Messages.KindSpecified(argument);
throw Fail(new ArgumentException(m, argument.Name));
}

return ref argument;
}

/// <summary>
/// Requires the date-time argument to either be <c>null</c> or have its
/// <see cref="DateTime.Kind" /> specified, i.e. not to have it as
/// <see cref="DateTimeKind.Unspecified" />.
/// </summary>
/// <param name="argument">The argument.</param>
/// <param name="message">
/// The message of the exception that will be thrown if the precondition is not satisfied.
/// </param>
/// <returns><paramref name="argument" />.</returns>
/// <exception cref="ArgumentException">
/// <paramref name="argument" />'s <see cref="DateTime.Kind" /> property is
/// <see cref="DateTimeKind.Unspecified" />.
/// </exception>
[AssertionMethod]
[DebuggerStepThrough]
[GuardFunction("DateTime", "gtks")]
public static ref readonly ArgumentInfo<DateTime?> KindSpecified(
in this ArgumentInfo<DateTime?> argument, Func<DateTime, string>? message = null)
{
if (argument.HasValue())
{
var value = argument.GetValueOrDefault();
if (value.Kind == DateTimeKind.Unspecified)
{
var m = message?.Invoke(value) ?? Messages.KindSpecified(argument);
throw Fail(new ArgumentException(m, argument.Name));
}
}

return ref argument;
}

/// <summary>
/// Requires the date-time argument not to have its <see cref="DateTime.Kind" />
/// specified, i.e. to have it as <see cref="DateTimeKind.Unspecified" />.
/// </summary>
/// <param name="argument">The argument.</param>
/// <param name="message">
/// The message of the exception that will be thrown if the precondition is not satisfied.
/// </param>
/// <returns><paramref name="argument" />.</returns>
/// <exception cref="ArgumentException">
/// <paramref name="argument" />'s <see cref="DateTime.Kind" /> property is not
/// <see cref="DateTimeKind.Unspecified" />.
/// </exception>
[AssertionMethod]
[DebuggerStepThrough]
[GuardFunction("DateTime", "gtku")]
public static ref readonly ArgumentInfo<DateTime> KindUnspecified(
in this ArgumentInfo<DateTime> argument, Func<DateTime, string>? message = null)
{
if (argument.Value.Kind != DateTimeKind.Unspecified)
{
var m = message?.Invoke(argument.Value) ?? Messages.KindUnspecified(argument);
throw Fail(new ArgumentException(m, argument.Name));
}

return ref argument;
}

/// <summary>
/// Requires the date-time argument either to be <c>null</c> or not to have its
/// <see cref="DateTime.Kind" /> specified, i.e. to have it as
/// <see cref="DateTimeKind.Unspecified" />.
/// </summary>
/// <param name="argument">The argument.</param>
/// <param name="message">
/// The message of the exception that will be thrown if the precondition is not satisfied.
/// </param>
/// <returns><paramref name="argument" />.</returns>
/// <exception cref="ArgumentException">
/// <paramref name="argument" />'s <see cref="DateTime.Kind" /> property is not
/// <see cref="DateTimeKind.Unspecified" />.
/// </exception>
[AssertionMethod]
[DebuggerStepThrough]
[GuardFunction("DateTime", "gtku")]
public static ref readonly ArgumentInfo<DateTime?> KindUnspecified(
in this ArgumentInfo<DateTime?> argument, Func<DateTime, string>? message = null)
{
if (argument.HasValue())
{
var value = argument.GetValueOrDefault();
if (value.Kind != DateTimeKind.Unspecified)
{
var m = message?.Invoke(value) ?? Messages.KindUnspecified(argument);
throw Fail(new ArgumentException(m, argument.Name));
}
}

return ref argument;
}
}
}
15 changes: 10 additions & 5 deletions src/Guard.IEnumerable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1047,11 +1047,16 @@ NewExpression GetSetNew(ParameterExpression? comparerParam)

var countGetter = GetCountGetter();
if (countGetter != null)
{
var setCtor = setType.GetConstructor(new[] { typeof(int) });
var countCall = Expression.Call(collectionParam, countGetter);
return Expression.New(setCtor, countCall);
}
try
{
var setCtor = setType.GetConstructor(new[] { typeof(int) });
var countCall = Expression.Call(collectionParam, countGetter);
return Expression.New(setCtor, countCall);
}
catch (Exception)
{
// Swallow and use default
}

return Expression.New(setType);
}
Expand Down
8 changes: 6 additions & 2 deletions src/Guard.Messages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,6 @@ public static string UriHttps(in ArgumentInfo<Uri> argument)
=> $"{argument.Name} must be an absolute URI with the HTTPS scheme.";

#if !NETSTANDARD1_0

public static string EmailHasHost(in ArgumentInfo<MailAddress> argument, string host)
=> argument.Secure ? Require(argument) : $"{argument.Name} must have the host '{host}'.";

Expand All @@ -282,9 +281,14 @@ public static string EmailHasDisplayName(in ArgumentInfo<MailAddress> argument)

public static string EmailDoesNotHaveDisplayName(in ArgumentInfo<MailAddress> argument)
=> $"{argument.Name} cannot have a display name specified.";

#endif

public static string KindSpecified<T>(in ArgumentInfo<T> argument)
=> $"{argument.Name} must have its kind specified.";

public static string KindUnspecified<T>(in ArgumentInfo<T> argument)
=> $"{argument.Name} cannot have its kind specified.";

private static string ToString(object? obj) => obj?.ToString() ?? "null";

private static string Join(IEnumerable collection)
Expand Down
2 changes: 1 addition & 1 deletion src/Guard.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<TargetFrameworks>netstandard2.0;netstandard1.0</TargetFrameworks>
<AssemblyName>Dawn.Guard</AssemblyName>
<Product>Dawn Utils</Product>
<Version>1.11.0</Version>
<Version>1.12.0</Version>
<FileVersion>1.0.0</FileVersion>
<RootNamespace>Dawn</RootNamespace>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
Expand Down
Loading

0 comments on commit ac05452

Please sign in to comment.