summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorŁukasz Gołębiewski <lukasz.golebiewski@gmail.com>2020-01-26 11:36:49 +0000
committerGitHub <noreply@github.com>2020-01-26 11:36:49 +0000
commitb501e5c92dac8b89ff0c1f962a4be4ecbc261e97 (patch)
treeab882893d57edad78ce6de5ae68f274c83404ae9
parent5eb4902883d9d3937641d6a2c6249993242bf098 (diff)
downloadstylish-haskell-b501e5c92dac8b89ff0c1f962a4be4ecbc261e97.tar.gz
Expose "format" function in Sylish.hs (#259)
* Expose "format" function in Sylish.hs It's going to be needed for the haskell-ide integration * Update tests/Language/Haskell/StylishSpec.hs Co-Authored-By: Jasper Van der Jeugt <jaspervdj@gmail.com> * Remove empty line Co-authored-by: Jasper Van der Jeugt <jaspervdj@gmail.com>
-rw-r--r--lib/Language/Haskell/Stylish.hs12
-rw-r--r--stylish-haskell.cabal1
-rw-r--r--testdata/test-config.yaml3
-rw-r--r--tests/Language/Haskell/StylishSpec.hs51
-rw-r--r--tests/TestSuite.hs4
5 files changed, 70 insertions, 1 deletions
diff --git a/lib/Language/Haskell/Stylish.hs b/lib/Language/Haskell/Stylish.hs
index a40a7d2..4f6aa1f 100644
--- a/lib/Language/Haskell/Stylish.hs
+++ b/lib/Language/Haskell/Stylish.hs
@@ -16,6 +16,8 @@ module Language.Haskell.Stylish
-- * Misc
, module Language.Haskell.Stylish.Verbose
, version
+ , format
+ , ConfigPath(..)
, Lines
, Step
) where
@@ -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 }
+
+-- |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
diff --git a/stylish-haskell.cabal b/stylish-haskell.cabal
index de12c11..1f509a1 100644
--- a/stylish-haskell.cabal
+++ b/stylish-haskell.cabal
@@ -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
diff --git a/testdata/test-config.yaml b/testdata/test-config.yaml
new file mode 100644
index 0000000..b81fdc2
--- /dev/null
+++ b/testdata/test-config.yaml
@@ -0,0 +1,3 @@
+steps:
+ - records: {}
+indent: 2
diff --git a/tests/Language/Haskell/StylishSpec.hs b/tests/Language/Haskell/StylishSpec.hs
new file mode 100644
index 0000000..77a23b1
--- /dev/null
+++ b/tests/Language/Haskell/StylishSpec.hs
@@ -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
+ ]
+
+--------------------------------------------------------------------------------
+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 = do
+ actual <- format Nothing (Just fileLocation) input
+ actual @?= result
+ 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\""
diff --git a/tests/TestSuite.hs b/tests/TestSuite.hs
index a6f51ea..1138323 100644
--- a/tests/TestSuite.hs
+++ b/tests/TestSuite.hs
@@ -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