2016-07-13 21:32:53 +03:00
|
|
|
{-# LANGUAGE DataKinds #-}
|
2016-05-17 20:09:14 +03:00
|
|
|
module DiffSummarySpec where
|
2016-05-16 21:43:53 +03:00
|
|
|
|
2016-05-26 20:40:54 +03:00
|
|
|
import Prologue
|
2016-06-17 20:33:50 +03:00
|
|
|
import Data.Record
|
2016-05-16 21:43:53 +03:00
|
|
|
import Test.Hspec
|
2016-07-14 22:21:14 +03:00
|
|
|
import Test.Hspec.QuickCheck
|
2016-05-17 22:59:07 +03:00
|
|
|
import Diff
|
|
|
|
import Syntax
|
2016-07-14 20:01:16 +03:00
|
|
|
import Term
|
2016-05-17 20:09:14 +03:00
|
|
|
import Patch
|
2016-05-17 22:59:07 +03:00
|
|
|
import Category
|
2016-05-16 21:43:53 +03:00
|
|
|
import DiffSummary
|
2016-07-13 21:32:53 +03:00
|
|
|
import Text.PrettyPrint.Leijen.Text (pretty)
|
|
|
|
import Test.Hspec.QuickCheck
|
2016-07-14 18:52:40 +03:00
|
|
|
import Diff.Arbitrary
|
2016-07-14 20:01:16 +03:00
|
|
|
import Data.List (partition)
|
2016-07-22 19:36:03 +03:00
|
|
|
import Term.Arbitrary
|
|
|
|
import Interpreter
|
2016-07-22 21:51:08 +03:00
|
|
|
import Info
|
2016-07-29 21:31:02 +03:00
|
|
|
import Source
|
|
|
|
import Data.Functor.Both
|
2016-05-17 20:09:14 +03:00
|
|
|
|
2016-07-29 21:31:02 +03:00
|
|
|
arrayInfo :: Record '[Category, Range]
|
|
|
|
arrayInfo = ArrayLiteral .: Range 0 3 .: RNil
|
2016-05-17 22:59:07 +03:00
|
|
|
|
2016-07-29 21:31:02 +03:00
|
|
|
literalInfo :: Record '[Category, Range]
|
|
|
|
literalInfo = StringLiteral .: Range 1 2 .: RNil
|
2016-05-17 22:59:07 +03:00
|
|
|
|
2016-07-29 21:31:02 +03:00
|
|
|
testDiff :: Diff Text (Record '[Category, Range])
|
2016-05-17 22:59:07 +03:00
|
|
|
testDiff = free $ Free (pure arrayInfo :< Indexed [ free $ Pure (Insert (cofree $ literalInfo :< Leaf "a")) ])
|
|
|
|
|
2016-05-31 23:15:40 +03:00
|
|
|
testSummary :: DiffSummary DiffInfo
|
2016-08-08 21:59:51 +03:00
|
|
|
testSummary = DiffSummary { patch = Insert (LeafInfo "string" "a"), parentAnnotation = Nothing }
|
2016-05-17 20:09:14 +03:00
|
|
|
|
2016-05-31 23:15:40 +03:00
|
|
|
replacementSummary :: DiffSummary DiffInfo
|
2016-08-08 21:59:51 +03:00
|
|
|
replacementSummary = DiffSummary { patch = Replace (LeafInfo "string" "a") (LeafInfo "symbol" "b"), parentAnnotation = Just (Info.FunctionCall, "foo") }
|
2016-05-18 17:24:08 +03:00
|
|
|
|
2016-07-29 21:34:28 +03:00
|
|
|
sources :: Both (Source Char)
|
2016-07-29 21:31:02 +03:00
|
|
|
sources = both (fromText "[]") (fromText "[a]")
|
|
|
|
|
2016-05-17 20:09:14 +03:00
|
|
|
spec :: Spec
|
|
|
|
spec = parallel $ do
|
2016-07-30 01:37:41 +03:00
|
|
|
describe "diffSummaries" $ do
|
2016-05-17 22:59:07 +03:00
|
|
|
it "outputs a diff summary" $ do
|
2016-08-08 21:59:51 +03:00
|
|
|
diffSummaries sources testDiff `shouldBe` [ DiffSummary { patch = Insert (LeafInfo "string" "a"), parentAnnotation = Nothing } ]
|
2016-07-14 22:21:14 +03:00
|
|
|
|
|
|
|
prop "equal terms produce identity diffs" $
|
2016-07-29 21:31:02 +03:00
|
|
|
\ a -> let term = toTerm (a :: ArbitraryTerm Text (Record '[Category, Range])) in
|
2016-07-30 01:37:41 +03:00
|
|
|
diffSummaries sources (diffTerms wrap (==) diffCost term term) `shouldBe` []
|
2016-07-14 22:21:14 +03:00
|
|
|
|
2016-07-22 21:39:54 +03:00
|
|
|
describe "annotatedSummaries" $ do
|
2016-05-17 20:09:14 +03:00
|
|
|
it "should print adds" $
|
2016-07-16 00:11:45 +03:00
|
|
|
annotatedSummaries testSummary `shouldBe` ["Added the 'a' string"]
|
2016-05-18 17:24:08 +03:00
|
|
|
it "prints a replacement" $ do
|
2016-08-08 21:59:51 +03:00
|
|
|
annotatedSummaries replacementSummary `shouldBe` ["Replaced the 'a' string with the 'b' symbol in the foo function call"]
|
2016-07-15 21:10:31 +03:00
|
|
|
describe "DiffInfo" $ do
|
|
|
|
prop "patches in summaries match the patches in diffs" $
|
|
|
|
\a -> let
|
2016-07-29 21:31:02 +03:00
|
|
|
diff = (toDiff (a :: ArbitraryDiff Text (Record '[Category, Cost, Range])))
|
2016-07-30 01:37:41 +03:00
|
|
|
summaries = diffSummaries sources diff
|
2016-07-15 21:10:31 +03:00
|
|
|
patches = toList diff
|
|
|
|
in
|
|
|
|
case (partition isBranchNode (patch <$> summaries), partition isIndexedOrFixed patches) of
|
|
|
|
((branchPatches, otherPatches), (branchDiffPatches, otherDiffPatches)) ->
|
|
|
|
(() <$ branchPatches, () <$ otherPatches) `shouldBe` (() <$ branchDiffPatches, () <$ otherDiffPatches)
|
|
|
|
prop "generates one LeafInfo for each child in an arbitrary branch patch" $
|
|
|
|
\a -> let
|
2016-07-29 21:31:02 +03:00
|
|
|
diff = (toDiff (a :: ArbitraryDiff Text (Record '[Category, Range])))
|
2016-07-30 01:37:41 +03:00
|
|
|
diffInfoPatches = patch <$> diffSummaries sources diff
|
2016-07-15 21:10:31 +03:00
|
|
|
syntaxPatches = toList diff
|
|
|
|
extractLeaves :: DiffInfo -> [DiffInfo]
|
|
|
|
extractLeaves (BranchInfo children _ _) = join $ extractLeaves <$> children
|
|
|
|
extractLeaves leaf = [ leaf ]
|
|
|
|
|
2016-07-29 21:31:02 +03:00
|
|
|
extractDiffLeaves :: Term Text (Record '[Category, Range]) -> [ Term Text (Record '[Category, Range]) ]
|
2016-07-15 21:10:31 +03:00
|
|
|
extractDiffLeaves term = case unwrap term of
|
|
|
|
(Indexed children) -> join $ extractDiffLeaves <$> children
|
|
|
|
(Fixed children) -> join $ extractDiffLeaves <$> children
|
2016-07-26 20:45:50 +03:00
|
|
|
Commented children leaf -> children <> maybeToList leaf >>= extractDiffLeaves
|
2016-07-15 21:10:31 +03:00
|
|
|
_ -> [ term ]
|
|
|
|
in
|
|
|
|
case (partition isBranchNode diffInfoPatches, partition isIndexedOrFixed syntaxPatches) of
|
|
|
|
((branchPatches, _), (diffPatches, _)) ->
|
|
|
|
let listOfLeaves = foldMap extractLeaves (join $ toList <$> branchPatches)
|
2016-07-26 20:44:12 +03:00
|
|
|
listOfDiffLeaves = foldMap extractDiffLeaves (diffPatches >>= toList)
|
2016-07-15 21:10:31 +03:00
|
|
|
in
|
|
|
|
length listOfLeaves `shouldBe` length listOfDiffLeaves
|
|
|
|
|
|
|
|
isIndexedOrFixed :: Patch (Term a annotation) -> Bool
|
2016-07-26 23:44:33 +03:00
|
|
|
isIndexedOrFixed = any (isIndexedOrFixed' . unwrap)
|
2016-07-16 03:32:48 +03:00
|
|
|
|
|
|
|
isIndexedOrFixed' :: Syntax a f -> Bool
|
2016-07-15 21:10:31 +03:00
|
|
|
isIndexedOrFixed' syntax = case syntax of
|
|
|
|
(Indexed _) -> True
|
|
|
|
(Fixed _) -> True
|
|
|
|
_ -> False
|
2016-07-16 03:32:48 +03:00
|
|
|
|
|
|
|
isBranchInfo :: DiffInfo -> Bool
|
2016-07-15 21:10:31 +03:00
|
|
|
isBranchInfo info = case info of
|
|
|
|
(BranchInfo _ _ _) -> True
|
|
|
|
(LeafInfo _ _) -> False
|
2016-07-16 03:32:48 +03:00
|
|
|
|
2016-07-15 21:10:31 +03:00
|
|
|
isBranchNode :: Patch DiffInfo -> Bool
|
2016-07-26 21:04:15 +03:00
|
|
|
isBranchNode = any isBranchInfo
|