1
1
mirror of https://github.com/github/semantic.git synced 2024-11-24 08:54:07 +03:00
semantic/test/CommandSpec.hs

83 lines
3.2 KiB
Haskell
Raw Normal View History

module CommandSpec where
2017-04-04 00:39:04 +03:00
import Command
import Data.Functor.Both as Both
2017-04-04 00:39:04 +03:00
import Data.Maybe
import Data.String
import Language
import Prologue hiding (readFile, toList)
2017-04-04 03:09:02 +03:00
import Source
import Test.Hspec hiding (shouldBe, shouldNotBe, shouldThrow, errorCall)
import Test.Hspec.Expectations.Pretty
spec :: Spec
spec = parallel $ do
describe "readFile" $ do
it "returns a blob for extant files" $ do
2017-06-15 17:17:41 +03:00
blob <- readFile "semantic-diff.cabal" Nothing
path blob `shouldBe` "semantic-diff.cabal"
it "returns a nullBlob for absent files" $ do
2017-06-15 17:17:41 +03:00
blob <- readFile "this file should not exist" Nothing
nullBlob blob `shouldBe` True
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
blobs <- blobsFromFilePath "test/fixtures/input/diff.json"
2017-05-17 22:47:45 +03:00
blobs `shouldBe` [both a b]
it "returns blobs when there's no before" $ do
blobs <- blobsFromFilePath "test/fixtures/input/diff-no-before.json"
blobs `shouldBe` [both (emptySourceBlob "method.rb") b]
it "returns blobs when there's null before" $ do
blobs <- blobsFromFilePath "test/fixtures/input/diff-null-before.json"
blobs `shouldBe` [both (emptySourceBlob "method.rb") b]
it "returns blobs when there's no after" $ do
blobs <- blobsFromFilePath "test/fixtures/input/diff-no-after.json"
blobs `shouldBe` [both a (emptySourceBlob "method.rb")]
it "returns blobs when there's null after" $ do
blobs <- blobsFromFilePath "test/fixtures/input/diff-null-after.json"
blobs `shouldBe` [both a (emptySourceBlob "method.rb")]
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
let b' = sourceBlob "test.kt" Nothing "fun main(args: Array<String>) {\nprintln(\"hi\")\n}\n"
blobs `shouldBe` [both (emptySourceBlob "test.kt") b']
it "detects language based on filepath for empty language" $ do
blobs <- blobsFromFilePath "test/fixtures/input/diff-empty-language.json"
blobs `shouldBe` [both a b]
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
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-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
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
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
where blobsFromFilePath path = do
h <- openFile path ReadMode
2017-06-15 17:17:41 +03:00
blobs <- readBlobPairsFromHandle h
pure blobs
data Fixture = Fixture { shas :: Both String, expectedBlobs :: [Both SourceBlob] }