1
1
mirror of https://github.com/github/semantic.git synced 2024-11-28 01:47:01 +03:00
semantic/test/Semantic/IO/Spec.hs

82 lines
3.1 KiB
Haskell
Raw Normal View History

module Semantic.IO.Spec (spec) where
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(..))
2018-03-13 21:10:50 +03:00
import SpecHelpers
spec :: Spec
spec = parallel $ do
describe "readFile" $ do
it "returns a blob for extant files" $ do
2018-04-21 02:27:53 +03:00
Just blob <- readFile (File "semantic.cabal" Nothing)
2018-02-26 21:16:56 +03:00
blobPath blob `shouldBe` "semantic.cabal"
it "throws for absent files" $ do
2018-04-21 02:30:06 +03:00
readFile (File "this file should not exist" Nothing) `shouldThrow` anyIOException
2017-05-17 22:53:05 +03:00
describe "readBlobPairsFromHandle" $ do
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
2018-04-18 23:55:21 +03:00
blobs <- blobsFromFilePath "test/fixtures/cli/diff.json"
2017-12-11 20:09:07 +03:00
blobs `shouldBe` [blobPairDiffing a b]
2017-05-17 22:47:45 +03:00
it "returns blobs when there's no before" $ do
2018-04-18 23:55:21 +03:00
blobs <- blobsFromFilePath "test/fixtures/cli/diff-no-before.json"
2017-12-11 20:09:07 +03:00
blobs `shouldBe` [blobPairInserting b]
it "returns blobs when there's null before" $ do
2018-04-18 23:55:21 +03:00
blobs <- blobsFromFilePath "test/fixtures/cli/diff-null-before.json"
2017-12-11 20:09:07 +03:00
blobs `shouldBe` [blobPairInserting b]
it "returns blobs when there's no after" $ do
2018-04-18 23:55:21 +03:00
blobs <- blobsFromFilePath "test/fixtures/cli/diff-no-after.json"
2017-12-11 20:09:07 +03:00
blobs `shouldBe` [blobPairDeleting a]
it "returns blobs when there's null after" $ do
2018-04-18 23:55:21 +03:00
blobs <- blobsFromFilePath "test/fixtures/cli/diff-null-after.json"
2017-12-11 20:09:07 +03:00
blobs `shouldBe` [blobPairDeleting a]
it "returns blobs for unsupported language" $ do
h <- openFileForReading "test/fixtures/cli/diff-unsupported-language.json"
2017-06-15 17:17:41 +03:00
blobs <- readBlobPairsFromHandle h
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']
it "detects language based on filepath for empty language" $ do
2018-04-18 23:55:21 +03:00
blobs <- blobsFromFilePath "test/fixtures/cli/diff-empty-language.json"
2017-12-11 20:09:07 +03:00
blobs `shouldBe` [blobPairDiffing a b]
it "throws on blank input" $ do
h <- openFileForReading "test/fixtures/cli/blank.json"
2017-06-15 17:17:41 +03:00
readBlobPairsFromHandle h `shouldThrow` (== ExitFailure 1)
2017-05-17 22:47:45 +03:00
it "throws if language field not given" $ do
h <- openFileForReading "test/fixtures/cli/diff-no-language.json"
2017-06-15 17:17:41 +03:00
readBlobsFromHandle h `shouldThrow` (== ExitFailure 1)
it "throws if null on before and after" $ do
h <- openFileForReading "test/fixtures/cli/diff-null-both-sides.json"
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 <- openFileForReading "test/fixtures/cli/parse.json"
2017-06-15 17:17:41 +03:00
blobs <- readBlobsFromHandle h
let a = sourceBlob "method.rb" (Just Ruby) "def foo; end"
2017-05-17 23:34:09 +03:00
blobs `shouldBe` [a]
it "throws on blank input" $ do
h <- openFileForReading "test/fixtures/cli/blank.json"
2017-06-15 17:17:41 +03:00
readBlobsFromHandle h `shouldThrow` (== ExitFailure 1)
2017-05-17 23:34:09 +03:00
where blobsFromFilePath path = do
h <- openFileForReading path
2017-06-15 17:17:41 +03:00
blobs <- readBlobPairsFromHandle h
pure blobs