Browse files

Moved all tests from Data/Object/Yaml.hs to runtests.hs

  • Loading branch information...
1 parent f2610eb commit 225d144cf155e01e2f51fbc0022aa0406fe3ba4b @antage antage committed May 11, 2010
Showing with 65 additions and 91 deletions.
  1. +0 −75 Data/Object/Yaml.hs
  2. +65 −16 runtests.hs
View
75 Data/Object/Yaml.hs
@@ -17,9 +17,6 @@ module Data.Object.Yaml
, encodeFile
, decode
, decodeFile
-#if TEST
- , testSuite
-#endif
) where
import qualified Text.Libyaml as Y
@@ -46,15 +43,6 @@ import qualified Data.Iteratee as I
import Control.Monad.CatchIO hiding (try)
import Prelude hiding (catch)
-#if TEST
-import Test.Framework (testGroup, Test)
-import Test.Framework.Providers.HUnit
---import Test.Framework.Providers.QuickCheck (testProperty)
-import Test.HUnit hiding (Test, path)
---import Test.QuickCheck
-import qualified Data.ByteString.Char8 as B8
-#endif
-
-- | Equality depends on 'value' and 'tag', not 'style'.
data YamlScalar = YamlScalar
{ value :: ByteString
@@ -280,66 +268,3 @@ run' iter = do
Right (Done x _) -> return $ Right x
Right (Cont _ e) -> return $ Left $ InvalidYaml e
-#if TEST
-mkScalar :: String -> YamlScalar
-mkScalar s = YamlScalar (cs s) StrTag Folded
-
-sample :: YamlObject
-sample = Sequence
- [ Scalar $ mkScalar "foo"
- , Mapping
- [ (mkScalar "bar1", Scalar $ mkScalar "bar2")
- ]
- ]
-
-sampleEncoded :: String
-sampleEncoded = "- !!str >-\n foo\n- !!str \"bar1\": !!str >-\n bar2\n"
-
-sampleStr :: Object String String
-sampleStr = mapKeysValues fromYamlScalar fromYamlScalar sample
-
-testSuite :: Test
-testSuite = testGroup "Data.Object.Yaml"
- [ testCase "encode" caseEncode
- , testCase "encode file" caseEncodeFile
- , testCase "encode/decode" caseEncodeDecode
- , testCase "encode/decode file" caseEncodeDecodeFile
- , testCase "encode/decode strings" caseEncodeDecodeStrings
- , testCase "decode invalid file" caseDecodeInvalid
- ]
-
-caseEncode :: Assertion
-caseEncode = do
- let out = encode sample
- B8.unpack out @?= sampleEncoded
-
-caseEncodeFile :: Assertion
-caseEncodeFile = do
- let tmpfile = "tmp.yaml"
- encodeFile tmpfile sample
- out <- B8.readFile tmpfile
- B8.unpack out @?= sampleEncoded
-
-caseEncodeDecode :: Assertion
-caseEncodeDecode = do
- out <- decode $ encode sample
- out @?= sample
-
-caseEncodeDecodeFile :: Assertion
-caseEncodeDecodeFile = do
- let fp = "tmp.yaml"
- encodeFile fp sample
- out <- join $ decodeFile fp
- out @?= sample
-
-caseEncodeDecodeStrings :: Assertion
-caseEncodeDecodeStrings = do
- out <- decode $ encode $ toYamlObject sampleStr
- fromYamlObject out @?= sampleStr
-
-caseDecodeInvalid :: Assertion
-caseDecodeInvalid = do
- let invalid = B8.pack "\tthis is 'not' valid :-)"
- Nothing @=? (decode invalid :: Maybe YamlObject)
-
-#endif
View
81 runtests.hs
@@ -6,20 +6,69 @@ import Test.HUnit hiding (Test)
import Data.Maybe
import qualified Data.ByteString.Char8 as B8
+import qualified Text.Libyaml as LY
+
import Data.Object
-import qualified Data.Object.Yaml as Y
+import Data.Object.Yaml
+
+import Control.Monad
-ys :: (Y.IsYamlScalar a) => a -> Y.YamlScalar
-ys = Y.toYamlScalar
+mkFoldedScalar :: String -> YamlScalar
+mkFoldedScalar s = YamlScalar (cs s) LY.StrTag LY.Folded
+
+mkScalar :: String -> YamlScalar
+mkScalar = toYamlScalar
+
+sample :: YamlObject
+sample = Sequence
+ [ Scalar $ mkFoldedScalar "foo"
+ , Mapping
+ [ (mkFoldedScalar "bar1", Scalar $ mkFoldedScalar "bar2")
+ ]
+ ]
+
+sampleStr :: Object String String
+sampleStr = mapKeysValues fromYamlScalar fromYamlScalar sample
main :: IO ()
main = defaultMain
- [ Y.testSuite
- , testSuite
+ [ testSuite
+ , testSuiteOfAliases
]
testSuite :: Test
-testSuite = testGroup "Tests using samples"
+testSuite = testGroup "Data.Object.Yaml"
+ [ testCase "encode/decode" caseEncodeDecode
+ , testCase "encode/decode file" caseEncodeDecodeFile
+ , testCase "encode/decode strings" caseEncodeDecodeStrings
+ , testCase "decode invalid file" caseDecodeInvalid
+ ]
+
+caseEncodeDecode :: Assertion
+caseEncodeDecode = do
+ out <- decode $ encode sample
+ out @?= sample
+
+caseEncodeDecodeFile :: Assertion
+caseEncodeDecodeFile = do
+ let fp = "tmp.yaml"
+ encodeFile fp sample
+ out <- join $ decodeFile fp
+ out @?= sample
+
+caseEncodeDecodeStrings :: Assertion
+caseEncodeDecodeStrings = do
+ out <- decode $ encode $ toYamlObject sampleStr
+ fromYamlObject out @?= sampleStr
+
+caseDecodeInvalid :: Assertion
+caseDecodeInvalid = do
+ let invalid = B8.pack "\tthis is 'not' valid :-)"
+ Nothing @=? (decode invalid :: Maybe YamlObject)
+
+
+testSuiteOfAliases :: Test
+testSuiteOfAliases = testGroup "Tests of aliases"
[ testCase "simple scalar alias" caseSimpleScalarAlias
, testCase "simple sequence alias" caseSimpleSequenceAlias
, testCase "simple mapping alias" caseSimpleMappingAlias
@@ -30,50 +79,50 @@ testSuite = testGroup "Tests using samples"
caseSimpleScalarAlias :: Assertion
caseSimpleScalarAlias = do
- let maybeRes = Y.decode yamlBS :: Maybe (Y.YamlObject)
+ let maybeRes = decode yamlBS :: Maybe YamlObject
isJust maybeRes @? "decoder should return Just YamlObject but returned Nothing"
let res = fromJust maybeRes
- res @?= Sequence [Scalar (ys "foo"), Scalar (ys "baz"), Scalar (ys "foo")]
+ res @?= Sequence [Scalar (mkScalar "foo"), Scalar (mkScalar "baz"), Scalar (mkScalar "foo")]
where yamlString = "- &anch foo\n- baz\n- *anch"
yamlBS = B8.pack yamlString
caseSimpleSequenceAlias :: Assertion
caseSimpleSequenceAlias = do
- let maybeRes = Y.decode yamlBS :: Maybe (Y.YamlObject)
+ let maybeRes = decode yamlBS :: Maybe YamlObject
isJust maybeRes @? "decoder should return Just YamlObject but returned Nothing"
let res = fromJust maybeRes
- res @?= Mapping [(ys "seq", Sequence [Scalar (ys "foo"), Scalar (ys "baz")]), (ys "seq2", Sequence [Scalar (ys "foo"), Scalar (ys "baz")])]
+ res @?= Mapping [(mkScalar "seq", Sequence [Scalar (mkScalar "foo"), Scalar (mkScalar "baz")]), (mkScalar "seq2", Sequence [Scalar (mkScalar "foo"), Scalar (mkScalar "baz")])]
where yamlString = "seq: &anch\n - foo\n - baz\nseq2: *anch"
yamlBS = B8.pack yamlString
caseSimpleMappingAlias :: Assertion
caseSimpleMappingAlias = do
- let maybeRes = Y.decode yamlBS :: Maybe (Y.YamlObject)
+ let maybeRes = decode yamlBS :: Maybe YamlObject
isJust maybeRes @? "decoder should return Just YamlObject but returned Nothing"
let res = fromJust maybeRes
- res @?= Mapping [(ys "map", Mapping [(ys "key1", Scalar (ys "foo")), (ys "key2", Scalar (ys "baz"))]), (ys "map2", Mapping [(ys "key1", Scalar (ys "foo")), (ys "key2", Scalar (ys "baz"))])]
+ res @?= Mapping [(mkScalar "map", Mapping [(mkScalar "key1", Scalar (mkScalar "foo")), (mkScalar "key2", Scalar (mkScalar "baz"))]), (mkScalar "map2", Mapping [(mkScalar "key1", Scalar (mkScalar "foo")), (mkScalar "key2", Scalar (mkScalar "baz"))])]
where yamlString = "map: &anch\n key1: foo\n key2: baz\nmap2: *anch"
yamlBS = B8.pack yamlString
caseMappingAliasBeforeAnchor :: Assertion
caseMappingAliasBeforeAnchor = do
- let res = Y.decode yamlBS :: Maybe (Y.YamlObject)
+ let res = decode yamlBS :: Maybe YamlObject
isNothing res @? "decode should return Nothing due to unknown alias"
where yamlString = "map: *anch\nmap2: &anch\n key1: foo\n key2: baz"
yamlBS = B8.pack yamlString
caseMappingAliasInsideAnchor :: Assertion
caseMappingAliasInsideAnchor = do
- let res = Y.decode yamlBS :: Maybe (Y.YamlObject)
+ let res = decode yamlBS :: Maybe YamlObject
isNothing res @? "decode should return Nothing due to unknown alias"
where yamlString = "map: &anch\n key1: foo\n key2: *anch"
yamlBS = B8.pack yamlString
caseScalarAliasOverriding :: Assertion
caseScalarAliasOverriding = do
- let maybeRes = Y.decode yamlBS :: Maybe (Y.YamlObject)
+ let maybeRes = decode yamlBS :: Maybe YamlObject
isJust maybeRes @? "decoder should return Just YamlObject but returned Nothing"
let res = fromJust maybeRes
- res @?= Sequence [Scalar (ys "foo"), Scalar (ys "baz"), Scalar (ys "foo"), Scalar (ys "boo"), Scalar (ys "buz"), Scalar (ys "boo")]
+ res @?= Sequence [Scalar (mkScalar "foo"), Scalar (mkScalar "baz"), Scalar (mkScalar "foo"), Scalar (mkScalar "boo"), Scalar (mkScalar "buz"), Scalar (mkScalar "boo")]
where yamlString = "- &anch foo\n- baz\n- *anch\n- &anch boo\n- buz\n- *anch"
yamlBS = B8.pack yamlString

0 comments on commit 225d144

Please sign in to comment.