From a22110a5dea79df5c403bc22281a1941a9c60715 Mon Sep 17 00:00:00 2001 From: Jun Matsushita Date: Thu, 21 Dec 2023 13:46:13 +0100 Subject: [PATCH 1/3] Add back insert tests --- test/Main.purs | 90 ++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 73 insertions(+), 17 deletions(-) diff --git a/test/Main.purs b/test/Main.purs index a9be582..f258e53 100644 --- a/test/Main.purs +++ b/test/Main.purs @@ -6,13 +6,15 @@ import Control.Comonad.Cofree (head, (:<)) import Data.Maybe (Maybe(..), fromJust) import Effect (Effect) import Effect.Aff (launchAff_) +import Effect.Class (liftEffect) +import Effect.Console (log) import Partial.Unsafe (unsafePartial) import Test.Spec (describe, it) import Test.Spec.Assertions (shouldEqual) import Test.Spec.Reporter (consoleReporter) import Test.Spec.Runner (runSpec) -import Yoga.Tree (Tree, leaf, scanTree, showTree) -import Yoga.Tree.Zipper (down, findDownWhere, findFromRoot, findUp, flattenLocDepthFirst, fromTree, modifyValue, next, toTree, value) +import Yoga.Tree (Tree, leaf, mkLeaf, scanTree, showTree) +import Yoga.Tree.Zipper (down, findDownWhere, findFromRoot, findUp, firstChild, flattenLocDepthFirst, fromTree, insertAfter, lastChild, modifyValue, next, toTree, value) newtype SpecTree a = SpecTree (Tree a) @@ -106,21 +108,75 @@ main = launchAff_ $ runSpec [ consoleReporter ] do SpecTree root' `shouldEqual` SpecTree result SpecTree root'' `shouldEqual` SpecTree result' - -- it "Insert" do - - -- let root1 = unsafePartial $ toTree $ insertAfter (mkLeaf 100) (fromJust $ down root) - -- let root2 = unsafePartial $ toTree $ insertAfter (mkLeaf 100) (fromJust $ (down root) >>= next >>= next >>= down >>= next >>= down) - -- let root3 = unsafePartial $ toTree $ insertAfter (mkLeaf 100) (fromJust $ (firstChild root)) - -- let root4 = unsafePartial $ toTree $ insertAfter (mkLeaf 100) (fromJust $ (lastChild root)) - - -- let result1 = 1 :< [] - -- let result2 = 1 :< [] - -- let result3 = 1 :< [] - -- let result4 = 1 :< [] - -- shouldEqual (eq root1 result1) true - -- shouldEqual (eq root2 result2) true - -- shouldEqual (eq root3 result3) true - -- shouldEqual (eq root4 result4) true + it "Insert" do + + let root1 = unsafePartial $ toTree $ insertAfter (mkLeaf 100) (fromJust $ down root) + let root2 = unsafePartial $ toTree $ insertAfter (mkLeaf 100) (fromJust $ (down root) >>= next >>= next >>= down >>= next >>= down) + let root3 = unsafePartial $ toTree $ insertAfter (mkLeaf 100) (fromJust $ (firstChild root)) + let root4 = unsafePartial $ toTree $ insertAfter (mkLeaf 100) (fromJust $ (lastChild root)) + + let result1 = + 1 :< + [ (2 :< []) + , (100 :< []) + , (3 :< []) + , (4 :< + [ (5 :< []) + , (6 :< + [7 :< []]) + , (8 :< []) + ] + ) + ] + + let result2 = + 1 :< + [ (2 :< []) + , (3 :< []) + , (4 :< + [ (5 :< []) + , (6 :< + [ (7 :< []) + , (100 :< []) + ]) + , (8 :< []) + ] + ) + ] + let result3 = + 1 :< + [ (2 :< []) + , (100 :< []) + , (3 :< []) + , (4 :< + [ (5 :< []) + , (6 :< + [(7 :< []) + ] + ) + , (8 :< []) + ] + ) + ] + let result4 = + 1 :< + [ (2 :< []) + , (3 :< []) + , (4 :< + [ (5 :< []) + , (6 :< + [ 7 :< [] ]) + , (8 :< []) + ] + ) + , (100 :< []) + ] + + + shouldEqual (eq root1 result1) true + shouldEqual (eq root2 result2) true + shouldEqual (eq root3 result3) true + shouldEqual (eq root4 result4) true it "Should findDownWhere with single node" do let tree = 1 :< [] From a4fdc37f83fd727a7ddc6d93660d38a24c22042c Mon Sep 17 00:00:00 2001 From: Jun Matsushita Date: Thu, 21 Dec 2023 13:46:32 +0100 Subject: [PATCH 2/3] Fix lastChild --- src/Yoga/Tree/Zipper.purs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Yoga/Tree/Zipper.purs b/src/Yoga/Tree/Zipper.purs index 71fa305..11dd965 100644 --- a/src/Yoga/Tree/Zipper.purs +++ b/src/Yoga/Tree/Zipper.purs @@ -78,7 +78,7 @@ last l@(Loc r) = case uncons (reverse r.after) of Loc $ { node: c - , before: cs <> r.before + , before: cs <> r.node : r.before , after: [] , parents: r.parents } From 5595c95c43de83a8fb6e9c50a18e6e4049d09b35 Mon Sep 17 00:00:00 2001 From: Jun Matsushita Date: Thu, 21 Dec 2023 13:47:16 +0100 Subject: [PATCH 3/3] Cleanup --- test/Main.purs | 2 -- 1 file changed, 2 deletions(-) diff --git a/test/Main.purs b/test/Main.purs index f258e53..48d7615 100644 --- a/test/Main.purs +++ b/test/Main.purs @@ -6,8 +6,6 @@ import Control.Comonad.Cofree (head, (:<)) import Data.Maybe (Maybe(..), fromJust) import Effect (Effect) import Effect.Aff (launchAff_) -import Effect.Class (liftEffect) -import Effect.Console (log) import Partial.Unsafe (unsafePartial) import Test.Spec (describe, it) import Test.Spec.Assertions (shouldEqual)