2017-05-09 17:19:45 +03:00
|
|
|
{-# OPTIONS_GHC -fno-warn-orphans #-}
|
2017-02-14 22:29:24 +03:00
|
|
|
{-# LANGUAGE DataKinds #-}
|
|
|
|
module TOCSpec where
|
|
|
|
|
2017-02-23 07:05:20 +03:00
|
|
|
import Data.Aeson
|
2017-02-15 21:15:57 +03:00
|
|
|
import Category as C
|
2017-02-14 22:29:24 +03:00
|
|
|
import Data.Functor.Both
|
2017-02-15 21:15:57 +03:00
|
|
|
import Data.Functor.Listable
|
2017-04-24 22:53:11 +03:00
|
|
|
import RWS
|
2017-02-14 22:29:24 +03:00
|
|
|
import Data.Record
|
2017-05-08 22:08:01 +03:00
|
|
|
import Data.Text.Listable
|
2017-02-14 22:29:24 +03:00
|
|
|
import Diff
|
|
|
|
import Info
|
2017-02-14 22:54:32 +03:00
|
|
|
import Interpreter
|
2017-02-14 23:48:34 +03:00
|
|
|
import Patch
|
2017-02-14 22:29:24 +03:00
|
|
|
import Prologue hiding (fst, snd)
|
2017-04-20 02:33:27 +03:00
|
|
|
import Renderer
|
2017-02-14 22:54:32 +03:00
|
|
|
import Renderer.TOC
|
2017-02-14 22:29:24 +03:00
|
|
|
import Source
|
2017-02-17 02:22:46 +03:00
|
|
|
import Syntax as S
|
2017-02-15 21:15:57 +03:00
|
|
|
import Term
|
2017-04-20 02:33:27 +03:00
|
|
|
import Semantic
|
2017-04-19 19:12:19 +03:00
|
|
|
import SpecHelpers
|
2017-02-14 22:29:24 +03:00
|
|
|
import Test.Hspec (Spec, describe, it, parallel)
|
|
|
|
import Test.Hspec.Expectations.Pretty
|
2017-02-15 21:15:57 +03:00
|
|
|
import Test.Hspec.LeanCheck
|
2017-02-16 03:13:34 +03:00
|
|
|
import Test.LeanCheck
|
2017-02-14 22:29:24 +03:00
|
|
|
|
|
|
|
spec :: Spec
|
|
|
|
spec = parallel $ do
|
2017-02-23 07:05:20 +03:00
|
|
|
describe "diffTOC" $ do
|
2017-02-14 22:29:24 +03:00
|
|
|
it "blank if there are no methods" $
|
2017-02-17 02:47:18 +03:00
|
|
|
diffTOC blankDiffBlobs blankDiff `shouldBe` [ ]
|
2017-02-14 22:29:24 +03:00
|
|
|
|
2017-02-15 21:15:57 +03:00
|
|
|
it "summarizes changed methods" $ do
|
|
|
|
sourceBlobs <- blobsForPaths (both "ruby/methods.A.rb" "ruby/methods.B.rb")
|
2017-04-24 17:46:32 +03:00
|
|
|
Just diff <- diffBlobPair sourceBlobs
|
2017-02-17 02:47:18 +03:00
|
|
|
diffTOC sourceBlobs diff `shouldBe`
|
2017-02-17 22:34:47 +03:00
|
|
|
[ JSONSummary $ Summarizable C.SingletonMethod "self.foo" (sourceSpanBetween (1, 1) (2, 4)) "added"
|
2017-02-17 02:47:18 +03:00
|
|
|
, JSONSummary $ InSummarizable C.Method "bar" (sourceSpanBetween (4, 1) (6, 4))
|
|
|
|
, JSONSummary $ Summarizable C.Method "baz" (sourceSpanBetween (4, 1) (5, 4)) "removed" ]
|
|
|
|
|
2017-02-14 22:53:25 +03:00
|
|
|
it "dedupes changes in same parent method" $ do
|
2017-02-17 19:46:56 +03:00
|
|
|
sourceBlobs <- blobsForPaths (both "javascript/duplicate-parent.A.js" "javascript/duplicate-parent.B.js")
|
2017-04-24 17:46:32 +03:00
|
|
|
Just diff <- diffBlobPair sourceBlobs
|
2017-02-17 02:47:18 +03:00
|
|
|
diffTOC sourceBlobs diff `shouldBe`
|
|
|
|
[ JSONSummary $ InSummarizable C.Function "myFunction" (sourceSpanBetween (1, 1) (6, 2)) ]
|
2017-02-14 22:53:25 +03:00
|
|
|
|
|
|
|
it "dedupes similar methods" $ do
|
2017-02-17 19:46:56 +03:00
|
|
|
sourceBlobs <- blobsForPaths (both "javascript/erroneous-duplicate-method.A.js" "javascript/erroneous-duplicate-method.B.js")
|
2017-04-24 17:46:32 +03:00
|
|
|
Just diff <- diffBlobPair sourceBlobs
|
2017-02-17 02:47:18 +03:00
|
|
|
diffTOC sourceBlobs diff `shouldBe`
|
|
|
|
[ JSONSummary $ Summarizable C.Function "performHealthCheck" (sourceSpanBetween (8, 1) (29, 2)) "modified" ]
|
2017-02-15 21:15:57 +03:00
|
|
|
|
2017-02-17 19:45:51 +03:00
|
|
|
it "summarizes Go methods with receivers with special formatting" $ do
|
|
|
|
sourceBlobs <- blobsForPaths (both "go/method-with-receiver.A.go" "go/method-with-receiver.B.go")
|
2017-04-24 17:46:32 +03:00
|
|
|
Just diff <- diffBlobPair sourceBlobs
|
2017-02-17 19:45:51 +03:00
|
|
|
diffTOC sourceBlobs diff `shouldBe`
|
|
|
|
[ JSONSummary $ Summarizable C.Method "(*apiClient) CheckAuth" (sourceSpanBetween (3,1) (3,101)) "added" ]
|
|
|
|
|
2017-02-17 19:09:42 +03:00
|
|
|
it "summarizes Ruby methods that start with two identifiers" $ do
|
|
|
|
sourceBlobs <- blobsForPaths (both "ruby/method-starts-with-two-identifiers.A.rb" "ruby/method-starts-with-two-identifiers.B.rb")
|
2017-04-24 17:46:32 +03:00
|
|
|
Just diff <- diffBlobPair sourceBlobs
|
2017-02-17 19:09:42 +03:00
|
|
|
diffTOC sourceBlobs diff `shouldBe`
|
|
|
|
[ JSONSummary $ InSummarizable C.Method "foo" (sourceSpanBetween (1, 1) (4, 4)) ]
|
|
|
|
|
2017-02-16 19:29:49 +03:00
|
|
|
it "handles unicode characters in file" $ do
|
|
|
|
sourceBlobs <- blobsForPaths (both "ruby/unicode.A.rb" "ruby/unicode.B.rb")
|
2017-04-24 17:46:32 +03:00
|
|
|
Just diff <- diffBlobPair sourceBlobs
|
2017-02-17 02:47:18 +03:00
|
|
|
diffTOC sourceBlobs diff `shouldBe`
|
|
|
|
[ JSONSummary $ Summarizable C.Method "foo" (sourceSpanBetween (6, 1) (7, 4)) "added" ]
|
2017-02-16 19:29:49 +03:00
|
|
|
|
2017-02-17 01:20:22 +03:00
|
|
|
prop "inserts of methods and functions are summarized" $
|
2017-02-16 23:23:40 +03:00
|
|
|
\name body ->
|
2017-02-17 01:20:22 +03:00
|
|
|
let diff = programWithInsert name (unListableF body)
|
|
|
|
in numTocSummaries diff `shouldBe` 1
|
2017-02-16 23:23:40 +03:00
|
|
|
|
2017-02-17 01:20:22 +03:00
|
|
|
prop "deletes of methods and functions are summarized" $
|
2017-02-16 23:23:40 +03:00
|
|
|
\name body ->
|
2017-02-17 01:20:22 +03:00
|
|
|
let diff = programWithDelete name (unListableF body)
|
|
|
|
in numTocSummaries diff `shouldBe` 1
|
|
|
|
|
|
|
|
prop "replacements of methods and functions are summarized" $
|
|
|
|
\name body ->
|
|
|
|
let diff = programWithReplace name (unListableF body)
|
|
|
|
in numTocSummaries diff `shouldBe` 1
|
2017-02-16 03:13:34 +03:00
|
|
|
|
2017-02-17 02:22:46 +03:00
|
|
|
prop "changes inside methods and functions are summarizied" . forAll (isMeaningfulTerm `filterT` tiers) $
|
|
|
|
\body ->
|
|
|
|
let diff = programWithChange (unListableF body)
|
|
|
|
in numTocSummaries diff `shouldBe` 1
|
|
|
|
|
2017-02-17 02:41:13 +03:00
|
|
|
prop "other changes don't summarize" . forAll ((not . isMethodOrFunction) `filterT` tiers) $
|
|
|
|
\body ->
|
|
|
|
let diff = programWithChangeOutsideFunction (unListableF body)
|
|
|
|
in numTocSummaries diff `shouldBe` 0
|
|
|
|
|
2017-02-15 21:15:57 +03:00
|
|
|
prop "equal terms produce identity diffs" $
|
2017-02-17 01:20:22 +03:00
|
|
|
\a -> let term = defaultFeatureVectorDecorator (Info.category . headF) (unListableF a :: Term') in
|
2017-02-21 23:23:30 +03:00
|
|
|
diffTOC blankDiffBlobs (diffTerms term term) `shouldBe` []
|
2017-02-14 22:29:24 +03:00
|
|
|
|
2017-02-23 07:05:20 +03:00
|
|
|
describe "JSONSummary" $ do
|
|
|
|
it "encodes InSummarizable to JSON" $ do
|
|
|
|
let summary = JSONSummary $ InSummarizable C.Method "foo" (sourceSpanBetween (1, 1) (4, 4))
|
|
|
|
encode summary `shouldBe` "{\"span\":{\"start\":[1,1],\"end\":[4,4]},\"category\":\"Method\",\"term\":\"foo\",\"changeType\":\"modified\"}"
|
|
|
|
|
|
|
|
it "encodes Summarizable to JSON" $ do
|
|
|
|
let summary = JSONSummary $ Summarizable C.SingletonMethod "self.foo" (sourceSpanBetween (1, 1) (2, 4)) "added"
|
|
|
|
encode summary `shouldBe` "{\"span\":{\"start\":[1,1],\"end\":[2,4]},\"category\":\"Method\",\"term\":\"self.foo\",\"changeType\":\"added\"}"
|
|
|
|
|
2017-04-21 23:56:19 +03:00
|
|
|
describe "diff with ToCRenderer" $ do
|
|
|
|
it "produces JSON output" $ do
|
|
|
|
blobs <- blobsForPaths (both "ruby/methods.A.rb" "ruby/methods.B.rb")
|
2017-04-24 17:46:32 +03:00
|
|
|
output <- diffBlobPairs ToCRenderer [blobs]
|
2017-04-20 02:33:27 +03:00
|
|
|
output `shouldBe` "{\"changes\":{\"ruby/methods.A.rb -> ruby/methods.B.rb\":[{\"span\":{\"start\":[1,1],\"end\":[2,4]},\"category\":\"Method\",\"term\":\"self.foo\",\"changeType\":\"added\"},{\"span\":{\"start\":[4,1],\"end\":[6,4]},\"category\":\"Method\",\"term\":\"bar\",\"changeType\":\"modified\"},{\"span\":{\"start\":[4,1],\"end\":[5,4]},\"category\":\"Method\",\"term\":\"baz\",\"changeType\":\"removed\"}]},\"errors\":{}}\n"
|
2017-02-23 07:05:20 +03:00
|
|
|
|
2017-04-21 23:56:19 +03:00
|
|
|
it "produces JSON output if there are parse errors" $ do
|
|
|
|
blobs <- blobsForPaths (both "ruby/methods.A.rb" "ruby/methods.X.rb")
|
2017-04-24 17:46:32 +03:00
|
|
|
output <- diffBlobPairs ToCRenderer [blobs]
|
2017-04-20 02:33:27 +03:00
|
|
|
output `shouldBe` "{\"changes\":{},\"errors\":{\"ruby/methods.A.rb -> ruby/methods.X.rb\":[{\"span\":{\"start\":[1,1],\"end\":[3,1]},\"error\":\"def bar\\nen\\n\"}]}}\n"
|
2017-02-23 07:05:20 +03:00
|
|
|
|
2017-05-08 22:08:01 +03:00
|
|
|
type Diff' = SyntaxDiff Text DefaultFields
|
|
|
|
type Term' = SyntaxTerm Text DefaultFields
|
2017-02-17 01:20:22 +03:00
|
|
|
|
|
|
|
numTocSummaries :: Diff' -> Int
|
2017-02-17 02:47:18 +03:00
|
|
|
numTocSummaries diff = Prologue.length $ filter (not . isErrorSummary) (diffTOC blankDiffBlobs diff)
|
2017-02-17 01:20:22 +03:00
|
|
|
|
2017-02-17 02:41:13 +03:00
|
|
|
-- Return a diff where body is inserted in the expressions of a function. The function is present in both sides of the diff.
|
2017-02-17 02:22:46 +03:00
|
|
|
programWithChange :: Term' -> Diff'
|
2017-05-08 20:29:04 +03:00
|
|
|
programWithChange body = wrap (pure programInfo :< Indexed [ function' ])
|
2017-02-17 02:22:46 +03:00
|
|
|
where
|
2017-05-08 20:29:04 +03:00
|
|
|
function' = wrap (pure functionInfo :< S.Function name' [] [ pure (Insert body) ] )
|
|
|
|
name' = wrap (pure (Range 0 0 :. C.Identifier :. sourceSpanBetween (0,0) (0,0) :. Nil) :< Leaf "foo")
|
2017-02-17 02:41:13 +03:00
|
|
|
|
|
|
|
-- Return a diff where term is inserted in the program, below a function found on both sides of the diff.
|
|
|
|
programWithChangeOutsideFunction :: Term' -> Diff'
|
2017-05-08 20:29:04 +03:00
|
|
|
programWithChangeOutsideFunction term = wrap (pure programInfo :< Indexed [ function', term' ])
|
2017-02-17 02:41:13 +03:00
|
|
|
where
|
2017-05-08 20:29:04 +03:00
|
|
|
function' = wrap (pure functionInfo :< S.Function name' [] [] )
|
|
|
|
name' = wrap (pure (Range 0 0 :. C.Identifier :. sourceSpanBetween (0,0) (0,0) :. Nil) :< Leaf "foo")
|
|
|
|
term' = pure (Insert term)
|
2017-02-17 02:22:46 +03:00
|
|
|
|
2017-05-08 22:08:01 +03:00
|
|
|
programWithInsert :: Text -> Term' -> Diff'
|
2017-02-17 01:20:22 +03:00
|
|
|
programWithInsert name body = programOf $ Insert (functionOf name body)
|
|
|
|
|
2017-05-08 22:08:01 +03:00
|
|
|
programWithDelete :: Text -> Term' -> Diff'
|
2017-02-17 01:20:22 +03:00
|
|
|
programWithDelete name body = programOf $ Delete (functionOf name body)
|
2017-02-16 23:23:40 +03:00
|
|
|
|
2017-05-08 22:08:01 +03:00
|
|
|
programWithReplace :: Text -> Term' -> Diff'
|
2017-02-17 01:20:22 +03:00
|
|
|
programWithReplace name body = programOf $ Replace (functionOf name body) (functionOf (name <> "2") body)
|
2017-02-16 23:23:40 +03:00
|
|
|
|
2017-02-17 01:20:22 +03:00
|
|
|
programOf :: Patch Term' -> Diff'
|
2017-05-08 20:29:04 +03:00
|
|
|
programOf patch = wrap (pure programInfo :< Indexed [ pure patch ])
|
2017-02-16 23:23:40 +03:00
|
|
|
|
2017-05-08 22:08:01 +03:00
|
|
|
functionOf :: Text -> Term' -> Term'
|
2017-03-29 19:36:05 +03:00
|
|
|
functionOf name body = cofree $ functionInfo :< S.Function name' [] [body]
|
2017-02-17 02:22:46 +03:00
|
|
|
where
|
|
|
|
name' = cofree $ (Range 0 0 :. C.Identifier :. sourceSpanBetween (0,0) (0,0) :. Nil) :< Leaf name
|
2017-02-16 03:13:34 +03:00
|
|
|
|
2017-03-31 22:22:26 +03:00
|
|
|
programInfo :: Record DefaultFields
|
2017-02-16 23:23:40 +03:00
|
|
|
programInfo = Range 0 0 :. C.Program :. sourceSpanBetween (0,0) (0,0) :. Nil
|
2017-02-16 03:13:34 +03:00
|
|
|
|
2017-03-31 22:22:26 +03:00
|
|
|
functionInfo :: Record DefaultFields
|
2017-02-16 23:23:40 +03:00
|
|
|
functionInfo = Range 0 0 :. C.Function :. sourceSpanBetween (0,0) (0,0) :. Nil
|
2017-02-16 03:13:34 +03:00
|
|
|
|
2017-02-17 02:41:13 +03:00
|
|
|
-- Filter tiers for terms that we consider "meaniningful" in TOC summaries.
|
2017-03-31 22:22:26 +03:00
|
|
|
isMeaningfulTerm :: ListableF (Term (Syntax leaf)) (Record DefaultFields) -> Bool
|
2017-02-17 02:22:46 +03:00
|
|
|
isMeaningfulTerm a = case runCofree (unListableF a) of
|
|
|
|
(_ :< S.Indexed _) -> False
|
|
|
|
(_ :< S.Fixed _) -> False
|
|
|
|
(_ :< S.Commented _ _) -> False
|
|
|
|
(_ :< S.ParseError _) -> False
|
|
|
|
_ -> True
|
2017-02-16 03:13:34 +03:00
|
|
|
|
2017-02-17 02:41:13 +03:00
|
|
|
-- Filter tiers for terms if the Syntax is a Method or a Function.
|
2017-03-31 22:22:26 +03:00
|
|
|
isMethodOrFunction :: ListableF (Term (Syntax leaf)) (Record DefaultFields) -> Bool
|
2017-02-17 02:41:13 +03:00
|
|
|
isMethodOrFunction a = case runCofree (unListableF a) of
|
|
|
|
(_ :< S.Method{}) -> True
|
|
|
|
(_ :< S.Function{}) -> True
|
2017-02-28 03:00:53 +03:00
|
|
|
(a :< _) | getField a == C.Function -> True
|
|
|
|
(a :< _) | getField a == C.Method -> True
|
|
|
|
(a :< _) | getField a == C.SingletonMethod -> True
|
2017-02-17 02:41:13 +03:00
|
|
|
_ -> False
|
|
|
|
|
2017-02-14 22:29:24 +03:00
|
|
|
blobsForPaths :: Both FilePath -> IO (Both SourceBlob)
|
|
|
|
blobsForPaths paths = do
|
2017-04-19 19:12:19 +03:00
|
|
|
sources <- traverse (readFileToUnicode . ("test/fixtures/toc/" <>)) paths
|
2017-02-14 22:29:24 +03:00
|
|
|
pure $ SourceBlob <$> sources <*> pure mempty <*> paths <*> pure (Just Source.defaultPlainBlob)
|
|
|
|
|
|
|
|
sourceSpanBetween :: (Int, Int) -> (Int, Int) -> SourceSpan
|
|
|
|
sourceSpanBetween (s1, e1) (s2, e2) = SourceSpan (SourcePos s1 e1) (SourcePos s2 e2)
|
|
|
|
|
|
|
|
blankDiff :: Diff (Syntax Text) (Record '[Category, Range, SourceSpan])
|
2017-05-08 20:29:04 +03:00
|
|
|
blankDiff = wrap (pure arrayInfo :< Indexed [ pure (Insert (cofree $ literalInfo :< Leaf "\"a\"")) ])
|
2017-02-17 02:22:46 +03:00
|
|
|
where
|
|
|
|
arrayInfo = ArrayLiteral :. Range 0 3 :. sourceSpanBetween (1, 1) (1, 5) :. Nil
|
|
|
|
literalInfo = StringLiteral :. Range 1 2 :. sourceSpanBetween (1, 2) (1, 4) :. Nil
|
2017-02-14 22:29:24 +03:00
|
|
|
|
2017-02-17 02:47:18 +03:00
|
|
|
blankDiffBlobs :: Both SourceBlob
|
|
|
|
blankDiffBlobs = both (SourceBlob (fromText "[]") nullOid "a.js" (Just defaultPlainBlob)) (SourceBlob (fromText "[a]") nullOid "b.js" (Just defaultPlainBlob))
|
2017-02-15 21:15:57 +03:00
|
|
|
|
|
|
|
unListableDiff :: Functor f => ListableF (Free (TermF f (ListableF (Join (,)) annotation))) (Patch (ListableF (Term f) annotation)) -> Diff f annotation
|
|
|
|
unListableDiff diff = hoistFree (first unListableF) $ fmap unListableF <$> unListableF diff
|
2017-05-08 22:08:01 +03:00
|
|
|
|
|
|
|
instance Listable Text where
|
|
|
|
tiers = unListableText `mapT` tiers
|