Skip to content

Commit 2743480

Browse files
committed
add unit tests
1 parent 051508b commit 2743480

File tree

6 files changed

+113
-13
lines changed

6 files changed

+113
-13
lines changed

.github/workflows/unit-tests.yml

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
name: 'Unit tests'
2+
3+
on:
4+
workflow_dispatch:
5+
pull_request:
6+
push:
7+
8+
# Kill other jobs when we trigger this workflow by sending new commits
9+
# to the PR.
10+
# https://stackoverflow.com/a/72408109
11+
concurrency:
12+
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
13+
cancel-in-progress: true
14+
15+
jobs:
16+
unit-tests:
17+
name: Run unit tests with Expecto
18+
runs-on: ubuntu-22.04
19+
steps:
20+
- name: Checkout repo
21+
uses: actions/checkout@v4
22+
- name: Setup .NET
23+
uses: actions/setup-dotnet@v4
24+
with:
25+
dotnet-version: '8.x'
26+
- name: Install dependencies
27+
run: dotnet restore
28+
- name: Run tests
29+
run: dotnet run --project tests/

src/Main.fsproj

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
</ItemGroup>
1212

1313
<ItemGroup>
14+
<Compile Include="TruncateTags.fs" />
1415
<Compile Include="Program.fs" />
1516
</ItemGroup>
1617

src/Program.fs

+11-9
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,16 @@ type RETURN_CODE =
3838
| SUCCESS = 0
3939
| FAIL = 1
4040

41-
let truncate (valuesToTake: int) (args: string array) : string array =
42-
match valuesToTake = -1 with
43-
| true -> args
44-
| false -> args |> Array.truncate valuesToTake
41+
// let getTruncatedTags (valuesToTake: int) (args: string array) : string array =
42+
// let truncate (valuesToTake: int) (args: string array) : string array =
43+
// match valuesToTake = -1 with
44+
// | true -> args
45+
// | false -> args |> Array.truncate valuesToTake
46+
47+
// args
48+
// |> Array.map (fun s -> s.Split(' ') |> Array.filter (String.IsNullOrEmpty >> not))
49+
// |> Array.concat // flat
50+
// |> truncate valuesToTake
4551

4652
let getWorkflowNewPath () =
4753
Path.Combine [| Directory.GetCurrentDirectory(); "workflow.new.yml" |]
@@ -69,11 +75,7 @@ let main (args: string array) : int =
6975
let! valuesToTake = GitHubHelpers.getValuesToTake ()
7076
let! workflowKey = GitHubHelpers.getWorkflowKey ()
7177

72-
let tags =
73-
args
74-
|> Array.map (fun s -> s.Split(' ') |> Array.filter (String.IsNullOrEmpty >> not))
75-
|> Array.concat // flat
76-
|> truncate valuesToTake
78+
let tags = Main.TruncateTags.getTruncatedTags valuesToTake args
7779

7880
do! Validations.validateTagsAreNotEmpty tags
7981

src/TruncateTags.fs

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
namespace Main
2+
3+
module TruncateTags =
4+
5+
open System
6+
7+
let getTruncatedTags (valuesToTake: int) (args: string array) : string array =
8+
let truncate (valuesToTake: int) (args: string array) : string array =
9+
match valuesToTake = -1 with
10+
| true -> args
11+
| false -> args |> Array.truncate valuesToTake
12+
13+
args
14+
|> Array.map (fun s -> s.Split(' ') |> Array.filter (String.IsNullOrEmpty >> not))
15+
|> Array.concat // flat
16+
|> truncate valuesToTake

tests/Program.fs

+50-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,50 @@
1-
// For more information see https://aka.ms/fsharp-console-apps
2-
printfn "Hello from F#"
1+
open Expecto
2+
open Main.TruncateTags
3+
4+
let tests =
5+
let inputArgs =
6+
[| "v0.0.010 v0.0.009 v0.0.008 v0.0.007 v0.0.006 v0.0.005 v0.0.004 v0.0.003 v0.0.002 v0.0.001" |]
7+
8+
let valuesToTake = 5
9+
10+
testList
11+
"Truncated tags tests"
12+
[ test "one string" {
13+
let expected = inputArgs.[0].Split ' ' |> Array.take valuesToTake
14+
15+
Expect.equal (getTruncatedTags valuesToTake inputArgs) expected "Does not work with one string"
16+
}
17+
18+
test "many string" {
19+
let inputArgs = inputArgs.[0].Split ' '
20+
21+
let expected = inputArgs |> Array.take valuesToTake
22+
23+
Expect.equal (getTruncatedTags valuesToTake inputArgs) expected "Does not work with many strings"
24+
}
25+
26+
test "insufficient one string" {
27+
let inputArgs = [| "v0.0.003 v0.0.002 v0.0.001" |]
28+
29+
let expected = inputArgs.[0].Split ' '
30+
31+
Expect.equal
32+
(getTruncatedTags valuesToTake inputArgs)
33+
expected
34+
"Does not work with insufficient one string"
35+
}
36+
37+
test "insufficient many strings" {
38+
let inputArgs = [| "v0.0.003 v0.0.002 v0.0.001" |].[0].Split ' '
39+
40+
let expected = inputArgs
41+
42+
Expect.equal
43+
(getTruncatedTags valuesToTake inputArgs)
44+
expected
45+
"Does not work with insufficient many strings"
46+
} ]
47+
|> testLabel "getTruncatedTags"
48+
49+
[<EntryPoint>]
50+
let main args = runTestsWithCLIArgs [] args tests

tests/Tests.fsproj

+6-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,12 @@
99
<Compile Include="Program.fs" />
1010
</ItemGroup>
1111

12-
<ItemGroup>
13-
<PackageReference Include="Expecto" Version="10.2.1" />
12+
<ItemGroup>
13+
<ProjectReference Include="..\src\Main.fsproj" />
14+
</ItemGroup>
15+
16+
<ItemGroup>
17+
<PackageReference Include="Expecto" Version="10.2.1" />
1418
</ItemGroup>
1519

1620
</Project>

0 commit comments

Comments
 (0)