Skip to content

Commit

Permalink
Add failing test
Browse files Browse the repository at this point in the history
  • Loading branch information
chiroptical committed Jan 31, 2024
1 parent a234e8f commit 7b71439
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ format: format-nix format-haskell
ghcid: hpack
ghcid -c "cabal --ghc-options='${GHC_OPTIONS}' repl"

ghcid-test: hpack
ghcid -c "cabal --ghc-options='${GHC_OPTIONS}' repl derive-has-field-test"

hlint: hpack
hlint .

Expand All @@ -32,4 +35,4 @@ hackage: hpack
# cabal upload --publish dist-newstyle/sdist/derive-has-field-0.1.0.0.tar.gz
# cabal upload --publish -d dist-newstyle/derive-has-field-0.1.0.0-docs.tar.gz

.PHONY: build hpack test run format-haskell format-nix format ghcid hlint bounds hackage
.PHONY: build hpack test run format-haskell format-nix format ghcid ghcid-test hlint bounds hackage
3 changes: 2 additions & 1 deletion src/DeriveHasField.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import Control.Monad
import Data.Char (toLower)
import Data.Foldable as Foldable
import Data.Traversable (for)
import Debug.Trace
import GHC.Records
import Language.Haskell.TH
import Language.Haskell.TH.Datatype
Expand All @@ -23,7 +24,7 @@ makeDeriveHasField :: (String -> String) -> DatatypeInfo -> DecsQ
makeDeriveHasField fieldModifier datatypeInfo = do
-- We do not support sum of product types
constructorInfo <- case datatypeInfo.datatypeCons of
[info] -> pure info
[info] -> trace (show datatypeInfo) $ pure info
_ -> fail "deriveHasField: only supports product types with a single data constructor"

-- We only support data and newtype declarations
Expand Down
17 changes: 17 additions & 0 deletions test/DeriveHasFieldSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,20 @@ someType =
, someTypeSomeEitherField = Right 0
}

data OtherType a = OtherType
{ otherTypeField :: Maybe a
, otherTypeOtherField :: Maybe a
}

deriveHasFieldWith (dropPrefix "otherType") ''OtherType

otherType :: OtherType Int
otherType =
OtherType
{ otherTypeField = Just 0
, otherTypeOtherField = Nothing
}

spec :: Spec
spec = do
describe "deriveHasField" $ do
Expand All @@ -34,3 +48,6 @@ spec = do
someType.someOtherField `shouldBe` 0
someType.someMaybeField `shouldBe` Just 0
someType.someEitherField `shouldBe` Right 0
it "compiles and gets the right field" $ do
otherType.field `shouldBe` Just 0
otherType.otherField `shouldBe` Nothing

0 comments on commit 7b71439

Please sign in to comment.