From d93631bd1a8d40e6569fb7b248da33d1a5262019 Mon Sep 17 00:00:00 2001 From: Pascal Berger Date: Mon, 27 Jan 2020 13:06:32 +0100 Subject: [PATCH 01/14] Update bootstrapper --- build.ps1 | 119 ++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 85 insertions(+), 34 deletions(-) diff --git a/build.ps1 b/build.ps1 index bdfb32b..48a6534 100644 --- a/build.ps1 +++ b/build.ps1 @@ -5,11 +5,14 @@ ########################################################################## <# + .SYNOPSIS This is a Powershell script to bootstrap a Cake build. + .DESCRIPTION This Powershell script will download NuGet if missing, restore NuGet tools (including Cake) and execute your Cake build script with the parameters you provide. + .PARAMETER Script The build script to execute. .PARAMETER Target @@ -18,19 +21,22 @@ The build script target to run. The build configuration to use. .PARAMETER Verbosity Specifies the amount of information to be displayed. +.PARAMETER ShowDescription +Shows description about tasks. +.PARAMETER DryRun +Performs a dry run. .PARAMETER Experimental -Tells Cake to use the latest Roslyn release. -.PARAMETER WhatIf -Performs a dry run of the build script. -No tasks will be executed. +Uses the nightly builds of the Roslyn script engine. .PARAMETER Mono -Tells Cake to use the Mono scripting engine. +Uses the Mono Compiler rather than the Roslyn script engine. .PARAMETER SkipToolPackageRestore Skips restoring of packages. .PARAMETER ScriptArgs Remaining arguments are added here. + .LINK -http://cakebuild.net +https://cakebuild.net + #> [CmdletBinding()] @@ -41,9 +47,10 @@ Param( [string]$Configuration = "Release", [ValidateSet("Quiet", "Minimal", "Normal", "Verbose", "Diagnostic")] [string]$Verbosity = "Verbose", + [switch]$ShowDescription, + [Alias("WhatIf", "Noop")] + [switch]$DryRun, [switch]$Experimental, - [Alias("DryRun","Noop")] - [switch]$WhatIf, [switch]$Mono, [switch]$SkipToolPackageRestore, [Parameter(Position=0,Mandatory=$false,ValueFromRemainingArguments=$true)] @@ -75,6 +82,15 @@ function MD5HashFile([string] $filePath) } } +function GetProxyEnabledWebClient +{ + $wc = New-Object System.Net.WebClient + $proxy = [System.Net.WebRequest]::GetSystemWebProxy() + $proxy.Credentials = [System.Net.CredentialCache]::DefaultCredentials + $wc.Proxy = $proxy + return $wc +} + Write-Host "Preparing to run build script..." if(!$PSScriptRoot){ @@ -82,31 +98,15 @@ if(!$PSScriptRoot){ } $TOOLS_DIR = Join-Path $PSScriptRoot "tools" +$ADDINS_DIR = Join-Path $TOOLS_DIR "Addins" +$MODULES_DIR = Join-Path $TOOLS_DIR "Modules" $NUGET_EXE = Join-Path $TOOLS_DIR "nuget.exe" $CAKE_EXE = Join-Path $TOOLS_DIR "Cake/Cake.exe" $NUGET_URL = "https://dist.nuget.org/win-x86-commandline/latest/nuget.exe" $PACKAGES_CONFIG = Join-Path $TOOLS_DIR "packages.config" $PACKAGES_CONFIG_MD5 = Join-Path $TOOLS_DIR "packages.config.md5sum" - -# Should we use mono? -$UseMono = ""; -if($Mono.IsPresent) { - Write-Verbose -Message "Using the Mono based scripting engine." - $UseMono = "-mono" -} - -# Should we use the new Roslyn? -$UseExperimental = ""; -if($Experimental.IsPresent -and !($Mono.IsPresent)) { - Write-Verbose -Message "Using experimental version of Roslyn." - $UseExperimental = "-experimental" -} - -# Is this a dry run? -$UseDryRun = ""; -if($WhatIf.IsPresent) { - $UseDryRun = "-dryrun" -} +$ADDINS_PACKAGES_CONFIG = Join-Path $ADDINS_DIR "packages.config" +$MODULES_PACKAGES_CONFIG = Join-Path $MODULES_DIR "packages.config" # Make sure tools folder exists if ((Test-Path $PSScriptRoot) -and !(Test-Path $TOOLS_DIR)) { @@ -117,7 +117,9 @@ if ((Test-Path $PSScriptRoot) -and !(Test-Path $TOOLS_DIR)) { # Make sure that packages.config exist. if (!(Test-Path $PACKAGES_CONFIG)) { Write-Verbose -Message "Downloading packages.config..." - try { (New-Object System.Net.WebClient).DownloadFile("http://cakebuild.net/download/bootstrapper/packages", $PACKAGES_CONFIG) } catch { + try { + $wc = GetProxyEnabledWebClient + $wc.DownloadFile("https://cakebuild.net/download/bootstrapper/packages", $PACKAGES_CONFIG) } catch { Throw "Could not download packages.config." } } @@ -125,7 +127,7 @@ if (!(Test-Path $PACKAGES_CONFIG)) { # Try find NuGet.exe in path if not exists if (!(Test-Path $NUGET_EXE)) { Write-Verbose -Message "Trying to find nuget.exe in PATH..." - $existingPaths = $Env:Path -Split ';' | Where-Object { (![string]::IsNullOrEmpty($_)) -and (Test-Path $_) } + $existingPaths = $Env:Path -Split ';' | Where-Object { (![string]::IsNullOrEmpty($_)) -and (Test-Path $_ -PathType Container) } $NUGET_EXE_IN_PATH = Get-ChildItem -Path $existingPaths -Filter "nuget.exe" | Select -First 1 if ($NUGET_EXE_IN_PATH -ne $null -and (Test-Path $NUGET_EXE_IN_PATH.FullName)) { Write-Verbose -Message "Found in PATH at $($NUGET_EXE_IN_PATH.FullName)." @@ -137,7 +139,8 @@ if (!(Test-Path $NUGET_EXE)) { if (!(Test-Path $NUGET_EXE)) { Write-Verbose -Message "Downloading NuGet.exe..." try { - (New-Object System.Net.WebClient).DownloadFile($NUGET_URL, $NUGET_EXE) + $wc = GetProxyEnabledWebClient + $wc.DownloadFile($NUGET_URL, $NUGET_EXE) } catch { Throw "Could not download NuGet.exe." } @@ -160,16 +163,51 @@ if(-Not $SkipToolPackageRestore.IsPresent) { } Write-Verbose -Message "Restoring tools from NuGet..." - $NuGetOutput = Invoke-Expression "&`"$NUGET_EXE`" install -ExcludeVersion -PreRelease -OutputDirectory `"$TOOLS_DIR`" -Source https://www.myget.org/F/cake/api/v3/index.json" + $NuGetOutput = Invoke-Expression "&`"$NUGET_EXE`" install -ExcludeVersion -OutputDirectory `"$TOOLS_DIR`"" if ($LASTEXITCODE -ne 0) { - Throw "An error occured while restoring NuGet tools." + Throw "An error occurred while restoring NuGet tools." } else { $md5Hash | Out-File $PACKAGES_CONFIG_MD5 -Encoding "ASCII" } Write-Verbose -Message ($NuGetOutput | out-string) + + Pop-Location +} + +# Restore addins from NuGet +if (Test-Path $ADDINS_PACKAGES_CONFIG) { + Push-Location + Set-Location $ADDINS_DIR + + Write-Verbose -Message "Restoring addins from NuGet..." + $NuGetOutput = Invoke-Expression "&`"$NUGET_EXE`" install -ExcludeVersion -OutputDirectory `"$ADDINS_DIR`"" + + if ($LASTEXITCODE -ne 0) { + Throw "An error occurred while restoring NuGet addins." + } + + Write-Verbose -Message ($NuGetOutput | out-string) + + Pop-Location +} + +# Restore modules from NuGet +if (Test-Path $MODULES_PACKAGES_CONFIG) { + Push-Location + Set-Location $MODULES_DIR + + Write-Verbose -Message "Restoring modules from NuGet..." + $NuGetOutput = Invoke-Expression "&`"$NUGET_EXE`" install -ExcludeVersion -OutputDirectory `"$MODULES_DIR`"" + + if ($LASTEXITCODE -ne 0) { + Throw "An error occurred while restoring NuGet modules." + } + + Write-Verbose -Message ($NuGetOutput | out-string) + Pop-Location } @@ -178,7 +216,20 @@ if (!(Test-Path $CAKE_EXE)) { Throw "Could not find Cake.exe at $CAKE_EXE" } + + +# Build Cake arguments +$cakeArguments = @("$Script"); +if ($Target) { $cakeArguments += "-target=$Target" } +if ($Configuration) { $cakeArguments += "-configuration=$Configuration" } +if ($Verbosity) { $cakeArguments += "-verbosity=$Verbosity" } +if ($ShowDescription) { $cakeArguments += "-showdescription" } +if ($DryRun) { $cakeArguments += "-dryrun" } +if ($Experimental) { $cakeArguments += "-experimental" } +if ($Mono) { $cakeArguments += "-mono" } +$cakeArguments += $ScriptArgs + # Start Cake Write-Host "Running build script..." -Invoke-Expression "& `"$CAKE_EXE`" `"$Script`" -target=`"$Target`" -configuration=`"$Configuration`" -verbosity=`"$Verbosity`" $UseMono $UseDryRun $UseExperimental $ScriptArgs" +&$CAKE_EXE $cakeArguments exit $LASTEXITCODE \ No newline at end of file From 45a0cd606ce91efb64590becb9cbcbd27a3931cc Mon Sep 17 00:00:00 2001 From: Pascal Berger Date: Mon, 27 Jan 2020 13:07:02 +0100 Subject: [PATCH 02/14] Change name of build script to recipe.cake --- build.ps1 | 2 +- setup.cake => recipe.cake | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename setup.cake => recipe.cake (100%) diff --git a/build.ps1 b/build.ps1 index 48a6534..5fba873 100644 --- a/build.ps1 +++ b/build.ps1 @@ -41,7 +41,7 @@ https://cakebuild.net [CmdletBinding()] Param( - [string]$Script = "setup.cake", + [string]$Script = "recipe.cake", [string]$Target = "Default", [ValidateSet("Release", "Debug")] [string]$Configuration = "Release", diff --git a/setup.cake b/recipe.cake similarity index 100% rename from setup.cake rename to recipe.cake From 8374f4168944523d1a5252c3db372cd821d9d5cb Mon Sep 17 00:00:00 2001 From: marco-bertschi <25186516+marco-bertschi@users.noreply.github.com> Date: Mon, 27 Jan 2020 10:43:58 +0100 Subject: [PATCH 03/14] (GH-69) Extend documentation --- docs/input/docs/usage/examples.md | 48 +++++++++++++++++++++++++++++-- 1 file changed, 46 insertions(+), 2 deletions(-) diff --git a/docs/input/docs/usage/examples.md b/docs/input/docs/usage/examples.md index 23e78f5..3c49b13 100644 --- a/docs/input/docs/usage/examples.md +++ b/docs/input/docs/usage/examples.md @@ -55,18 +55,49 @@ public class Data() } ``` +If a default value is needed, for example when specifying a file extension, the value can be encapsulated by a `Maybe` as well: + +```csharp +public class Data() +{ + public Data(Foo foo, Maybe maybeFileExtension) + { + Foo = foo; + // If a file extension is specified the value from the Maybe is used + // If no value is provided by the Maybe the FileExtension is set to ".txt" + FileExtension = maybeFileExtension.ValueOrDefault(".txt"); + } + + public Foo { get; } + public string FileExtension { get; } +} +``` + # Optional method arguments Maybe can be declared as optional method arguments using the default keyword. ```csharp -/// maybeBar is an optional parameter. Maybe.None() is default. +/// maybeBar is an optional parameter. Maybe.None() is the default value. public void Foo(Maybe maybeBar = default) { maybeBar.Do(x => x.Bar()); } ``` +It's also possible to retrieve the value (or a default value) from the `Maybe` in a method: + +```csharp +private readonly defaultBar = new Bar(); + +/// maybeBar is an optional parameter. Maybe.None() is the default value. +/// By using ValueOrDefault a default instance of the Bar class can be used. +public void Foo(Maybe maybeBar = default) +{ + var bar = maybeBar.ValueOrDefault(this.defaultBar); +} +``` + # Projection method Maybe provides a projection method (`Maybe.Some(Func)`) with an argument of type Func. @@ -88,4 +119,17 @@ Maybe.Some(foo).Some(x => x.Bar).Do(x => ...); Maybe.Some(foo) is the factory method call for the creation of the maybe of type Foo. The second call Some(x => x.Bar) is the projection method projecting the Foo maybe to a maybe of its property type Bar. -Therefore projections of cascades of properties can be done without the need of null checks or null propagation. \ No newline at end of file +Therefore projections of cascades of properties can be done without the need of null checks or null propagation. + +# Assertion + +A `Maybe` can be asserted to have a value by calling `ValueOrException`: + +```csharp +/// maybeBar may or may not have a value. +public void Foo(Maybe maybeBar) +{ + // If maybeBar doesn't have a value an exception is thrown. + var bar = maybeBar.ValueOrException(nameof(maybeBar)); +} +``` From 3661c2d07d7e8bb283bb8653bce7191b5bd2e329 Mon Sep 17 00:00:00 2001 From: "dependabot-preview[bot]" <27856297+dependabot-preview[bot]@users.noreply.github.com> Date: Wed, 5 Feb 2020 08:43:20 +0000 Subject: [PATCH 04/14] Bump Microsoft.NET.Test.Sdk from 16.4.0 to 16.5.0 in /src Bumps [Microsoft.NET.Test.Sdk](https://github.com/microsoft/vstest) from 16.4.0 to 16.5.0. - [Release notes](https://github.com/microsoft/vstest/releases) - [Commits](https://github.com/microsoft/vstest/compare/v16.4.0...v16.5.0) Signed-off-by: dependabot-preview[bot] --- src/BBT.MaybePattern.Tests/BBT.MaybePattern.Tests.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/BBT.MaybePattern.Tests/BBT.MaybePattern.Tests.csproj b/src/BBT.MaybePattern.Tests/BBT.MaybePattern.Tests.csproj index 96baec6..15b5720 100644 --- a/src/BBT.MaybePattern.Tests/BBT.MaybePattern.Tests.csproj +++ b/src/BBT.MaybePattern.Tests/BBT.MaybePattern.Tests.csproj @@ -20,7 +20,7 @@ - + From 3f604f6ab3b3f1a8df7ad288c2abe54482b6bf0b Mon Sep 17 00:00:00 2001 From: "dependabot-preview[bot]" <27856297+dependabot-preview[bot]@users.noreply.github.com> Date: Sun, 16 Feb 2020 03:54:00 +0000 Subject: [PATCH 05/14] Bump SauceControl.InheritDoc from 0.4.0 to 1.0.0 in /src Bumps [SauceControl.InheritDoc](https://github.com/saucecontrol/InheritDoc) from 0.4.0 to 1.0.0. - [Release notes](https://github.com/saucecontrol/InheritDoc/releases) - [Commits](https://github.com/saucecontrol/InheritDoc/compare/v0.4.0...v1.0.0) Signed-off-by: dependabot-preview[bot] --- src/BBT.MaybePattern/BBT.MaybePattern.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/BBT.MaybePattern/BBT.MaybePattern.csproj b/src/BBT.MaybePattern/BBT.MaybePattern.csproj index f444282..9ace28f 100644 --- a/src/BBT.MaybePattern/BBT.MaybePattern.csproj +++ b/src/BBT.MaybePattern/BBT.MaybePattern.csproj @@ -34,7 +34,7 @@ all - + all runtime; build; native; contentfiles; analyzers; buildtransitive From 1c16fdd4084ecdfa0550d0bf53c017506c4f471f Mon Sep 17 00:00:00 2001 From: Pascal Berger Date: Sun, 16 Feb 2020 12:00:14 +0100 Subject: [PATCH 06/14] Remove Whitesource configuration --- .whitesource | 8 -------- 1 file changed, 8 deletions(-) delete mode 100644 .whitesource diff --git a/.whitesource b/.whitesource deleted file mode 100644 index 6ed2ec1..0000000 --- a/.whitesource +++ /dev/null @@ -1,8 +0,0 @@ -{ - "generalSettings": { - "shouldScanRepo": true - }, - "checkRunSettings": { - "vulnerableCheckRunConclusionLevel": "failure" - } -} From 9097cee0617329423638025049ce9ac22eb56e31 Mon Sep 17 00:00:00 2001 From: Pascal Berger Date: Sun, 16 Feb 2020 11:16:56 +0100 Subject: [PATCH 07/14] Update code owners --- .github/CODEOWNERS | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS index d6163ae..7096d7c 100644 --- a/.github/CODEOWNERS +++ b/.github/CODEOWNERS @@ -1,3 +1,3 @@ # These owners will be the default owners for everything in the repo and # will be requested for review when someone opens a pull request. -* @stefan-lindegger @Speeedy01 @marco-bertschi @pascalberger @christianbumann @x-jokay @silanosa @georgesgoetz \ No newline at end of file +* @stefan-lindegger @Speeedy01 @marco-bertschi @pascalberger @christianbumann @eoehen @georgesgoetz \ No newline at end of file From 30d095c95557450291fb19959f33d4d6a9a55775 Mon Sep 17 00:00:00 2001 From: "dependabot-preview[bot]" <27856297+dependabot-preview[bot]@users.noreply.github.com> Date: Tue, 21 Apr 2020 05:43:29 +0000 Subject: [PATCH 08/14] Bump Microsoft.NET.Test.Sdk from 16.5.0 to 16.6.0 in /src Bumps [Microsoft.NET.Test.Sdk](https://github.com/microsoft/vstest) from 16.5.0 to 16.6.0. - [Release notes](https://github.com/microsoft/vstest/releases) - [Commits](https://github.com/microsoft/vstest/compare/v16.5.0...v16.6.0) Signed-off-by: dependabot-preview[bot] --- src/BBT.MaybePattern.Tests/BBT.MaybePattern.Tests.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/BBT.MaybePattern.Tests/BBT.MaybePattern.Tests.csproj b/src/BBT.MaybePattern.Tests/BBT.MaybePattern.Tests.csproj index 15b5720..e4ecc3a 100644 --- a/src/BBT.MaybePattern.Tests/BBT.MaybePattern.Tests.csproj +++ b/src/BBT.MaybePattern.Tests/BBT.MaybePattern.Tests.csproj @@ -20,7 +20,7 @@ - + From 0aad0d21d626bce3698dd657c24e91acde72e67e Mon Sep 17 00:00:00 2001 From: "dependabot-preview[bot]" <27856297+dependabot-preview[bot]@users.noreply.github.com> Date: Mon, 27 Apr 2020 06:01:46 +0000 Subject: [PATCH 09/14] Bump Microsoft.NET.Test.Sdk from 16.6.0 to 16.6.1 in /src Bumps [Microsoft.NET.Test.Sdk](https://github.com/microsoft/vstest) from 16.6.0 to 16.6.1. - [Release notes](https://github.com/microsoft/vstest/releases) - [Commits](https://github.com/microsoft/vstest/compare/v16.6.0...v16.6.1) Signed-off-by: dependabot-preview[bot] --- src/BBT.MaybePattern.Tests/BBT.MaybePattern.Tests.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/BBT.MaybePattern.Tests/BBT.MaybePattern.Tests.csproj b/src/BBT.MaybePattern.Tests/BBT.MaybePattern.Tests.csproj index e4ecc3a..845a7ab 100644 --- a/src/BBT.MaybePattern.Tests/BBT.MaybePattern.Tests.csproj +++ b/src/BBT.MaybePattern.Tests/BBT.MaybePattern.Tests.csproj @@ -20,7 +20,7 @@ - + From aa46d6d0ed4c3798dca0a177be7e8764f7cf1ab1 Mon Sep 17 00:00:00 2001 From: "dependabot-preview[bot]" <27856297+dependabot-preview[bot]@users.noreply.github.com> Date: Mon, 27 Apr 2020 23:43:22 +0000 Subject: [PATCH 10/14] Bump Microsoft.CodeAnalysis.FxCopAnalyzers from 2.9.8 to 3.0.0 in /src Bumps [Microsoft.CodeAnalysis.FxCopAnalyzers](https://github.com/dotnet/roslyn-analyzers) from 2.9.8 to 3.0.0. - [Release notes](https://github.com/dotnet/roslyn-analyzers/releases) - [Changelog](https://github.com/dotnet/roslyn-analyzers/blob/master/PostReleaseActivities.md) - [Commits](https://github.com/dotnet/roslyn-analyzers/compare/v2.9.8...v3.0.0) Signed-off-by: dependabot-preview[bot] --- src/BBT.MaybePattern/BBT.MaybePattern.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/BBT.MaybePattern/BBT.MaybePattern.csproj b/src/BBT.MaybePattern/BBT.MaybePattern.csproj index 9ace28f..3f32f7b 100644 --- a/src/BBT.MaybePattern/BBT.MaybePattern.csproj +++ b/src/BBT.MaybePattern/BBT.MaybePattern.csproj @@ -31,7 +31,7 @@ - + all From 202c03c3fb344955fdc8d86673cf012d047c0cf0 Mon Sep 17 00:00:00 2001 From: Stefan Lindegger Date: Tue, 28 Apr 2020 21:22:17 +0200 Subject: [PATCH 11/14] (GH-78) Method overloads without arguments for methods ValueOrException added. --- src/BBT.MaybePattern.Tests/MaybeStructTests.cs | 16 +++++++++++++++- src/BBT.MaybePattern.Tests/MaybeTests.cs | 16 +++++++++++++++- src/BBT.MaybePattern/Maybe.cs | 10 ++++++++-- src/BBT.MaybePattern/MaybeStruct.cs | 10 ++++++++-- 4 files changed, 46 insertions(+), 6 deletions(-) diff --git a/src/BBT.MaybePattern.Tests/MaybeStructTests.cs b/src/BBT.MaybePattern.Tests/MaybeStructTests.cs index 9defbda..1a2648f 100644 --- a/src/BBT.MaybePattern.Tests/MaybeStructTests.cs +++ b/src/BBT.MaybePattern.Tests/MaybeStructTests.cs @@ -341,7 +341,21 @@ public void Should_Return_Value_If_Called_For_Some_Mabye() public sealed class TheValueOrExceptionMethod { [Fact] - public void Should_Throw_InvalidOperationException_If_Called_For_None_Mabye() + public void Should_Throw_InvalidOperationException_With_TypeName_If_Called_Without_Args_For_None_Mabye() + { + // Arrange + var maybeNone = Maybe.NoneStruct(); + + // Act + var exception = Record.Exception(() => maybeNone.ValueOrException()); + + // Assert + exception.ShouldBeOfType(); + exception.Message.ShouldContain(nameof(BaseStruct)); + } + + [Fact] + public void Should_Throw_InvalidOperationException_If_Called_With_Args_For_None_Mabye() { // Arrange var maybeNone = Maybe.NoneStruct(); diff --git a/src/BBT.MaybePattern.Tests/MaybeTests.cs b/src/BBT.MaybePattern.Tests/MaybeTests.cs index 7a36031..6ab508c 100644 --- a/src/BBT.MaybePattern.Tests/MaybeTests.cs +++ b/src/BBT.MaybePattern.Tests/MaybeTests.cs @@ -380,7 +380,21 @@ public void Should_Return_Value_If_Called_For_Some_Mabye() public sealed class TheValueOrExceptionMethod { [Fact] - public void Should_Throw_InvalidOperationException_If_Called_For_None_Mabye() + public void Should_Throw_InvalidOperationException_With_TypeName_If_Called_Without_Args_For_None_Mabye() + { + // Arrange + var maybeNone = Maybe.None(); + + // Act + var exception = Record.Exception(() => maybeNone.ValueOrException()); + + // Assert + exception.ShouldBeOfType(); + exception.Message.ShouldContain(nameof(BaseClass)); + } + + [Fact] + public void Should_Throw_InvalidOperationException_If_Called_With_Args_For_None_Mabye() { // Arrange var maybeNone = Maybe.None(); diff --git a/src/BBT.MaybePattern/Maybe.cs b/src/BBT.MaybePattern/Maybe.cs index 67303bd..cb02d0e 100644 --- a/src/BBT.MaybePattern/Maybe.cs +++ b/src/BBT.MaybePattern/Maybe.cs @@ -171,10 +171,16 @@ public T ValueOrDefault( /// Additional error message. /// The value. public T ValueOrException( - string maybeParameterName, + string maybeParameterName = "", string additionalMessage = "") { - return MaybeUtils.CheckParameterNotNull(this.value, maybeParameterName, additionalMessage); + var parameterName = maybeParameterName; + if (string.IsNullOrEmpty(maybeParameterName)) + { + parameterName = typeof(T).Name; + } + + return MaybeUtils.CheckParameterNotNull(this.value, parameterName, additionalMessage); } /// diff --git a/src/BBT.MaybePattern/MaybeStruct.cs b/src/BBT.MaybePattern/MaybeStruct.cs index f1c27f6..acc82e5 100644 --- a/src/BBT.MaybePattern/MaybeStruct.cs +++ b/src/BBT.MaybePattern/MaybeStruct.cs @@ -171,10 +171,16 @@ public T ValueOrDefault( /// Additional error message. /// The value. public T ValueOrException( - string maybeParameterName, + string maybeParameterName = "", string additionalMessage = "") { - return MaybeUtils.CheckParameterNotNull(this.value, maybeParameterName, additionalMessage) + var parameterName = maybeParameterName; + if (string.IsNullOrEmpty(maybeParameterName)) + { + parameterName = typeof(T).Name; + } + + return MaybeUtils.CheckParameterNotNull(this.value, parameterName, additionalMessage) .Value; } From 753255defe56d9097238ede1bec855e55fca2541 Mon Sep 17 00:00:00 2001 From: Stefan Lindegger Date: Tue, 28 Apr 2020 21:20:39 +0200 Subject: [PATCH 12/14] (GH-81) fixed issue with deserialization of MaybeStruct none case. --- src/BBT.MaybePattern.Tests/MaybeIntTests.cs | 53 +++++++++++++++++++ .../MaybeStructIntTests.cs | 53 +++++++++++++++++++ .../TestData/TestDataClass.cs | 16 ++++++ .../TestData/TestDataStruct.cs | 16 ++++++ src/BBT.MaybePattern.Tests/TestUtils.cs | 50 +++++++++++++++++ src/BBT.MaybePattern/MaybeStruct.cs | 2 +- 6 files changed, 189 insertions(+), 1 deletion(-) create mode 100644 src/BBT.MaybePattern.Tests/MaybeIntTests.cs create mode 100644 src/BBT.MaybePattern.Tests/MaybeStructIntTests.cs create mode 100644 src/BBT.MaybePattern.Tests/TestData/TestDataClass.cs create mode 100644 src/BBT.MaybePattern.Tests/TestData/TestDataStruct.cs create mode 100644 src/BBT.MaybePattern.Tests/TestUtils.cs diff --git a/src/BBT.MaybePattern.Tests/MaybeIntTests.cs b/src/BBT.MaybePattern.Tests/MaybeIntTests.cs new file mode 100644 index 0000000..7b2ad69 --- /dev/null +++ b/src/BBT.MaybePattern.Tests/MaybeIntTests.cs @@ -0,0 +1,53 @@ +namespace BBT.MaybePattern.Tests +{ + using BBT.MaybePattern; + using BBT.MaybePattern.Tests.TestData; + using Shouldly; + using Xunit; + + public sealed class MaybeIntTests + { + public sealed class SerializeAndDeserialize + { + /// + /// Serialize and deserialize a maybe of int. + /// + [Fact] + public void Should_Work_For_None() + { + // Arrange + var maybe = Maybe.None(); + var testData = new TestDataClass() { Maybe = maybe }; + + // Act & Assert + using (var stream = TestUtils.SerializeToStream(testData)) + { + var testDataDeserialized = (TestDataClass)TestUtils.DeserializeFromStream(stream); + + testDataDeserialized.Maybe.ShouldBeOfType>(); + testDataDeserialized.Maybe.HasValue.ShouldBeFalse(); + } + } + + /// + /// Serialize and deserialize a maybe of int. + /// + [Fact] + public void Should_Work_For_Some() + { + // Arrange + var maybe = Maybe.Some(new object()); + var testData = new TestDataClass() { Maybe = maybe }; + + // Act & Assert + using (var stream = TestUtils.SerializeToStream(testData)) + { + var testDataDeserialized = (TestDataClass)TestUtils.DeserializeFromStream(stream); + + testDataDeserialized.Maybe.ShouldBeOfType>(); + testDataDeserialized.Maybe.HasValue.ShouldBeTrue(); + } + } + } + } +} diff --git a/src/BBT.MaybePattern.Tests/MaybeStructIntTests.cs b/src/BBT.MaybePattern.Tests/MaybeStructIntTests.cs new file mode 100644 index 0000000..e44c4f0 --- /dev/null +++ b/src/BBT.MaybePattern.Tests/MaybeStructIntTests.cs @@ -0,0 +1,53 @@ +namespace BBT.MaybePattern.Tests +{ + using BBT.MaybePattern; + using BBT.MaybePattern.Tests.TestData; + using Shouldly; + using Xunit; + + public sealed class MaybeStructIntTests + { + public sealed class SerializeAndDeserialize + { + /// + /// Serialize and deserialize a maybe of int. + /// + [Fact] + public void Should_Work_For_None() + { + // Arrange + var maybeStruct = Maybe.NoneStruct(); + var testData = new TestDataStruct() { MaybeStruct = maybeStruct }; + + // Act & Assert + using (var stream = TestUtils.SerializeToStream(testData)) + { + var testDataDeserialized = (TestDataStruct)TestUtils.DeserializeFromStream(stream); + + testDataDeserialized.MaybeStruct.ShouldBeOfType>(); + testDataDeserialized.MaybeStruct.HasValue.ShouldBeFalse(); + } + } + + /// + /// Serialize and deserialize a maybe of int. + /// + [Fact] + public void Should_Work_For_Some() + { + // Arrange + var maybeStruct = Maybe.SomeStruct(5); + var testData = new TestDataStruct() { MaybeStruct = maybeStruct }; + + // Act & Assert + using (var stream = TestUtils.SerializeToStream(testData)) + { + var testDataDeserialized = (TestDataStruct)TestUtils.DeserializeFromStream(stream); + + testDataDeserialized.MaybeStruct.ShouldBeOfType>(); + testDataDeserialized.MaybeStruct.HasValue.ShouldBeTrue(); + } + } + } + } +} diff --git a/src/BBT.MaybePattern.Tests/TestData/TestDataClass.cs b/src/BBT.MaybePattern.Tests/TestData/TestDataClass.cs new file mode 100644 index 0000000..2d0a019 --- /dev/null +++ b/src/BBT.MaybePattern.Tests/TestData/TestDataClass.cs @@ -0,0 +1,16 @@ +namespace BBT.MaybePattern.Tests.TestData +{ + using System; + + /// + /// Used for test purposes. + /// + [Serializable] + public class TestDataClass + { + /// + /// Gets or sets the maybe. + /// + public Maybe Maybe { get; set; } + } +} diff --git a/src/BBT.MaybePattern.Tests/TestData/TestDataStruct.cs b/src/BBT.MaybePattern.Tests/TestData/TestDataStruct.cs new file mode 100644 index 0000000..e9ad7e8 --- /dev/null +++ b/src/BBT.MaybePattern.Tests/TestData/TestDataStruct.cs @@ -0,0 +1,16 @@ +namespace BBT.MaybePattern.Tests.TestData +{ + using System; + + /// + /// Used for test purposes. + /// + [Serializable] + public class TestDataStruct + { + /// + /// Gets or sets the maybe. + /// + public MaybeStruct MaybeStruct { get; set; } + } +} diff --git a/src/BBT.MaybePattern.Tests/TestUtils.cs b/src/BBT.MaybePattern.Tests/TestUtils.cs new file mode 100644 index 0000000..c6c85e4 --- /dev/null +++ b/src/BBT.MaybePattern.Tests/TestUtils.cs @@ -0,0 +1,50 @@ +namespace BBT.MaybePattern.Tests +{ + using System; + using System.IO; + using System.Runtime.Serialization.Formatters.Binary; + + /// + /// Provides utility method for test purposes. + /// + public class TestUtils + { + /// + /// Serializes the given object into memory stream. + /// + /// the object to be serialized. + /// The serialized object as memory stream. + public static MemoryStream SerializeToStream(object objectType) + { + var stream = new MemoryStream(); + try + { + var lFormatter = new BinaryFormatter(); + lFormatter.Serialize(stream, objectType); + return stream; + } + catch + { + if (stream != null) + { + stream.Dispose(); + } + } + + throw new InvalidOperationException(); + } + + /// + /// Deserializes as an object. + /// + /// the stream to deserialize. + /// the deserialized object. + public static object DeserializeFromStream(MemoryStream stream) + { + var formatter = new BinaryFormatter(); + stream.Seek(0, SeekOrigin.Begin); + var objectType = formatter.Deserialize(stream); + return objectType; + } + } +} diff --git a/src/BBT.MaybePattern/MaybeStruct.cs b/src/BBT.MaybePattern/MaybeStruct.cs index acc82e5..f37f31d 100644 --- a/src/BBT.MaybePattern/MaybeStruct.cs +++ b/src/BBT.MaybePattern/MaybeStruct.cs @@ -34,7 +34,7 @@ internal MaybeStruct(T? value) /// The streaming context. internal MaybeStruct(SerializationInfo info, StreamingContext context) { - this.value = MaybeUtils.GetDeserializedValue(info, nameof(this.value)); + this.value = MaybeUtils.GetDeserializedValue(info, nameof(this.value)); } /// From 5ac114ab3e6cbcab938d1e6b5093b153fe50d02a Mon Sep 17 00:00:00 2001 From: Pascal Berger Date: Wed, 29 Apr 2020 08:42:34 +0200 Subject: [PATCH 13/14] Update release notes link --- nuspec/nuget/BBT.MaybePattern.nuspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nuspec/nuget/BBT.MaybePattern.nuspec b/nuspec/nuget/BBT.MaybePattern.nuspec index ec539ff..aec0d54 100644 --- a/nuspec/nuget/BBT.MaybePattern.nuspec +++ b/nuspec/nuget/BBT.MaybePattern.nuspec @@ -15,7 +15,7 @@ Copyright © BBT Software AG Maybe Functional - https://github.com/bbtsoftware/BBT.Maybe/releases/tag/3.0.0 + https://github.com/bbtsoftware/BBT.Maybe/releases/tag/3.1.0 From 9cc30b4db93f6d02590446c04d73b6792c7e16bc Mon Sep 17 00:00:00 2001 From: Pascal Berger Date: Wed, 29 Apr 2020 08:44:37 +0200 Subject: [PATCH 14/14] (GH-85) Update projectUrl to website --- nuspec/nuget/BBT.MaybePattern.nuspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nuspec/nuget/BBT.MaybePattern.nuspec b/nuspec/nuget/BBT.MaybePattern.nuspec index aec0d54..7bb4d74 100644 --- a/nuspec/nuget/BBT.MaybePattern.nuspec +++ b/nuspec/nuget/BBT.MaybePattern.nuspec @@ -9,7 +9,7 @@ An option type for .NET. An option type for .NET. MIT - https://github.com/bbtsoftware/BBT.Maybe/ + https://bbtsoftware.github.io/BBT.Maybe/ icon.png false