Skip to content

Commit

Permalink
Add additional tests for other pattern combinations.
Browse files Browse the repository at this point in the history
  • Loading branch information
nojaf committed Dec 8, 2022
1 parent b7c2039 commit 5a7b75f
Show file tree
Hide file tree
Showing 2 changed files with 251 additions and 9 deletions.
230 changes: 230 additions & 0 deletions src/Fantomas.Core.Tests/MultilinePatterns.fs
Original file line number Diff line number Diff line change
Expand Up @@ -386,3 +386,233 @@ match parseResults with
)
) -> ()
"""

[<Test>]
let ``as pattern`` () =
formatSourceString
false
"""
match x with
| ABC(Y(
itemOne = OhSomeActivePatternThing(a,
b) as foo)) ->
()
"""
{ config with MaxLineLength = 30 }
|> prepend newline
|> should
equal
"""
match x with
| ABC(
Y(itemOne =
OhSomeActivePatternThing(
a, b
) as foo
)
) -> ()
"""

[<Test>]
let ``or pattern`` () =
formatSourceString
false
"""
match x with
| ABC( Y(itemOne = OhSomeActivePatternThing(a,b) | S(one, two, three, four))) -> ()
"""
{ config with MaxLineLength = 30 }
|> prepend newline
|> should
equal
"""
match x with
| ABC(
Y(itemOne =
OhSomeActivePatternThing(
a, b
) | S(
one,
two,
three,
four
)
)
) -> ()
"""

[<Test>]
let ``cons pattern`` () =
formatSourceString
false
"""
match x with
| ABC( Y(itemOne = OhSomeActivePatternThing(a,b) :: S(one, two, three, four))) -> ()
"""
{ config with MaxLineLength = 30 }
|> prepend newline
|> should
equal
"""
match x with
| ABC(
Y(itemOne =
OhSomeActivePatternThing(
a, b
) :: S(
one,
two,
three,
four
)
)
) -> ()
"""

[<Test>]
let ``array pattern`` () =
formatSourceString
false
"""
match x with
| [| Y(
itemOne = OhSomeActivePatternThing(a,
b))
S(one,
two,
three,
four,
five) |] -> ()
"""
{ config with MaxLineLength = 30 }
|> prepend newline
|> should
equal
"""
match x with
| [| Y(itemOne =
OhSomeActivePatternThing(
a, b
)
)
S(
one,
two,
three,
four,
five
) |] -> ()
"""

[<Test>]
let ``array pattern, alt`` () =
formatSourceString
false
"""
match x with
| [| Y(
itemOne = OhSomeActivePatternThing(a,
b))
S(one,
two,
three,
four,
five) |] -> ()
"""
{ config with
MaxLineLength = 30
MultilineBlockBracketsOnSameColumn = true }
|> prepend newline
|> should
equal
"""
match x with
| [|
Y(itemOne =
OhSomeActivePatternThing(
a, b
)
)
S(
one,
two,
three,
four,
five
)
|] -> ()
"""

[<Test>]
let ``record pattern`` () =
formatSourceString
false
"""
match x with
| { Y = Y(
itemOne = OhSomeActivePatternThing(a,
b))
V = S(one,
two,
three,
four,
five) } -> ()
"""
{ config with MaxLineLength = 30 }
|> prepend newline
|> should
equal
"""
match x with
| { Y = Y(itemOne =
OhSomeActivePatternThing(
a, b
)
)
V = S(
one,
two,
three,
four,
five
) } -> ()
"""

[<Test>]
let ``record pattern, alt`` () =
formatSourceString
false
"""
match x with
| { Y = Y(
itemOne = OhSomeActivePatternThing(a,
b))
V = S(one,
two,
three,
four,
five) } -> ()
"""
{ config with
MaxLineLength = 30
MultilineBlockBracketsOnSameColumn = true }
|> prepend newline
|> should
equal
"""
match x with
| {
Y = Y(itemOne =
OhSomeActivePatternThing(
a, b
)
)
V = S(
one,
two,
three,
four,
five
)
} -> ()
"""
30 changes: 21 additions & 9 deletions src/Fantomas.Core/CodePrinter2.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2543,15 +2543,27 @@ let genPat (p: Pattern) =

expressionFitsOnRestOfLine short long

ifElse
node.Patterns.IsEmpty
(genSingleTextNode node.OpenToken +> genSingleTextNode node.CloseToken)
(genSingleTextNode node.OpenToken
+> addSpaceIfSpaceAroundDelimiter
+> atCurrentColumn genPats
+> addSpaceIfSpaceAroundDelimiter
+> genSingleTextNode node.CloseToken)
|> genNode node
let emptyPattern =
genSingleTextNode node.OpenToken +> genSingleTextNode node.CloseToken

let small =
genSingleTextNode node.OpenToken
+> addSpaceIfSpaceAroundDelimiter
+> atCurrentColumn genPats
+> addSpaceIfSpaceAroundDelimiter
+> genSingleTextNode node.CloseToken

let multilineAlignBrackets =
genSingleTextNode node.OpenToken
+> indentSepNlnUnindent genPats
+> sepNln
+> genSingleTextNode node.CloseToken

let nonEmpty ctx =
let size = getRecordSize ctx node.Patterns
isSmallExpression size small (ifAlignBrackets multilineAlignBrackets small) ctx

ifElse node.Patterns.IsEmpty emptyPattern nonEmpty |> genNode node
| Pattern.Record node ->
let smallRecordExpr =
genSingleTextNode node.OpeningNode
Expand Down

0 comments on commit 5a7b75f

Please sign in to comment.