Tests/New-OutputObject.Tests.ps1

<#
    .SYNOPSIS
    Pester tests to validate the New-OutputObject.ps1 function
 
    .LINK
    https://github.com/it-praktyk/New-OutputObject
 
    .NOTES
    AUTHOR: Wojciech Sciesinski, wojciech[at]sciesinski[dot]net
    KEYWORDS: PowerShell, Pester, psd1, New-OutputObject, New-OutputObject
 
    CURRENT VERSION
    - 0.9.8 - 2017-05-06
 
    HISTORY OF VERSIONS
    https://github.com/it-praktyk/New-OutputObject/VERSIONS.md
 
#>


$ModuleName = "New-OutputObject"

#Provided path asume that your module manifest (a file with the psd1 extension) exists in the parent directory for directory where the current test script is stored
$RelativePathToModuleManifest = "{0}\..\{1}.psd1" -f $PSScriptRoot, $ModuleName

#Remove module if it's currently loaded
Get-Module -Name $ModuleName -ErrorAction SilentlyContinue | Remove-Module

Import-Module -FullyQualifiedName $RelativePathToModuleManifest -Force -Scope Global

$FunctionName = "New-OutputObject"

$ObjectTypes = @('File', 'Folder')

foreach ($ObjectType in $ObjectTypes) {

    If ($ObjectType -eq 'File') {

        $ItemTypeLower = 'file'

        $ExpectedObjectType = 'System.Io.FileInfo'

        [System.String]$DateTimeFormatToMock = 'yyyyMMdd-HHmmss'

        [String]$IncorrectFileNameOnly = 'Test-File-201606*08-1315.txt'

    }
    Else {

        $ItemTypeLower = 'folder'

        $ExpectedObjectType = 'System.Io.DirectoryInfo'

        [System.String]$DateTimeObjectToMock = 'yyyyMMdd'

        [String]$IncorrectDirectoryOnly = 'C:\AppData\Loc>al\'

    }

    Describe "Tests for $FunctionName and the ObjectType [$ObjectType]" {

        $LocationAtBegin = Get-Location

        Set-Location TestDrive:

        $ContextName = "run without parameters"

        Context "Function $FunctionName - $ContextName" {

            If ($ObjectType -eq 'File') {

                Mock -ModuleName New-OutputObject -CommandName Get-Date -MockWith { Return [System.String]'20161108-000002' } -ParameterFilter { $Format }

                $ExpectedOutputObjectName = "Output-20161108-000002.txt"

                $ResultProxyFunction = New-OutputFile

            }
            Else {
                Mock -ModuleName New-OutputObject -CommandName Get-Date -MockWith { Return [System.String]'20161108' } -ParameterFilter { $Format }

                $ExpectedOutputObjectName = "Output-20161108"

                $ResultProxyFunction = New-OutputFolder

            }

            $Result = New-OutputObject -ObjectType $ObjectType

            It "Function $FunctionName - $ContextName - OutputObjectPath - an object type" {

                $Result.OutputObjectPath | Should BeOfType $ExpectedObjectType

                $ResultProxyFunction.OutputObjectPath | Should BeOfType $ExpectedObjectType
            }

            It "Function $FunctionName - $ContextName - OutputObjectPath - Name " {

                $Result.OutputObjectPath.Name | Should Be $ExpectedOutputObjectName

                $ResultProxyFunction.OutputObjectPath.Name | Should Be $ExpectedOutputObjectName

            }


            It "Function $FunctionName - $ContextName - exit code" {

                $Result.ExitCode | Should Be 0

                $ResultProxyFunction.ExitCode | Should Be 0
            }

            It "Function $FunctionName - $ContextName - exit code description" {

                $Result.ExitCodeDescription | Should Be "Everything is fine :-)"

                $ResultProxyFunction.ExitCodeDescription | Should Be "Everything is fine :-)"
            }

        }

        $ContextName = "run with OutputObjectNamePrefix"

        Context "Function $FunctionName - $ContextName" {

            If ($ObjectType -eq 'File') {

                Mock -ModuleName New-OutputObject -CommandName Get-Date -MockWith { Return [System.String]'20161108-000002' } -ParameterFilter { $Format }

                $ExpectedOutputObjectName = "AAA-20161108-000002.txt"

                $ResultProxyFunction = New-OutputFile -OutputFileNamePrefix "AAA"

            }
            Else {
                Mock -ModuleName New-OutputObject -CommandName Get-Date -MockWith { Return [System.String]'20161108' } -ParameterFilter { $Format }

                $ExpectedOutputObjectName = "AAA-20161108"

                $ResultProxyFunction = New-OutputFolder -OutputFolderNamePrefix  "AAA"

            }

            $Result = New-OutputObject -ObjectType $ObjectType -OutputObjectNamePrefix "AAA"

            It "Function $FunctionName - $ContextName - OutputObjectPath - an object type" {

                $Result.OutputObjectPath | Should BeOfType $ExpectedObjectType

                $ResultProxyFunction.OutputObjectPath | Should BeOfType $ExpectedObjectType

            }

            It "Function $FunctionName - $ContextName - OutputObjectPath - Name " {

                $Result.OutputObjectPath.Name | Should Be $ExpectedOutputObjectName

                $ResultProxyFunction.OutputObjectPath.Name | Should Be $ExpectedOutputObjectName

            }


            It "Function $FunctionName - $ContextName - exit code" {

                $Result.ExitCode | Should Be 0

                $ResultProxyFunction.ExitCode | Should Be 0
            }

            It "Function $FunctionName - $ContextName - exit code description" {

                $Result.ExitCodeDescription | Should Be "Everything is fine :-)"

                $ResultProxyFunction.ExitCodeDescription | Should Be "Everything is fine :-)"
            }
        }

        $ContextName = "run with OutputObjectNameMidPart"

        Context "Function $FunctionName - $ContextName" {

            If ($ObjectType -eq 'File') {

                Mock -ModuleName New-OutputObject -CommandName Get-Date -MockWith { Return [System.String]'20161108-000002' } -ParameterFilter { $Format }

                $ExpectedOutputObjectName = "Output-BBB-20161108-000002.txt"

                $ResultProxyFunction = New-OutputFile -OutputFileNameMidPart "BBB"

            }
            Else {
                Mock -ModuleName New-OutputObject -CommandName Get-Date -MockWith { Return [System.String]'20161108' } -ParameterFilter { $Format }

                $ExpectedOutputObjectName = "Output-BBB-20161108"

                $ResultProxyFunction = New-OutputFolder -OutputFolderNameMidPart "BBB"

            }

            $Result = New-OutputObject -ObjectType $ObjectType -OutputObjectNameMidPart "BBB"

            It "Function $FunctionName - $ContextName - OutputObjectPath - an object type" {

                $Result.OutputObjectPath | Should BeOfType $ExpectedObjectType

                $ResultProxyFunction.OutputObjectPath | Should BeOfType $ExpectedObjectType
            }

            It "Function $FunctionName - $ContextName - OutputObjectPath - Name " {

                $Result.OutputObjectPath.Name | Should Be $ExpectedOutputObjectName

                $ResultProxyFunction.OutputObjectPath.Name | Should Be $ExpectedOutputObjectName
            }


            It "Function $FunctionName - $ContextName - exit code" {

                $Result.ExitCode | Should Be 0

                $ResultProxyFunction.ExitCode | Should Be 0
            }

            It "Function $FunctionName - $ContextName - exit code description" {

                $Result.ExitCodeDescription | Should Be "Everything is fine :-)"

                $ResultProxyFunction.ExitCodeDescription | Should Be "Everything is fine :-)"
            }
        }

        $ContextName = "run with OutputObjectNameSuffix"

        Context "Function $FunctionName - $ContextName" {

            If ($ObjectType -eq 'File') {

                Mock -ModuleName New-OutputObject -CommandName Get-Date -MockWith { Return [System.String]'20161108-000002' } -ParameterFilter { $Format }

                $ExpectedOutputObjectName = "Output-20161108-000002-CCC.txt"

                $ResultProxyFunction = New-OutputFile -OutputFileNameSuffix "CCC"

            }
            Else {
                Mock -ModuleName New-OutputObject -CommandName Get-Date -MockWith { Return [System.String]'20161108' } -ParameterFilter { $Format }

                $ExpectedOutputObjectName = "Output-20161108-CCC"

                $ResultProxyFunction = New-OutputFolder -OutputFolderNameSuffix "CCC"

            }

            $Result = New-OutputObject -ObjectType $ObjectType -OutputObjectNameSuffix "CCC"

            It "Function $FunctionName - $ContextName - OutputObjectPath - an object type" {

                $Result.OutputObjectPath | Should BeOfType $ExpectedObjectType

                $ResultProxyFunction.OutputObjectPath | Should BeOfType $ExpectedObjectType
            }

            It "Function $FunctionName - $ContextName - OutputObjectPath - Name " {

                $Result.OutputObjectPath.Name | Should Be $ExpectedOutputObjectName

                $ResultProxyFunction.OutputObjectPath.Name | Should Be $ExpectedOutputObjectName
            }


            It "Function $FunctionName - $ContextName - exit code" {

                $Result.ExitCode | Should Be 0

                $ResultProxyFunction.ExitCode | Should Be 0
            }

            It "Function $FunctionName - $ContextName - exit code description" {

                $Result.ExitCodeDescription | Should Be "Everything is fine :-)"

                $ResultProxyFunction.ExitCodeDescription | Should Be "Everything is fine :-)"
            }
        }


        $ContextName = "run with DateTimePartInOutputFileName"

        Context "Function $FunctionName - $ContextName" {

            If ($ObjectType -eq 'File') {

                Mock -ModuleName New-OutputObject -CommandName Get-Date -MockWith { Return [System.String]'20161101-120001' } -ParameterFilter { $Format }

                $ExpectedOutputObjectName = "Output-20161101-120001.txt"

                $ResultProxyFunction = New-OutputFile -DateTimePartInOutputFileName (Get-Date -Date "2016-11-01 12:00:01" -Format "yyyy-MM-dd hh:mm:ss")

            }
            Else {
                Mock -ModuleName New-OutputObject -CommandName Get-Date -MockWith { Return [System.String]'20161101' } -ParameterFilter { $Format }

                $ExpectedOutputObjectName = "Output-20161101"

                $ResultProxyFunction = New-OutputFolder -DateTimePartInOutputFolderName (Get-Date -Date "2016-11-01 12:00:01" -Format "yyyy-MM-dd hh:mm:ss")

            }

            $Result = New-OutputObject -ObjectType $ObjectType -DateTimePartInOutputObjectName (Get-Date -Date "2016-11-01 12:00:01" -Format "yyyy-MM-dd hh:mm:ss")


            It "Function $FunctionName - $ContextName - OutputObjectPath - an object type" {

                $Result.OutputObjectPath | Should BeOfType $ExpectedObjectType

                $ResultProxyFunction.OutputObjectPath | Should BeOfType $ExpectedObjectType
            }

            It "Function $FunctionName - $ContextName - OutputObjectPath - Name " {

                $Result.OutputObjectPath.Name | Should Be $ExpectedOutputObjectName

                $ResultProxyFunction.OutputObjectPath.Name | Should Be $ExpectedOutputObjectName
            }

            It "Function $FunctionName - $ContextName - exit code" {

                $Result.ExitCode | Should Be 0

                $ResultProxyFunction.ExitCode | Should Be 0
            }

            It "Function $FunctionName - $ContextName - exit code description" {

                $Result.ExitCodeDescription | Should Be "Everything is fine :-)"

                $ResultProxyFunction.ExitCodeDescription | Should Be "Everything is fine :-)"
            }
        }

        $ContextName = "run with DateTimePartInOutputObjectName, without DateTimePart"

        Context "Function $FunctionName - $ContextName" {

            If ($ObjectType -eq 'File') {

                $ExpectedOutputObjectName = "Output.txt"

                $params = @{

                    DateTimePartInOutputFileName = (Get-Date -Date "2016-11-01 12:00:01" -Format "yyyy-MM-dd hh:mm:ss")

                    IncludeDateTimePartInOutputFileName = $false

                }

                $ResultProxyFunction = New-OutputFile @params

            }
            Else {

                $ExpectedOutputObjectName = "Output"

                $params = @{

                    DateTimePartInOutputFolderName = (Get-Date -Date "2016-11-01 12:00:01" -Format "yyyy-MM-dd hh:mm:ss")

                    IncludeDateTimePartInOutputFolderName = $false

                }

                $ResultProxyFunction = New-OutputFolder @params



            }

            $params = @{

                ObjectType = $ObjectType

                DateTimePartInOutputObjectName = (Get-Date -Date "2016-11-01 12:00:01" -Format "yyyy-MM-dd hh:mm:ss")

                IncludeDateTimePartInOutputObjectName = $false


            }

            $Result = New-OutputObject @params

            It "Function $FunctionName - $ContextName - an object type" {

                $Result.OutputObjectPath | Should BeOfType $ExpectedObjectType

                $ResultProxyFunction.OutputObjectPath | Should BeOfType $ExpectedObjectType
            }

            It "Function $FunctionName - $ContextName - OutputObjectPath - Name " {

                $Result.OutputObjectPath.Name | Should Be $ExpectedOutputObjectName

                $ResultProxyFunction.OutputObjectPath.Name | Should Be $ExpectedOutputObjectName
            }

            It "Function $FunctionName - $ContextName - exit code" {

                $Result.ExitCode | Should Be 0

                $ResultProxyFunction.ExitCode | Should Be 0
            }

            It "Function $FunctionName - $ContextName - exit code description" {

                $Result.ExitCodeDescription | Should Be "Everything is fine :-)"

                $ResultProxyFunction.ExitCodeDescription | Should Be "Everything is fine :-)"
            }
        }

        $ContextName = "run with OutputFileNameExtension"

        Context "Function $FunctionName - $ContextName" {

            If ($ObjectType -eq 'File') {

                Mock -ModuleName New-OutputObject -CommandName Get-Date -MockWith { Return [System.String]'20161108-000002' } -ParameterFilter { $Format }

                $ExpectedOutputObjectName = "Output-20161108-000002.csv"

                $ResultProxyFunction = New-OutputFile -OutputFileNameExtension ".csv"

            }
            Else {
                Mock -ModuleName New-OutputObject -CommandName Get-Date -MockWith { Return [System.String]'20161108' } -ParameterFilter { $Format }

                $ExpectedOutputObjectName = "Output-20161108"

                $ResultProxyFunction = New-OutputFolder

            }

            $Result = New-OutputObject -ObjectType $ObjectType -OutputFileNameExtension "csv"

            It "Function $FunctionName - $ContextName - an object type" {

                $Result.OutputObjectPath | Should BeOfType $ExpectedObjectType

                $ResultProxyFunction.OutputObjectPath | Should BeOfType $ExpectedObjectType
            }

            It "Function $FunctionName - $ContextName - OutputObjectPath - Name " {

                $Result.OutputObjectPath.Name | Should Be $ExpectedOutputObjectName

                $ResultProxyFunction.OutputObjectPath.Name | Should Be $ExpectedOutputObjectName
            }

            It "Function $FunctionName - $ContextName - exit code" {

                $Result.ExitCode | Should Be 0

                $ResultProxyFunction.ExitCode | Should Be 0
            }

            It "Function $FunctionName - $ContextName - exit code description" {

                $Result.ExitCodeDescription | Should Be "Everything is fine :-)"

                $ResultProxyFunction.ExitCodeDescription | Should Be "Everything is fine :-)"
            }
        }

        $ContextName = "run with NamePartsSeparator"

        Context "Function $FunctionName - $ContextName" {

            If ($ObjectType -eq 'File') {

                Mock -ModuleName New-OutputObject -CommandName Get-Date -MockWith { Return [System.String]'20161108-000002' } -ParameterFilter { $Format }

                $ExpectedOutputObjectName = "Output_20161108-000002.txt"

                $ResultProxyFunction = New-OutputFile -NamePartsSeparator "_"

            }
            Else {
                Mock -ModuleName New-OutputObject -CommandName Get-Date -MockWith { Return [System.String]'20161108' } -ParameterFilter { $Format }

                $ExpectedOutputObjectName = "Output_20161108"

                $ResultProxyFunction = New-OutputFolder -NamePartsSeparator "_"

            }

            $Result = New-OutputObject -ObjectType $ObjectType -NamePartsSeparator "_"

            It "Function $FunctionName - $ContextName - OutputObjectPath - an object type" {

                $Result.OutputObjectPath | Should BeOfType $ExpectedObjectType

                $ResultProxyFunction.OutputObjectPath | Should BeOfType $ExpectedObjectType
            }

            It "Function $FunctionName - $ContextName - OutputObjectPath - Name " {

                $Result.OutputObjectPath.Name | Should Be $ExpectedOutputObjectName

                $ResultProxyFunction.OutputObjectPath.Name | Should Be $ExpectedOutputObjectName
            }


            It "Function $FunctionName - $ContextName - exit code" {

                $Result.ExitCode | Should Be 0

                $ResultProxyFunction.ExitCode | Should Be 0
            }

            It "Function $FunctionName - $ContextName - exit code description" {

                $Result.ExitCodeDescription | Should Be "Everything is fine :-)"

                $ResultProxyFunction.ExitCodeDescription | Should Be "Everything is fine :-)"
            }

        }

        $ContextName = "run with all name parts, with DateTimePartInOutputObjectName"

        Context "Function $FunctionName - $ContextName" {



            If ($ObjectType -eq 'File') {

                Mock -ModuleName New-OutputObject -CommandName Get-Date -MockWith { Return [System.String]'20161101-120001' } -ParameterFilter { $Format }

                $ExpectedOutputObjectName = "AAA-BBB-20161101-120001-CCC.txt"

                $params = @{

                    OutputFileNamePrefix = "AAA"

                    OutputFileNameMidPart = "BBB"

                    OutputFileNameSuffix = "CCC"

                    DateTimePartInOutputFileName = (Get-Date -Date "2016-11-01 12:00:01" -Format "yyyy-MM-dd hh:mm:ss")

                }

                $ResultProxyFunction = New-OutputFile @params

            }
            Else {
                Mock -ModuleName New-OutputObject -CommandName Get-Date -MockWith { Return [System.String]'20161101' } -ParameterFilter { $Format }

                $ExpectedOutputObjectName = "AAA-BBB-20161101-CCC"

                $params = @{

                    OutputFolderNamePrefix = "AAA"

                    OutputFolderNameMidPart = "BBB"

                    OutputFolderNameSuffix = "CCC"

                    DateTimePartInOutputFolderName = (Get-Date -Date "2016-11-01 12:00:01" -Format "yyyy-MM-dd hh:mm:ss")

                }

                $ResultProxyFunction = New-OutputFolder @params

            }

            $params = @{

                ObjectType = $ObjectType

                OutputObjectNamePrefix = "AAA"

                OutputObjectNameMidPart = "BBB"

                OutputObjectNameSuffix = "CCC"

                DateTimePartInOutputObjectName = (Get-Date -Date "2016-11-01 12:00:01" -Format "yyyy-MM-dd hh:mm:ss")

            }

            $Result = New-OutputObject @params

            It "Function $FunctionName - $ContextName - OutputObjectPath - an object type" {

                $Result.OutputObjectPath | Should BeOfType $ExpectedObjectType

                $ResultProxyFunction.OutputObjectPath | Should BeOfType $ExpectedObjectType
            }

            It "Function $FunctionName - $ContextName - OutputObjectPath - Name " {

                $Result.OutputObjectPath.Name | Should Be $ExpectedOutputObjectName

                $ResultProxyFunction.OutputObjectPath.Name | Should Be $ExpectedOutputObjectName
            }

            It "Function $FunctionName - $ContextName - exit code" {

                $Result.ExitCode | Should Be 0

                $ResultProxyFunction.ExitCode | Should Be 0
            }

            It "Function $FunctionName - $ContextName - exit code description" {

                $Result.ExitCodeDescription | Should Be "Everything is fine :-)"

                $ResultProxyFunction.ExitCodeDescription | Should Be "Everything is fine :-)"
            }
        }

        $ContextName = "run with all name parts, without DateTimePart"

        Context "Function $FunctionName - $ContextName" {

            If ($ObjectType -eq 'File') {

                $ExpectedOutputObjectName = "AAA-BBB-CCC.txt"

                $params = @{

                    OutputFileNamePrefix = "AAA"

                    OutputFileNameMidPart = "BBB"

                    OutputFileNameSuffix = "CCC"

                    DateTimePartInOutputFileName = (Get-Date -Date "2016-11-01 12:00:01" -Format "yyyy-MM-dd hh:mm:ss")

                    IncludeDateTimePartInOutputFileName = $false

                }

                $ResultProxyFunction = New-OutputFile @params

            }
            Else {

                $ExpectedOutputObjectName = "AAA-BBB-CCC"

                $params = @{

                    OutputFolderNamePrefix = "AAA"

                    OutputFolderNameMidPart = "BBB"

                    OutputFolderNameSuffix = "CCC"

                    DateTimePartInOutputFolderName = (Get-Date -Date "2016-11-01 12:00:01" -Format "yyyy-MM-dd hh:mm:ss")

                    IncludeDateTimePartInOutputFolderName = $false

                }

                $ResultProxyFunction = New-OutputFolder @params

            }

            $params = @{

                ObjectType = $ObjectType

                OutputObjectNamePrefix = "AAA"

                OutputObjectNameMidPart = "BBB"

                OutputObjectNameSuffix = "CCC"

                DateTimePartInOutputObjectName = (Get-Date -Date "2016-11-01 12:00:01" -Format "yyyy-MM-dd hh:mm:ss")

                IncludeDateTimePartInOutputObjectName = $false

            }

            $Result = New-OutputObject @params

            It "Function $FunctionName - $ContextName - OutputObjectPath - an object type" {

                $Result.OutputObjectPath | Should BeOfType $ExpectedObjectType

                $ResultProxyFunction.OutputObjectPath | Should BeOfType $ExpectedObjectType

            }

            It "Function $FunctionName - $ContextName - OutputObjectPath - Name " {

                $Result.OutputObjectPath.Name | Should Be $ExpectedOutputObjectName

                $ResultProxyFunction.OutputObjectPath.Name | Should Be $ExpectedOutputObjectName

            }

            It "Function $FunctionName - $ContextName - exit code" {

                $Result.ExitCode | Should Be 0

                $ResultProxyFunction.ExitCode | Should Be 0

            }

            It "Function $FunctionName - $ContextName - exit code description" {

                $Result.ExitCodeDescription | Should Be "Everything is fine :-)"

                $ResultProxyFunction.ExitCodeDescription | Should Be "Everything is fine :-)"

            }
        }

        $ContextName = "run without parameters, non existing destination directory."

        Context "Function $FunctionName - $ContextName" {

            $ParentPath = "TestDrive:\TestFolder"

            If ($ObjectType -eq 'File') {

                Mock -ModuleName New-OutputObject -CommandName Get-Date -MockWith { Return [System.String]'20161108-000002' } -ParameterFilter { $Format }

                $ExpectedOutputObjectName = "Output-20161108-000002.txt"

                $ResultProxyFunction = New-OutputFile -ParentPath $ParentPath

            }
            Else {
                Mock -ModuleName New-OutputObject -CommandName Get-Date -MockWith { Return [System.String]'20161108' } -ParameterFilter { $Format }

                $ExpectedOutputObjectName = "Output-20161108"

                $ResultProxyFunction = New-OutputFolder -ParentPath $ParentPath
            }

            $Result = New-OutputObject -ObjectType $ObjectType -ParentPath $ParentPath

            It "Function $FunctionName - $ContextName - OutputObjectPath - an object type" {

                $Result.OutputObjectPath | Should BeOfType $ExpectedObjectType

                $ResultProxyFunction.OutputObjectPath | Should BeOfType $ExpectedObjectType
            }

            It "Function $FunctionName - $ContextName - OutputObjectPath - Name " {

                $Result.OutputObjectPath.Name | Should Be $ExpectedOutputObjectName

                $ResultProxyFunction.OutputObjectPath.Name | Should Be $ExpectedOutputObjectName
            }

            It "Function $FunctionName - $ContextName - exit code" {

                $Result.ExitCode | Should Be 1

                $ResultProxyFunction.ExitCode | Should Be 1
            }

            It "Function $FunctionName - $ContextName - exit code description" {

                [System.String]$RequiredMessage = "Provided parent path {0} doesn't exist" -f $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath("$ParentPath")

                $Result.ExitCodeDescription | Should Be $RequiredMessage
            }
        }

        $ContextName = "run with existing, non writable destination folder."

        Context "Function $FunctionName - $ContextName" {

            If ($ObjectType -eq 'File') {

                Mock -ModuleName New-OutputObject -CommandName Get-Date -MockWith { Return [System.String]'20161108-000002' } -ParameterFilter { $Format }

                $ExpectedOutputObjectName = "Output-20161108-000002.txt"

            }
            Else {
                Mock -ModuleName New-OutputObject -CommandName Get-Date -MockWith { Return [System.String]'20161108' } -ParameterFilter { $Format }

                $ExpectedOutputObjectName = "Output-20161108"

            }

            $TestDestinationFolder = "TestDrive:\ExistingNotWritable\"

            New-Item -Path $TestDestinationFolder -ItemType Directory | Out-Null

            $ChangedACL = $OriginalAcl = Get-Acl -Path $TestDestinationFolder

            $colRights = [System.Security.AccessControl.FileSystemRights]"AppendData,WriteData"

            $InheritanceFlag = [System.Security.AccessControl.InheritanceFlags]::None

            $PropagationFlag = [System.Security.AccessControl.PropagationFlags]::InheritOnly

            $objType = [System.Security.AccessControl.AccessControlType]::Deny

            $objUser = New-Object System.Security.Principal.NTAccount($(whoami))

            $objACE = New-Object System.Security.AccessControl.FileSystemAccessRule ($objUser, $colRights, $InheritanceFlag, $PropagationFlag, $objType)

            $ChangedACL.AddAccessRule($objACE)

            Set-ACL -Path $TestDestinationFolder $ChangedACL



            If ($ObjectType -eq 'File') {

                $ResultProxyFunction = New-OutputFile -ParentPath $TestDestinationFolder

            }
            Else {

                $ResultProxyFunction = New-OutputFolder -ParentPath $TestDestinationFolder

            }


            $Result = New-OutputObject -ObjectType $ObjectType -ParentPath $TestDestinationFolder

            It "Function $FunctionName - $ContextName - OutputObjectPath - an object type" {

                $Result.OutputObjectPath | Should BeOfType $ExpectedObjectType

                $ResultProxyFunction.OutputObjectPath | Should BeOfType $ExpectedObjectType
            }

            It "Function $FunctionName - $ContextName - OutputObjectPath - Name " {

                $Result.OutputObjectPath.Name | Should Be $ExpectedOutputObjectName

                $ResultProxyFunction.OutputObjectPath.Name | Should Be $ExpectedOutputObjectName
            }

            It "Function $FunctionName - $ContextName - exit code" {

                $Result.ExitCode | Should Be 3

                $ResultProxyFunction.ExitCode | Should Be 3
            }

            It "Function $FunctionName - $ContextName - exit code description" {

                [System.String]$RequiredMessage = "Provided path {0} is not writable" -f $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath("$TestDestinationFolder")

                $Result.ExitCodeDescription | Should Be $RequiredMessage

                $ResultProxyFunction.ExitCodeDescription | Should Be $RequiredMessage
            }
        }

        $ContextName = "run with existing, non writable destination folder, break on error"

        Context "Function $FunctionName - $ContextName" {

            $TestDestinationFolder = "TestDrive:\ExistingNotWritable\"

            New-Item -Path $TestDestinationFolder -ItemType Container | Out-Null

            $ChangedACL = $OriginalAcl = Get-Acl -Path $TestDestinationFolder

            $colRights = [System.Security.AccessControl.FileSystemRights]"AppendData,WriteData"

            $InheritanceFlag = [System.Security.AccessControl.InheritanceFlags]::None

            $PropagationFlag = [System.Security.AccessControl.PropagationFlags]::InheritOnly

            $objType = [System.Security.AccessControl.AccessControlType]::Deny

            $objUser = New-Object System.Security.Principal.NTAccount($(whoami))

            $objACE = New-Object System.Security.AccessControl.FileSystemAccessRule ($objUser, $colRights, $InheritanceFlag, $PropagationFlag, $objType)

            $ChangedACL.AddAccessRule($objACE)

            Set-ACL -Path $TestDestinationFolder $ChangedACL

            If ($ObjectType -eq 'File') {

                Mock -ModuleName New-OutputObject -CommandName Get-Date -MockWith { Return [System.String]'20161108-000002' } -ParameterFilter { $Format }

                It "Function $FunctionName - $ContextName - OutputObjectPath - an object type" {
                    { $Result = New-OutputObject -ObjectType $ObjectType -ParentPath $TestDestinationFolder -BreakIfError } | Should Throw

                    { $ResultProxyFunction = New-OutputFile -ParentPath $TestDestinationFolder -BreakIfError } | Should Throw
                }


            }
            Else {

                Mock -ModuleName New-OutputObject -CommandName Get-Date -MockWith { Return [System.String]'20161108' } -ParameterFilter { $Format }

                It "Function $FunctionName - $ContextName - OutputObjectPath - an object type" {
                    { $Result = New-OutputObject -ObjectType $ObjectType -ParentPath $TestDestinationFolder -BreakIfError } | Should Throw

                    { $ResultProxyFunction = New-OutputFolder -ParentPath $TestDestinationFolder -BreakIfError } | Should Throw
                }

            }



        }

        [System.String]$ContextName = "run without parameters, destination {0} exists, decision overwrite" -f $ObjectType

        Context "Function $FunctionName - $ContextName" {

            If ($ObjectType -eq 'File') {

                Mock -ModuleName New-OutputObject -CommandName Get-Date -MockWith { Return [System.String]'20161108-000002' } -ParameterFilter { $Format }

                $ExpectedOutputObjectName = "Output-20161108-000002.txt"

                $OutputTypeToCreate = 'file'

                [System.String]$TestExistingObject = "TestDrive:\Output-20161108-000002.txt"

            }
            Else {

                Mock -ModuleName New-OutputObject -CommandName Get-Date -MockWith { Return [System.String]'20161108' } -ParameterFilter { $Format }

                $ExpectedOutputObjectName = "Output-20161108"

                $OutputTypeToCreate = 'directory'

                [System.String]$TestExistingObject = "TestDrive:\Output-20161108"
            }


            New-Item -Path $TestExistingObject -ItemType $OutputTypeToCreate

            Mock -ModuleName New-OutputObject -CommandName Get-OverwriteDecision -MockWith { Return [int]0 }

            If ($ObjectType -eq 'File') {

                $ResultProxyFunction = New-OutputFile

            }
            Else {

                $ResultProxyFunction = New-OutputFolder

            }

            $Result = New-OutputObject -ObjectType $ObjectType

            It "Function $FunctionName - $ContextName - OutputObjectPath - an object type" {

                $Result.OutputObjectPath | Should BeOfType $ExpectedObjectType

                $ResultProxyFunction.OutputObjectPath | Should BeOfType $ExpectedObjectType
            }

            It "Function $FunctionName - $ContextName - OutputObjectPath - Name " {

                $Result.OutputObjectPath.Name | Should Be $ExpectedOutputObjectName

                $ResultProxyFunction.OutputObjectPath.Name | Should Be $ExpectedOutputObjectName
            }


            It "Function $FunctionName - $ContextName - exit code" {

                $Result.ExitCode | Should Be 4

                $ResultProxyFunction.OutputObjectPath.Name | Should Be $ExpectedOutputObjectName
            }

            It "Function $FunctionName - $ContextName - exit code description" {

                [System.String]$RequiredMessage = "The {0} {1} already exist - can be overwritten" -f $ObjectType, $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath("$TestExistingObject")

                $Result.ExitCodeDescription | Should Be $RequiredMessage

                $ResultProxyFunction.ExitCodeDescription | Should Be $RequiredMessage
            }

        }

        [System.String]$ContextName = "run without parameters, destination {0} exists, decision leave" -f $ObjectType

        Context "Function $FunctionName - $ContextName" {

            If ($ObjectType -eq 'File') {

                Mock -ModuleName New-OutputObject -CommandName Get-Date -MockWith { Return [System.String]'20161108-000002' } -ParameterFilter { $Format }

                $ExpectedOutputObjectName = "Output-20161108-000002.txt"

                $OutputTypeToCreate = 'file'

                [System.String]$TestExistingObject = "TestDrive:\Output-20161108-000002.txt"

            }
            Else {

                Mock -ModuleName New-OutputObject -CommandName Get-Date -MockWith { Return [System.String]'20161108' } -ParameterFilter { $Format }

                $ExpectedOutputObjectName = "Output-20161108"

                $OutputTypeToCreate = 'directory'

                [System.String]$TestExistingObject = "TestDrive:\Output-20161108"
            }


            New-Item -Path $TestExistingObject -ItemType $OutputTypeToCreate

            Mock -ModuleName New-OutputObject -CommandName Get-OverwriteDecision -MockWith { Return [int]1 }

            If ($ObjectType -eq 'File') {

                $ResultProxyFunction = New-OutputFile

            }
            Else {

                $ResultProxyFunction = New-OutputFolder

            }

            $Result = New-OutputObject -ObjectType $ObjectType

            It "Function $FunctionName - $ContextName - OutputObjectPath - an object type" {

                $Result.OutputObjectPath | Should BeOfType $ExpectedObjectType

                $ResultProxyFunction.OutputObjectPath | Should BeOfType $ExpectedObjectType
            }

            It "Function $FunctionName - $ContextName - OutputObjectPath - Name " {

                $Result.OutputObjectPath.Name | Should Be $ExpectedOutputObjectName

                $ResultProxyFunction.OutputObjectPath.Name | Should Be $ExpectedOutputObjectName
            }


            It "Function $FunctionName - $ContextName - exit code" {

                $Result.ExitCode | Should Be 5

                $ResultProxyFunction.ExitCode | Should Be 5
            }

            It "Function $FunctionName - $ContextName - exit code description" {

                [System.String]$RequiredMessage = "The {0} {1} already exist - can't be overwritten" -f $ItemTypeLower, $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath("$TestExistingObject")

                $Result.ExitCodeDescription | Should Be $RequiredMessage

                $ResultProxyFunction.ExitCodeDescription | Should Be $RequiredMessage
            }

        }

        $ContextName = "run without parameters, destination file exists, decision cancel"

        Context "Function $FunctionName - $ContextName" {

            If ($ObjectType -eq 'File') {

                Mock -ModuleName New-OutputObject -CommandName Get-Date -MockWith { Return [System.String]'20161108-000002' } -ParameterFilter { $Format }

                $ExpectedOutputObjectName = "Output-20161108-000002.txt"

                $OutputTypeToCreate = 'file'

                [System.String]$TestExistingObject = "TestDrive:\Output-20161108-000002.txt"

            }
            Else {
                Mock -ModuleName New-OutputObject -CommandName Get-Date -MockWith { Return [System.String]'20161108' } -ParameterFilter { $Format }

                $ExpectedOutputObjectName = "Output-20161108"

                $OutputTypeToCreate = 'directory'

                [System.String]$TestExistingObject = "TestDrive:\Output-20161108"
            }



            New-Item -Path $TestExistingObject -ItemType $OutputTypeToCreate

            Mock -ModuleName New-OutputObject -CommandName Get-OverwriteDecision -MockWith { Return [int]2 }

            If ($ObjectType -eq 'File') {

                It "Function $FunctionName - $ContextName - OutputObjectPath - an object type" {

                    { $Result = New-OutputObject -ObjectType $ObjectType } | Should Throw

                    { $ResultProxyFunction = New-OutputFile } | Should Throw

                }
            }
            Else {

                It "Function $FunctionName - $ContextName - OutputObjectPath - an object type" {
                    { $Result = New-OutputObject -ObjectType $ObjectType } | Should Throw

                    { $ResultProxyFunction = New-OutputFolder } | Should Throw

                }
            }

        }


        $ContextName = "run without parameters, incorrect chars in DateTimePartFormat, BreakIfError"

        Context "Function $FunctionName - $ContextName" {

            If ($ObjectType -eq 'File') {

                Mock -ModuleName New-OutputObject -CommandName Get-Date -MockWith { Return [System.String]'20161108-000002' } -ParameterFilter { $Format }

                $ExpectedOutputObjectName = "Output-20161108-000002.txt"

            }
            Else {

                Mock -ModuleName New-OutputObject -CommandName Get-Date -MockWith { Return [System.String]'20161108' } -ParameterFilter { $Format }

                $ExpectedOutputObjectName = "Output-20161108"

            }


            If ($ObjectType -eq 'File') {

                It "Function $FunctionName - $ContextName" {
                    { $Result = New-OutputObject -ObjectType $ObjectType -DateTimePartFormat "yyyyMMdd-HH:mm:ss" -BreakIfError } | Should Throw

                    { $ResultProxyFunction = New-OutputFile -DateTimePartFormat "yyyyMMdd-HH:mm:ss" -BreakIfError } | Should Throw

                }
            }
            Else {

                It "Function $FunctionName - $ContextName" {

                    { $Result = New-OutputObject -ObjectType $ObjectType -DateTimePartFormat "yyyyMMdd-HH>mm/ss" -BreakIfError } | Should Throw

                    { $ResultProxyFunction = New-OutputFolder -DateTimePartFormat "yyyyMMdd-HH>mm/ss" -BreakIfError } | Should Throw

                }
            }

        }


        $ContextName = "run without parameters, incorrect chars in DateTimePartFormat, not BreakIfError"

        Context "Function $FunctionName - $ContextName" {

            If ($ObjectType -eq 'File') {

                $Result = New-OutputObject -ObjectType $ObjectType -DateTimePartFormat "yyyyMMdd-HH:mm:ss"

                $ResultProxyFunction = New-OutputFile -DateTimePartFormat "yyyyMMdd-HH:mm:ss"


            }
            Else {

                $Result = New-OutputObject -ObjectType $ObjectType -DateTimePartFormat "yyyyMMdd-HH>mm/ss"

                $ResultProxyFunction = New-OutputFolder -DateTimePartFormat "yyyyMMdd-HH>mm/ss"

            }

            It "Function $FunctionName - $ContextName - exit code" {

                $Result.ExitCode | Should Be 2

                $ResultProxyFunction.ExitCode | Should Be 2
            }

            It "Function $FunctionName - $ContextName - exit code description" {

                [System.String]$RequiredMessage = "The name not created due to unaccepatable chars"

                $Result.ExitCodeDescription | Should Be $RequiredMessage

                $ResultProxyFunction.ExitCodeDescription | Should Be $RequiredMessage
            }


        }

        Set-Location -Path $LocationAtBegin

    }

}