Haskell Behavior Driven Development 2012.5.27 @kazu_yamamoto 1
Haskell 4 Mew Firemacs Mighty ghc-mod 2
Ruby/Java HackageDB 3
Haskeller 4
Haskeller 5
Q) Haskeller A) 6
7
Haskeller Haskell 8
9
10
Haskell 11
Haskell Haskell 12
13
Haskell 14
Haskell 15
Haskell 16
\^^/ 17
18
19
Haskeller HUnit QuickCheck 20
QuickCheck prop_doublesort :: [Int] -> Bool prop_doublesort xs = sort xs == sort (sort xs) > quickcheck prop_doublesort +++ OK, passed 100 tests. 21
22
Haskell QuickCheck sort :: Ord a => [a] -> [a] lookup :: Eq a => a -> [(a, b)] -> Maybe b delete :: Eq a => a -> [a] -> [a] HUnit ( ) hgetline :: Handle -> IO String writefile :: FilePath -> String -> IO () forkio :: IO () -> IO ThreadId 23
7 1946 12 24
25
26
QuickCheck QuickCheck prop_model x xs = linearsearch x xs == binarysearch x xs 27
QuickCheck 28
Haskeller 29
Simon HENGEL doctest 30
doctest Python def factorial(n): """Return the factorial of n, an exact integer >= 0. If the result is small enough to fit in an int, return an int. Else return a long. >>> [factorial(n) for n in range(6)] [1, 1, 2, 6, 24, 120] >>> factorial(30) 265252859812191058636308480000000L """ import math if not n >= 0: raise ValueError("n must be >= 0")... 31
Haskell Haddock 32
Haddock -- unlines is an inverse operation to lines. -- It joins lines, after appending a terminating -- newline to each. unlines :: [String] -> String unlines [] = [] unlines (l:ls) = l ++ \n : unlines ls Haddock HTML 33
">" Data.Map -- /O(1)/. Is the map empty? -- -- > Data.Map.null (empty) == True -- > Data.Map.null (singleton 1 a ) == False null :: Map k a -> Bool null Tip = True null (Bin _) = False 34
35
Simon HENGEL ">>>" >>> length [] 0 >>> writefile "tmpfile" "Hello" >>> readfile "tmpfile" "Hello" >>> head [] *** Exception: Prelude.head: empty list Haddock 2.8 36
doctest Haddock 0.5.2 Haddock API 0.6 GHC API GHCi doctest 0.6.x GHCi 0.7 GHCi 37
"prop>" prop> Data.Map.null empty == True prop> \xs -> sort xs == sort (sort (xs::[int])) prop> sort xs == sort (sort (xs::[int])) Haddock 38
haskell-src-exts... GHCi > sort xs == sort (sort (xs::[int])) <interactive>:1:6: Not in scope: xs <interactive>:1:24: Not in scope: xs "\xs -> " 39
doctest doctest 40
v2.3 QuickCheck > quickcheck $ length [] == 0 +++ OK, passed 100 tests. True 100 v2.4 > quickcheck $ length [] == 0 +++ OK, passed 1 tests. 41
/ > valid $ deletemin $ deletemin $ fromlist [(i,()) i <- [0,2,5,1,6,4,8,9,7,11,10,3]] False 42
Trystan SPANGLER Hspec 43
Q) Hspec A) Ruby Rspec Haskell Rspec BDD 44
Q) BDD (Behavior Driven Development) A) TDD (Test Driven Development) 45
Haskell 46
Hspec describe "deletemin" $ do it "maintains the balance even if applied doubly" $ valid $ deletemin $ deletemin $ fromlist [(i::int,()) i <- [0,2,5,1,6,4,8,9,7,11,10,3]] it... it... it... 47
IO setup teardown withdb action = bracket (connectsqlite3 "my.db") disconnect action describe "database adaptor" $ do it "returns 23, when 23 is selected" $ withdb $ \connection -> run connection "select 23;" shouldreturn 23 48
Haskell Behavior Driven Development 49
Haskell BDD (1) (1) rev :: [a] -> [a] rev = undefined undefined undefined undefined placeholders notimplemented todo 50
Haskell BDD (2) (2) Haddock -- -- rev @xs@ returns the elements of @xs@ -- in reverse order. @xs@ must be finite. -- -- >>> rev [1,2,3] -- [3,2,1] -- -- prop> rev [] == rev [] -- prop> rev (xs++ys) == rev ys ++ rev (xs::[int]) -- prop> rev (rev xs) == (xs::[int]) rev :: [a] -> [a] rev = undefined 51
Haskell BDD (3) (3) Hspec describe "rev" $ do it "returns the first element in the last" $ property $ \xs -> not (null xs) ==> head (rev xs) == last (xs :: [Int]) it "returns the last element in the first" $ property $ \xs -> not (null xs) ==> last (rev xs) == head (xs :: [Int]) 52
(4) Haskell BDD (4) rev :: [a] -> [a] rev = foldl (flip (:)) [] doctest Hspec Cabal cabal test test-suite doctests type: exitcode-stdio-1.0 main-is: doctests.hs build-depends: base, doctest test-suite spec type: exitcode-stdio-1.0 main-is: Spec.hs build-depends: base, hspec 53
rev = foldl (flip (:)) [] ghc-mod rev :: [a] -> [a] rev = foldl (flip (:)) [] -- -- >>> rev [1,2,3] -- [3,2,1] rev :: [a] -> [a] rev = foldl (flip (:)) [] 54
QuickCheck doctest Hspec ( ) Cabal 55
Simon HENGEL (doctest Hspec ) Hspec github issue 56