1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
|
module Language.Haskell.Stylish.Config.Tests
( tests
) where
--------------------------------------------------------------------------------
import qualified Data.Aeson.Types as Aeson
import qualified Data.ByteString.Lazy.Char8 as BL8
import qualified Data.Set as Set
import qualified Data.YAML.Aeson as Yaml
import System.Directory
import Test.Framework (Test, testGroup)
import Test.Framework.Providers.HUnit (testCase)
import Test.HUnit (Assertion, assert, (@?=))
--------------------------------------------------------------------------------
import Language.Haskell.Stylish.Config
import Language.Haskell.Stylish.Tests.Util
--------------------------------------------------------------------------------
tests :: Test
tests = testGroup "Language.Haskell.Stylish.Config"
[ testCase "Extensions extracted correctly from .cabal file"
testExtensionsFromDotCabal
, testCase "Extensions extracted correctly from .stylish-haskell.yaml file"
testExtensionsFromDotStylish
, testCase "Extensions extracted correctly from .stylish-haskell.yaml and .cabal files"
testExtensionsFromBoth
, testCase "Correctly read .stylish-haskell.yaml file with default max column number"
testDefaultColumns
, testCase "Correctly read .stylish-haskell.yaml file with specified max column number"
testSpecifiedColumns
, testCase "Correctly read .stylish-haskell.yaml file with no max column number"
testNoColumns
, testCase "Backwards-compatible align options"
testBoolSimpleAlign
]
--------------------------------------------------------------------------------
-- | Put an example config files (.cabal/.stylish-haskell.yaml/both)
-- into the current directory and extract extensions from it.
createFilesAndGetConfig :: [(FilePath, String)] -> IO Config
createFilesAndGetConfig files = withTestDirTree $ do
mapM_ (\(k, v) -> writeFile k v) files
-- create an empty directory and change into it
createDirectory "src"
setCurrentDirectory "src"
-- from that directory read the config file and extract extensions
-- to make sure the search for .cabal file works
config <- loadConfig (const (pure ())) Nothing
pure config
--------------------------------------------------------------------------------
testExtensionsFromDotCabal :: Assertion
testExtensionsFromDotCabal =
assert $ (expected ==) . Set.fromList . configLanguageExtensions <$>
createFilesAndGetConfig [("test.cabal", dotCabal True)]
where
expected = Set.fromList ["ScopedTypeVariables", "DataKinds"]
--------------------------------------------------------------------------------
testExtensionsFromDotStylish :: Assertion
testExtensionsFromDotStylish =
assert $ (expected ==) . Set.fromList . configLanguageExtensions <$>
createFilesAndGetConfig [(".stylish-haskell.yaml", dotStylish)]
where
expected = Set.fromList ["TemplateHaskell", "QuasiQuotes"]
--------------------------------------------------------------------------------
testExtensionsFromBoth :: Assertion
testExtensionsFromBoth =
assert $ (expected ==) . Set.fromList . configLanguageExtensions <$>
createFilesAndGetConfig [ ("test.cabal", dotCabal True)
, (".stylish-haskell.yaml", dotStylish)]
where
expected = Set.fromList
["ScopedTypeVariables", "DataKinds", "TemplateHaskell", "QuasiQuotes"]
--------------------------------------------------------------------------------
testSpecifiedColumns :: Assertion
testSpecifiedColumns =
assert $ (expected ==) . configColumns <$>
createFilesAndGetConfig [(".stylish-haskell.yaml", dotStylish)]
where
expected = Just 110
--------------------------------------------------------------------------------
testDefaultColumns :: Assertion
testDefaultColumns =
assert $ (expected ==) . configColumns <$>
createFilesAndGetConfig [(".stylish-haskell.yaml", dotStylish2)]
where
expected = Just 80
--------------------------------------------------------------------------------
testNoColumns :: Assertion
testNoColumns =
assert $ (expected ==) . configColumns <$>
createFilesAndGetConfig [(".stylish-haskell.yaml", dotStylish3)]
where
expected = Nothing
--------------------------------------------------------------------------------
testBoolSimpleAlign :: Assertion
testBoolSimpleAlign = do
Right val <- pure $ Yaml.decode1 $ BL8.pack config
Aeson.Success conf <- pure $ Aeson.parse parseConfig val
length (configSteps conf) @?= 1
where
config = unlines
[ "steps:"
, " - simple_align:"
, " cases: true"
, " top_level_patterns: always"
, " records: false"
]
-- | Example cabal file borrowed from
-- https://www.haskell.org/cabal/users-guide/developing-packages.html
-- with some default-extensions added
dotCabal :: Bool -> String
dotCabal includeExtensions = unlines $
[ "name: TestPackage"
, "version: 0.0"
, "synopsis: Package with library and two programs"
, "license: BSD3"
, "author: Angela Author"
, "build-type: Simple"
, "cabal-version: >= 1.2"
, ""
, "library"
, " build-depends: HUnit"
, " exposed-modules: A, B, C"
] ++
[if includeExtensions then " default-extensions: ScopedTypeVariables"
else ""]
++
[ ""
, "executable program1"
, " main-is: Main.hs"
, " hs-source-dirs: prog1"
, " other-modules: A, B"
] ++
[if includeExtensions then " default-extensions: DataKinds"
else ""]
-- | Example .stylish-haskell.yaml
dotStylish :: String
dotStylish = unlines $
[ "steps:"
, " - imports:"
, " align: none"
, " list_align: after_alias"
, " long_list_align: inline"
, " separate_lists: true"
, " - language_pragmas:"
, " style: vertical"
, " align: false"
, " remove_redundant: true"
, " - trailing_whitespace: {}"
, " - records:"
, " equals: \"same_line\""
, " first_field: \"indent 2\""
, " field_comment: 2"
, " deriving: 4"
, " via: \"indent 2\""
, "columns: 110"
, "language_extensions:"
, " - TemplateHaskell"
, " - QuasiQuotes"
]
-- | Example .stylish-haskell.yaml
dotStylish2 :: String
dotStylish2 = unlines $
[ "steps:"
, " - imports:"
, " align: none"
, " list_align: after_alias"
, " long_list_align: inline"
, " separate_lists: true"
, " - language_pragmas:"
, " style: vertical"
, " align: false"
, " remove_redundant: true"
, " - trailing_whitespace: {}"
, "language_extensions:"
, " - TemplateHaskell"
, " - QuasiQuotes"
]
-- | Example .stylish-haskell.yaml
dotStylish3 :: String
dotStylish3 = unlines $
[ "steps:"
, " - imports:"
, " align: none"
, " list_align: after_alias"
, " long_list_align: inline"
, " separate_lists: true"
, " - language_pragmas:"
, " style: vertical"
, " align: false"
, " remove_redundant: true"
, " - trailing_whitespace: {}"
, "columns: null"
, "language_extensions:"
, " - TemplateHaskell"
, " - QuasiQuotes"
]
|