tree-diff 0.0.1 → 0.0.2
raw patch · 8 files changed
+84/−29 lines, 8 filesdep ~QuickCheckdep ~ansi-terminaldep ~tastysetup-changed
Dependency ranges changed: QuickCheck, ansi-terminal, tasty
Files
- ChangeLog.md +5/−0
- Setup.hs +0/−2
- fixtures/exfoo.expr +3/−1
- src/Data/TreeDiff/Class.hs +13/−2
- src/Data/TreeDiff/Golden.hs +4/−4
- src/Data/TreeDiff/Pretty.hs +37/−4
- tests/Tests.hs +4/−2
- tree-diff.cabal +18/−14
ChangeLog.md view
@@ -1,5 +1,10 @@ # Revision history for tree-diff +## 0.0.2++- Print compact diffs+- Add `ToExpr` instance for `ShortByteString`+ ## 0.0.1 - Use `BS.readFile` and `BS.writeFile` in `ediffGolden`.
− Setup.hs
@@ -1,2 +0,0 @@-import Distribution.Simple-main = defaultMain
fixtures/exfoo.expr view
@@ -1,4 +1,6 @@ Foo {fooBar = [Just "pub", Just (concat ["night\n", "club"])], fooInt = 42,- fooQuu = _×_ 125.375 Proxy}+ fooNew = True,+ fooQuu = _×_ 125.375 Proxy,+ fooStr = "Some Name"}
src/Data/TreeDiff/Class.hs view
@@ -57,8 +57,9 @@ import qualified Data.Time as Time -- bytestring-import qualified Data.ByteString as BS-import qualified Data.ByteString.Lazy as LBS+import qualified Data.ByteString as BS+import qualified Data.ByteString.Lazy as LBS+import qualified Data.ByteString.Short as SBS -- scientific import qualified Data.Scientific as Sci@@ -377,6 +378,16 @@ -- BS.concat ["foo\n", "bar\n"] instance ToExpr BS.ByteString where toExpr = stringToExpr "BS.concat" . bsUnconcat BS.null BS.elemIndex BS.splitAt++-- | >>> traverse_ (print . prettyExpr . toExpr . SBS.toShort . BS8.pack) ["", "\n", "foo", "foo\n", "foo\nbar", "foo\nbar\n"]+-- ""+-- "\n"+-- "foo"+-- "foo\n"+-- mconcat ["foo\n", "bar"]+-- mconcat ["foo\n", "bar\n"]+instance ToExpr SBS.ShortByteString where+ toExpr = stringToExpr "mconcat" . bsUnconcat BS.null BS.elemIndex BS.splitAt . SBS.fromShort bsUnconcat :: forall bs int. Num int
src/Data/TreeDiff/Golden.hs view
@@ -16,8 +16,8 @@ -- | Make a golden tests. ----- 'ediffGolden' is testing framework agnostic, thus the test framework--- looks intimdating.+-- 'ediffGolden' is testing framework agnostic, thus the type+-- looks intimidating. -- -- An example using @tasty-golden@, -- 'goldenTest' is imported from "Test.Tasty.Golden.Advanced"@@ -30,7 +30,7 @@ -- -- The 'ediffGolden' will read an 'Expr' from provided path to golden file, -- and compare it with a 'toExpr' of a result. If values differ,--- the diff of two will be printed.+-- the (compact) diff of two will be printed. -- -- See <https://github.com/phadej/tree-diff/blob/master/tests/Tests.hs> -- for a proper example.@@ -53,5 +53,5 @@ cmp a b | a == b = return Nothing | otherwise = return $ Just $- setSGRCode [Reset] ++ show (ansiWlEditExpr $ ediff a b)+ setSGRCode [Reset] ++ show (ansiWlEditExprCompact $ ediff a b) wrt expr = BS.writeFile fp $ T.encodeUtf8 $ T.pack $ show (prettyExpr expr) ++ "\n"
src/Data/TreeDiff/Pretty.hs view
@@ -4,27 +4,33 @@ Pretty (..), ppExpr, ppEditExpr,+ ppEditExprCompact, -- * pretty prettyPretty, prettyExpr, prettyEditExpr,+ prettyEditExprCompact, -- * ansi-wl-pprint ansiWlPretty, ansiWlExpr, ansiWlEditExpr,+ ansiWlEditExprCompact, -- ** background ansiWlBgPretty, ansiWlBgExpr, ansiWlBgEditExpr,+ ansiWlBgEditExprCompact, -- * Utilities escapeName, ) where import Data.Char (isAlphaNum, isPunctuation, isSymbol, ord)+import Data.Either (partitionEithers) import Data.TreeDiff.Expr import Numeric (showHex) import Text.Read (readMaybe) + import qualified Data.Map as Map import qualified Text.PrettyPrint as HJ import qualified Text.PrettyPrint.ANSI.Leijen as WL@@ -119,7 +125,14 @@ -- | Pretty print an @'Edit' 'EditExpr'@ using explicit pretty-printing dictionary. ppEditExpr :: Pretty doc -> Edit EditExpr -> doc-ppEditExpr p = ppSep p . ppEdit False+ppEditExpr = ppEditExpr' False++-- | Like 'ppEditExpr' but print unchanged parts only shallowly+ppEditExprCompact :: Pretty doc -> Edit EditExpr -> doc+ppEditExprCompact = ppEditExpr' True++ppEditExpr' :: Bool -> Pretty doc -> Edit EditExpr -> doc+ppEditExpr' compact p = ppSep p . ppEdit False where ppEdit b (Cpy (EditExp expr)) = [ ppCpy p $ ppExpr' p b expr ] ppEdit b (Cpy expr) = [ ppEExpr b expr ]@@ -134,15 +147,23 @@ ppEExpr b (EditApp x xs) = ppParens' b $ ppHang p (ppCon p (escapeName x)) $ ppSep p $ concatMap (ppEdit True) xs ppEExpr _ (EditRec x xs) = ppHang p (ppCon p (escapeName x)) $ ppRec p $- map ppField' $ Map.toList xs+ justs ++ [ (n, ppCon p "...") | n <- take 1 nothings ]+ where+ xs' = map ppField' $ Map.toList xs+ (nothings, justs) = partitionEithers xs'+ ppEExpr _ (EditLst xs) = ppLst p (concatMap (ppEdit False) xs) ppEExpr b (EditExp x) = ppExpr' p b x - ppField' (n, e) = (escapeName n, ppSep p $ ppEdit False e)+ ppField' (n, Cpy (EditExp e)) | compact, not (isScalar e) = Left n+ ppField' (n, e) = Right (escapeName n, ppSep p $ ppEdit False e) ppParens' True = ppParens p ppParens' False = id + isScalar (App _ []) = True+ isScalar _ = False+ ------------------------------------------------------------------------------- -- pretty -------------------------------------------------------------------------------@@ -173,6 +194,10 @@ prettyEditExpr :: Edit EditExpr -> HJ.Doc prettyEditExpr = ppEditExpr prettyPretty +-- | Compact 'prettyEditExpr'.+prettyEditExprCompact :: Edit EditExpr -> HJ.Doc+prettyEditExprCompact = ppEditExprCompact prettyPretty+ ------------------------------------------------------------------------------- -- ansi-wl-pprint -------------------------------------------------------------------------------@@ -200,6 +225,10 @@ ansiWlEditExpr :: Edit EditExpr -> WL.Doc ansiWlEditExpr = ppEditExpr ansiWlPretty +-- | Compact 'ansiWlEditExpr'+ansiWlEditExprCompact :: Edit EditExpr -> WL.Doc+ansiWlEditExprCompact = ppEditExprCompact ansiWlPretty+ ------------------------------------------------------------------------------- -- Background -------------------------------------------------------------------------------@@ -209,7 +238,7 @@ ansiWlBgPretty = ansiWlPretty { ppIns = \d -> WL.ondullgreen $ WL.white $ WL.plain $ WL.char '+' WL.<> d , ppDel = \d -> WL.ondullred $ WL.white $ WL.plain $ WL.char '-' WL.<> d- } + } -- | Pretty print 'Expr' using @ansi-wl-pprint@. ansiWlBgExpr :: Expr -> WL.Doc@@ -218,3 +247,7 @@ -- | Pretty print @'Edit' 'EditExpr'@ using @ansi-wl-pprint@. ansiWlBgEditExpr :: Edit EditExpr -> WL.Doc ansiWlBgEditExpr = ppEditExpr ansiWlBgPretty++-- | Compact 'ansiWlBgEditExpr'.+ansiWlBgEditExprCompact :: Edit EditExpr -> WL.Doc+ansiWlBgEditExprCompact = ppEditExprCompact ansiWlBgPretty
tests/Tests.hs view
@@ -93,7 +93,8 @@ { fooInt :: Int , fooBar :: [Maybe String] , fooQuu :: (Double, Proxy ())- -- , fooNew :: Bool+ , fooNew :: Bool+ , fooStr :: String } deriving (Eq, Show, Generic) @@ -104,7 +105,8 @@ { fooInt = 42 , fooBar = [Just "pub", Just "night\nclub"] , fooQuu = (125.375, Proxy)- -- , fooNew = True+ , fooNew = True+ , fooStr = "Some Name" } exFooTests :: TestTree
tree-diff.cabal view
@@ -1,6 +1,9 @@+cabal-version: >=1.10 name: tree-diff-version: 0.0.1+version: 0.0.2+ synopsis: Diffing of (expression) trees.+category: Data, Testing description: Common diff algorithm works on list structures: .@@ -34,16 +37,17 @@ license-file: LICENSE author: Oleg Grenrus <oleg.grenrus@iki.fi> maintainer: Oleg.Grenrus <oleg.grenrus@iki.fi>-copyright: (c) 2017 Oleg Grenrus-category: Data+copyright: (c) 2017-2018 Oleg Grenrus build-type: Simple extra-source-files: README.md ChangeLog.md-cabal-version: >=1.10 tested-with: GHC==7.8.4, GHC==7.10.3, GHC==8.0.2,- GHC==8.2.1+ GHC==8.2.2,+ GHC==8.4.4,+ GHc==8.6.2+ extra-source-files: fixtures/exfoo.expr @@ -63,20 +67,20 @@ Data.TreeDiff.Golden Data.TreeDiff.QuickCheck build-depends:- base >=4.7 && <4.11,- aeson >=1.2.1.0 && <1.3,+ base >=4.7 && <4.13,+ aeson >=1.2.1.0 && <1.5, ansi-wl-pprint >=0.6.8.1 && <0.7, ansi-terminal >=0.6.3.1 && <0.9,- base-compat >=0.9.3 && <0.10,+ base-compat >=0.9.3 && <0.11, bytestring >=0.10.4.0 && <0.11,- containers >=0.5.5.1 && <0.6,- generics-sop >=0.3.1.0 && <0.4,+ containers >=0.5.5.1 && <0.7,+ generics-sop >=0.3.1.0 && <0.5, hashable >=1.2.6.1 && <1.3, MemoTrie >=0.6.8 && <0.7, parsec >=3.1.11 && <3.2, parsers >=0.12.7 && <0.13, pretty >=1.1.1.1 && <1.2,- QuickCheck >=2.10.0.1 && <2.11,+ QuickCheck >=2.10.0.1 && <2.13, scientific >=0.3.5.2 && <0.4, tagged >=0.8.5 && <0.9, text >=1.2.2.2 && <1.3,@@ -119,7 +123,7 @@ ansi-terminal, ansi-wl-pprint, parsec,- trifecta >=1.7.1.1 && <1.8,- tasty >=0.11.2.5 && <1.1,+ trifecta >=1.7.1.1 && <2.1,+ tasty >=0.11.2.5 && <1.2, tasty-golden >=2.3.1.1 && <2.4,- tasty-quickcheck >=0.9.1 && <0.10+ tasty-quickcheck >=0.9.1 && <0.11