diff options
author | Sam Truzjan <pxqr.sta@gmail.com> | 2013-12-14 21:00:50 +0400 |
---|---|---|
committer | Sam Truzjan <pxqr.sta@gmail.com> | 2013-12-14 21:00:50 +0400 |
commit | 40f95ab06298d1f9d5e0b5d07ca14b9f81264692 (patch) | |
tree | ae1a9cb16cccd787a60129a88ef87c1d3a701dc2 /tests/System/Torrent/StorageSpec.hs | |
parent | 7f263c7569e907dc46d5583ae751a446950b5fdb (diff) |
Add basic tests for storage
Diffstat (limited to 'tests/System/Torrent/StorageSpec.hs')
-rw-r--r-- | tests/System/Torrent/StorageSpec.hs | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/tests/System/Torrent/StorageSpec.hs b/tests/System/Torrent/StorageSpec.hs new file mode 100644 index 00000000..8d9dfd8f --- /dev/null +++ b/tests/System/Torrent/StorageSpec.hs | |||
@@ -0,0 +1,42 @@ | |||
1 | module System.Torrent.StorageSpec (spec) where | ||
2 | import Control.Exception | ||
3 | import System.FilePath | ||
4 | import System.Directory | ||
5 | import System.IO.Unsafe | ||
6 | import Test.Hspec | ||
7 | |||
8 | import Data.Torrent.Layout | ||
9 | import Data.Torrent.Piece | ||
10 | import System.Torrent.Storage | ||
11 | |||
12 | |||
13 | layout :: FileLayout FileSize | ||
14 | layout = | ||
15 | [ (dir </> "_a", 20) | ||
16 | , (dir </> "_b", 50) | ||
17 | , (dir </> "_c", 100) | ||
18 | , (dir </> "_d", 5) | ||
19 | ] | ||
20 | where | ||
21 | dir = unsafePerformIO $ getTemporaryDirectory | ||
22 | |||
23 | createLayout :: IO () | ||
24 | createLayout = | ||
25 | bracket (open ReadWriteEx 0 layout) close (const (return ())) | ||
26 | |||
27 | spec :: Spec | ||
28 | spec = before createLayout $ do | ||
29 | describe "writePiece" $ do | ||
30 | it "should fail gracefully on write operation in RO mode" $ do | ||
31 | s <- open ReadOnly 0 layout | ||
32 | writePiece (Piece 0 "") s `shouldThrow` (== StorageIsRO) | ||
33 | close s | ||
34 | |||
35 | it "should fail on negative index" $ do | ||
36 | withStorage ReadWrite 0 layout $ \ s -> | ||
37 | writePiece (Piece (-1) "") s `shouldThrow` (== InvalidIndex (-1)) | ||
38 | |||
39 | describe "readPiece" $ do | ||
40 | it "should fail on negative index" $ | ||
41 | withStorage ReadOnly 0 layout $ \ s -> | ||
42 | readPiece (-1) s `shouldThrow` (== InvalidIndex (-1)) | ||