2017-11-27 21:52:48 +03:00
|
|
|
module Semantic.IO.Spec where
|
2017-03-01 01:55:57 +03:00
|
|
|
|
2017-06-24 17:09:50 +03:00
|
|
|
import Data.Blob
|
2017-04-21 23:56:19 +03:00
|
|
|
import Data.Functor.Both as Both
|
2017-11-27 22:17:48 +03:00
|
|
|
import Data.Language
|
2017-04-04 00:39:04 +03:00
|
|
|
import Data.Maybe
|
2017-04-21 01:13:28 +03:00
|
|
|
import Data.String
|
2017-07-28 21:37:02 +03:00
|
|
|
import Prelude hiding (readFile)
|
2017-11-27 21:09:03 +03:00
|
|
|
import Semantic.IO
|
2017-07-28 21:37:02 +03:00
|
|
|
import System.Exit (ExitCode(..))
|
|
|
|
import System.IO (IOMode(..), openFile)
|
2017-12-11 21:51:52 +03:00
|
|
|
import Test.Hspec hiding (shouldBe, shouldNotBe, shouldThrow, errorCall, anyIOException)
|
2017-03-01 01:55:57 +03:00
|
|
|
import Test.Hspec.Expectations.Pretty
|
|
|
|
|
|
|
|
spec :: Spec
|
|
|
|
spec = parallel $ do
|
2017-04-21 01:13:28 +03:00
|
|
|
describe "readFile" $ do
|
|
|
|
it "returns a blob for extant files" $ do
|
2017-12-11 21:51:52 +03:00
|
|
|
Just blob <- readFile "semantic-diff.cabal" Nothing
|
2017-06-24 17:21:54 +03:00
|
|
|
blobPath blob `shouldBe` "semantic-diff.cabal"
|
2017-04-21 01:13:28 +03:00
|
|
|
|
2017-12-11 21:51:52 +03:00
|
|
|
it "throws for absent files" $ do
|
|
|
|
readFile "this file should not exist" Nothing `shouldThrow` anyIOException
|
2017-04-21 01:13:28 +03:00
|
|
|
|
2017-05-17 22:53:05 +03:00
|
|
|
describe "readBlobPairsFromHandle" $ do
|
2017-05-23 21:00:20 +03:00
|
|
|
let a = sourceBlob "method.rb" (Just Ruby) "def foo; end"
|
|
|
|
let b = sourceBlob "method.rb" (Just Ruby) "def bar(x); end"
|
2017-05-17 22:47:45 +03:00
|
|
|
it "returns blobs for valid JSON encoded diff input" $ do
|
2017-05-23 21:00:20 +03:00
|
|
|
blobs <- blobsFromFilePath "test/fixtures/input/diff.json"
|
2017-12-11 20:09:07 +03:00
|
|
|
blobs `shouldBe` [blobPairDiffing a b]
|
2017-05-17 22:47:45 +03:00
|
|
|
|
2017-05-23 21:00:20 +03:00
|
|
|
it "returns blobs when there's no before" $ do
|
|
|
|
blobs <- blobsFromFilePath "test/fixtures/input/diff-no-before.json"
|
2017-12-11 20:09:07 +03:00
|
|
|
blobs `shouldBe` [blobPairInserting b]
|
2017-05-23 21:00:20 +03:00
|
|
|
|
|
|
|
it "returns blobs when there's null before" $ do
|
|
|
|
blobs <- blobsFromFilePath "test/fixtures/input/diff-null-before.json"
|
2017-12-11 20:09:07 +03:00
|
|
|
blobs `shouldBe` [blobPairInserting b]
|
2017-05-23 21:00:20 +03:00
|
|
|
|
|
|
|
it "returns blobs when there's no after" $ do
|
|
|
|
blobs <- blobsFromFilePath "test/fixtures/input/diff-no-after.json"
|
2017-12-11 20:09:07 +03:00
|
|
|
blobs `shouldBe` [blobPairDeleting a]
|
2017-05-23 21:00:20 +03:00
|
|
|
|
|
|
|
it "returns blobs when there's null after" $ do
|
|
|
|
blobs <- blobsFromFilePath "test/fixtures/input/diff-null-after.json"
|
2017-12-11 20:09:07 +03:00
|
|
|
blobs `shouldBe` [blobPairDeleting a]
|
2017-05-23 21:00:20 +03:00
|
|
|
|
|
|
|
|
2017-05-19 00:04:44 +03:00
|
|
|
it "returns blobs for unsupported language" $ do
|
|
|
|
h <- openFile "test/fixtures/input/diff-unsupported-language.json" ReadMode
|
2017-06-15 17:17:41 +03:00
|
|
|
blobs <- readBlobPairsFromHandle h
|
2017-05-23 21:00:20 +03:00
|
|
|
let b' = sourceBlob "test.kt" Nothing "fun main(args: Array<String>) {\nprintln(\"hi\")\n}\n"
|
2017-12-11 20:09:07 +03:00
|
|
|
blobs `shouldBe` [blobPairInserting b']
|
2017-05-19 00:04:44 +03:00
|
|
|
|
|
|
|
it "detects language based on filepath for empty language" $ do
|
2017-05-23 21:00:20 +03:00
|
|
|
blobs <- blobsFromFilePath "test/fixtures/input/diff-empty-language.json"
|
2017-12-11 20:09:07 +03:00
|
|
|
blobs `shouldBe` [blobPairDiffing a b]
|
2017-05-19 00:04:44 +03:00
|
|
|
|
|
|
|
it "throws on blank input" $ do
|
2017-05-17 22:47:45 +03:00
|
|
|
h <- openFile "test/fixtures/input/blank.json" ReadMode
|
2017-06-15 17:17:41 +03:00
|
|
|
readBlobPairsFromHandle h `shouldThrow` (== ExitFailure 1)
|
2017-05-17 22:47:45 +03:00
|
|
|
|
2017-05-19 00:04:44 +03:00
|
|
|
it "throws if language field not given" $ do
|
|
|
|
h <- openFile "test/fixtures/input/diff-no-language.json" ReadMode
|
2017-06-15 17:17:41 +03:00
|
|
|
readBlobsFromHandle h `shouldThrow` (== ExitFailure 1)
|
2017-05-19 00:04:44 +03:00
|
|
|
|
2017-12-11 23:59:14 +03:00
|
|
|
it "throws if null on before and after" $ do
|
|
|
|
h <- openFile "test/fixtures/input/diff-null-both-sides.json" ReadMode
|
|
|
|
readBlobPairsFromHandle h `shouldThrow` (== ExitFailure 1)
|
|
|
|
|
2017-05-17 23:34:09 +03:00
|
|
|
describe "readBlobsFromHandle" $ do
|
|
|
|
it "returns blobs for valid JSON encoded parse input" $ do
|
|
|
|
h <- openFile "test/fixtures/input/parse.json" ReadMode
|
2017-06-15 17:17:41 +03:00
|
|
|
blobs <- readBlobsFromHandle h
|
2017-05-19 00:04:44 +03:00
|
|
|
let a = sourceBlob "method.rb" (Just Ruby) "def foo; end"
|
2017-05-17 23:34:09 +03:00
|
|
|
blobs `shouldBe` [a]
|
|
|
|
|
2017-05-19 00:04:44 +03:00
|
|
|
it "throws on blank input" $ do
|
2017-05-17 23:34:09 +03:00
|
|
|
h <- openFile "test/fixtures/input/blank.json" ReadMode
|
2017-06-15 17:17:41 +03:00
|
|
|
readBlobsFromHandle h `shouldThrow` (== ExitFailure 1)
|
2017-05-17 23:34:09 +03:00
|
|
|
|
2017-06-15 06:20:12 +03:00
|
|
|
where blobsFromFilePath path = do
|
2017-05-23 21:00:20 +03:00
|
|
|
h <- openFile path ReadMode
|
2017-06-15 17:17:41 +03:00
|
|
|
blobs <- readBlobPairsFromHandle h
|
2017-05-23 21:00:20 +03:00
|
|
|
pure blobs
|
2017-04-21 01:13:28 +03:00
|
|
|
|
2017-06-24 17:15:31 +03:00
|
|
|
data Fixture = Fixture { shas :: Both String, expectedBlobs :: [Both Blob] }
|