Skip to content

Commit 028d118

Browse files
authored
Add tests for ref sorting (#6615)
1 parent d58159c commit 028d118

4 files changed

Lines changed: 92 additions & 18 deletions

File tree

eng/Build.ps1

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ param (
4646
[switch]$warnAsError = $true,
4747
[switch][Alias('test')]$testDesktop,
4848
[switch]$testCoreClr,
49+
[switch]$testFSharpCompiler,
4950
[switch]$testFSharpQA,
5051
[switch]$testFSharpCore,
5152
[switch]$testVs,
@@ -77,6 +78,7 @@ function Print-Usage() {
7778
Write-Host " -testAll Run all tests"
7879
Write-Host " -testDesktop Run tests against full .NET Framework"
7980
Write-Host " -testCoreClr Run tests against CoreCLR"
81+
Write-Host " -testFSharpCompiler Run F# Compiler unit tests"
8082
Write-Host " -testFSharpQA Run F# Cambridge tests"
8183
Write-Host " -testFSharpCore Run FSharpCore unit tests"
8284
Write-Host " -testVs Run F# editor unit tests"
@@ -279,16 +281,16 @@ try {
279281
}
280282

281283
if ($testFSharpCore) {
282-
Write-Host "Environment Variables"
283-
Get-Childitem Env:
284284
TestUsingNUnit -testProject "$RepoRoot\tests\FSharp.Core.UnitTests\FSharp.Core.UnitTests.fsproj" -targetFramework $desktopTargetFramework
285285
TestUsingNUnit -testProject "$RepoRoot\tests\FSharp.Core.UnitTests\FSharp.Core.UnitTests.fsproj" -targetFramework $coreclrTargetFramework
286286
}
287287

288+
if ($testFSharpCompiler) {
289+
TestUsingNUnit -testProject "$RepoRoot\tests\FSharp.Compiler.UnitTests\FSharp.Compiler.UnitTests.fsproj" -targetFramework $desktopTargetFramework
290+
TestUsingNUnit -testProject "$RepoRoot\tests\FSharp.Compiler.UnitTests\FSharp.Compiler.UnitTests.fsproj" -targetFramework $coreclrTargetFramework
291+
}
288292

289293
if ($testVs) {
290-
Write-Host "Environment Variables"
291-
Get-Childitem Env:
292294
TestUsingNUnit -testProject "$RepoRoot\vsintegration\tests\GetTypesVS.UnitTests\GetTypesVS.UnitTests.fsproj" -targetFramework $desktopTargetFramework
293295
TestUsingNUnit -testProject "$RepoRoot\vsintegration\tests\UnitTests\VisualFSharp.UnitTests.fsproj" -targetFramework $desktopTargetFramework
294296
}

src/fsharp/DotNetFrameworkDependencies.fs

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -38,39 +38,44 @@ module internal FSharp.Compiler.DotNetFrameworkDependencies
3838
//
3939
// Format:
4040
// =======
41-
// $(Major).$(Minor).$(Build) [-SomePrefix]
41+
// $(Major).$(Minor).$(Build) [-SomeSuffix]
4242
// Major, Minor, Build collates normally
43-
// Strings without -SomePrefix collate higher than SomePrefix,
44-
// SomePrefix collates using normal alphanumeric rules
43+
// Strings without -SomeSuffix collate higher than SomeSuffix,
44+
// SomeSuffix collates using normal alphanumeric rules
4545
//
4646
let deconstructVersion (version:string) =
47-
let getSuffix =
47+
let version, suffix =
4848
let pos = version.IndexOf("-")
49-
if pos >= 0 then version.Substring(pos + 1) else ""
50-
let elements = version.Split('.')
49+
if pos >= 0 then
50+
version.Substring(0, pos), version.Substring(pos + 1)
51+
else version, ""
5152

53+
let elements = version.Split('.')
5254
if elements.Length < 3 then
53-
struct (0, 0, 0, getSuffix)
55+
struct (0, 0, 0, suffix)
5456
else
55-
struct (Int32.Parse(elements.[0]), Int32.Parse(elements.[1]), Int32.Parse(elements.[2]), getSuffix)
57+
struct (Int32.Parse(elements.[0]), Int32.Parse(elements.[1]), Int32.Parse(elements.[2]), suffix)
5658

5759
let versionCompare c1 c2 =
5860
if c1 = c2 then 0
5961
else
6062
try
6163
let struct (major1, minor1, build1, suffix1 ) = deconstructVersion c1
6264
let struct (major2, minor2, build2, suffix2 ) = deconstructVersion c2
63-
64-
let v = major2 - major1
65-
if v = 0 then 0
65+
let v = major1 - major2
66+
if v <> 0 then v
6667
else
67-
let v = minor2 - minor1
68+
let v = minor1 - minor2
6869
if v <> 0 then v
6970
else
70-
let v = build2 - build1
71+
let v = build1 - build2
7172
if v <> 0 then v
7273
else
73-
String.CompareOrdinal(suffix2, suffix1)
74+
match String.IsNullOrEmpty(suffix1), String.IsNullOrEmpty(suffix2) with
75+
| true, true -> 0
76+
| true, false -> 1
77+
| false, true -> -1
78+
| false, false -> String.Compare(suffix1, suffix2, StringComparison.InvariantCultureIgnoreCase)
7479
with _ -> 0
7580

7681
let executionTfm =

tests/FSharp.Compiler.UnitTests/FSharp.Compiler.UnitTests.fsproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
<Compile Include="HashIfExpression.fs" />
1818
<Compile Include="ProductVersion.fs" />
1919
<Compile Include="EditDistance.fs" />
20+
<Compile Include="VersionCompare.fs" />
2021
</ItemGroup>
2122

2223
<ItemGroup>
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information.
2+
namespace FSharp.Compiler.UnitTests
3+
4+
open System
5+
open System.Globalization
6+
open System.Text
7+
open NUnit.Framework
8+
open FSharp.Compiler
9+
10+
[<TestFixture>]
11+
module VersionCompare =
12+
open FSharp.Compiler.DotNetFrameworkDependencies
13+
14+
[<TestCase("1.0.0", "1.0.1", ExpectedResult = -1)>] // 1.0.0 < 1.0.1
15+
[<TestCase("1.0.0", "1.0.0", ExpectedResult = 0)>] // 1.0.0 = 1.0.0
16+
[<TestCase("1.0.1", "1.0.0", ExpectedResult = 1)>] // 1.0.1 > 1.0.1
17+
[<TestCase("0.0.9", "1.0.0-Suffix1", ExpectedResult = -1)>] // 0.0.9 < 1.0.0-Suffix1
18+
[<TestCase("1.0.0", "1.0.0-Suffix1", ExpectedResult = 1)>] // 1.0.0 > 1.0.0-Suffix1
19+
[<TestCase("1.0.0-Suffix1", "1.0.0", ExpectedResult = -1)>] // 1.0.0-Suffix1 < 1.0.0
20+
[<TestCase("1.0.0-Suffix1", "1.0.0-Suffix2", ExpectedResult = -1)>] // 1.0.0-Suffix1 < 1.0.0-Suffix2
21+
[<TestCase("1.0.0-Suffix2", "1.0.0-Suffix1", ExpectedResult = 1)>] // 1.0.0-Suffix2 > 1.0.0-Suffix1
22+
[<TestCase("1.0.0-Suffix1", "1.0.0-Suffix1", ExpectedResult = 0)>] // 1.0.0-Suffix1 > 1.0.0-Suffix2
23+
[<TestCase("1.0.1", "1.0.0-Suffix1", ExpectedResult = 1)>] // 1.0.1 > 1.0.0-Suffix1
24+
[<TestCase("1.0.0-Suffix1", "1.0.1", ExpectedResult = -1)>] // 1.0.0-Suffix1 < 1.0.1
25+
let VersionCompareTest (str1: string, str2: string) : int =
26+
versionCompare str1 str2
27+
28+
29+
[<Test>]
30+
[<TestCase("", ExpectedResult = "3.0.0-preview4-27610-06")>]
31+
let VersionCompareSortArrayHighestPreview _: string =
32+
let versions = [|
33+
"1.0.0-preview4-20000-01"
34+
"3.0.0-preview4-27610-06"
35+
"1.0.0-preview4-20000-02"
36+
"3.0.0-preview4-27610-05"
37+
"3.0.0-preview4-27609-10"
38+
|]
39+
versions |> Array.sortWith (versionCompare) |> Array.last
40+
41+
[<Test>]
42+
[<TestCase("", ExpectedResult = "3.0.0")>]
43+
let VersionCompareSortArrayHighestRelease _: string =
44+
let versions = [|
45+
"1.0.0-preview4-20000-01"
46+
"3.0.0"
47+
"3.0.0-preview4-27610-06"
48+
"1.0.0-preview4-20000-02"
49+
"3.0.0-preview4-27610-05"
50+
"3.0.0-preview4-27609-10"
51+
|]
52+
versions |> Array.sortWith (versionCompare) |> Array.last
53+
54+
[<Test>]
55+
[<TestCase("", ExpectedResult = "3.0.1")>]
56+
let VersionCompareSortArrayEvenHighestRelease _: string =
57+
let versions = [|
58+
"3.0.1"
59+
"1.0.0-preview4-20000-01"
60+
"3.0.0"
61+
"3.0.0-preview4-27610-06"
62+
"1.0.0-preview4-20000-02"
63+
"3.0.0-preview4-27610-05"
64+
"3.0.0-preview4-27609-10"
65+
|]
66+
versions |> Array.sortWith (versionCompare) |> Array.last

0 commit comments

Comments
 (0)