diff --git a/test/Neuron/Util/TreeSpec.hs b/test/Neuron/Util/TreeSpec.hs new file mode 100644 index 000000000..6eb878f7b --- /dev/null +++ b/test/Neuron/Util/TreeSpec.hs @@ -0,0 +1,58 @@ +{-# LANGUAGE NoImplicitPrelude #-} + +module Neuron.Util.TreeSpec + ( spec, + ) +where + +import Data.Tree (Forest, Tree (..)) +import qualified Neuron.Util.Tree as Z +import Relude +import System.FilePath (()) +import Test.Hspec + +spec :: Spec +spec = do + describe "Path tree" $ do + context "Tree building" $ do + forM_ treeCases $ \(name, paths, tree) -> do + it name $ do + Z.mkTreeFromPaths paths `shouldBe` tree + context "Tree folding" $ do + forM_ foldingCases $ \(name, tree, folded) -> do + it name $ do + let mergePaths (p, _) (p', b') = (p p', b') + res = fmap fst $ Z.foldTreeOnWith snd mergePaths tree + res `shouldBe` folded + +treeCases :: [(String, [[String]], Forest String)] +treeCases = + [ ( "works on one level", + [["journal"], ["science"]], + [Node "journal" [], Node "science" []] + ), + ( "groups paths with common prefix", + [["math", "algebra"], ["math", "calculus"]], + [Node "math" [Node "algebra" [], Node "calculus" []]] + ), + ( "ignores tag when there is also tag/subtag", + [["math"], ["math", "algebra"]], + [Node "math" [Node "algebra" []]] + ) + ] + +foldingCases :: [(String, Tree (String, Bool), Tree String)] +foldingCases = + [ ( "folds tree on one level", + Node ("math", True) [Node ("note", False) []], + Node "math/note" [] + ), + ( "folds across multiple levels", + Node ("math", True) [Node ("algebra", True) [Node ("note", False) []]], + Node "math/algebra/note" [] + ), + ( "does not fold tree when the predicate is false", + Node ("math", False) [Node ("note", False) []], + Node "math" [Node "note" []] + ) + ] diff --git a/test/Neuron/Zettelkasten/Tag/TreeSpec.hs b/test/Neuron/Zettelkasten/Tag/TreeSpec.hs deleted file mode 100644 index 82859b129..000000000 --- a/test/Neuron/Zettelkasten/Tag/TreeSpec.hs +++ /dev/null @@ -1,66 +0,0 @@ -{-# LANGUAGE OverloadedStrings #-} -{-# LANGUAGE QuasiQuotes #-} -{-# LANGUAGE ViewPatterns #-} -{-# LANGUAGE NoImplicitPrelude #-} - -module Neuron.Zettelkasten.Tag.TreeSpec - ( spec, - ) -where - -import Data.Tree (Forest, Tree (..)) -import qualified Neuron.Zettelkasten.Tag as Z -import qualified Neuron.Zettelkasten.Tag.Tree as Z -import Relude -import Test.Hspec - -spec :: Spec -spec = do - describe "Tag tree" $ do - forM_ tagTreeCases $ \(name, fmap Z.Tag -> tags, foldTree, fmap (fmap Z.Tag) -> tree) -> do - it name $ do - let res = bool id Z.foldTagTree foldTree $ Z.mkTagTree tags - fmap (fmap fst) res `shouldBe` tree - -tagTreeCases :: [(String, [Text], Bool, Forest Text)] -tagTreeCases = - [ -- Full trees - ( "works with simple tags", - ["journal", "science"], - False, - [Node "journal" [], Node "science" []] - ), - ( "groups tags with common prefix together", - ["math/algebra", "math/calculus"], - False, - [Node "math" [Node "algebra" [], Node "calculus" []]] - ), - ( "works with a mix of hierarchical and simple tags", - ["journal", "science/biology", "science/physics"], - False, - [ Node "journal" [], - Node "science" [Node "biology" [], Node "physics" []] - ] - ), - ( "ignores tag when there is also tag/subtag", - ["math", "math/algebra"], - False, - [Node "math" [Node "algebra" []]] - ), - -- Folded trees - ( "folds tree when some tag only has one subtag", - ["math/note"], - True, - [Node "math/note" []] - ), - ( "folds across multiple levels", - ["math/algebra/linear"], - True, - [Node "math/algebra/linear" []] - ), - ( "does not fold tree when the parent tag exists by itself", - ["math", "math/note"], - True, - [Node "math" [Node "note" []]] - ) - ]