Skip to content

Commit

Permalink
Add coverage for parsing nested parens/lists in field types (#1376)
Browse files Browse the repository at this point in the history
* Add coverage for nested parens/lists

* Add TH spec

* Add test modules to cabal file

* Fix indentation and add to changelog
  • Loading branch information
danbroooks committed Mar 21, 2022
1 parent b8484fa commit f2e930f
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 0 deletions.
2 changes: 2 additions & 0 deletions persistent/ChangeLog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Changelog for persistent

* [#1376](https://github.com/yesodweb/persistent/pull/1376)
* Add coverage for parsing nested parens/lists in field types
* [#1370](https://github.com/yesodweb/persistent/pull/1370)
* Add spec to assert Persistent.TH is the only import required when defining entities

Expand Down
2 changes: 2 additions & 0 deletions persistent/persistent.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@ test-suite test
Database.Persist.TH.MaybeFieldDefsSpec
Database.Persist.TH.MultiBlockSpec
Database.Persist.TH.MultiBlockSpec.Model
Database.Persist.TH.NestedSymbolsInTypeSpec
Database.Persist.TH.NestedSymbolsInTypeSpecImports
Database.Persist.TH.OverloadedLabelSpec
Database.Persist.TH.RequireOnlyPersistImportSpec
Database.Persist.TH.SharedPrimaryKeyImportedSpec
Expand Down
10 changes: 10 additions & 0 deletions persistent/test/Database/Persist/QuasiSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,16 @@ CustomerTransfer
baz = FTTypeCon Nothing "Baz"
parseFieldType "Foo [Bar] Baz" `shouldBe` Right (
foo `FTApp` bars `FTApp` baz)
it "nested list / parens (list inside parens)" $ do
let maybeCon = FTTypeCon Nothing "Maybe"
int = FTTypeCon Nothing "Int"
parseFieldType "Maybe (Maybe [Int])" `shouldBe` Right
(maybeCon `FTApp` (maybeCon `FTApp` FTList int))
it "nested list / parens (parens inside list)" $ do
let maybeCon = FTTypeCon Nothing "Maybe"
int = FTTypeCon Nothing "Int"
parseFieldType "[Maybe (Maybe Int)]" `shouldBe` Right
(FTList (maybeCon `FTApp` (maybeCon `FTApp` int)))
it "fails on lowercase starts" $ do
parseFieldType "nothanks" `shouldBe` Left "PSFail ('n',\"othanks\")"

Expand Down
44 changes: 44 additions & 0 deletions persistent/test/Database/Persist/TH/NestedSymbolsInTypeSpec.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{-# OPTIONS_GHC -Wno-unused-local-binds #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE OverloadedLabels #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE UndecidableInstances #-}

module Database.Persist.TH.NestedSymbolsInTypeSpec where

import Data.Map
import Database.Persist.TH.NestedSymbolsInTypeSpecImports
import TemplateTestImports

mkPersist sqlSettings [persistLowerCase|
PathEntitySimple
readOnly (Maybe (SomePath ReadOnly))

PathEntityNested
paths (Maybe (Map Text [SomePath ReadWrite]))
|]

spec :: Spec
spec = describe "NestedSymbolsInType" $ do
it "should support nested parens" $ do
let mkPathEntitySimple :: Maybe (SomePath ReadOnly) -> PathEntitySimple
mkPathEntitySimple = PathEntitySimple
pathEntitySimpleReadOnly' :: PathEntitySimple -> Maybe (SomePath ReadOnly)
pathEntitySimpleReadOnly' = pathEntitySimpleReadOnly
compiles

it "should support deeply nested parens + square brackets" $ do
let mkPathEntityNested :: Maybe (Map Text [SomePath ReadWrite]) -> PathEntityNested
mkPathEntityNested = PathEntityNested
pathEntityNestedPaths' :: PathEntityNested -> Maybe (Map Text [SomePath ReadWrite])
pathEntityNestedPaths' = pathEntityNestedPaths
compiles

compiles :: Expectation
compiles = True `shouldBe` True
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE LambdaCase #-}

module Database.Persist.TH.NestedSymbolsInTypeSpecImports where

import Data.Proxy
import TemplateTestImports

data ReadOnly
data ReadWrite

newtype SomePath a = SomePath Text

instance PersistFieldSql (SomePath a) where
sqlType _ = SqlString

instance PersistField (SomePath a) where
toPersistValue (SomePath n) =
toPersistValue n
fromPersistValue v =
SomePath <$> fromPersistValue v
2 changes: 2 additions & 0 deletions persistent/test/Database/Persist/THSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import qualified Database.Persist.TH.ForeignRefSpec as ForeignRefSpec
import qualified Database.Persist.TH.ImplicitIdColSpec as ImplicitIdColSpec
import qualified Database.Persist.TH.JsonEncodingSpec as JsonEncodingSpec
import qualified Database.Persist.TH.KindEntitiesSpec as KindEntitiesSpec
import qualified Database.Persist.TH.NestedSymbolsInTypeSpec as NestedSymbolsInTypeSpec
import qualified Database.Persist.TH.MaybeFieldDefsSpec as MaybeFieldDefsSpec
import qualified Database.Persist.TH.MigrationOnlySpec as MigrationOnlySpec
import qualified Database.Persist.TH.MultiBlockSpec as MultiBlockSpec
Expand Down Expand Up @@ -174,6 +175,7 @@ spec :: Spec
spec = describe "THSpec" $ do
PersistWithSpec.spec
KindEntitiesSpec.spec
NestedSymbolsInTypeSpec.spec
OverloadedLabelSpec.spec
SharedPrimaryKeySpec.spec
SharedPrimaryKeyImportedSpec.spec
Expand Down

0 comments on commit f2e930f

Please sign in to comment.