Added Map instance. Added tests for StrMap and Map instances.

This commit is contained in:
Dominick Gendill 2017-05-09 18:23:42 -06:00
parent ae9461cbc6
commit 75083ae70f
3 changed files with 53 additions and 5 deletions

View File

@ -1,12 +1,13 @@
module Data.YAML.Foreign.Encode where
import Data.Map as M
import Data.Map (Map)
import Data.StrMap as StrMap
import Data.StrMap (StrMap)
import Data.Array (toUnfoldable)
import Data.Function.Uncurried (Fn4, runFn4)
import Data.List (List)
import Data.Maybe (Maybe, maybe)
import Data.StrMap (StrMap, insert)
import Data.Tuple (Tuple(..), fst, snd)
import Prelude (class Eq, class Show, map, show, ($), (<>), (==), (<<<))
import Unsafe.Coerce (unsafeCoerce)
@ -49,6 +50,9 @@ class ToYAML a where
instance strMapToYAML :: (ToYAML a) => ToYAML (StrMap a) where
toYAML strMap = YObject $ StrMap.fold (\acc key value -> M.insert key (toYAML value) acc) M.empty strMap
instance mapToYAML :: (ToYAML a) => ToYAML (Map String a) where
toYAML m = YObject $ M.mapWithKey (\key value -> toYAML value) m
instance booleanToYAML :: ToYAML Boolean where
toYAML = YBoolean

View File

@ -1,8 +1,8 @@
module Test.Instances where
import Prelude (class Eq, class Show, bind, pure, ($), (=<<), (<$>), map, (<=<))
import Prelude (class Eq, class Show, bind, pure, ($), (=<<))
import Data.Traversable (traverse)
import Data.Foreign (readArray, readNumber, readString, readInt, F, Foreign, ForeignError(..), fail, readString)
import Data.Foreign (readArray, readNumber, readString, readInt, F, Foreign, ForeignError(..), fail)
import Data.Foreign.Index (readProp)
import Data.Generic (class Generic, gShow, gEq)
import Data.YAML.Foreign.Encode

View File

@ -14,6 +14,10 @@ import Test.Spec.Assertions (shouldEqual)
import Test.Spec.Reporter.Console (consoleReporter)
import Test.Spec.Runner (RunnerEffects, run)
import Control.Monad.Eff.Console (log, CONSOLE)
import Data.Map as Map
import Data.Map (Map)
import Data.StrMap as StrMap
import Data.StrMap (StrMap)
yamlInput :: String
yamlInput = """
@ -67,6 +71,40 @@ yamlOutput = """- Mobility: Fix
Scale: 1
"""
yamlMapOutput :: String
yamlMapOutput = """key:
- Mobility: Fix
Points:
- X: 10
'Y': 10
- X: 20
'Y': 10
- X: 5
'Y': 5
Coverage: 10
Name: House
Scale: 9.5
- Mobility: Fix
Points:
- X: 1
'Y': 1
- X: 2
'Y': 2
- X: 0
'Y': 0
Coverage: 10
Name: Tree
Scale: 1
"""
testStrMap :: StrMap (Array GeoObject)
testStrMap = StrMap.singleton "key" parsedData
testMap :: Map String (Array GeoObject)
testMap = Map.singleton "key" parsedData
parsedData :: Array GeoObject
parsedData =
[ GeoObject
@ -87,7 +125,7 @@ parsedData =
main :: Eff (RunnerEffects ()) Unit
main = run [consoleReporter] do
void $ describe "purescript-yaml" do
describe "purescript-yaml" do
describe "decode" do
it "Decodes YAML" do
let decoded =
@ -95,7 +133,13 @@ main = run [consoleReporter] do
readArray >>=
traverse readGeoObject
(runExcept decoded) `shouldEqual` (Right parsedData)
void $ describe "encode" do
describe "encode" do
it "Encodes YAML" $ do
let encoded = printYAML parsedData
encoded `shouldEqual` yamlOutput
let encodedStrMap = printYAML testStrMap
encodedStrMap `shouldEqual` yamlMapOutput
let encodedMap = printYAML testMap
encodedMap `shouldEqual` yamlMapOutput