|
| 1 | +module FsLex.Core.Tests.DriverTests |
| 2 | + |
| 3 | +open System.IO |
| 4 | +open FSharp.Text.Lexing |
| 5 | +open FsLexYacc.FsLex.Driver |
| 6 | +open Expecto |
| 7 | + |
| 8 | +let private writeTopCodeToInterface (header: string) = |
| 9 | + let output = Path.GetTempFileName() |
| 10 | + let outputi = String.concat "" [ output; "i" ] |
| 11 | + |
| 12 | + try |
| 13 | + using (new Writer(output, outputi)) (writeTopCode (header, Position.Empty)) |
| 14 | + File.ReadAllText outputi |
| 15 | + finally |
| 16 | + File.Delete output |
| 17 | + File.Delete outputi |
| 18 | + |
| 19 | +[<Tests>] |
| 20 | +let tests = |
| 21 | + testList "Driver" [ |
| 22 | + testList "getHeaderDeclarations" [ |
| 23 | + test "keeps the module declaration and the opens" { |
| 24 | + let actual = |
| 25 | + getHeaderDeclarations "module Test.Lexer\n\nopen System\nopen System.Text\n\nlet x = 1" |
| 26 | + |
| 27 | + Expect.sequenceEqual |
| 28 | + actual |
| 29 | + [| "module Test.Lexer"; "open System"; "open System.Text" |] |
| 30 | + "Module declaration and opens should be kept" |
| 31 | + } |
| 32 | + |
| 33 | + test "skips a nested module definition" { |
| 34 | + let actual = |
| 35 | + getHeaderDeclarations "open System\nmodule Ranges =\n let isInt8BadMax x = 1 <<< 7 = x" |
| 36 | + |
| 37 | + Expect.sequenceEqual actual [| "open System" |] "A nested module definition should be skipped" |
| 38 | + } |
| 39 | + |
| 40 | + test "keeps a module abbreviation" { |
| 41 | + let actual = getHeaderDeclarations "module Range = FSharp.Compiler.Text.Range" |
| 42 | + |
| 43 | + Expect.sequenceEqual |
| 44 | + actual |
| 45 | + [| "module Range = FSharp.Compiler.Text.Range" |] |
| 46 | + "A module abbreviation should be kept" |
| 47 | + } |
| 48 | + ] |
| 49 | + |
| 50 | + testList "writeTopCode" [ |
| 51 | + test "the header in the signature file is newline terminated" { |
| 52 | + let actual = writeTopCodeToInterface "module Test.Lexer\nopen System\nlet x = 1" |
| 53 | + |
| 54 | + Expect.equal |
| 55 | + actual |
| 56 | + (sprintf "module Test.Lexer%sopen System%s" System.Environment.NewLine System.Environment.NewLine) |
| 57 | + "Every header line should be written on its own line" |
| 58 | + } |
| 59 | + |
| 60 | + test "a header without declarations writes nothing" { |
| 61 | + let actual = writeTopCodeToInterface "let x = 1" |
| 62 | + |
| 63 | + Expect.equal actual "" "Nothing should be written when there is no declaration to repeat" |
| 64 | + } |
| 65 | + ] |
| 66 | + ] |
0 commit comments