Skip to content

Commit edc777a

Browse files
committed
Add new step Signature that will format function fignatures
The step is a NOP still but additional tests were already created specifing how we anticipate the step to behave. This step is heavily inspired by https://github.com/input-output-hk/ouroboros-network/blob/bf8579cc2ff2a7bc4ba23150eff659cfd1c6ccca/ouroboros-consensus/docs/StyleGuide.md
1 parent 03b34d3 commit edc777a

File tree

4 files changed

+159
-0
lines changed

4 files changed

+159
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module Language.Haskell.Stylish.Step.Signature where
2+
3+
import Language.Haskell.Stylish.Step
4+
5+
data Config = Config
6+
{ maxColumnLength :: Int
7+
}
8+
9+
step :: Config -> Step
10+
step _ = makeStep "Signature" (\ls _ -> ls)

stylish-haskell.cabal

+3
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ Library
3737
Language.Haskell.Stylish.Step.Imports
3838
Language.Haskell.Stylish.Step.ModuleHeader
3939
Language.Haskell.Stylish.Step.LanguagePragmas
40+
Language.Haskell.Stylish.Step.Signature
4041
Language.Haskell.Stylish.Step.SimpleAlign
4142
Language.Haskell.Stylish.Step.Squash
4243
Language.Haskell.Stylish.Step.Tabs
@@ -137,6 +138,8 @@ Test-suite stylish-haskell-tests
137138
Language.Haskell.Stylish.Step.ModuleHeader.Tests
138139
Language.Haskell.Stylish.Step.LanguagePragmas
139140
Language.Haskell.Stylish.Step.LanguagePragmas.Tests
141+
Language.Haskell.Stylish.Step.Signature
142+
Language.Haskell.Stylish.Step.Signature.Tests
140143
Language.Haskell.Stylish.Step.SimpleAlign
141144
Language.Haskell.Stylish.Step.SimpleAlign.Tests
142145
Language.Haskell.Stylish.Step.Squash
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
{-# LANGUAGE OverloadedLists #-}
2+
{-# LANGUAGE OverloadedStrings #-}
3+
module Language.Haskell.Stylish.Step.Signature.Tests
4+
( tests
5+
) where
6+
7+
import Language.Haskell.Stylish.Step.Signature
8+
import Language.Haskell.Stylish.Tests.Util (assertSnippet, testStep)
9+
import Test.Framework (Test, testGroup)
10+
import Test.Framework.Providers.HUnit (testCase)
11+
import Test.HUnit (Assertion, (@=?))
12+
13+
tests :: Test
14+
tests = testGroup "Language.Haskell.Stylish.Step.Signature.Tests"
15+
[ testCase "do not wrap signature if it fits max column length" case00
16+
-- , testCase "wrap signature if it does not fit max column length" case01
17+
-- , testCase "how it behaves when there is a list of constraints" case02
18+
-- , testCase "how it behaves when there is a explicit forall" case03
19+
-- , testCase "how it behaves when there is a explicit forall" case04
20+
-- , testCase "how it behaves when there is a large function in the argument" case05
21+
]
22+
23+
config :: Int -> Config
24+
config maxColumnLength = Config
25+
{ maxColumnLength = maxColumnLength
26+
}
27+
28+
case00 :: Assertion
29+
case00 = expected @=? testStep (step $ config 80) input
30+
where
31+
input = unlines
32+
[ "module Herp where"
33+
, ""
34+
, "fooBar :: a -> b -> a"
35+
, "fooBar v _ = v"
36+
]
37+
expected = input
38+
39+
case01 :: Assertion
40+
case01 = expected @=? testStep (step $ config 20) input
41+
where
42+
input = unlines
43+
[ "module Herp where"
44+
, ""
45+
, "fooBar :: a -> b -> a"
46+
, "fooBar v _ = v"
47+
]
48+
expected = unlines
49+
[ "module Herp where"
50+
, ""
51+
, "fooBar ::"
52+
, " a"
53+
, " -> b"
54+
, " -> a"
55+
, "fooBar v _ = v"
56+
]
57+
58+
case02 :: Assertion
59+
case02 = expected @=? testStep (step $ config 20) input
60+
where
61+
input = unlines
62+
[ "module Herp where"
63+
, ""
64+
, "fooBar :: (Eq a, Show b) => a -> b -> a"
65+
, "fooBar v _ = v"
66+
]
67+
expected = unlines
68+
[ "module Herp where"
69+
, ""
70+
, "fooBar ::"
71+
, " (Eq a, Show b)"
72+
, " => a"
73+
, " -> b"
74+
, " -> a"
75+
, "fooBar v _ = v"
76+
]
77+
78+
case03 :: Assertion
79+
case03 = expected @=? testStep (step $ config 20) input
80+
where
81+
input = unlines
82+
[ "module Herp where"
83+
, ""
84+
, "fooBar :: forall a . b. (Eq a, Show b) => a -> b -> a"
85+
, "fooBar v _ = v"
86+
]
87+
expected = unlines
88+
[ "module Herp where"
89+
, ""
90+
, "fooBar ::"
91+
, " forall a . b."
92+
, " (Eq a, Show b)"
93+
, " => a"
94+
, " -> b"
95+
, " -> a"
96+
, "fooBar v _ = v"
97+
]
98+
99+
case04 :: Assertion
100+
case04 = expected @=? testStep (step $ config 20) input
101+
where
102+
input = unlines
103+
[ "module Herp where"
104+
, ""
105+
, "fooBar :: forall a . b. c. (Eq a, Show b, Ord c) => a -> b -> c -> a"
106+
, "fooBar v _ _ = v"
107+
]
108+
expected = unlines
109+
[ "module Herp where"
110+
, ""
111+
, "fooBar ::"
112+
, " forall a . b. ("
113+
, " Eq a"
114+
, " , Show b"
115+
, " , Ord c)"
116+
, " )"
117+
, " => a"
118+
, " -> b"
119+
, " -> a"
120+
, "fooBar v _ = v"
121+
]
122+
123+
case05 :: Assertion
124+
case05 = expected @=? testStep (step $ config 20) input
125+
where
126+
input = unlines
127+
[ "module Herp where"
128+
, ""
129+
, "fooBar :: => a -> (forall c. Eq c => c -> a -> a) -> a"
130+
, "fooBar v _ = v"
131+
]
132+
expected = unlines
133+
[ "module Herp where"
134+
, ""
135+
, "fooBar ::"
136+
, " => a"
137+
, " -> ( forall c. Eq c"
138+
, " => c"
139+
, " -> a"
140+
, " -> a"
141+
, " )"
142+
, " -> a"
143+
, "fooBar v _ = v"
144+
]

tests/TestSuite.hs

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import qualified Language.Haskell.Stylish.Step.Imports.Tests
1616
import qualified Language.Haskell.Stylish.Step.Imports.FelixTests
1717
import qualified Language.Haskell.Stylish.Step.ModuleHeader.Tests
1818
import qualified Language.Haskell.Stylish.Step.LanguagePragmas.Tests
19+
import qualified Language.Haskell.Stylish.Step.Signature.Tests
1920
import qualified Language.Haskell.Stylish.Step.SimpleAlign.Tests
2021
import qualified Language.Haskell.Stylish.Step.Squash.Tests
2122
import qualified Language.Haskell.Stylish.Step.Tabs.Tests
@@ -34,6 +35,7 @@ main = defaultMain
3435
, Language.Haskell.Stylish.Step.Imports.FelixTests.tests
3536
, Language.Haskell.Stylish.Step.LanguagePragmas.Tests.tests
3637
, Language.Haskell.Stylish.Step.ModuleHeader.Tests.tests
38+
, Language.Haskell.Stylish.Step.Signature.Tests.tests
3739
, Language.Haskell.Stylish.Step.SimpleAlign.Tests.tests
3840
, Language.Haskell.Stylish.Step.Squash.Tests.tests
3941
, Language.Haskell.Stylish.Step.Tabs.Tests.tests

0 commit comments

Comments
 (0)