File: UtilSpec.hs

package info (click to toggle)
haskell-interpolate 0.2.1-5
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 100 kB
  • sloc: haskell: 435; makefile: 3
file content (57 lines) | stat: -rw-r--r-- 1,809 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
module Data.String.Interpolate.UtilSpec (main, spec) where

import           Prelude ()
import           Prelude.Compat

import           Test.Hspec
import           Test.QuickCheck

import           Data.String.Interpolate.Util

main :: IO ()
main = hspec spec

emptyLine :: Gen String
emptyLine = (++ "\n") <$> listOf (elements " \t")

spec :: Spec
spec = do
  describe "unindent" $ do
    it "removes indentation" $ do
      let xs = "    foo\n  bar\n   baz  \n"
      unindent xs `shouldBe` "  foo\nbar\n baz  \n"

    it "removes the first line of the string if it is empty" $ do
      forAll emptyLine $ \xs -> do
        let ys = "  foo\nbar\n baz\n"
        unindent (xs ++ ys) `shouldBe` ys

    it "does not affect additional empty lines at the beginning" $ do
      unindent "  \n  \nfoo" `shouldBe` "  \nfoo"

    it "empties the last line if it only consists of spaces" $ do
      let xs = "foo\n  "
      unindent xs `shouldBe` "foo\n"

    it "does not affect other whitespace lines at the end" $ do
      unindent "foo\n  \n  " `shouldBe` "foo\n  \n"

    it "disregards empty lines when calculating indentation" $ do
      let xs = "  foo\n\n \n  bar\n"
      unindent xs `shouldBe` "foo\n\n\nbar\n"

    it "correctly handles strings that do not end with a newline" $ do
      let xs = "foo"
      unindent xs `shouldBe` xs

    it "does not affect lines consisting of whitespace (apart from unindenting)" $ do
      unindent " foo\n  \n bar" `shouldBe` "foo\n \nbar"

    it "is total" $ do
      property $ \xs -> length (unindent xs) `shouldSatisfy` (>= 0)

    context "when all lines are empty" $ do
      it "does not unindent at all" $ do
        forAll emptyLine $ \x -> (forAll $ listOf emptyLine) $ \xs -> do
          let ys = concat xs
          unindent (x ++ ys) `shouldBe` ys