Skip to content

Commit

Permalink
Add unit tests for AUVersion.ps1
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Démoulins authored and majkinetor committed Oct 29, 2017
1 parent 9f82836 commit 457efde
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 9 deletions.
38 changes: 31 additions & 7 deletions AU/Private/AUVersion.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,26 @@ class AUVersion : System.IComparable {
[int] CompareTo($obj) {
if ($obj -eq $null) { return 1 }
if ($obj -isnot [AUVersion]) { throw "AUVersion expected: $($obj.GetType())" }
if ($obj.Version -ne $this.Version) { return $this.Version.CompareTo($obj.Version) }
if ($obj.Prerelease -and $this.Prerelease) { return $this.Prerelease.CompareTo($obj.Prerelease) }
if (!$obj.Prerelease -and !$this.Prerelease) { return 0 }
if ($obj.Prerelease) { return 1 }
return -1
$t = $this.GetParts()
$o = $obj.GetParts()
for ($i = 0; $i -lt $t.Length -and $i -lt $o.Length; $i++) {
if ($t[$i].GetType() -ne $o[$i].GetType()) {
$t[$i] = [string] $t[$i]
$o[$i] = [string] $o[$i]
}
if ($t[$i] -gt $o[$i]) { return 1 }
if ($t[$i] -lt $o[$i]) { return -1 }
}
if ($t.Length -eq 1 -and $o.Length -gt 1) { return 1 }
if ($o.Length -eq 1 -and $t.Length -gt 1) { return -1 }
if ($t.Length -gt $o.Length) { return 1 }
if ($t.Length -lt $o.Length) { return -1 }
return 0
}

[bool] Equals($obj) { return $obj -is [AUVersion] -and $obj -and $this.ToString().Equals($obj.ToString()) }
[bool] Equals($obj) { return $this.CompareTo($obj) -eq 0 }

[int] GetHashCode() { return $this.ToString().GetHashCode() }
[int] GetHashCode() { return $this.GetParts().GetHashCode() }

[string] ToString() {
$result = $this.Version.ToString()
Expand All @@ -67,6 +77,20 @@ class AUVersion : System.IComparable {
if ($fieldCount -eq -1) { return $this.Version.ToString() }
return $this.Version.ToString($fieldCount)
}

hidden [object[]] GetParts() {
$result = @($this.Version)
if ($this.Prerelease) {
$this.Prerelease -split '\.' | % {
if ($_ -match '[0-9]+') {
$result += [int] $_
} else {
$result += $_
}
}
}
return $result
}
}

function ConvertTo-AUVersion($Version) {
Expand Down
2 changes: 1 addition & 1 deletion build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ $b = {

$ErrorActionPreference = 'Stop'

Write-Host "`n==| Bulding $module_name $version`n"
Write-Host "`n==| Building $module_name $version`n"
init

$module_path = "$build_dir/$module_name"
Expand Down
75 changes: 75 additions & 0 deletions tests/Get-Version.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
remove-module AU -ea ignore
import-module $PSScriptRoot\..\AU

Describe 'Get-Version' -Tag getversion {
$saved_pwd = $pwd

BeforeEach {
cd TestDrive:\
rm -Recurse -Force TestDrive:\test_package -ea ignore
cp -Recurse -Force $PSScriptRoot\test_package TestDrive:\test_package
}

It 'should convert a strict version' {
$expected = '1.2.3.4-beta.1+xyz.001'
$res = ConvertTo-AUVersion $expected

$res | Should Not BeNullOrEmpty
$res.Version | Should Be ([version] '1.2.3.4')
$res.Prerelease | Should BeExactly 'beta.1'
$res.BuildMetadata | Should BeExactly 'xyz.001'
$res.ToString() | Should BeExactly $expected
$res.ToString(2) | Should BeExactly '1.2'
$res.ToString(-1) | Should BeExactly '1.2.3.4'
}

It 'should not convert a non-strict version' {
{ ConvertTo-AUVersion '1.2.3.4a' } | Should Throw
{ ConvertTo-AUVersion 'v1.2.3.4-beta.1+xyz.001' } | Should Throw
}

It 'should parse a non strict version' {
$res = Get-Version 'v1.2.3.4beta.1+xyz.001'

$res | Should Not BeNullOrEmpty
$res.Version | Should Be ([version] '1.2.3.4')
$res.Prerelease | Should BeExactly 'beta.1'
$res.BuildMetadata | Should BeExactly 'xyz.001'
}

$testCases = @(
@{A = '1.0.0' ; B = '1.0.0' ; ExpectedResult = 0}
@{A = '1.0.0' ; B = '2.0.0' ; ExpectedResult = -1}
@{A = '2.0.0' ; B = '2.1.0' ; ExpectedResult = -1}
@{A = '2.1.0' ; B = '2.1.1' ; ExpectedResult = -1}
@{A = '1.0.0-alpha' ; B = '1.0.0-alpha' ; ExpectedResult = 0}
@{A = '1.0.0-alpha' ; B = '1.0.0' ; ExpectedResult = -1}
@{A = '1.0.0-alpha.1' ; B = '1.0.0-alpha.1' ; ExpectedResult = 0}
@{A = '1.0.0-alpha.1' ; B = '1.0.0-alpha.01' ; ExpectedResult = 0}
@{A = '1.0.0-alpha' ; B = '1.0.0-alpha.1' ; ExpectedResult = -1}
@{A = '1.0.0-alpha.1' ; B = '1.0.0-alpha.beta'; ExpectedResult = -1}
@{A = '1.0.0-alpha.beta'; B = '1.0.0-beta' ; ExpectedResult = -1}
@{A = '1.0.0-beta' ; B = '1.0.0-beta.2' ; ExpectedResult = -1}
@{A = '1.0.0-beta.2' ; B = '1.0.0-beta.11' ; ExpectedResult = -1}
@{A = '1.0.0-beta.11' ; B = '1.0.0-rc.1' ; ExpectedResult = -1}
@{A = '1.0.0-rc.1' ; B = '1.0.0' ; ExpectedResult = -1}
@{A = '1.0.0' ; B = '1.0.0+1' ; ExpectedResult = 0}
@{A = '1.0.0+1' ; B = '1.0.0+2' ; ExpectedResult = 0}
@{A = '1.0.0-alpha' ; B = '1.0.0-alpha+1' ; ExpectedResult = 0}
@{A = '1.0.0-alpha+1' ; B = '1.0.0-alpha+2' ; ExpectedResult = 0}
)

It 'should compare 2 versions successfully' -TestCases $testCases { param([string] $A, [string] $B, [int] $ExpectedResult)
$VersionA = ConvertTo-AUVersion $A
$VersionB = ConvertTo-AUVersion $B
if ($ExpectedResult -gt 0 ) {
$VersionA | Should BeGreaterThan $VersionB
} elseif ($ExpectedResult -lt 0 ) {
$VersionA | Should BeLessThan $VersionB
} else {
$VersionA | Should Be $VersionB
}
}

cd $saved_pwd
}
2 changes: 1 addition & 1 deletion tests/Update-Package.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ Describe 'Update-Package' -Tag update {
(nuspec_file).package.metadata.description.InnerText.Trim() | Should Be $readme
}

It 'deesnt set description from README.md with NoReadme parameter' {
It 'does not set description from README.md with NoReadme parameter' {
$readme = 'dummy readme & test'
'','', $readme | Out-File $TestDrive\test_package\README.md
$res = update -NoReadme
Expand Down

0 comments on commit 457efde

Please sign in to comment.