{-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE NoImplicitPrelude #-} {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE QuasiQuotes #-} {-# LANGUAGE RecordWildCards #-} {-# LANGUAGE TypeFamilies #-} module Headroom.HeaderSpec ( spec ) where import Headroom.Configuration ( makeHeadersConfig , parseConfiguration ) import Headroom.Configuration.Types ( Configuration(..) , HeaderConfig(..) , HeaderSyntax(..) , HeadersConfig(..) ) import Headroom.Data.Regex ( re ) import Headroom.Embedded ( defaultConfig ) import Headroom.FileSupport ( analyzeSourceCode , fileSupport ) import Headroom.FileSystem ( loadFile ) import Headroom.FileType.Types ( FileType(..) ) import Headroom.Header import Headroom.Header.Types ( HeaderInfo(..) ) import Headroom.SourceCode ( LineType(..) , SourceCode(..) ) import RIO import RIO.FilePath ( () ) import Test.Hspec hiding ( after , before ) spec :: Spec spec = do let samplesDir = "test-data" "code-samples" lHeaderConfig pb pa = HeaderConfig ["hs"] 0 0 0 0 pb pa (LineComment [re|^--|] Nothing) bHeaderConfig = bHeaderConfigM 0 0 0 0 bHeaderConfigM mtc mtf mbc mbf pb pa = HeaderConfig ["hs"] mtc mtf mbc mbf pb pa (BlockComment [re|^{-\||] [re|(? analyzeSourceCode (fileSupport ft) <$> loadFile (samplesDir p) defaultConfig' <- parseConfiguration defaultConfig HeadersConfig {..} <- makeHeadersConfig (cLicenseHeaders defaultConfig') sampleC1 <- loadSample C $ "c" "sample1.c" sampleC2 <- loadSample C $ "c" "sample2.c" sampleCpp1 <- loadSample CPP $ "cpp" "sample1.cpp" sampleCpp2 <- loadSample CPP $ "cpp" "sample2.cpp" sampleCss1 <- loadSample CSS $ "css" "sample1.css" sampleCss2 <- loadSample CSS $ "css" "sample2.css" sampleHs1 <- loadSample Haskell $ "haskell" "sample1.hs" sampleHs2 <- loadSample Haskell $ "haskell" "sample2.hs" sampleHtml1 <- loadSample HTML $ "html" "sample1.html" sampleHtml2 <- loadSample HTML $ "html" "sample2.html" sampleJava1 <- loadSample Java $ "java" "sample1.java" sampleJava2 <- loadSample Java $ "java" "sample2.java" sampleJs1 <- loadSample JS $ "js" "sample1.js" sampleRust1 <- loadSample Rust $ "rust" "sample1.rs" sampleScala1 <- loadSample Scala $ "scala" "sample1.scala" sampleScala2 <- loadSample Scala $ "scala" "sample2.scala" sampleShell1 <- loadSample Shell $ "shell" "sample1.sh" findHeader hscC sampleC1 `shouldBe` Just (1, 3) findHeader hscC sampleC2 `shouldBe` Nothing findHeader hscCpp sampleCpp1 `shouldBe` Just (1, 3) findHeader hscCpp sampleCpp2 `shouldBe` Nothing findHeader hscCss sampleCss1 `shouldBe` Just (1, 4) findHeader hscCss sampleCss2 `shouldBe` Nothing findHeader hscHaskell sampleHs1 `shouldBe` Just (1, 3) findHeader hscHaskell sampleHs2 `shouldBe` Nothing findHeader hscHtml sampleHtml1 `shouldBe` Just (1, 4) findHeader hscHtml sampleHtml2 `shouldBe` Nothing findHeader hscJava sampleJava1 `shouldBe` Just (0, 2) findHeader hscJava sampleJava2 `shouldBe` Nothing findHeader hscJs sampleJs1 `shouldBe` Just (0, 2) findHeader hscRust sampleRust1 `shouldBe` Just (0, 2) findHeader hscScala sampleScala1 `shouldBe` Just (0, 2) findHeader hscScala sampleScala2 `shouldBe` Nothing findHeader hscShell sampleShell1 `shouldBe` Just (2, 3) describe "findBlockHeader" $ do let s = [re|^{-\||] e = [re|(?") , (Code , "->") , (Code , "RESULT") , (Comment, "<-") , (Code , "<-") , (Code , "also some code") ] fstSplit = [[re|->|]] sndSplit = [[re|<-|]] it "handles empty source code and conditions" $ do splitSource [] [] mempty `shouldBe` (mempty, mempty, mempty) it "handles source code and empty conditions" $ do splitSource [] [] sample `shouldBe` (mempty, sample, mempty) it "splits source code with 1st split condition" $ do let before = SourceCode [(Code, "some code"), (Comment, "->"), (Code, "->")] middle = SourceCode [ (Code , "RESULT") , (Comment, "<-") , (Code , "<-") , (Code , "also some code") ] after = mempty expected = (before, middle, after) splitSource fstSplit [] sample `shouldBe` expected it "splits source code with 2nd split condition" $ do let before = mempty middle = SourceCode [ (Code , "some code") , (Comment, "->") , (Code , "->") , (Code , "RESULT") , (Comment, "<-") ] after = SourceCode [(Code, "<-"), (Code, "also some code")] expected = (before, middle, after) splitSource [] sndSplit sample `shouldBe` expected it "splits source code with both conditions" $ do let before = SourceCode [(Code, "some code"), (Comment, "->"), (Code, "->")] middle = SourceCode [(Code, "RESULT"), (Comment, "<-")] after = SourceCode [(Code, "<-"), (Code, "also some code")] expected = (before, middle, after) splitSource fstSplit sndSplit sample `shouldBe` expected it "splits source code when nothing matches the 1st split condition" $ do let sample' = SourceCode [ (Code , "some code") , (Comment, "->") , (Code , "RESULT") , (Comment, "<-") , (Code , "<-") , (Code , "also some code") ] expected = (mempty, sample', mempty) splitSource fstSplit [] sample' `shouldBe` expected it "splits source code when nothing matches the 2nd split condition" $ do let sample' = SourceCode [ (Code , "some code") , (Comment, "->") , (Code , "->") , (Code , "RESULT") , (Comment, "<-") , (Code , "also some code") ] expected = (mempty, sample', mempty) splitSource [] sndSplit sample' `shouldBe` expected it "splits source code when nothing matches both conditions" $ do let sample' = SourceCode [ (Code , "some code") , (Comment, "->") , (Code , "RESULT") , (Comment, "<-") , (Code , "also some code") ] expected = (mempty, sample', mempty) splitSource fstSplit sndSplit sample' `shouldBe` expected it "handles case when 2nd split is found before 1st split" $ do let before = mempty middle = SourceCode [(Code, "some code"), (Comment, "->")] after = SourceCode [ (Code , "->") , (Code , "RESULT") , (Comment, "<-") , (Code , "<-") , (Code , "also some code") ] expected = (before, middle, after) splitSource sndSplit fstSplit sample `shouldBe` expected it "handles case when 1st split is also after 2nd split" $ do let sample' = SourceCode [ (Code , "some code") , (Comment, "->") , (Code , "->") , (Code , "RESULT") , (Comment, "<-") , (Code , "<-") , (Code , "->") , (Code , "also some code") ] before = SourceCode [(Code, "some code"), (Comment, "->"), (Code, "->")] middle = SourceCode [(Code, "RESULT"), (Comment, "<-")] after = SourceCode [(Code, "<-"), (Code, "->"), (Code, "also some code")] expected = (before, middle, after) splitSource fstSplit sndSplit sample' `shouldBe` expected