Skip to content

Expose "format" function in Sylish.hs #259

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 26, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions lib/Language/Haskell/Stylish.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ module Language.Haskell.Stylish
-- * Misc
, module Language.Haskell.Stylish.Verbose
, version
, format
, ConfigPath(..)
, Lines
, Step
) where
Expand Down Expand Up @@ -91,3 +93,13 @@ runStep exts mfp ls step =
runSteps :: Extensions -> Maybe FilePath -> [Step] -> Lines
-> Either String Lines
runSteps exts mfp steps ls = foldM (runStep exts mfp) ls steps

newtype ConfigPath = ConfigPath { unConfigPath :: FilePath }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I love the newtype added. Type safety for the win :)


-- |Formats given contents optionally using the config provided as first param.
-- The second file path is the location from which the contents were read.
-- If provided, it's going to be printed out in the error message.
format :: Maybe ConfigPath -> Maybe FilePath -> String -> IO (Either String Lines)
format maybeConfigPath maybeFilePath contents = do
conf <- loadConfig (makeVerbose True) (fmap unConfigPath maybeConfigPath)
pure $ runSteps (configLanguageExtensions conf) maybeFilePath (configSteps conf) $ lines contents
1 change: 1 addition & 0 deletions stylish-haskell.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ Test-suite stylish-haskell-tests
Type: exitcode-stdio-1.0

Other-modules:
Language.Haskell.StylishSpec
Language.Haskell.Stylish.Align
Language.Haskell.Stylish.Block
Language.Haskell.Stylish.Config
Expand Down
3 changes: 3 additions & 0 deletions testdata/test-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
steps:
- records: {}
indent: 2
51 changes: 51 additions & 0 deletions tests/Language/Haskell/StylishSpec.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
module Language.Haskell.StylishSpec where

--------------------------------------------------------------------------------
import Test.Framework (Test, testGroup)
import Test.Framework.Providers.HUnit (testCase)
import Test.HUnit (Assertion, (@?=))

--------------------------------------------------------------------------------
import Language.Haskell.Stylish

--------------------------------------------------------------------------------
import System.IO.Unsafe
--------------------------------------------------------------------------------

tests :: Test
tests = testGroup "Language.Haskell.Stylish.Step.Tabs.Tests"
[ testCase "case 01" case01
, testCase "case 02" case02
, testCase "case 03" case03
]

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove those two empty lines maybe?


--------------------------------------------------------------------------------
case01 :: Assertion
case01 = (@?=) result (unsafePerformIO $ format Nothing Nothing input)
where
input = "module Herp where\n data Foo = Bar | Baz"
result = Right [ "module Herp where"
, "data Foo = Bar"
, " | Baz"
]

case02 :: Assertion
case02 = (@?=) result (unsafePerformIO $ format (Just configLocation) Nothing input)
where
configLocation = ConfigPath "testdata/test-config.yaml"
input = "module Herp where\n data Foo = Bar | Baz"
result = Right [ "module Herp where"
, "data Foo = Bar"
, " | Baz"
]

case03 :: Assertion
case03 = (@?=) result (unsafePerformIO $ format Nothing (Just fileLocation) input)
where
fileLocation = "directory/File.hs"
input = "module Herp"
result = Left $
"Language.Haskell.Stylish.Parse.parseModule: could not parse " <>
fileLocation <>
": ParseFailed (SrcLoc \"<unknown>.hs\" 2 1) \"Parse error: EOF\""
4 changes: 3 additions & 1 deletion tests/TestSuite.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ import qualified Language.Haskell.Stylish.Step.Squash.Tests
import qualified Language.Haskell.Stylish.Step.Tabs.Tests
import qualified Language.Haskell.Stylish.Step.TrailingWhitespace.Tests
import qualified Language.Haskell.Stylish.Step.UnicodeSyntax.Tests
import qualified Language.Haskell.StylishSpec


--------------------------------------------------------------------------------
main :: IO ()
main = defaultMain
[ Language.Haskell.Stylish.Parse.Tests.tests
[ Language.Haskell.StylishSpec.tests
, Language.Haskell.Stylish.Parse.Tests.tests
, Language.Haskell.Stylish.Config.Tests.tests
, Language.Haskell.Stylish.Step.Data.Tests.tests
, Language.Haskell.Stylish.Step.Imports.Tests.tests
Expand Down