diff options
author | Maxim Koltsov <kolmax94@gmail.com> | 2020-02-17 19:32:01 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-02-17 19:32:01 +0100 |
commit | b8a731eb948b98019b8663c6fc653d2c930df2b1 (patch) | |
tree | ac2b95867755910564075caac605636e9babcaf7 /tests | |
parent | ab85690eb35dec46c8eb80a930337249f34b9f80 (diff) | |
download | stylish-haskell-b8a731eb948b98019b8663c6fc653d2c930df2b1.tar.gz |
Introduce nicer style for records (#266)
Diffstat (limited to 'tests')
-rw-r--r-- | tests/Language/Haskell/Stylish/Config/Tests.hs | 7 | ||||
-rw-r--r-- | tests/Language/Haskell/Stylish/Step/Data/Tests.hs | 311 | ||||
-rw-r--r-- | tests/Language/Haskell/Stylish/Tests.hs | 67 |
3 files changed, 276 insertions, 109 deletions
diff --git a/tests/Language/Haskell/Stylish/Config/Tests.hs b/tests/Language/Haskell/Stylish/Config/Tests.hs index 464ebb7..a8b2ee2 100644 --- a/tests/Language/Haskell/Stylish/Config/Tests.hs +++ b/tests/Language/Haskell/Stylish/Config/Tests.hs @@ -148,8 +148,11 @@ dotStylish = unlines $ , " align: false" , " remove_redundant: true" , " - trailing_whitespace: {}" - , " - records: {}" - , "indent: 2" + , " - records:" + , " equals: \"same_line\"" + , " first_field: \"indent 2\"" + , " field_comment: 2" + , " deriving: 4" , "columns: 110" , "language_extensions:" , " - TemplateHaskell" diff --git a/tests/Language/Haskell/Stylish/Step/Data/Tests.hs b/tests/Language/Haskell/Stylish/Step/Data/Tests.hs index ff5ca3b..1e7f254 100644 --- a/tests/Language/Haskell/Stylish/Step/Data/Tests.hs +++ b/tests/Language/Haskell/Stylish/Step/Data/Tests.hs @@ -31,10 +31,14 @@ tests = testGroup "Language.Haskell.Stylish.Step.Data.Tests" , testCase "case 18" case18 , testCase "case 19" case19 , testCase "case 20 (issue 262)" case20 + , testCase "case 21" case21 + , testCase "case 22" case22 + , testCase "case 23" case23 + , testCase "case 24" case24 ] case00 :: Assertion -case00 = expected @=? testStep (step 2) input +case00 = expected @=? testStep (step sameSameStyle) input where input = unlines [ "module Herp where" @@ -45,7 +49,7 @@ case00 = expected @=? testStep (step 2) input expected = input case01 :: Assertion -case01 = expected @=? testStep (step 2) input +case01 = expected @=? testStep (step indentIndentStyle) input where input = unlines [ "module Herp where" @@ -56,13 +60,14 @@ case01 = expected @=? testStep (step 2) input expected = unlines [ "module Herp where" , "" - , "data Foo = Foo" - , " { a :: Int" - , " }" + , "data Foo" + , " = Foo" + , " { a :: Int" + , " }" ] case02 :: Assertion -case02 = expected @=? testStep (step 2) input +case02 = expected @=? testStep (step indentIndentStyle) input where input = unlines [ "module Herp where" @@ -72,14 +77,15 @@ case02 = expected @=? testStep (step 2) input expected = unlines [ "module Herp where" , "" - , "data Foo = Foo" - , " { a :: Int" - , " , a2 :: String" - , " }" + , "data Foo" + , " = Foo" + , " { a :: Int" + , " , a2 :: String" + , " }" ] case03 :: Assertion -case03 = expected @=? testStep (step 2) input +case03 = expected @=? testStep (step indentIndentStyle) input where input = unlines [ "module Herp where" @@ -89,14 +95,15 @@ case03 = expected @=? testStep (step 2) input expected = unlines [ "module Herp where" , "" - , "data Foo a = Foo" - , " { a :: a" - , " , a2 :: String" - , " }" + , "data Foo a" + , " = Foo" + , " { a :: a" + , " , a2 :: String" + , " }" ] case04 :: Assertion -case04 = expected @=? testStep (step 2) input +case04 = expected @=? testStep (step indentIndentStyle) input where input = unlines [ "module Herp where" @@ -106,17 +113,18 @@ case04 = expected @=? testStep (step 2) input expected = unlines [ "module Herp where" , "" - , "data Foo a = Foo" - , " { a :: a" - , " , a2 :: String" - , " }" + , "data Foo a" + , " = Foo" + , " { a :: a" + , " , a2 :: String" + , " }" , " | Bar" - , " { b :: a" - , " }" + , " { b :: a" + , " }" ] case05 :: Assertion -case05 = expected @=? testStep (step 2) input +case05 = expected @=? testStep (step indentIndentStyle) input where input = unlines [ "module Herp where" @@ -129,14 +137,15 @@ case05 = expected @=? testStep (step 2) input expected = unlines [ "module Herp where" , "" - , "data Foo = Foo" - , " { a :: Int" - , " , a2 :: String" - , " }" + , "data Foo" + , " = Foo" + , " { a :: Int" + , " , a2 :: String" + , " }" ] case06 :: Assertion -case06 = expected @=? testStep (step 2) input +case06 = expected @=? testStep (step sameSameStyle) input where input = unlines [ "module Herp where" @@ -146,7 +155,7 @@ case06 = expected @=? testStep (step 2) input expected = input case07 :: Assertion -case07 = expected @=? testStep (step 2) input +case07 = expected @=? testStep (step sameSameStyle) input where input = unlines [ "module Herp where" @@ -156,7 +165,7 @@ case07 = expected @=? testStep (step 2) input expected = input case08 :: Assertion -case08 = input @=? testStep (step 2) input +case08 = input @=? testStep (step sameSameStyle) input where input = unlines [ "module Herp where" @@ -166,7 +175,7 @@ case08 = input @=? testStep (step 2) input ] case09 :: Assertion -case09 = expected @=? testStep (step 4) input +case09 = expected @=? testStep (step indentIndentStyle4) input where input = unlines [ "module Herp where" @@ -176,18 +185,19 @@ case09 = expected @=? testStep (step 4) input expected = unlines [ "module Herp where" , "" - , "data Foo a b = Foo" - , " { a :: a" - , " , a2 :: String" - , " }" + , "data Foo a b" + , " = Foo" + , " { a :: a" + , " , a2 :: String" + , " }" , " | Bar" - , " { b :: a" - , " , c :: b" - , " }" + , " { b :: a" + , " , c :: b" + , " }" ] case10 :: Assertion -case10 = expected @=? testStep (step 2) input +case10 = expected @=? testStep (step indentIndentStyle) input where input = unlines [ "module Herp where" @@ -198,15 +208,16 @@ case10 = expected @=? testStep (step 2) input expected = unlines [ "module Herp where" , "" - , "data Foo = Foo" - , " { a :: Int" - , " }" + , "data Foo" + , " = Foo" + , " { a :: Int" + , " }" , " deriving (Eq, Generic)" , " deriving (Show)" ] case11 :: Assertion -case11 = expected @=? testStep (step 2) input +case11 = expected @=? testStep (step indentIndentStyle) input where input = unlines [ "{-# LANGUAGE DerivingStrategies #-}" @@ -219,14 +230,15 @@ case11 = expected @=? testStep (step 2) input [ "{-# LANGUAGE DerivingStrategies #-}" , "module Herp where" , "" - , "data Foo = Foo" - , " { a :: Int" - , " }" + , "data Foo" + , " = Foo" + , " { a :: Int" + , " }" , " deriving stock (Show)" ] case12 :: Assertion -case12 = expected @=? testStep (step 4) input +case12 = expected @=? testStep (step indentIndentStyle4) input where input = unlines [ "module Herp where" @@ -237,15 +249,16 @@ case12 = expected @=? testStep (step 4) input expected = unlines [ "module Herp where" , "" - , "data Point = Point" - , " { pointX, pointY :: Double" - , " , pointName :: String" - , " }" + , "data Point" + , " = Point" + , " { pointX, pointY :: Double" + , " , pointName :: String" + , " }" , " deriving (Show)" ] case13 :: Assertion -case13 = expected @=? testStep (step 2) input +case13 = expected @=? testStep (step indentIndentStyle) input where input = unlines [ "module Herp where" @@ -257,13 +270,14 @@ case13 = expected @=? testStep (step 2) input [ "module Herp where" , "" , "-- this is a comment" - , "data Foo = Foo" - , " { a :: Int" - , " }" + , "data Foo" + , " = Foo" + , " { a :: Int" + , " }" ] case14 :: Assertion -case14 = expected @=? testStep (step 2) input +case14 = expected @=? testStep (step indentIndentStyle) input where input = unlines [ "module Herp where" @@ -277,13 +291,14 @@ case14 = expected @=? testStep (step 2) input , "" , "{- this is" , " a comment -}" - , "data Foo = Foo" - , " { a :: Int" - , " }" + , "data Foo" + , " = Foo" + , " { a :: Int" + , " }" ] case15 :: Assertion -case15 = expected @=? testStep (step 2) input +case15 = expected @=? testStep (step indentIndentStyle) input where input = unlines [ "module Herp where" @@ -296,14 +311,15 @@ case15 = expected @=? testStep (step 2) input expected = unlines [ "module Herp where" , "" - , "data Foo a = Foo" - , " { a :: a -- comment" - , " , a2 :: String" - , " }" + , "data Foo a" + , " = Foo" + , " { a :: a -- comment" + , " , a2 :: String" + , " }" ] case16 :: Assertion -case16 = expected @=? testStep (step 2) input +case16 = expected @=? testStep (step indentIndentStyle) input where input = unlines [ "module Herp where" @@ -315,13 +331,14 @@ case16 = expected @=? testStep (step 2) input expected = unlines [ "module Herp where" , "" - , "data Foo = Foo" - , " { a :: Int -- ^ comment" - , " }" + , "data Foo" + , " = Foo" + , " { a :: Int -- ^ comment" + , " }" ] case17 :: Assertion -case17 = expected @=? testStep (step 2) input +case17 = expected @=? testStep (step indentIndentStyle) input where input = unlines [ "module Herp where" @@ -335,15 +352,16 @@ case17 = expected @=? testStep (step 2) input expected = unlines [ "module Herp where" , "" - , "data Foo a = Foo" - , " { a :: a" - , " -- comment" - , " , a2 :: String" - , " }" + , "data Foo a" + , " = Foo" + , " { a :: a" + , " -- comment" + , " , a2 :: String" + , " }" ] case18 :: Assertion -case18 = expected @=? testStep (step 2) input +case18 = expected @=? testStep (step indentIndentStyle) input where input = unlines [ "module Herp where" @@ -357,15 +375,16 @@ case18 = expected @=? testStep (step 2) input expected = unlines [ "module Herp where" , "" - , "data Foo a = Foo" - , " { a :: a" - , " -- ^ comment" - , " , a2 :: String" - , " }" + , "data Foo a" + , " = Foo" + , " { a :: a" + , " -- ^ comment" + , " , a2 :: String" + , " }" ] case19 :: Assertion -case19 = expected @=? testStep (step 2) input +case19 = expected @=? testStep (step indentIndentStyle) input where input = unlines [ "module Herp where" @@ -379,21 +398,139 @@ case19 = expected @=? testStep (step 2) input expected = unlines [ "module Herp where" , "" - , "data Foo a = Foo" - , " { firstName, lastName :: String" - , " -- ^ names" - , " , age :: Int" - , " }" + , "data Foo a" + , " = Foo" + , " { firstName, lastName :: String" + , " -- ^ names" + , " , age :: Int" + , " }" ] -- | Should not break Enums (data without records) formating -- -- See https://github.com/jaspervdj/stylish-haskell/issues/262 case20 :: Assertion -case20 = input @=? testStep (step 2) input +case20 = input @=? testStep (step indentIndentStyle) input where input = unlines [ "module Herp where" , "" , "data Tag = Title | Text deriving (Eq, Show)" ] + +case21 :: Assertion +case21 = expected @=? testStep (step sameSameStyle) input + where + input = unlines + [ "data Foo a" + , " = Foo { a :: Int," + , " a2 :: String" + , " -- ^ some haddock" + , " }" + , " | Bar { b :: a } deriving (Eq, Show)" + , " deriving (ToJSON)" + ] + + expected = unlines + [ "data Foo a = Foo { a :: Int" + , " , a2 :: String" + , " -- ^ some haddock" + , " }" + , " | Bar { b :: a" + , " }" + , " deriving (Eq, Show)" + , " deriving (ToJSON)" + ] + +case22 :: Assertion +case22 = expected @=? testStep (step sameIndentStyle) input + where + input = unlines + [ "data Foo a" + , " = Foo { a :: Int," + , " a2 :: String" + , " -- ^ some haddock" + , " }" + , " | Bar { b :: a } deriving (Eq, Show)" + , " deriving (ToJSON)" + ] + + expected = unlines + [ "data Foo a = Foo" + , " { a :: Int" + , " , a2 :: String" + , " -- ^ some haddock" + , " }" + , " | Bar" + , " { b :: a" + , " }" + , " deriving (Eq, Show)" + , " deriving (ToJSON)" + ] + +case23 :: Assertion +case23 = expected @=? testStep (step indentSameStyle) input + where + input = unlines + [ "data Foo a" + , " = Foo { a :: Int," + , " a2 :: String" + , " -- ^ some haddock" + , " }" + , " | Bar { b :: a } deriving (Eq, Show)" + , " deriving (ToJSON)" + ] + + expected = unlines + [ "data Foo a" + , " = Foo { a :: Int" + , " , a2 :: String" + , " -- ^ some haddock" + , " }" + , " | Bar { b :: a" + , " }" + , " deriving (Eq, Show)" + , " deriving (ToJSON)" + ] + +case24 :: Assertion +case24 = expected @=? testStep (step indentIndentStyle) input + where + input = unlines + [ "data Foo a" + , " = Foo { a :: Int," + , " a2 :: String" + , " -- ^ some haddock" + , " }" + , " | Bar { b :: a } deriving (Eq, Show)" + , " deriving (ToJSON)" + ] + + expected = unlines + [ "data Foo a" + , " = Foo" + , " { a :: Int" + , " , a2 :: String" + , " -- ^ some haddock" + , " }" + , " | Bar" + , " { b :: a" + , " }" + , " deriving (Eq, Show)" + , " deriving (ToJSON)" + ] + +sameSameStyle :: Config +sameSameStyle = Config SameLine SameLine 2 2 + +sameIndentStyle :: Config +sameIndentStyle = Config SameLine (Indent 2) 2 2 + +indentSameStyle :: Config +indentSameStyle = Config (Indent 2) SameLine 2 2 + +indentIndentStyle :: Config +indentIndentStyle = Config (Indent 2) (Indent 2) 2 2 + +indentIndentStyle4 :: Config +indentIndentStyle4 = Config (Indent 4) (Indent 4) 4 4 diff --git a/tests/Language/Haskell/Stylish/Tests.hs b/tests/Language/Haskell/Stylish/Tests.hs index e7faa9b..97eab8a 100644 --- a/tests/Language/Haskell/Stylish/Tests.hs +++ b/tests/Language/Haskell/Stylish/Tests.hs @@ -20,13 +20,14 @@ import Language.Haskell.Stylish.Tests.Util -------------------------------------------------------------------------------- tests :: Test -tests = testGroup "Language.Haskell.Stylish.Step.Tabs.Tests" +tests = testGroup "Language.Haskell.Stylish.Tests" [ testCase "case 01" case01 , testCase "case 02" case02 , testCase "case 03" case03 , testCase "case 04" case04 , testCase "case 05" case05 , testCase "case 06" case06 + , testCase "case 07" case07 ] @@ -35,12 +36,7 @@ case01 :: Assertion case01 = (@?= result) =<< format Nothing Nothing input where input = "module Herp where\ndata Foo = Bar | Baz { baz :: Int }" - result = Right [ "module Herp where" - , "data Foo = Bar" - , " | Baz" - , " { baz :: Int" - , " }" - ] + result = Right $ lines input -------------------------------------------------------------------------------- @@ -48,8 +44,11 @@ case02 :: Assertion case02 = withTestDirTree $ do writeFile "test-config.yaml" $ unlines [ "steps:" - , " - records: {}" - , "indent: 2" + , " - records:" + , " equals: \"indent 2\"" + , " first_field: \"indent 2\"" + , " field_comment: 2" + , " deriving: 2" ] actual <- format (Just $ ConfigPath "test-config.yaml") Nothing input @@ -57,16 +56,44 @@ case02 = withTestDirTree $ do where input = "module Herp where\ndata Foo = Bar | Baz { baz :: Int }" result = Right [ "module Herp where" - , "data Foo = Bar" + , "data Foo" + , " = Bar" , " | Baz" - , " { baz :: Int" - , " }" + , " { baz :: Int" + , " }" ] - -------------------------------------------------------------------------------- case03 :: Assertion -case03 = (@?= result) =<< format Nothing (Just fileLocation) input +case03 = withTestDirTree $ do + writeFile "test-config.yaml" $ unlines + [ "steps:" + , " - records:" + , " equals: \"same_line\"" + , " first_field: \"same_line\"" + , " field_comment: 2" + , " deriving: 2" + ] + + actual <- format (Just $ ConfigPath "test-config.yaml") Nothing input + actual @?= result + where + input = unlines [ "module Herp where" + , "data Foo" + , " = Bar" + , " | Baz" + , " { baz :: Int" + , " }" + ] + result = Right [ "module Herp where" + , "data Foo = Bar" + , " | Baz { baz :: Int" + , " }" + ] + +-------------------------------------------------------------------------------- +case04 :: Assertion +case04 = (@?= result) =<< format Nothing (Just fileLocation) input where fileLocation = "directory/File.hs" input = "module Herp" @@ -78,8 +105,8 @@ case03 = (@?= result) =<< format Nothing (Just fileLocation) input -------------------------------------------------------------------------------- -- | When providing current dir including folders and files. -case04 :: Assertion -case04 = withTestDirTree $ do +case05 :: Assertion +case05 = withTestDirTree $ do createDirectory aDir >> writeFile c fileCont mapM_ (flip writeFile fileCont) fs result <- findHaskellFiles False input @@ -95,8 +122,8 @@ case04 = withTestDirTree $ do -------------------------------------------------------------------------------- -- | When the input item is not file, do not recurse it. -case05 :: Assertion -case05 = withTestDirTree $ do +case06 :: Assertion +case06 = withTestDirTree $ do mapM_ (flip writeFile "") input result <- findHaskellFiles False input result @?= expected @@ -107,8 +134,8 @@ case05 = withTestDirTree $ do -------------------------------------------------------------------------------- -- | Empty input should result in empty output. -case06 :: Assertion -case06 = withTestDirTree $ do +case07 :: Assertion +case07 = withTestDirTree $ do mapM_ (flip writeFile "") input result <- findHaskellFiles False input result @?= expected |