mirror of
https://github.com/gren-lang/compiler.git
synced 2024-09-11 08:45:42 +03:00
Add test harness for format tests
This commit is contained in:
parent
9b893e31c4
commit
12ff29f699
@ -70,6 +70,7 @@ data Error
|
||||
| NoPortModulesInPackage A.Region
|
||||
| NoEffectsOutsideKernel A.Region
|
||||
| ParseError Module
|
||||
deriving (Show)
|
||||
|
||||
-- MODULE
|
||||
|
||||
@ -104,6 +105,7 @@ data Module
|
||||
Infix Row Col
|
||||
| --
|
||||
Declarations Decl Row Col
|
||||
deriving (Show)
|
||||
|
||||
data Exposing
|
||||
= ExposingSpace Space Row Col
|
||||
@ -117,6 +119,7 @@ data Exposing
|
||||
| --
|
||||
ExposingIndentEnd Row Col
|
||||
| ExposingIndentValue Row Col
|
||||
deriving (Show)
|
||||
|
||||
-- DECLARATIONS
|
||||
|
||||
@ -129,6 +132,7 @@ data Decl
|
||||
| DeclDef Name.Name DeclDef Row Col
|
||||
| --
|
||||
DeclFreshLineAfterDocComment Row Col
|
||||
deriving (Show)
|
||||
|
||||
data DeclDef
|
||||
= DeclDefSpace Space Row Col
|
||||
@ -142,6 +146,7 @@ data DeclDef
|
||||
DeclDefIndentType Row Col
|
||||
| DeclDefIndentEquals Row Col
|
||||
| DeclDefIndentBody Row Col
|
||||
deriving (Show)
|
||||
|
||||
data Port
|
||||
= PortSpace Space Row Col
|
||||
@ -151,6 +156,7 @@ data Port
|
||||
| PortIndentName Row Col
|
||||
| PortIndentColon Row Col
|
||||
| PortIndentType Row Col
|
||||
deriving (Show)
|
||||
|
||||
-- TYPE DECLARATIONS
|
||||
|
||||
@ -161,6 +167,7 @@ data DeclType
|
||||
| DT_Union CustomType Row Col
|
||||
| --
|
||||
DT_IndentName Row Col
|
||||
deriving (Show)
|
||||
|
||||
data TypeAlias
|
||||
= AliasSpace Space Row Col
|
||||
@ -170,6 +177,7 @@ data TypeAlias
|
||||
| --
|
||||
AliasIndentEquals Row Col
|
||||
| AliasIndentBody Row Col
|
||||
deriving (Show)
|
||||
|
||||
data CustomType
|
||||
= CT_Space Space Row Col
|
||||
@ -183,6 +191,7 @@ data CustomType
|
||||
| CT_IndentBar Row Col
|
||||
| CT_IndentAfterBar Row Col
|
||||
| CT_IndentAfterEquals Row Col
|
||||
deriving (Show)
|
||||
|
||||
-- EXPRESSIONS
|
||||
|
||||
|
@ -249,8 +249,9 @@ Test-Suite gren-tests
|
||||
Helpers.Parse
|
||||
|
||||
-- tests
|
||||
Parse.SpaceSpec
|
||||
Integration.FormatSpec
|
||||
Parse.RecordUpdateSpec
|
||||
Parse.SpaceSpec
|
||||
Parse.UnderscorePatternSpec
|
||||
|
||||
Build-Depends:
|
||||
|
@ -3,6 +3,7 @@
|
||||
module Format
|
||||
( Flags (..),
|
||||
run,
|
||||
formatByteString,
|
||||
)
|
||||
where
|
||||
|
||||
|
62
tests/Integration/FormatSpec.hs
Normal file
62
tests/Integration/FormatSpec.hs
Normal file
@ -0,0 +1,62 @@
|
||||
{-# LANGUAGE OverloadedStrings #-}
|
||||
|
||||
-- | Integration tests for formatting that cover both the parsing and formatting
|
||||
-- (tests go from text to text).
|
||||
module Integration.FormatSpec where
|
||||
|
||||
import Data.ByteString.Builder qualified as Builder
|
||||
import Data.Text (Text)
|
||||
import Data.Text qualified as Text
|
||||
import Data.Text.Encoding qualified as TE
|
||||
import Data.Text.Lazy qualified as LazyText
|
||||
import Data.Text.Lazy.Encoding qualified as LTE
|
||||
import Format qualified
|
||||
import Test.Hspec
|
||||
|
||||
spec :: Spec
|
||||
spec = do
|
||||
describe "top-level definition" $ do
|
||||
it "formats" $
|
||||
["f = {}"]
|
||||
`shouldFormatModuleBodyAs` [ "f =",
|
||||
" {}"
|
||||
]
|
||||
describe "expressions" $ do
|
||||
describe "record" $ do
|
||||
it "formats with fields" $
|
||||
["{a=1, b = 2}"]
|
||||
`shouldFormatExpressionAs` [ "{ a = 1",
|
||||
", b = 2",
|
||||
"}"
|
||||
]
|
||||
|
||||
shouldFormatModuleBodyAs :: [Text] -> [LazyText.Text] -> IO ()
|
||||
shouldFormatModuleBodyAs inputLines expectedOutputLines =
|
||||
let input = TE.encodeUtf8 $ Text.unlines inputLines
|
||||
expectedOutput = LazyText.unlines expectedOutputLines
|
||||
actualOutput = LTE.decodeUtf8 . Builder.toLazyByteString <$> Format.formatByteString input
|
||||
in case LazyText.stripPrefix "module Main exposing (..)\n\n\n\n" <$> actualOutput of
|
||||
Nothing ->
|
||||
expectationFailure "shouldFormatModuleBodyAs: failed to format"
|
||||
Just Nothing ->
|
||||
expectationFailure "shouldFormatModuleBodyAs: internal error: could not strip module header"
|
||||
Just (Just actualModuleBody) ->
|
||||
actualModuleBody `shouldBe` expectedOutput
|
||||
|
||||
shouldFormatExpressionAs :: [Text] -> [LazyText.Text] -> IO ()
|
||||
shouldFormatExpressionAs inputLines expectedOutputLines =
|
||||
let input = TE.encodeUtf8 $ "expr = " <> Text.unlines inputLines
|
||||
expectedOutput = LazyText.unlines expectedOutputLines
|
||||
actualOutput = LTE.decodeUtf8 . Builder.toLazyByteString <$> Format.formatByteString input
|
||||
cleanOutput i =
|
||||
LazyText.stripPrefix "module Main exposing (..)\n\n\n\nexpr =\n" i
|
||||
>>= (return . LazyText.lines)
|
||||
>>= traverse (LazyText.stripPrefix " ")
|
||||
>>= (return . LazyText.unlines)
|
||||
in case fmap cleanOutput actualOutput of
|
||||
Nothing ->
|
||||
expectationFailure "shouldFormatExpressionAs: failed to format"
|
||||
Just Nothing ->
|
||||
expectationFailure "shouldFormatExpressionAs: internal error: could clean output"
|
||||
Just (Just actualExpression) ->
|
||||
actualExpression `shouldBe` expectedOutput
|
Loading…
Reference in New Issue
Block a user