Skip to content

Instantly share code, notes, and snippets.

Created December 20, 2009 17:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/260580 to your computer and use it in GitHub Desktop.
Save anonymous/260580 to your computer and use it in GitHub Desktop.
module BsonTest where
import Data.Binary.Put
import Data.Binary.Get
import qualified Data.ByteString.Lazy as L
import qualified Data.ByteString.Char8 as C8
import qualified Data.ByteString.Internal as I
import Data.ByteString hiding (putStrLn,map,zip, filter)
import System.Directory
import Control.Monad
import Debug.Trace
import BSONValues
data TestResult = TestResult { filename :: String, bytes :: L.ByteString, obj :: BSONValue }
main = do loadDir "basic_types" >> loadDir "complex" >> loadDir "single_types"
loadDir n = do paths <- getDirectoryContents n
let files = filter ((isSuffixOf (C8.pack "bson")) . C8.pack) paths
globs <- sequence [ getFile $ mkFile n f | f <- files ]
let results = [ TestResult (mkFile n (fst fb)) (snd fb) (runObjects (snd fb)) | fb <- zip files globs]
mapM testPut results
runObjects b = (runGet getBSONObject) b
getFile f = L.readFile f
mkFile d f = d++"/"++f
putObj :: (FilePath, BSONValue) -> IO ()
putObj (f, o) = do let str = runPut $ putBSONValue o in
L.writeFile (f ++ "2") str
testPut :: TestResult -> IO ()
testPut (TestResult f b o) = let str = (runPut $ putBSONValue o) in
if (str == b) then putStrLn $ f ++ ":: SUCCEEDED"
else putStrLn $ f ++ ":: FAILED"
--rightseq = sequence . map (uncurry (fmap fmap (,)))
--rightseq (bs, io) = do { m <- io; return (bs, m) }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment