{-# LANGUAGE QuasiQuotes #-}-- | The tests of the quasiquoter 'sqlText', and of the layout of its-- quotations, which 'unquoteSqlText' reads.module Database.DagMigrations.PostgreSQL.SqlTextSpec (spec) whereimport Data.List qualified as Listimport Data.Text (Text)import Data.Text qualified as Textimport Database.DagMigrations.PostgreSQL.SqlText (SqlTextError (..), sqlText, unquoteSqlText)import Hedgehog (Gen)import Hedgehog.Gen qualified as Genimport Hedgehog.Range qualified as Rangeimport Test.Hspecimport Test.Hspec.Hedgehog (forAll, hedgehog, (===))spec :: Specspec = describe "sqlText" $ do it "gives the quoted lines without their shared indentation, each ending in a line feed" $ [sqlText| CREATE TABLE saved_views ( name text PRIMARY KEY, definition text NOT NULL DEFAULT '{"version": 1}' ); COMMENT ON TABLE saved_views IS 'a \n stays a backslash and an n'; |] `shouldBe` ( "CREATE TABLE saved_views (\n name text PRIMARY KEY,\n definition text NOT NULL DEFAULT '{\"version\": 1}'\n);\n\n" <> "COMMENT ON TABLE saved_views IS 'a \\n stays a backslash and an n';\n" ) it "gives empty text for a quotation with no line between the opening line and the closing line" $ unquoteSqlText "\n " `shouldBe` Right "" it "removes only the indentation that every line holds, and ignores the indentation of the closing line" $ unquoteSqlText "\n SELECT\n 1\n " `shouldBe` Right "SELECT\n 1\n" it "keeps the spaces at the end of a line" $ unquoteSqlText "\n SELECT 1 \n " `shouldBe` Right "SELECT 1 \n" it "gives an empty line for a line of spaces shorter than the indentation, and keeps the spaces beyond it" $ unquoteSqlText "\n SELECT 1;\n \n\n \n SELECT 2;\n " `shouldBe` Right "SELECT 1;\n\n\n \nSELECT 2;\n" it "gives an empty line for each line when no line holds more than spaces" $ unquoteSqlText "\n \n\n " `shouldBe` Right "\n\n" it "counts a tab as a character of the line, not as indentation" $ unquoteSqlText "\n SELECT 1;\n\tSELECT 2;\n " `shouldBe` Right " SELECT 1;\n\tSELECT 2;\n" it "treats a carriage return before a line feed as part of the line break" $ unquoteSqlText "\r\n SELECT 1;\r\n SELECT 2;\r\n " `shouldBe` Right "SELECT 1;\nSELECT 2;\n" it "accepts spaces after the opening of the quotation" $ unquoteSqlText " \n SELECT 1;\n " `shouldBe` Right "SELECT 1;\n" it "refuses a quotation without a line break" $ unquoteSqlText " SELECT 1; " `shouldBe` Left SqlTextOnOneLine it "refuses SQL on the line of the opening" $ unquoteSqlText " SELECT 1;\n " `shouldBe` Left SqlTextAfterOpening it "refuses SQL on the line of the closing" $ unquoteSqlText "\n SELECT 1;\n SELECT 2; " `shouldBe` Left SqlTextBeforeClosing it "gives back text whose lines end in line feeds, when one of its lines starts with a character other than a space" $ hedgehog $ do indentation <- forAll (Gen.int (Range.linear 0 16)) flushLine <- forAll (Text.cons <$> Gen.filter (/= ' ') lineCharacter <*> lineText) otherLines <- forAll (Gen.list (Range.linear 0 12) lineText) position <- forAll (Gen.int (Range.linear 0 (length otherLines))) let (linesBefore, linesAfter) = List.splitAt position otherLines sqlLines = linesBefore <> [flushLine] <> linesAfter indent line = if Text.null line then line else Text.replicate indentation " " <> line quotation = Text.intercalate "\n" ([" "] <> map indent sqlLines <> [Text.replicate indentation " "]) unquoteSqlText (Text.unpack quotation) === Right (foldMap (<> "\n") sqlLines)-- | A character of a line of SQL: anything but a line feed or a carriage-- return, with spaces, tabs, quotation marks, backslashes and the-- characters of @|]@ often.lineCharacter :: Gen CharlineCharacter = Gen.frequency [ (3, Gen.element [' ', '\t', '|', ']', '\\', '"', '\'']), (10, Gen.alphaNum), (1, Gen.filter (`notElem` ['\n', '\r']) Gen.unicode) ]-- | The text of a line of SQL.lineText :: Gen TextlineText = Gen.text (Range.linear 0 40) lineCharacter