Skip to content

Commit

Permalink
Do not output full path for SourceIdentifier in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vipentti committed Dec 26, 2023
1 parent b507fbf commit 482f8dd
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/Visp.Compiler/Syntax/LexHelpers.fs
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ let symbolOrKeywordToken (args: LexArgs) (lexbuf: FSharp.Text.Lexing.LexBuffer<_
filename |> System.IO.Path.GetFullPath |> System.IO.Path.GetDirectoryName

let dirname =
if Syntax.SyntaxWriteUtilThreadStatics.NormalizeLineEndings then
if Syntax.SyntaxWriteUtilThreadStatics.RunningTests then
dirname.Replace('\\', '/')
else
dirname
Expand Down
19 changes: 13 additions & 6 deletions src/Visp.Compiler/Syntax/Syntax.fs
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,11 @@ type SynStringKind =

type SyntaxWriteUtilThreadStatics =
[<System.ThreadStatic; DefaultValue>]
static val mutable private normalizeLineEndings: bool
static val mutable private runningTests: bool

static member NormalizeLineEndings
with get () = SyntaxWriteUtilThreadStatics.normalizeLineEndings
and set v = SyntaxWriteUtilThreadStatics.normalizeLineEndings <- v
static member RunningTests
with get () = SyntaxWriteUtilThreadStatics.runningTests
and set v = SyntaxWriteUtilThreadStatics.runningTests <- v

module StringWriterUtils =
let inline writeDebugStringType (name: string) (text: string) kind range =
Expand All @@ -129,7 +129,7 @@ module StringWriterUtils =

for ch in text do
match ch with
| '\r' when SyntaxWriteUtilThreadStatics.NormalizeLineEndings -> ()
| '\r' when SyntaxWriteUtilThreadStatics.RunningTests -> ()
| it -> ignore (sb.Append it)

sb.Append "\", " |> ignore
Expand Down Expand Up @@ -184,7 +184,14 @@ type SynConst =
| Char it -> sprintf "Char %A" it
| Decimal it -> sprintf "Decimal %A" it
| UserNum(va, su) -> sprintf "UserNum (%s, %s)" va su
| SourceIdentifier(va, su, _) -> sprintf "SourceIdentifier (%s, %s)" va su
| SourceIdentifier(va, su, _) ->
if SyntaxWriteUtilThreadStatics.RunningTests then
match va with
| "__SOURCE_DIRECTORY__" ->
sprintf "SourceIdentifier (\"%s\", \"%s\")" va (System.IO.Path.GetFileName su)
| _ -> sprintf "SourceIdentifier (\"%s\", \"%s\")" va su
else
sprintf "SourceIdentifier (%s, %s)" va su
| String(text, k, r) -> StringWriterUtils.writeDebugStringType "String" text k r

type SynTyped = SynTyped of name: SynSymbol * argtype: SynType * range: range
Expand Down
4 changes: 2 additions & 2 deletions tests/Visp.Compiler.UnitTests/TestUtils.fs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ let runTokenTest (name: string) =
try
let parsed = CoreParser.debugLexFile path
let nameParam = name.Replace('/', '_').Replace('\\', '_')
Syntax.SyntaxWriteUtilThreadStatics.NormalizeLineEndings <- true
Syntax.SyntaxWriteUtilThreadStatics.RunningTests <- true
let output = parsed |> String.concat "\n"
return! verify output "token-snapshots" nameParam
with :? ParseHelpers.SyntaxError as syn ->
Expand All @@ -69,7 +69,7 @@ let runStructuredOutputTest (name: string) =

let nameParam = name.Replace('/', '_').Replace('\\', '_')

Syntax.SyntaxWriteUtilThreadStatics.NormalizeLineEndings <- true
Syntax.SyntaxWriteUtilThreadStatics.RunningTests <- true

let output = (sprintf "%120A" parsed).Replace("\\r\\n", "\\n").Replace("\r\n", "\n")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,15 @@
[FunctionCall
(Symbol (SynSymbol printfn),
[Const (String ("Line: %s", Regular, (9,13--9,23)), (9,13--9,23));
Const (SourceIdentifier (__LINE__, 9), (9,24--9,32))], (9,5--9,32));
Const (SourceIdentifier ("__LINE__", "9"), (9,24--9,32))], (9,5--9,32));
FunctionCall
(Symbol (SynSymbol printfn),
[Const (String ("Source Directory: %s", Regular, (10,13--10,35)), (10,13--10,35));
Const
(SourceIdentifier (__SOURCE_DIRECTORY__, {SolutionDirectory}visp/tests/parsing),
(10,36--10,56))], (10,5--10,56));
Const (SourceIdentifier ("__SOURCE_DIRECTORY__", "parsing"), (10,36--10,56))], (10,5--10,56));
FunctionCall
(Symbol (SynSymbol printfn),
[Const (String ("Source File: %s", Regular, (11,13--11,30)), (11,13--11,30));
Const (SourceIdentifier (__SOURCE_FILE__, keyword-strings-0.visp), (11,31--11,46))], (11,5--11,46))],
Const (SourceIdentifier ("__SOURCE_FILE__", "keyword-strings-0.visp"), (11,31--11,46))], (11,5--11,46))],
None, (8,1--11,47)), (8,0--12,1));
Expr
(LetOrUse
Expand Down

0 comments on commit 482f8dd

Please sign in to comment.