Permalink
Browse files

cabal test with hunit

  • Loading branch information...
1 parent 8e6e9cf commit df7b0a2027c3678127b999db562373456945b216 @freizl freizl committed Jun 10, 2012
Showing with 95 additions and 62 deletions.
  1. +9 −0 Makefile
  2. +17 −11 snaplet-oauth.cabal
  3. +2 −2 test/Utils.hs
  4. +3 −22 test/Weibo/Api.hs
  5. +34 −0 test/Weibo/Types.hs
  6. +30 −27 test/unit-test.hs
View
@@ -12,14 +12,23 @@ clean:
conf:
cabal configure
+conf-test:
+ cabal configure --enable-tests
+
build: conf
cabal build
+build-test: conf-test
+ cabal build
+
rebuild: clean build
install: build
cabal install
+test: build-test
+ cabal test
+
test-demo:
cd test/ && runghc snap.hs -b 127.0.0.1 -p 9988
View
@@ -25,23 +25,15 @@ Library
Exposed-modules:
Snap.Snaplet.OAuth
- -- Packages needed in order to build this package.
Build-Depends:
base >= 4 && < 5,
snap >= 0.8 && < 0.9,
snap-core >= 0.8 && < 0.9,
- hoauth2 >= 0.2.2 && < 0.3,
+ hoauth2 == 0.2.2 ,
failure >= 0.1 && < 0.2,
bytestring >= 0.9 && < 1.0,
data-lens-template >= 2.1 && < 2.2,
- data-lens >= 2.0 && < 2.1
-
-
- -- Modules not exported by this package.
- -- Other-modules:
-
- -- Extra tools (e.g. alex, hsc2hs, ...) needed to build the source.
- -- Build-tools:
+ data-lens >= 2.0 && < 2.1
if impl(ghc >= 6.12.0)
ghc-options: -threaded -Wall -fwarn-tabs -funbox-strict-fields -O2
@@ -50,4 +42,18 @@ Library
else
ghc-options: -threaded -Wall -fwarn-tabs -funbox-strict-fields -O2
-fno-warn-orphans
-
+
+Test-suite oauth-unit-tests
+ Type: exitcode-stdio-1.0
+ Hs-source-dirs: test
+ Main-is: unit-test.hs
+ Ghc-options: -Wall
+ Build-depends:
+ HUnit >= 1.2 && < 1.3,
+ aeson >= 0.6 && < 0.7,
+ text >= 0.11 && < 0.12,
+ bytestring-show >= 0.3 && < 0.4,
+ test-framework >= 0.6 && < 0.7,
+ test-framework-hunit >= 0.2 && < 0.3,
+ -- Copied from regular dependencies:
+ base >= 4 && < 5
View
@@ -14,8 +14,8 @@ intToByteString = toStrickBS' . TSB.show
toStrickBS' :: LBS.ByteString -> BS.ByteString
toStrickBS' = BS.concat . LBS.toChunks
-pack' :: String -> BS.ByteString
-pack' = T.encodeUtf8 . T.pack
+sToBS :: String -> BS.ByteString
+sToBS = T.encodeUtf8 . T.pack
lbsToText :: LBS.ByteString -> T.Text
lbsToText = T.decodeUtf8 . toStrickBS'
View
@@ -17,34 +17,15 @@ import Network.HTTP.Conduit
import qualified Data.ByteString.Char8 as BS8
import qualified Data.ByteString.Lazy.Char8 as BSL
import qualified Network.HTTP.Types as HT
-import Control.Monad.Trans (liftIO)
-import Control.Monad.IO.Class (MonadIO)
+--import Control.Monad.Trans (liftIO)
+--import Control.Monad.IO.Class (MonadIO)
import Network.OAuth2.HTTP.HttpClient
import Network.OAuth2.OAuth2
+import Weibo.Types
import Utils
----------------------------------------------------------------
-
-
-accountUidUri :: BS.ByteString
-accountUidUri = pack' "https://api.weibo.com/2/account/get_uid.json"
-
-accountShowUri :: BS.ByteString
-accountShowUri = pack' "https://api.weibo.com/2/users/show.json"
-
-accountStatusUpdate :: String
-accountStatusUpdate ="https://api.weibo.com/2/statuses/update.json"
-
----------------------------------------------------------------
-
--- | UID data type
-data WeiboUserId = WeiboUserId { weiboUserId :: Int } deriving (Show)
-
-instance FromJSON WeiboUserId where
- parseJSON (Object o) = WeiboUserId <$> o .: "uid"
- parseJSON _ = mzero
---------------------------------------------------------------
View
@@ -0,0 +1,34 @@
+
+{-# LANGUAGE OverloadedStrings #-}
+
+
+module Weibo.Types where
+
+import Control.Applicative ((<$>))
+import Control.Monad (mzero)
+import Data.Aeson
+import qualified Data.ByteString as BS
+
+import Utils
+
+---------------------------------------------------------------
+
+
+accountUidUri :: BS.ByteString
+accountUidUri = sToBS "https://api.weibo.com/2/account/get_uid.json"
+
+accountShowUri :: BS.ByteString
+accountShowUri = sToBS "https://api.weibo.com/2/users/show.json"
+
+accountStatusUpdate :: String
+accountStatusUpdate ="https://api.weibo.com/2/statuses/update.json"
+
+
+---------------------------------------------------------------
+
+-- | UID data type
+data WeiboUserId = WeiboUserId { weiboUserId :: Int } deriving (Show, Eq)
+
+instance FromJSON WeiboUserId where
+ parseJSON (Object o) = WeiboUserId <$> o .: "uid"
+ parseJSON _ = mzero
View
@@ -1,42 +1,45 @@
{-# LANGUAGE TemplateHaskell #-}
+{-# LANGUAGE OverloadedStrings #-}
-import Control.Category
-import Prelude hiding ((.))
-import Snap hiding (Response)
-import Network.HTTP.Conduit
-import Control.Monad.Trans (liftIO)
-import Control.Monad.IO.Class (MonadIO)
-import qualified Data.ByteString.Lazy.Char8 as BSL
import Data.Aeson (decode)
-import Test.HUnit
+import Data.Maybe (isJust)
+import qualified Data.ByteString.Lazy.Char8 as BSL
+import Test.Framework (Test, testGroup, defaultMain)
+import Test.Framework.Providers.HUnit (testCase)
+import Test.HUnit ((@?), (@?=))
-import Network.OAuth2.OAuth2
-import Utils
+import Weibo.Types
-data Quux = Quux
+main :: IO ()
+main = testSuits
-data Foo = Foo { _quux :: Quux }
+testSuits :: IO ()
+testSuits = defaultMain
+ [ uidTests
+ ]
-makeLenses [''Foo]
+uidTests :: Test
+uidTests = testGroup "uid test cases"
+ [ testCase "uid shall be 12345" $ getUid @?= (Just aOid)
+ , testCase "uid shall be any number" $ isJust getUid @? "uid any number test"
+ , testCase "uid is not string" $ getInvalidUid @?= Nothing
+ ]
---appQuuxLens :: Lens Foo Quux
---appQuuxLens = quux . snapletValue . Foo
+------------------------------------------------
+invalidUidString :: BSL.ByteString
+invalidUidString = "{\"uid\" : \"12345\" }"
-main :: IO ()
-main = do
- print prop_getUid
- print prop_getInvalidUid
- print $ intToByteString 1234
+getInvalidUid :: Maybe WeiboUserId
+getInvalidUid = decode invalidUidString
-invalidUidString :: BSL.ByteString
-invalidUidString = "{\"uid\" : \"222222\" }"
+------------------------------------------------
-prop_getInvalidUid :: Maybe WeiboUserId
-prop_getInvalidUid = decode invalidUidString
+aOid :: WeiboUserId
+aOid = WeiboUserId 12345
uidString :: BSL.ByteString
-uidString = "{\"uid\" : 222222 }"
+uidString = "{\"uid\" : 12345 }"
-prop_getUid :: Maybe WeiboUserId
-prop_getUid = decode uidString
+getUid :: Maybe WeiboUserId
+getUid = decode uidString

0 comments on commit df7b0a2

Please sign in to comment.