Skip to content

Commit

Permalink
Refactor based on review
Browse files Browse the repository at this point in the history
  • Loading branch information
kentcb committed Jul 24, 2021
1 parent 5993ed4 commit eca7114
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 48 deletions.
9 changes: 9 additions & 0 deletions src/Fantomas.CoreGlobalTool/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"profiles": {
"Fantomas.CoreGlobalTool": {
"commandName": "Project",
"commandLineArgs": "--recurse .",
"workingDirectory": "C:\\Users\\Kent\\Repository\\Chaldal\\Egg.Shell"
}
}
}
131 changes: 101 additions & 30 deletions src/Fantomas.Tests/UtilsTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -4,45 +4,47 @@ open System
open NUnit.Framework
open Fantomas
open Fantomas.Tests.TestHelper
open FsCheck

let private mergeAndCompare a b expected =
let result =
String.merge Environment.NewLine a b
|> String.normalizeNewLine
module String =
let private mergeAndCompare a b expected =
let result =
String.merge Environment.NewLine a b
|> String.normalizeNewLine

let normalizedExpected = String.normalizeNewLine expected
normalizedExpected == result
let normalizedExpected = String.normalizeNewLine expected
normalizedExpected == result

[<Test>]
let ``Merging of source code that starts with a hash`` () =
let a =
"""#if NOT_DEFINED
[<Test>]
let ``Merging of source code that starts with a hash`` () =
let a =
"""#if NOT_DEFINED
printfn \"meh\"
#else
#endif
"""

let b =
"""#if NOT_DEFINED
let b =
"""#if NOT_DEFINED
#else
printfn \"foo\"
#endif
"""

"""#if NOT_DEFINED
"""#if NOT_DEFINED
printfn \"meh\"
#else
printfn \"foo\"
#endif
"""
|> mergeAndCompare a b
|> mergeAndCompare a b

[<Test>]
let ``Merging of defines content work when source code starts with a newline`` () =
let a =
"""
[<Test>]
let ``Merging of defines content work when source code starts with a newline`` () =
let a =
"""
[<Literal>]
let private assemblyConfig() =
#if TRACE
Expand All @@ -53,8 +55,8 @@ let private assemblyConfig() =
x
"""

let b =
"""
let b =
"""
[<Literal>]
let private assemblyConfig() =
#if TRACE
Expand All @@ -65,7 +67,7 @@ let private assemblyConfig() =
x
"""

"""
"""
[<Literal>]
let private assemblyConfig() =
#if TRACE
Expand All @@ -75,12 +77,12 @@ let private assemblyConfig() =
#endif
x
"""
|> mergeAndCompare a b
|> mergeAndCompare a b

[<Test>]
let ``Only split on control structure keyword`` () =
let a =
"""
[<Test>]
let ``Only split on control structure keyword`` () =
let a =
"""
#if INTERACTIVE
#else
#load "../FSharpx.TypeProviders/SetupTesting.fsx"
Expand All @@ -91,8 +93,8 @@ SetupTesting.generateSetupScript __SOURCE_DIRECTORY__
#endif
"""

let b =
"""
let b =
"""
#if INTERACTIVE
#else
Expand All @@ -101,7 +103,7 @@ SetupTesting.generateSetupScript __SOURCE_DIRECTORY__
#endif
"""

"""
"""
#if INTERACTIVE
#else
#load "../FSharpx.TypeProviders/SetupTesting.fsx"
Expand All @@ -111,4 +113,73 @@ SetupTesting.generateSetupScript __SOURCE_DIRECTORY__
#load "__setup__.fsx"
#endif
"""
|> mergeAndCompare a b
|> mergeAndCompare a b

module List =
[<Test>]
let ``When n greater than or equal to list length`` () =
let property (xs: int list, n: int) : bool =
let actual = List.splitAround n xs

match actual with
| None -> n >= List.length xs
| Some (before, None) -> before = xs
| _ -> false

let gen =
gen {
let! xs = Arb.generate<int> |> Gen.listOf
let len = List.length xs

let! n =
Arb.generate<int>
|> Gen.filter (fun n -> n >= len)

return (xs, n)
}

property
|> Prop.forAll (Arb.fromGen gen)
|> Check.QuickThrowOnFailure

[<Test>]
let ``When n less than list length`` () =
let property (xs: int list, n: int) : bool =
let actual = List.splitAround n xs

match actual with
| None -> n < 0
| Some (_, None) -> List.isEmpty xs
| Some (before, Some (at, after)) ->
let xsLength = List.length xs
let beforeLength = List.length before
let afterLength = List.length after

beforeLength = max 0 n
&& afterLength = xsLength - beforeLength - 1
&& before @ (at :: after) = xs

let gen =
gen {
let! xs = Arb.generate<int> |> Gen.listOf
let len = List.length xs
let! n = Arb.generate<int> |> Gen.filter (fun n -> n < len)
return (xs, n)
}

property
|> Prop.forAll (Arb.fromGen gen)
|> Check.QuickThrowOnFailure


[<Test>]
let ``When n equals list length`` () =
let property (xs: int list) : bool =
let actual = List.splitAround (List.length xs) xs

match actual with
| None -> false
| Some (before, None) -> before = xs
| Some (_, Some _) -> false

Check.QuickThrowOnFailure property
20 changes: 2 additions & 18 deletions src/Fantomas/TokenParser.fs
Original file line number Diff line number Diff line change
Expand Up @@ -547,19 +547,6 @@ let private (|InterpStringEndOrPartToken|_|) token =
let escapedCharacterRegex =
System.Text.RegularExpressions.Regex("(\\\\(a|b|f|n|r|t|u|v|x|'|\\\"|\\\\))+")

let private splitAround<'a> (n: int) (xs: 'a list) : ('a list * ('a * 'a list) option) option =
let rec go (n: int) (heads: 'a list) (rest: 'a list) =
if n = 0 then
match rest with
| [] -> Some(List.rev heads, None)
| x :: rest -> Some(List.rev heads, Some(x, rest))
else
match rest with
| [] -> None
| x :: rest -> go (n - 1) (x :: heads) rest

go n [] xs

let private (|EndOfInterpolatedString|_|) tokens =
match tokens with
| StringTextToken _ :: rest ->
Expand All @@ -578,11 +565,8 @@ let private (|EndOfInterpolatedString|_|) tokens =
match maybeLastTokenIndex with
| None -> None
| Some lastTokenIndex ->
match tokens |> splitAround (lastTokenIndex + 1) with
| Some (tokens, Some (endToken, rest)) ->
match tokens with
| [] -> None
| _ -> Some(tokens, endToken, rest)
match tokens |> List.splitAround (lastTokenIndex + 1) with
| Some ((_ :: _) as tokens, Some (endToken, rest)) -> Some(tokens, endToken, rest)
| _ -> None
| _ -> None

Expand Down
14 changes: 14 additions & 0 deletions src/Fantomas/Utils.fs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,20 @@ module List =
| [ _ ] -> false
| _ -> true

let splitAround<'a> (n: int) (xs: 'a list) : ('a list * ('a * 'a list) option) option =
let rec go n heads rest =
if n = 0 then
match rest with
| [] -> Some(List.rev heads, None)
| x :: rest -> Some(List.rev heads, Some(x, rest))
else
match rest with
| [] -> None
| x :: rest -> go (n - 1) (x :: heads) rest

if n < 0 then None else go n [] xs


module Map =
let tryFindOrDefault (defaultValue: 'g) (key: 't) (map: Map<'t, 'g>) =
match Map.tryFind key map with
Expand Down

0 comments on commit eca7114

Please sign in to comment.