Stephanevg / PSClassUtils

A set of utilities to work with Powershell Classes
http://powershelldistrict.com/how-to-generate-a-uml-diagram-using-powershell/
91 stars 23 forks source link

Generated test for VOID method still tests for a return type #122

Open Stephanevg opened 4 years ago

Stephanevg commented 4 years ago

Regarding this class below.

The SetVersionType (and for that matter all the void methods) still have a test to test their return type.

When a method has no return type the test to test for the type should not be generated.

See use case below.

Class PCCPackage {
    [String]$Name
    [version]$Version
    [system.io.DirectoryInfo]$Path
    [String]$OS
    [String]$Type
    [String]$Platform

    PCCPAckage(){

    }

    PccPAckage([String]$Path){
        $P = [System.Io.DirectoryInfo]::New($Path)
        If($P.Exists){
            if(!($P.Name.ToString().ToUpper().StartsWith("PCC.CHK"))){
                Throw "the module folder doesn't start with the required prefexi 'PCC.CHK'!"
            }
            $this.Path = $P
            $this.Name = $p.Name.ToUpper()
            $this.SetValues()
        }else{
            Throw "$($Path) not present"
        }
    }

    PCCPackage([String]$Name,[String]$Path){
        $this.Name = $Name
        $this.Path = $Path
        $This.SetValues()

    }

    SetValues(){
        $this._SetVersion()
        $this._SetPlatform()
        $this._SetOS()
        $this._SetType()
    }

    Hidden _SetVersion(){
        try{

            $ver = (Get-Module $this.Path.FullName -ListAvailable -ErrorAction Stop).Version.ToString() 
        }Catch{
            $ver = $null
        }
        $this.Version = $ver
    }

    hidden _SetPlatform(){
        $p =  $this.Name.Split(".")[2]
        $This.Platform =$p
    }

    hidden _SetOS(){

        $this.OS = $this.Name.Split(".")[3]
    }

    Hidden _SetType(){
        $t = $this.Name.Split(".")[4]
        $This.Type = $t
    }

    Hidden [String] GetOS(){
        return $this.Os

    }

    [String] GetPath(){
        REturn $this.Path.Full
    }
    [version] GetVersion(){
        return $this.version
    }
}

See the _SEtVersion() test


#Public Method
It '[PCCPackage] --> _SetVersion() :  - should return type []' {

# -- Arrange

# -- Act

$Instance = [PCCPackage]::New()
# -- Assert

($Instance._SetVersion()).GetType().Name | should be 

} #End It Block
Stephanevg commented 4 years ago

Adding the return types (even VOID ones) actually fixes this issue:

image

We need to implement the same behaviour as for the [VOID] for the NotDefinedreturntypes (which is in the end [VOID]

Si this code exapmle with [VOID]

Class PCCPackage {
    [String]$Name
    [version]$Version
    [system.io.DirectoryInfo]$Path
    [String]$OS
    [String]$Type
    [String]$Platform

    PCCPAckage(){

    }

    PccPAckage([String]$Path){
        $P = [System.Io.DirectoryInfo]::New($Path)
        If($P.Exists){
            if(!($P.Name.ToString().ToUpper().StartsWith("PCC.CHK"))){
                Throw "the module folder doesn't start with the required prefexi 'PCC.CHK'!"
            }
            $this.Path = $P
            $this.Name = $p.Name.ToUpper()
            $this.SetValues()
        }else{
            Throw "$($Path) not present"
        }
    }

    PCCPackage([String]$Name,[String]$Path){
        $this.Name = $Name
        $this.Path = $Path
        $This.SetValues()

    }

    [void]SetValues(){
        $this._SetVersion()
        $this._SetPlatform()
        $this._SetOS()
        $this._SetType()
    }

    Hidden [void]_SetVersion(){
        try{

            $ver = (Get-Module $this.Path.FullName -ListAvailable -ErrorAction Stop).Version.ToString() 
        }Catch{
            $ver = $null
        }
        $this.Version = $ver
    }

    hidden [void] _SetPlatform(){
        $p =  $this.Name.Split(".")[2]
        $This.Platform =$p
    }

    hidden [void] _SetOS(){

        $this.OS = $this.Name.Split(".")[3]
    }

    Hidden [void] _SetType(){
        $t = $this.Name.Split(".")[4]
        $This.Type = $t
    }

    Hidden [String] GetOS(){
        return $this.Os

    }

    [String] GetFullPath(){
        REturn $this.Path.FullName
    }
    [version] GetVersion(){
        return $this.version
    }
}