2017-04-21 23:56:19 +03:00
|
|
|
{-# LANGUAGE DuplicateRecordFields #-}
|
2017-11-27 21:57:25 +03:00
|
|
|
module Semantic.CLI.Spec where
|
2017-04-11 03:10:34 +03:00
|
|
|
|
2017-07-28 21:37:02 +03:00
|
|
|
import Control.Monad (when)
|
|
|
|
import Data.ByteString (ByteString)
|
|
|
|
import Data.Foldable (for_)
|
2017-07-20 03:01:59 +03:00
|
|
|
import Data.Functor.Both
|
2017-11-27 22:17:48 +03:00
|
|
|
import Data.Language
|
2017-07-28 21:37:02 +03:00
|
|
|
import Data.Semigroup ((<>))
|
2017-11-27 21:30:38 +03:00
|
|
|
import Rendering.Renderer
|
2017-11-27 19:57:27 +03:00
|
|
|
import Semantic.CLI
|
2017-07-20 00:23:44 +03:00
|
|
|
import Semantic.Task
|
2017-07-28 21:37:02 +03:00
|
|
|
import System.IO (Handle)
|
2017-04-11 03:10:34 +03:00
|
|
|
import Test.Hspec hiding (shouldBe, shouldNotBe, shouldThrow, errorCall)
|
|
|
|
import Test.Hspec.Expectations.Pretty
|
|
|
|
|
|
|
|
spec :: Spec
|
|
|
|
spec = parallel $ do
|
2017-06-16 19:51:42 +03:00
|
|
|
describe "runDiff" $
|
2017-07-19 21:01:56 +03:00
|
|
|
for_ diffFixtures $ \ (diffRenderer, diffMode, expected) ->
|
2017-06-16 19:51:42 +03:00
|
|
|
it ("renders to " <> show diffRenderer <> " in mode " <> show diffMode) $ do
|
2017-07-20 00:23:44 +03:00
|
|
|
output <- runTask $ runDiff diffRenderer diffMode
|
2017-04-21 23:56:19 +03:00
|
|
|
output `shouldBe'` expected
|
2017-06-16 19:47:47 +03:00
|
|
|
|
|
|
|
describe "runParse" $
|
2017-07-19 21:01:56 +03:00
|
|
|
for_ parseFixtures $ \ (parseTreeRenderer, parseMode, expected) ->
|
2017-06-16 19:47:47 +03:00
|
|
|
it ("renders to " <> show parseTreeRenderer <> " in mode " <> show parseMode) $ do
|
2017-07-20 00:23:44 +03:00
|
|
|
output <- runTask $ runParse parseTreeRenderer parseMode
|
2017-04-21 23:56:19 +03:00
|
|
|
output `shouldBe'` expected
|
2017-04-21 01:13:28 +03:00
|
|
|
where
|
2017-04-21 23:56:19 +03:00
|
|
|
shouldBe' actual expected = do
|
|
|
|
when (actual /= expected) $ print actual
|
|
|
|
actual `shouldBe` expected
|
|
|
|
|
2017-07-19 22:40:56 +03:00
|
|
|
parseFixtures :: [(SomeRenderer TermRenderer, Either Handle [(FilePath, Maybe Language)], ByteString)]
|
2017-06-16 19:47:47 +03:00
|
|
|
parseFixtures =
|
2017-07-19 21:01:56 +03:00
|
|
|
[ (SomeRenderer SExpressionTermRenderer, pathMode, sExpressionParseTreeOutput)
|
|
|
|
, (SomeRenderer JSONTermRenderer, pathMode, jsonParseTreeOutput)
|
|
|
|
, (SomeRenderer JSONTermRenderer, pathMode', jsonParseTreeOutput')
|
2017-07-19 22:40:56 +03:00
|
|
|
, (SomeRenderer JSONTermRenderer, Right [], emptyJsonParseTreeOutput)
|
2018-01-17 23:59:08 +03:00
|
|
|
, (SomeRenderer (SymbolsTermRenderer defaultSymbolFields), Right [("test/fixtures/ruby/method-declaration.A.rb", Just Ruby)], symbolsOutput)
|
2018-01-18 00:31:56 +03:00
|
|
|
, (SomeRenderer TagsTermRenderer, Right [("test/fixtures/ruby/method-declaration.A.rb", Just Ruby)], tagsOutput)
|
2017-06-16 19:47:47 +03:00
|
|
|
]
|
2017-07-19 22:40:56 +03:00
|
|
|
where pathMode = Right [("test/fixtures/ruby/and-or.A.rb", Just Ruby)]
|
|
|
|
pathMode' = Right [("test/fixtures/ruby/and-or.A.rb", Just Ruby), ("test/fixtures/ruby/and-or.B.rb", Just Ruby)]
|
2017-06-16 19:47:47 +03:00
|
|
|
|
2017-08-24 21:46:26 +03:00
|
|
|
sExpressionParseTreeOutput = "(Program\n (And\n (Identifier)\n (Identifier)))\n"
|
2018-01-08 20:36:17 +03:00
|
|
|
jsonParseTreeOutput = "{\"trees\":[{\"path\":\"test/fixtures/ruby/and-or.A.rb\",\"programNode\":{\"category\":\"Program\",\"children\":[{\"category\":\"And\",\"children\":[{\"category\":\"Identifier\",\"children\":[],\"name\":\"foo\",\"sourceRange\":[0,3],\"sourceSpan\":{\"start\":[1,1],\"end\":[1,4]}},{\"category\":\"Identifier\",\"children\":[],\"name\":\"bar\",\"sourceRange\":[8,11],\"sourceSpan\":{\"start\":[1,9],\"end\":[1,12]}}],\"sourceRange\":[0,11],\"sourceSpan\":{\"start\":[1,1],\"end\":[1,12]}}],\"sourceRange\":[0,12],\"sourceSpan\":{\"start\":[1,1],\"end\":[2,1]}},\"language\":\"Ruby\"}]}\n"
|
|
|
|
jsonParseTreeOutput' = "{\"trees\":[{\"path\":\"test/fixtures/ruby/and-or.A.rb\",\"programNode\":{\"category\":\"Program\",\"children\":[{\"category\":\"And\",\"children\":[{\"category\":\"Identifier\",\"children\":[],\"name\":\"foo\",\"sourceRange\":[0,3],\"sourceSpan\":{\"start\":[1,1],\"end\":[1,4]}},{\"category\":\"Identifier\",\"children\":[],\"name\":\"bar\",\"sourceRange\":[8,11],\"sourceSpan\":{\"start\":[1,9],\"end\":[1,12]}}],\"sourceRange\":[0,11],\"sourceSpan\":{\"start\":[1,1],\"end\":[1,12]}}],\"sourceRange\":[0,12],\"sourceSpan\":{\"start\":[1,1],\"end\":[2,1]}},\"language\":\"Ruby\"},{\"path\":\"test/fixtures/ruby/and-or.B.rb\",\"programNode\":{\"category\":\"Program\",\"children\":[{\"category\":\"Or\",\"children\":[{\"category\":\"Identifier\",\"children\":[],\"name\":\"foo\",\"sourceRange\":[0,3],\"sourceSpan\":{\"start\":[1,1],\"end\":[1,4]}},{\"category\":\"Identifier\",\"children\":[],\"name\":\"bar\",\"sourceRange\":[7,10],\"sourceSpan\":{\"start\":[1,8],\"end\":[1,11]}}],\"sourceRange\":[0,10],\"sourceSpan\":{\"start\":[1,1],\"end\":[1,11]}},{\"category\":\"And\",\"children\":[{\"category\":\"Or\",\"children\":[{\"category\":\"Identifier\",\"children\":[],\"name\":\"a\",\"sourceRange\":[11,12],\"sourceSpan\":{\"start\":[2,1],\"end\":[2,2]}},{\"category\":\"Identifier\",\"children\":[],\"name\":\"b\",\"sourceRange\":[16,17],\"sourceSpan\":{\"start\":[2,6],\"end\":[2,7]}}],\"sourceRange\":[11,17],\"sourceSpan\":{\"start\":[2,1],\"end\":[2,7]}},{\"category\":\"Identifier\",\"children\":[],\"name\":\"c\",\"sourceRange\":[22,23],\"sourceSpan\":{\"start\":[2,12],\"end\":[2,13]}}],\"sourceRange\":[11,23],\"sourceSpan\":{\"start\":[2,1],\"end\":[2,13]}}],\"sourceRange\":[0,24],\"sourceSpan\":{\"start\":[1,1],\"end\":[3,1]}},\"language\":\"Ruby\"}]}\n"
|
|
|
|
emptyJsonParseTreeOutput = "{\"trees\":[]}\n"
|
2018-01-17 23:59:08 +03:00
|
|
|
symbolsOutput = "{\"files\":[{\"path\":\"test/fixtures/ruby/method-declaration.A.rb\",\"symbols\":[{\"span\":{\"start\":[1,1],\"end\":[2,4]},\"kind\":\"Method\",\"symbol\":\"foo\"}],\"language\":\"Ruby\"}]}\n"
|
2018-01-18 00:31:56 +03:00
|
|
|
tagsOutput = "[{\"span\":{\"start\":[1,1],\"end\":[2,4]},\"path\":\"test/fixtures/ruby/method-declaration.A.rb\",\"kind\":\"Method\",\"symbol\":\"foo\",\"line\":\"def foo\",\"language\":\"Ruby\"}]\n"
|
2017-04-21 23:56:19 +03:00
|
|
|
|
|
|
|
|
2017-07-19 22:40:56 +03:00
|
|
|
diffFixtures :: [(SomeRenderer DiffRenderer, Either Handle [Both (FilePath, Maybe Language)], ByteString)]
|
2017-06-16 19:51:42 +03:00
|
|
|
diffFixtures =
|
2017-11-22 01:11:16 +03:00
|
|
|
[ (SomeRenderer JSONDiffRenderer, pathMode, jsonOutput)
|
2017-07-19 21:01:56 +03:00
|
|
|
, (SomeRenderer SExpressionDiffRenderer, pathMode, sExpressionOutput)
|
2017-10-10 20:18:11 +03:00
|
|
|
, (SomeRenderer ToCDiffRenderer, pathMode, tocOutput)
|
2017-06-16 19:51:42 +03:00
|
|
|
]
|
2017-07-20 03:01:59 +03:00
|
|
|
where pathMode = Right [both ("test/fixtures/ruby/method-declaration.A.rb", Just Ruby) ("test/fixtures/ruby/method-declaration.B.rb", Just Ruby)]
|
2017-04-21 23:56:19 +03:00
|
|
|
|
2018-01-08 20:36:17 +03:00
|
|
|
jsonOutput = "{\"diffs\":[{\"diff\":{\"merge\":{\"after\":{\"category\":\"Program\",\"sourceRange\":[0,21],\"sourceSpan\":{\"start\":[1,1],\"end\":[4,1]}},\"children\":[{\"merge\":{\"after\":{\"category\":\"Method\",\"sourceRange\":[0,20],\"sourceSpan\":{\"start\":[1,1],\"end\":[3,4]}},\"children\":[{\"merge\":{\"after\":{\"category\":\"Empty\",\"sourceRange\":[0,0],\"sourceSpan\":{\"start\":[1,1],\"end\":[1,1]}},\"children\":[],\"before\":{\"category\":\"Empty\",\"sourceRange\":[0,0],\"sourceSpan\":{\"start\":[1,1],\"end\":[1,1]}}}},{\"patch\":{\"replace\":[{\"category\":\"Identifier\",\"children\":[],\"name\":\"foo\",\"sourceRange\":[4,7],\"sourceSpan\":{\"start\":[1,5],\"end\":[1,8]}},{\"category\":\"Identifier\",\"children\":[],\"name\":\"bar\",\"sourceRange\":[4,7],\"sourceSpan\":{\"start\":[1,5],\"end\":[1,8]}}]}},{\"patch\":{\"insert\":{\"category\":\"Identifier\",\"children\":[],\"name\":\"a\",\"sourceRange\":[8,9],\"sourceSpan\":{\"start\":[1,9],\"end\":[1,10]}}}},{\"merge\":{\"after\":{\"category\":\"\",\"sourceRange\":[13,16],\"sourceSpan\":{\"start\":[2,3],\"end\":[2,6]}},\"children\":[{\"patch\":{\"insert\":{\"category\":\"Identifier\",\"children\":[],\"name\":\"baz\",\"sourceRange\":[13,16],\"sourceSpan\":{\"start\":[2,3],\"end\":[2,6]}}}}],\"before\":{\"category\":\"[]\",\"sourceRange\":[8,11],\"sourceSpan\":{\"start\":[2,1],\"end\":[2,4]}}}}],\"before\":{\"category\":\"Method\",\"sourceRange\":[0,11],\"sourceSpan\":{\"start\":[1,1],\"end\":[2,4]}}}}],\"before\":{\"category\":\"Program\",\"sourceRange\":[0,12],\"sourceSpan\":{\"start\":[1,1],\"end\":[3,1]}}}},\"stat\":{\"replace\":[{\"path\":\"test/fixtures/ruby/method-declaration.A.rb\",\"language\":\"Ruby\"},{\"path\":\"test/fixtures/ruby/method-declaration.B.rb\",\"language\":\"Ruby\"}],\"path\":\"test/fixtures/ruby/method-declaration.A.rb -> test/fixtures/ruby/method-declaration.B.rb\"}}]}\n"
|
2017-08-24 21:46:26 +03:00
|
|
|
sExpressionOutput = "(Program\n (Method\n (Empty)\n { (Identifier)\n ->(Identifier) }\n {+(Identifier)+}\n (\n {+(Identifier)+})))\n"
|
2017-06-16 19:51:42 +03:00
|
|
|
tocOutput = "{\"changes\":{\"test/fixtures/ruby/method-declaration.A.rb -> test/fixtures/ruby/method-declaration.B.rb\":[{\"span\":{\"start\":[1,1],\"end\":[3,4]},\"category\":\"Method\",\"term\":\"bar\",\"changeType\":\"modified\"}]},\"errors\":{}}\n"
|