Skip to content
This repository was archived by the owner on Apr 5, 2025. It is now read-only.

Commit 0a6786e

Browse files
Fix stdout brittle tests
1 parent efe8fd7 commit 0a6786e

File tree

6 files changed

+50
-28
lines changed

6 files changed

+50
-28
lines changed

src/Fargo/Token.fs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,9 @@ module Token =
8585
result.Close()
8686
| n ->
8787
let txt = input.Substring(pos+1, n-pos-1)
88-
if txt <> "" then
89-
result.Add { Text = txt
90-
Extent = extent (pos+1) n
91-
Quotes = Quotes quote }
88+
result.Add { Text = txt
89+
Extent = extent (pos+1) n
90+
Quotes = Quotes quote }
9291
loop input (n+1) &result
9392

9493
let ofString (input: string) =

tests/Fargo.Test/AllAtOnce.fs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,10 @@ let complete pos input =
7272
Fargo.Run.complete p pos (Token.ofString input)
7373

7474
let complete2 pos input =
75-
let w = new IO.StringWriter()
76-
Console.SetOut(w)
77-
78-
Run.run "test" p [|"complete"; "--position"; string pos; input |] (fun _ x -> printfn "%A" x; Task.FromResult 0) |> ignore
79-
Regex.Replace(w.ToString(),$"{'\x1b'}\[\d+m","").Split('\n')
80-
|> Array.map (fun l -> l.TrimEnd())
81-
|> Array.filter (not << String.IsNullOrEmpty)
82-
|> String.concat Environment.NewLine
75+
Testing.withStdout (fun _ ->
76+
77+
Run.run "test" p [|"complete"; "--position"; string pos; input |] (fun _ x -> task { printfn "%A" x; return 0 }) |> ignore
78+
)
8379

8480

8581
[<Fact>]

tests/Fargo.Test/Fargo.Test.fsproj

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,13 @@
44
<TargetFramework>net7.0</TargetFramework>
55
<GenerateDocumentationFile>true</GenerateDocumentationFile>
66
</PropertyGroup>
7-
<ItemGroup >
8-
<Compile Include="*.fs"/>
7+
<ItemGroup>
8+
<Compile Include="Testing.fs" />
9+
<Compile Include="Token.fs" />
10+
<Compile Include="Parsing.fs" />
11+
<Compile Include="Usage.fs" />
12+
<Compile Include="Completion.fs" />
13+
<Compile Include="AllAtOnce.fs" />
914
</ItemGroup>
1015
<ItemGroup>
1116
<ProjectReference Include="..\..\src\Fargo\Fargo.fsproj" />

tests/Fargo.Test/Testing.fs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
module Testing
2+
3+
open System
4+
open System.Text.RegularExpressions
5+
6+
let testLock = obj()
7+
8+
let withStdout f =
9+
lock testLock (fun _ ->
10+
11+
let w = new IO.StringWriter()
12+
Console.SetOut(w)
13+
14+
f()
15+
16+
Regex.Replace(w.ToString(),$"{'\x1b'}\[\d+m","").Split('\n')
17+
|> Array.map (fun l -> l.TrimEnd())
18+
|> Array.filter (not << String.IsNullOrEmpty)
19+
|> String.concat Environment.NewLine
20+
)
21+
22+
23+
24+
25+

tests/Fargo.Test/Token.fs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,11 @@ let ``Token.ofString then Token.toString should give same result`` (NonNull (arg
104104
let trimmed = args.TrimEnd()
105105
let result = trimmed |> Token.ofString |> Token.toString
106106
result =! trimmed
107+
108+
109+
[<Fact>]
110+
let ``Token.ofString then Token.toString should give same result with empty quotes `` () =
111+
let trimmed = "\"\""
112+
let result = trimmed |> Token.ofString |> Token.toString
113+
result =! trimmed
114+

tests/Fargo.Test/Usage.fs

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,11 @@ let (=!) (actual:'a) (expected: 'a) = Differ.Assert(expected, actual )
1212

1313
let outUsage (p,c) input =
1414
let _,_,usages = p (Token.ofString input)
15-
let w = new IO.StringWriter()
16-
Console.SetOut(w)
17-
printHelp usages
18-
Regex.Replace(w.ToString(),$"{'\x1b'}\[\d+m","").Split('\n')
19-
|> Array.map (fun l -> l.TrimEnd())
20-
|> Array.filter (not << String.IsNullOrEmpty)
21-
|> String.concat Environment.NewLine
15+
Testing.withStdout(fun _ -> printHelp usages)
2216

2317
let outRun p input =
24-
let w = new IO.StringWriter()
25-
Console.SetOut(w)
26-
run "test" p [|input|] (fun _ v -> printfn "%A" v; Task.FromResult 0) |> ignore
27-
Regex.Replace(w.ToString(),$"{'\x1b'}\[\d+m","").Split('\n')
28-
|> Array.map (fun l -> l.TrimEnd())
29-
|> Array.filter (not << String.IsNullOrEmpty)
30-
|> String.concat Environment.NewLine
18+
Testing.withStdout(fun _ ->
19+
run "test" p [|input|] (fun _ v -> task { printfn "%A" v; return 0}) |> ignore)
3120

3221
[<Fact>]
3322
let ``arg usage``() =

0 commit comments

Comments
 (0)