2018-12-12 02:10:57 +03:00
|
|
|
{-# LANGUAGE GADTs, OverloadedLists, TypeOperators #-}
|
2018-07-31 03:05:44 +03:00
|
|
|
|
2018-07-31 17:43:46 +03:00
|
|
|
module Reprinting.Spec where
|
2018-07-30 23:00:09 +03:00
|
|
|
|
2018-09-19 18:54:12 +03:00
|
|
|
import SpecHelpers hiding (inject, project)
|
2018-07-30 23:00:09 +03:00
|
|
|
|
2018-09-19 18:54:12 +03:00
|
|
|
import Data.Foldable
|
|
|
|
import Data.Functor.Foldable (cata, embed)
|
|
|
|
import qualified Data.Machine as Machine
|
|
|
|
|
2018-10-15 19:47:18 +03:00
|
|
|
import Control.Rewriting hiding (context)
|
2018-09-19 18:54:12 +03:00
|
|
|
import Data.Algebra
|
|
|
|
import Data.Blob
|
2018-07-30 23:00:09 +03:00
|
|
|
import qualified Data.Language as Language
|
2018-09-19 18:54:12 +03:00
|
|
|
import Data.Reprinting.Scope
|
2018-10-15 19:47:18 +03:00
|
|
|
import Data.Reprinting.Token
|
2018-09-19 18:54:12 +03:00
|
|
|
import Data.Sum
|
2018-07-31 23:48:00 +03:00
|
|
|
import qualified Data.Syntax.Literal as Literal
|
2018-09-19 18:54:12 +03:00
|
|
|
import Language.JSON.PrettyPrint
|
|
|
|
import Language.Python.PrettyPrint
|
|
|
|
import Language.Ruby.PrettyPrint
|
|
|
|
import Reprinting.Pipeline
|
|
|
|
import Reprinting.Tokenize
|
|
|
|
import Semantic.IO
|
2018-07-30 23:00:09 +03:00
|
|
|
|
2018-12-12 02:10:57 +03:00
|
|
|
increaseNumbers :: (Literal.Float :< fs, Apply Functor fs) => Rule (Term (Sum fs) History)
|
|
|
|
increaseNumbers = do
|
|
|
|
(Literal.Float c) <- target >>= guardTerm
|
|
|
|
create (Literal.Float (c <> "0"))
|
|
|
|
|
2018-07-30 23:00:09 +03:00
|
|
|
spec :: Spec
|
2018-07-31 21:07:16 +03:00
|
|
|
spec = describe "reprinting" $ do
|
2018-08-22 23:47:30 +03:00
|
|
|
context "JSON" $ do
|
|
|
|
let path = "test/fixtures/javascript/reprinting/map.json"
|
|
|
|
(src, tree) <- runIO $ do
|
2018-10-23 22:28:21 +03:00
|
|
|
src <- blobSource <$> readBlobFromFile' (File path Language.JSON)
|
2018-08-22 23:47:30 +03:00
|
|
|
tree <- parseFile jsonParser path
|
|
|
|
pure (src, tree)
|
|
|
|
|
|
|
|
describe "tokenization" $ do
|
|
|
|
|
|
|
|
it "should pass over a pristine tree" $ do
|
|
|
|
let tagged = mark Unmodified tree
|
2018-09-12 01:54:37 +03:00
|
|
|
let toks = Machine.run $ tokenizing src tagged
|
2018-09-12 18:46:41 +03:00
|
|
|
toks `shouldSatisfy` not . null
|
|
|
|
head toks `shouldSatisfy` isControl
|
|
|
|
last toks `shouldSatisfy` isChunk
|
2018-08-22 23:47:30 +03:00
|
|
|
|
|
|
|
it "should emit control tokens but only 1 chunk for a wholly-modified tree" $ do
|
2018-09-12 01:54:37 +03:00
|
|
|
let toks = Machine.run $ tokenizing src (mark Refactored tree)
|
2018-09-19 18:54:12 +03:00
|
|
|
for_ @[] [List, Hash] $ \t -> do
|
|
|
|
toks `shouldSatisfy` elem (Control (Enter t))
|
|
|
|
toks `shouldSatisfy` elem (Control (Exit t))
|
2018-08-22 23:47:30 +03:00
|
|
|
|
|
|
|
describe "pipeline" $ do
|
|
|
|
|
|
|
|
it "should roundtrip exactly over a pristine tree" $ do
|
|
|
|
let tagged = mark Unmodified tree
|
|
|
|
let printed = runReprinter src defaultJSONPipeline tagged
|
|
|
|
printed `shouldBe` Right src
|
|
|
|
|
|
|
|
it "should roundtrip exactly over a wholly-modified tree" $ do
|
|
|
|
let tagged = mark Refactored tree
|
|
|
|
let printed = runReprinter src defaultJSONPipeline tagged
|
|
|
|
printed `shouldBe` Right src
|
|
|
|
|
2018-12-12 02:10:57 +03:00
|
|
|
it "should be able to parse the output of a refactor" $ do
|
2019-02-02 02:04:23 +03:00
|
|
|
let (Just tagged) = rewrite (mark Unmodified tree) (topDownAny increaseNumbers)
|
2018-12-12 02:10:57 +03:00
|
|
|
let (Right printed) = runReprinter src defaultJSONPipeline tagged
|
2019-03-25 23:14:04 +03:00
|
|
|
tree' <- runTaskOrDie (parse jsonParser (Blob printed path Language.JSON mempty))
|
2018-12-12 02:10:57 +03:00
|
|
|
length tree' `shouldSatisfy` (/= 0)
|