Skip to content

Commit

Permalink
new tests (replace hunit with golden)
Browse files Browse the repository at this point in the history
  • Loading branch information
soficshift committed Jan 30, 2024
1 parent 51c3b44 commit f58554e
Show file tree
Hide file tree
Showing 93 changed files with 2,357 additions and 375 deletions.
6 changes: 5 additions & 1 deletion org-exporters/src/Org/Exporters/Processing/OrgData.hs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,11 @@ data OrgData = OrgData
, footnotes :: Map FootnoteLabel (Id, Either OrgObjects OrgElements)
, bibliography :: [(Text, CslJson Text)]
}
deriving (Eq, Ord, Show, Typeable, Generic)
deriving (Eq, Ord, Show, Typeable, Generic, NFData)

-- FIXME upstream
deriving instance (Generic (CslJson Text))
deriving instance (NFData (CslJson Text))

initialOrgData :: OrgData
initialOrgData = OrgData mempty [] defaultExporterSettings defaultOrgOptions mempty mempty []
Expand Down
8 changes: 4 additions & 4 deletions org-exporters/src/Org/Types/Variants/Annotated.hs
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ data OrgF k ix = OrgF {props :: StandardProperties, annotations :: Object, datum
instance TestIxEq (OrgF k) where
testIxEq x y = testIxEq x y.datum

deriving instance (Eq (P.OrgF k a)) => (Eq (OrgF k a))
deriving instance (Ord (P.OrgF k a)) => (Ord (OrgF k a))
deriving instance (Show (P.OrgF k a)) => (Show (OrgF k a))
deriving instance (NFData (P.OrgF k a)) => (NFData (OrgF k a))
deriving instance (AllOrgIx Eq k) => (Eq (OrgF k a))
deriving instance (AllOrgIx Ord k) => (Ord (OrgF k a))
deriving instance (AllOrgIx Show k) => (Show (OrgF k a))
deriving instance (AllOrgIx NFData k) => (NFData (OrgF k ix))

$(deriveGenericK ''OrgF)
deriving via (Generically OrgF) instance (Endofunctor (~>) OrgF)
Expand Down
2 changes: 1 addition & 1 deletion org-exporters/test/test-org-exporters.hs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import Org.Exporters.LaTeX qualified as L
import Org.Exporters.Pandoc qualified as P
import Org.Exporters.Processing (OrgData, processAll)
import Org.Parser (defaultOrgOptions, parseOrgDoc)
import Org.Types (OrgDocument)
import Org.Types.Variants.Annotated (OrgDocument)
import Relude.Unsafe (fromJust)
import System.Directory qualified as D
import System.FilePath (takeBaseName, (</>))
Expand Down
9 changes: 5 additions & 4 deletions org-parser/org-parser.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,12 @@ test-suite test
hs-source-dirs: test
main-is: test-org-parser.hs
build-depends:
, neat-interpolation >=0.5 && <0.6
, filepath >=1.4.2 && <1.5
, neat-interpolation >=0.5 && <0.6
, org-parser
, tasty >=1.4 && <1.5
, tasty-hunit >=0.10 && <0.11
, tree-diff >=0.3 && <0.4
, tasty >=1.4 && <1.5
, tasty-golden >=2.3 && <2.4
, tree-diff >=0.3 && <0.4

other-modules:
Tests.Document
Expand Down
11 changes: 7 additions & 4 deletions org-parser/src/Org/Parser/Definitions.hs
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,20 @@ getsO f = asks (f . (.options))

-- * Marked parsers

data Marked a = Marked
data Marked m a = Marked
{ marks :: String
, parser :: a
, parser :: m a
}
deriving (Functor)

instance (Alternative m) => Semigroup (Marked (m a)) where
mapMarkedP :: (m a -> n b) -> Marked m a -> Marked n b
mapMarkedP f m = m { parser = f m.parser }

instance (Alternative m) => Semigroup (Marked m a) where
Marked s1 p1 <> Marked s2 p2 =
Marked (s1 ++ s2) (p1 <|> p2)

instance (Alternative m) => Monoid (Marked (m a)) where
instance (Alternative m) => Monoid (Marked m a) where
mempty = Marked [] empty
mconcat ms =
Marked
Expand Down
4 changes: 2 additions & 2 deletions org-parser/src/Org/Parser/Elements.hs
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,9 @@ paraIndented begin minI kws =

end :: OrgParser (Int, Int, OrgElements)
end =
eofEnd <|> do
eofEnd <|> try do
_ <- newline
postBlank <- length <$> many blankline
postBlank <- length <$> lookAhead (many blankline)
liftA2 (,postBlank,) getOffset do
((blanklineEnd <|> indentEnd <|> headingEnd) $> mempty)
<|> elementIndented minI True
Expand Down
2 changes: 1 addition & 1 deletion org-parser/src/Org/Parser/MarkupContexts.hs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ withBalancedContext lchar rchar allowed p = try do
markupContext ::
Monoid k =>
(Int -> Int -> Text -> k) ->
Marked (OrgParser k) ->
Marked OrgParser k ->
OrgParser k
markupContext f elems = go
where
Expand Down
72 changes: 37 additions & 35 deletions org-parser/src/Org/Parser/Objects.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module Org.Parser.Objects
, Marked (..)
, markupContext
, plainMarkupContext
, mapMarkedP

-- * General purpose parsers
, markup
Expand Down Expand Up @@ -42,6 +43,7 @@ module Org.Parser.Objects
-- * Auxiliary
, linkToTarget
, parseTimestamp
, withPos
)
where

Expand All @@ -62,10 +64,10 @@ withPos p = do

-- * Sets of objects

minimalSet :: Marked (OrgParser OrgObjects)
minimalSet :: Marked OrgParser OrgObjects
minimalSet =
withPos
<$> mconcat
mapMarkedP withPos
$ mconcat
[ endline
, code
, verbatim
Expand All @@ -83,11 +85,11 @@ minimalSet =
, macro
]

standardSet :: Marked (OrgParser OrgObjects)
standardSet :: Marked OrgParser OrgObjects
standardSet =
(minimalSet <>)
$ withPos
<$> mconcat
. mapMarkedP withPos
$ mconcat
[ regularLink
, footnoteReference
, timestamp
Expand All @@ -107,7 +109,7 @@ standardSet =
'plainMarkupContext' = 'markupContext' 'B.plain'
@
-}
plainMarkupContext :: Marked (OrgParser OrgObjects) -> OrgParser OrgObjects
plainMarkupContext :: Marked OrgParser OrgObjects -> OrgParser OrgObjects
plainMarkupContext = markupContext (\s e t -> object s e (Plain t))

newlineAndClear :: OrgParser Char
Expand Down Expand Up @@ -149,7 +151,7 @@ emphasisSkip s = try $ do
markup ::
(OrgObjects -> OrgObjectD) ->
Char ->
Marked (OrgParser OrgObjectD)
Marked OrgParser OrgObjectD
markup f c = Marked [c] $ try do
emphasisPre c
st <- getFullState
Expand All @@ -160,61 +162,61 @@ markup f c = Marked [c] $ try do
rawMarkup ::
(Text -> OrgObjectData Org ObjIx) ->
Char ->
Marked (OrgParser OrgObjectD)
Marked OrgParser OrgObjectD
rawMarkup f d = Marked [d] $ try do
emphasisPre d
f . fst <$> skipManyTill' (emphasisSkip d) (emphasisPost d)

-- | Parse a code object.
code :: Marked (OrgParser OrgObjectD)
code :: Marked OrgParser OrgObjectD
code = rawMarkup Code '~'

-- | Parse a verbatim object.
verbatim :: Marked (OrgParser OrgObjectD)
verbatim :: Marked OrgParser OrgObjectD
verbatim = rawMarkup Verbatim '='

-- | Parse an italic object.
italic :: Marked (OrgParser OrgObjectD)
italic :: Marked OrgParser OrgObjectD
italic = markup Italic '/'

-- | Parse an underline object.
underline :: Marked (OrgParser OrgObjectD)
underline :: Marked OrgParser OrgObjectD
underline = markup Underline '_'

-- | Parse a bold object.
bold :: Marked (OrgParser OrgObjectD)
bold :: Marked OrgParser OrgObjectD
bold = markup Bold '*'

-- | Parse a strikethrough object.
strikethrough :: Marked (OrgParser OrgObjectD)
strikethrough :: Marked OrgParser OrgObjectD
strikethrough = markup Strikethrough '+'

-- | Parse a single-quoted object.
singleQuoted :: Marked (OrgParser OrgObjectD)
singleQuoted :: Marked OrgParser OrgObjectD
singleQuoted = markup (Quoted SingleQuote) '\''

-- | Parse a double-quoted object.
doubleQuoted :: Marked (OrgParser OrgObjectD)
doubleQuoted :: Marked OrgParser OrgObjectD
doubleQuoted = markup (Quoted DoubleQuote) '"'

-- TODO why is this parsed? can't it live inside plain??

-- | An endline character that can be treated as a space, not a line break.
endline :: Marked (OrgParser OrgObjectD)
endline :: Marked OrgParser OrgObjectD
endline = Marked "\n" $ try $ newlineAndClear *> hspace $> Plain "\n"

-- * Entities and LaTeX fragments

-- | Parse an entity object.
entity :: Marked (OrgParser OrgObjectD)
entity :: Marked OrgParser OrgObjectD
entity = Marked "\\" $ try do
_ <- char '\\'
name <- choice (map string defaultEntitiesNames)
void (string "{}") <|> notFollowedBy asciiAlpha
pure $ Entity name

-- | Parse a LaTeX fragment object.
latexFragment :: Marked (OrgParser OrgObjectD)
latexFragment :: Marked OrgParser OrgObjectD
latexFragment = Marked "\\" $ try do
_ <- char '\\'
mathFragment <|> rawFragment
Expand Down Expand Up @@ -247,7 +249,7 @@ latexFragment = Marked "\\" $ try do
pure $ open `T.cons` str `T.snoc` close

-- | Parse a TeX math fragment object.
texMathFragment :: Marked (OrgParser OrgObjectD)
texMathFragment :: Marked OrgParser OrgObjectD
texMathFragment = Marked "$" $ try $ display <|> inline
where
display = try $ do
Expand Down Expand Up @@ -293,7 +295,7 @@ texMathFragment = Marked "$" $ try $ display <|> inline
-- * Export snippets

-- | Parse an export snippet object.
exportSnippet :: Marked (OrgParser OrgObjectD)
exportSnippet :: Marked OrgParser OrgObjectD
exportSnippet = Marked "@" $ try do
_ <- string "@@"
backend <-
Expand All @@ -308,7 +310,7 @@ exportSnippet = Marked "@" $ try do
-- The following code for org-cite citations was adapted and improved upon pandoc's.

-- | Parse a citation object.
citation :: Marked (OrgParser OrgObjectD)
citation :: Marked OrgParser OrgObjectD
citation = Marked "[" do
Cite <$> withBalancedContext '[' ']' (const True) orgCite

Expand Down Expand Up @@ -395,7 +397,7 @@ orgCiteKeyChar c =
-- * Inline Babel calls

-- | Parse an inline babel call object.
inlBabel :: Marked (OrgParser OrgObjectD)
inlBabel :: Marked OrgParser OrgObjectD
inlBabel = Marked "c" $ try do
_ <- string "call_"
name <-
Expand All @@ -413,7 +415,7 @@ inlBabel = Marked "c" $ try do
-- * Inline source blocks

-- | Parse an inline source object.
inlSrc :: Marked (OrgParser OrgObjectD)
inlSrc :: Marked OrgParser OrgObjectD
inlSrc = Marked "s" $ try do
_ <- string "src_"
name <-
Expand All @@ -429,14 +431,14 @@ inlSrc = Marked "s" $ try do
-- * Line breaks

-- | Parse a linebreak object.
linebreak :: Marked (OrgParser OrgObjectD)
linebreak :: Marked OrgParser OrgObjectD
linebreak = Marked "\\" $ try do
LineBreak <$ string "\\\\" <* blankline' <* clearLastChar

-- * Links

-- | Parse a angle link object.
angleLink :: Marked (OrgParser OrgObjectD)
angleLink :: Marked OrgParser OrgObjectD
angleLink = Marked "<" $ try do
_ <- char '<'
s <- getOffset
Expand All @@ -452,7 +454,7 @@ angleLink = Marked "<" $ try do
return $ Link (URILink protocol tgt) (object s (e - 1) $ Plain $ protocol <> ":" <> tgt)

-- | Parse a regular link object.
regularLink :: Marked (OrgParser OrgObjectD)
regularLink :: Marked OrgParser OrgObjectD
regularLink = Marked "[" $ try do
_ <- string "[["
str <- linkTarget
Expand Down Expand Up @@ -498,7 +500,7 @@ linkToTarget link
-- * Targets and radio targets

-- | Parse a target object.
target :: Marked (OrgParser OrgObjectD)
target :: Marked OrgParser OrgObjectD
target = Marked "<" $ try do
_ <- string "<<"
str <- takeWhile1P (Just "dedicated target") (\c -> c /= '<' && c /= '>' && c /= '\n')
Expand All @@ -510,7 +512,7 @@ target = Marked "<" $ try do
-- * Subscripts and superscripts

-- | Parse a subscript or a superscript object.
suscript :: Marked (OrgParser OrgObjectD)
suscript :: Marked OrgParser OrgObjectD
suscript = Marked "_^" $ try do
lchar <- gets (.lastChar)
for_ lchar $ guard . not . isSpace
Expand All @@ -532,7 +534,7 @@ suscript = Marked "_^" $ try do
plain =
liftA2 (<>) sign
$ withMContext (const True) isAlphaNum plainEnd
$ plainMarkupContext (withPos <$> entity <> latexFragment)
$ plainMarkupContext (mapMarkedP withPos $ entity <> latexFragment)

plainEnd :: OrgParser ()
plainEnd = try do
Expand All @@ -544,7 +546,7 @@ suscript = Marked "_^" $ try do
-- * Macros

-- | Parse a macro object.
macro :: Marked (OrgParser OrgObjectD)
macro :: Marked OrgParser OrgObjectD
macro = Marked "{" $ try do
_ <- string "{{{"
_ <- lookAhead $ satisfy isAsciiAlpha
Expand All @@ -561,7 +563,7 @@ macro = Marked "{" $ try do
-- * Footnote references

-- | Parse a footnote reference object.
footnoteReference :: Marked (OrgParser OrgObjectD)
footnoteReference :: Marked OrgParser OrgObjectD
footnoteReference = Marked "[" $ withBalancedContext '[' ']' (const True) do
_ <- string "fn:"
lbl <-
Expand All @@ -583,7 +585,7 @@ footnoteReference = Marked "[" $ withBalancedContext '[' ']' (const True) do
-- * Timestamps

-- | Parse a timestamp object.
timestamp :: Marked (OrgParser OrgObjectD)
timestamp :: Marked OrgParser OrgObjectD
timestamp = Marked "<[" $ Timestamp <$> parseTimestamp

-- | Parse a timestamp.
Expand Down Expand Up @@ -640,7 +642,7 @@ parseTimestamp = try $ do
-- * Statistic Cookies

-- | Parse a statistic cookie object.
statisticCookie :: Marked (OrgParser OrgObjectD)
statisticCookie :: Marked OrgParser OrgObjectD
statisticCookie = Marked "[" $ try do
_ <- char '['
res <- Left <$> fra <|> Right <$> pct
Expand Down
6 changes: 6 additions & 0 deletions org-parser/src/Org/Types/Variants/ParseInfo.hs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ import Org.Types.Ix
import Org.Types.Variants.Plain qualified as P

data OrgF k ix = OrgF {props :: StandardProperties, datum :: P.OrgF k ix}
deriving (Generic)

deriving instance (AllOrgIx Eq k) => (Eq (OrgF k a))
deriving instance (AllOrgIx Ord k) => (Ord (OrgF k a))
deriving instance (AllOrgIx Show k) => (Show (OrgF k a))
deriving instance (AllOrgIx NFData k) => (NFData (OrgF k ix))

$(deriveGenericK ''OrgF)
deriving via (Generically OrgF) instance (Endofunctor (~>) OrgF)
Expand Down
Loading

0 comments on commit f58554e

Please sign in to comment.