on master help sign in
Merge branch 'master' into webgl
2d75c39John P Mayer Jr
Elm-Test+1−1

Submodule changed.

IO+1−1

Submodule changed.

automaton+1−1

Submodule changed.

libraries/Array.elm+30−17
@@ -11,7 +11,7 @@ reads, updates, and appends.
11@docs length, push, append
11@docs length, push, append
12
12
13# Get and Set
13# Get and Set
14−@docs get, getMaybe, getWithDefault, set
14+@docs get, getSafe, getUnsafe, set
15
15
16# Taking Arrays Apart
16# Taking Arrays Apart
17@docs toList, toIndexedList, slice
17@docs toList, toIndexedList, slice
@@ -82,20 +82,30 @@ map = Native.Array.map
82indexedMap : (Int -> a -> b) -> Array a -> Array b
82indexedMap : (Int -> a -> b) -> Array a -> Array b
83indexedMap = Native.Array.indexedMap
83indexedMap = Native.Array.indexedMap
84
84
85−{-| Reduce an array from the left.
85+{-| Reduce an array from the left. Read `foldl` as “fold from the left”.
86
86
87 foldl (::) [] (fromList [1,2,3]) == [3,2,1]
87 foldl (::) [] (fromList [1,2,3]) == [3,2,1]
88-}
88-}
89foldl : (a -> b -> b) -> b -> Array a -> b
89foldl : (a -> b -> b) -> b -> Array a -> b
90foldl = Native.Array.foldl
90foldl = Native.Array.foldl
91
91
92−{-| Reduce an array from the right.
92+{-| Reduce an array from the right. Read `foldr` as “fold from the right”.
93
93
94 foldr (+) 0 (repeat 3 5) == 15
94 foldr (+) 0 (repeat 3 5) == 15
95-}
95-}
96foldr : (a -> b -> b) -> b -> Array a -> b
96foldr : (a -> b -> b) -> b -> Array a -> b
97foldr = Native.Array.foldr
97foldr = Native.Array.foldr
98
98
99+{-| Keep only elements that satisfy the predicate:
100+
101+ filter isEven (fromList [1..6]) == (fromList [2,4,6])
102+-}
103+filter : (a -> Bool) -> Array a -> Array a
104+filter isOkay arr =
105+ let update x xs = if isOkay x then Native.Array.push x xs else xs
106+ in
107+ Native.Array.foldl update Native.Array.empty arr
108+
99{-| Return an empty array.
109{-| Return an empty array.
100
110
101 length empty == 0
111 length empty == 0
@@ -110,22 +120,25 @@ empty = Native.Array.empty
110push : a -> Array a -> Array a
120push : a -> Array a -> Array a
111push = Native.Array.push
121push = Native.Array.push
112
122
113−{-| Return the element at the index. Breaks, if index is out of range. If the
123+{-| Get the element at a particular index.
114−array length is unkown, use withDefaultGet oder getWithDefault.
124+
125+ getUnsafe 2 (A.fromList [3,2,1]) == 1
115
126
116− get 2 (A.fromList [3,2,1]) == 1
127+Warning: this function will result in a runtime error if the index is not found,
128+so it is best to use `get` or `getSafe` unless you are very confident the index
129+will be found.
117-}
130-}
118−get : Int -> Array a -> a
131+getUnsafe : Int -> Array a -> a
119−get = Native.Array.get
132+getUnsafe = Native.Array.get
120
133
121{-| Return Just the element at the index or Nothing if the index is out of range.
134{-| Return Just the element at the index or Nothing if the index is out of range.
122
135
123− getMaybe 2 (fromList [3,2,1]) == Just 2
136+ get 2 (fromList [3,2,1]) == Just 2
124− getMaybe 5 (fromList [3,2,1]) == Nothing
137+ get 5 (fromList [3,2,1]) == Nothing
125− getMaybe -1 (fromList [3,2,1]) == Nothing
138+ get -1 (fromList [3,2,1]) == Nothing
126-}
139-}
127−getMaybe : Int -> Array a -> Maybe a
140+get : Int -> Array a -> Maybe a
128−getMaybe i array =
141+get i array =
129 if 0 <= i && i < Native.Array.length array
142 if 0 <= i && i < Native.Array.length array
130 then Just (Native.Array.get i array)
143 then Just (Native.Array.get i array)
131 else Nothing
144 else Nothing
@@ -133,11 +146,11 @@ getMaybe i array =
133{-| Get the element at the index. If the index is out of range, the given default
146{-| Get the element at the index. If the index is out of range, the given default
134element is returned.
147element is returned.
135
148
136− getWithDefault 0 2 (fromList [3,2,1]) == 1
149+ getSafe 0 2 (fromList [3,2,1]) == 1
137− getWithDefault 0 5 (fromList [3,2,1]) == 0
150+ getSafe 0 5 (fromList [3,2,1]) == 0
138-}
151-}
139−getWithDefault : a -> Int -> Array a -> a
152+getSafe : a -> Int -> Array a -> a
140−getWithDefault default i array =
153+getSafe default i array =
141 if 0 <= i && i < Native.Array.length array
154 if 0 <= i && i < Native.Array.length array
142 then Native.Array.get i array
155 then Native.Array.get i array
143 else default
156 else default
libraries/Dict.elm+76−18
@@ -1,6 +1,8 @@
1module Dict (empty,singleton,insert,update
1module Dict (empty,singleton,insert,update
2− ,lookup,findWithDefault
2+ ,get,getSafe,getUnsafe
3 ,remove,member
3 ,remove,member
4+ ,filter
5+ ,partition
4 ,foldl,foldr,map
6 ,foldl,foldr,map
5 ,union,intersect,diff
7 ,union,intersect,diff
6 ,keys,values
8 ,keys,values
@@ -17,7 +19,7 @@ Insert, remove, and query operations all take *O(log n)* time.
17@docs empty, singleton, insert, update, remove
19@docs empty, singleton, insert, update, remove
18
20
19# Query
21# Query
20−@docs member, lookup, findWithDefault
22+@docs member, get, getMaybe, getWithDefault
21
23
22# Combine
24# Combine
23@docs union, intersect, diff
25@docs union, intersect, diff
@@ -26,7 +28,7 @@ Insert, remove, and query operations all take *O(log n)* time.
26@docs keys, values, toList, fromList
28@docs keys, values, toList, fromList
27
29
28# Transform
30# Transform
29−@docs map, foldl, foldr
31+@docs map, foldl, foldr, filter, partition
30
32
31-}
33-}
32
34
@@ -84,33 +86,74 @@ max t =
84 RBNode _ _ _ _ r -> max r
86 RBNode _ _ _ _ r -> max r
85 RBEmpty _ -> Native.Error.raise "(max Empty) is not defined"
87 RBEmpty _ -> Native.Error.raise "(max Empty) is not defined"
86
88
87−{-| Lookup the value associated with a key. -}
89+{-| Get the value associated with a key. If the key is not found, return
90+`Nothing`. This is useful when you are not sure if a key will be in the
91+dictionary.
92+
93+ animals = fromList [ ("Tom", Cat), ("Jerry", Mouse) ]
94+
95+ get "Tom" animals == Just Cat
96+ get "Mouse" animals == Just Mouse
97+ get "Spike" animals == Nothing
98+
99+The `getSafe` and `getUnsafe` are built-in ways to handle common ways of
100+using the resulting `Maybe`.
101+-}
88−lookup : comparable -> Dict comparable v -> Maybe v
102+get : comparable -> Dict comparable v -> Maybe v
89−lookup k t =
103+get k t =
90 case t of
104 case t of
91 RBEmpty LBlack -> Nothing
105 RBEmpty LBlack -> Nothing
92 RBNode _ k' v l r ->
106 RBNode _ k' v l r ->
93 case Native.Utils.compare k k' of
107 case Native.Utils.compare k k' of
94− LT -> lookup k l
108+ LT -> get k l
95 EQ -> Just v
109 EQ -> Just v
96− GT -> lookup k r
110+ GT -> get k r
111+
112+{-| Get the value associated with a key. If the key is not found,
113+return a default value.
97
114
98−{-| Find the value associated with a key. If the key is not found,
115+ animals = fromList [ ("Tom", Cat), ("Jerry", Mouse) ]
99−return the default value. -}
116+
117+ getSafe Dog "Tom" animals == Cat
118+ getSafe Dog "Mouse" animals == Mouse
119+ getSafe Dog "Spike" animals == Dog
120+-}
100−findWithDefault : v -> comparable -> Dict comparable v -> v
121+getSafe : v -> comparable -> Dict comparable v -> v
101−findWithDefault base k t =
122+getSafe base k t =
102 case t of
123 case t of
103 RBEmpty LBlack -> base
124 RBEmpty LBlack -> base
104 RBNode _ k' v l r ->
125 RBNode _ k' v l r ->
105 case Native.Utils.compare k k' of
126 case Native.Utils.compare k k' of
106− LT -> findWithDefault base k l
127+ LT -> getSafe base k l
107 EQ -> v
128 EQ -> v
108− GT -> findWithDefault base k r
129+ GT -> getSafe base k r
130+
131+{-| Get the value associated with a key.
132+
133+ animals = fromList [ ("Tom", Cat), ("Jerry", Mouse) ]
134+
135+ get "Tom" animals == Cat
136+ get "Mouse" animals == Mouse
137+ get "Spike" animals == -- Runtime Error!
138+
139+Warning: this function will result in a runtime error if the key is not found,
140+so it is best to use `get` or `getSafe` unless you are very confident the key
141+will be found.
142+-}
143+getUnsafe : comparable -> Dict comparable v -> v
144+getUnsafe k t =
145+ case t of
146+ RBEmpty LBlack -> Native.Error.raise "key not found when using 'getUnsafe'"
147+ RBNode _ k' v l r ->
148+ case Native.Utils.compare k k' of
149+ LT -> getUnsafe k l
150+ EQ -> v
151+ GT -> getUnsafe k r
109
152
110{-| Determine if a key is in a dictionary. -}
153{-| Determine if a key is in a dictionary. -}
111member : comparable -> Dict comparable v -> Bool
154member : comparable -> Dict comparable v -> Bool
112-- Does t contain k?
155-- Does t contain k?
113−member k t = isJust <| lookup k t
156+member k t = isJust <| get k t
114
157
115ensureBlackRoot : Dict k v -> Dict k v
158ensureBlackRoot : Dict k v -> Dict k v
116ensureBlackRoot t =
159ensureBlackRoot t =
@@ -327,9 +370,7 @@ union t1 t2 = foldl insert t2 t1
327{-| Keep a key-value pair when its key appears in the second dictionary.
370{-| Keep a key-value pair when its key appears in the second dictionary.
328Preference is given to values in the first dictionary. -}
371Preference is given to values in the first dictionary. -}
329intersect : Dict comparable v -> Dict comparable v -> Dict comparable v
372intersect : Dict comparable v -> Dict comparable v -> Dict comparable v
330−intersect t1 t2 =
373+intersect t1 t2 = filter (\k _ -> k `member` t2) t1
331− let combine k v t = if k `member` t2 then insert k v t else t
332− in foldl combine empty t1
333
374
334{-| Keep a key-value pair when its key does not appear in the second dictionary.
375{-| Keep a key-value pair when its key does not appear in the second dictionary.
335Preference is given to the first dictionary. -}
376Preference is given to the first dictionary. -}
@@ -351,3 +392,20 @@ toList t = foldr (\k v acc -> (k,v) :: acc) [] t
351{-| Convert an association list into a dictionary. -}
392{-| Convert an association list into a dictionary. -}
352fromList : [(comparable,v)] -> Dict comparable v
393fromList : [(comparable,v)] -> Dict comparable v
353fromList assocs = List.foldl (\(k,v) d -> insert k v d) empty assocs
394fromList assocs = List.foldl (\(k,v) d -> insert k v d) empty assocs
395+
396+{-| Keep a key-value pair when it satisfies a predicate. -}
397+filter : (comparable -> v -> Bool) -> Dict comparable v -> Dict comparable v
398+filter p dict =
399+ let add k v t = if p k v then insert k v t else t
400+ in foldl add empty dict
401+
402+{-| Partition a dictionary according to a predicate. The first dictionary
403+contains all key-value pairs which satisfy the predicate, and the second
404+contains the rest.
405+-}
406+partition : (comparable -> v -> Bool) -> Dict comparable v -> (Dict comparable v, Dict comparable v)
407+partition p dict =
408+ let add k v (t1, t2) = if p k v
409+ then (insert k v t1, t2)
410+ else (t1, insert k v t2)
411+ in foldl add (empty, empty) dict
libraries/List.elm+7−1
@@ -163,7 +163,13 @@ maximum = foldl1 max
163minimum : [comparable] -> comparable
163minimum : [comparable] -> comparable
164minimum = foldl1 min
164minimum = foldl1 min
165
165
166−{-| Split a list based on the predicate. -}
166+{-| Partition a list based on a predicate. The first list contains all values
167+that satisfy the predicate, and the second list contains all the value that do
168+not.
169+
170+ partition (\x -> x < 3) [0..5] == ([0,1,2], [3,4,5])
171+ partition isEven [0..5] == ([0,2,4], [1,3,5])
172+-}
167partition : (a -> Bool) -> [a] -> ([a],[a])
173partition : (a -> Bool) -> [a] -> ([a],[a])
168partition pred lst =
174partition pred lst =
169 case lst of
175 case lst of
libraries/Native/Array.js+2−2
@@ -242,14 +242,14 @@ Elm.Native.Array.make = function(elm) {
242 }
242 }
243
243
244 function foldl(f, b, a) {
244 function foldl(f, b, a) {
245− for (var i = a.table.length - 1; i >= 0; i--) {
245+ for (var i = 0; i < a.table.length; i++) {
246 b = A2(f, a.height == 0 ? a.table[i] : foldl(f, b, a.table[i]), b);
246 b = A2(f, a.height == 0 ? a.table[i] : foldl(f, b, a.table[i]), b);
247 }
247 }
248 return b;
248 return b;
249 }
249 }
250
250
251 function foldr(f, b, a) {
251 function foldr(f, b, a) {
252− for (var i = 0; i < a.table.length; i++) {
252+ for (var i = a.table.length; i--; ) {
253 b = A2(f, a.height == 0 ? a.table[i] : foldr(f, b, a.table[i]), b);
253 b = A2(f, a.height == 0 ? a.table[i] : foldr(f, b, a.table[i]), b);
254 }
254 }
255 return b;
255 return b;
libraries/Set.elm+11−1
@@ -2,6 +2,7 @@
2module Set (empty,singleton,insert,remove
2module Set (empty,singleton,insert,remove
3 ,member
3 ,member
4 ,foldl,foldr,map
4 ,foldl,foldr,map
5+ ,filter,partition
5 ,union,intersect,diff
6 ,union,intersect,diff
6 ,toList,fromList
7 ,toList,fromList
7 ) where
8 ) where
@@ -25,7 +26,7 @@ Insert, remove, and query operations all take *O(log n)* time.
25@docs toList, fromList
26@docs toList, fromList
26
27
27# Transform
28# Transform
28−@docs map, foldl, foldr
29+@docs map, foldl, foldr, filter, partition
29
30
30-}
31-}
31
32
@@ -88,3 +89,12 @@ foldr f b s = Dict.foldr (\k _ b -> f k b) b s
88{-| Map a function onto a set, creating a new set with no duplicates. -}
89{-| Map a function onto a set, creating a new set with no duplicates. -}
89map : (comparable -> comparable') -> Set comparable -> Set comparable'
90map : (comparable -> comparable') -> Set comparable -> Set comparable'
90map f s = fromList (List.map f (toList s))
91map f s = fromList (List.map f (toList s))
92+
93+{-| Create a new set consisting only of elements which satisfy a predicate. -}
94+filter : (comparable -> Bool) -> Set comparable -> Set comparable
95+filter p set = Dict.filter (\k _ -> p k) set
96+
97+{-| Create two new sets; the first consisting of elements which satisfy a
98+predicate, the second consisting of elements which do not. -}
99+partition : (comparable -> Bool) -> Set comparable -> (Set comparable, Set comparable)
100+partition p set = Dict.partition (\k _ -> p k) set
server/LICENSEdeleted−30
@@ -1,30 +0,0 @@
1−Copyright (c)2012-2014, Evan Czaplicki
2−
3−All rights reserved.
4−
5−Redistribution and use in source and binary forms, with or without
6−modification, are permitted provided that the following conditions are met:
7−
8− * Redistributions of source code must retain the above copyright
9− notice, this list of conditions and the following disclaimer.
10−
11− * Redistributions in binary form must reproduce the above
12− copyright notice, this list of conditions and the following
13− disclaimer in the documentation and/or other materials provided
14− with the distribution.
15−
16− * Neither the name of Evan Czaplicki nor the names of other
17− contributors may be used to endorse or promote products derived
18− from this software without specific prior written permission.
19−
20−THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21−"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22−LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23−A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24−OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25−SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26−LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27−DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28−THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29−(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30−OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
server/Server.hsdeleted−127
@@ -1,127 +0,0 @@
1−{-# OPTIONS_GHC -Wall #-}
2−{-# LANGUAGE OverloadedStrings, DeriveDataTypeable #-}
3−module Main where
4−
5−import Control.Applicative ((<$>),(<|>))
6−import Control.Monad (guard)
7−import Control.Monad.Trans (MonadIO(liftIO))
8−import qualified Data.ByteString as BS
9−import qualified Data.ByteString.Char8 as BSC
10−import qualified Data.HashMap.Strict as Map
11−import qualified Data.Version as Version
12−import System.Console.CmdArgs
13−import System.Directory
14−import System.Exit
15−import System.FilePath
16−import System.Process
17−import System.IO (hGetContents, Handle)
18−
19−import Paths_elm_server (version)
20−import qualified Elm.Internal.Paths as Elm
21−import Snap.Core
22−import Snap.Http.Server
23−import Snap.Util.FileServe
24−
25−data Flags = Flags
26− { port :: Int
27− , runtime :: Maybe FilePath
28− } deriving (Data,Typeable,Show,Eq)
29−
30−flags :: Flags
31−flags = Flags
32− { port = 8000 &= help "set the port of the server"
33− , runtime = Nothing &= typFile
34− &= help "Specify a custom location for Elm's runtime system."
35− } &= help "Quickly reload Elm projects in your browser. Just refresh to recompile.\n\
36− \It serves static files and freshly recompiled Elm files."
37− &= helpArg [explicit, name "help", name "h"]
38− &= versionArg [ explicit, name "version", name "v"
39− , summary (Version.showVersion version)
40− ]
41− &= summary ("Elm Server " ++ Version.showVersion version ++
42− ", (c) Evan Czaplicki 2011-2014")
43−
44−
45−config :: Config Snap a
46−config = setAccessLog ConfigNoLog (setErrorLog ConfigNoLog defaultConfig)
47−
48−-- | Set up the server.
49−main :: IO ()
50−main = do
51− cargs <- cmdArgs flags
52− putStrLn $ "Elm Server " ++ Version.showVersion version ++
53− ": Just refresh a page to recompile it!"
54− httpServe (setPort (port cargs) config) $
55− serveRuntime (maybe Elm.runtime id (runtime cargs))
56− <|> serveElm
57− <|> serveDirectoryWith directoryConfig "."
58−
59−directoryConfig :: MonadSnap m => DirectoryConfig m
60−directoryConfig =
61− fancyDirectoryConfig
62− { indexGenerator = defaultIndexGenerator defaultMimeTypes indexStyle
63− , mimeTypes = Map.insert ".elm" "text/html" defaultMimeTypes
64− }
65−
66−indexStyle :: BS.ByteString
67−indexStyle =
68− "body { margin:0; font-family:sans-serif; background:rgb(245,245,245);\
69− \ font-family: calibri, verdana, helvetica, arial; }\
70− \div.header { padding: 40px 50px; font-size: 24px; }\
71− \div.content { padding: 0 40px }\
72− \div.footer { display:none; }\
73− \table { width:100%; border-collapse:collapse; }\
74− \td { padding: 6px 10px; }\
75− \tr:nth-child(odd) { background:rgb(216,221,225); }\
76− \td { font-family:monospace }\
77− \th { background:rgb(90,99,120); color:white; text-align:left;\
78− \ padding:10px; font-weight:normal; }"
79−
80−runtimeName :: String
81−runtimeName = "elm-runtime.js"
82−
83−serveRuntime :: FilePath -> Snap ()
84−serveRuntime runtimePath =
85− do file <- BSC.unpack . rqPathInfo <$> getRequest
86− guard (file == runtimeName)
87− serveFileAs "application/javascript" runtimePath
88−
89−serveElm :: Snap ()
90−serveElm =
91− do file <- BSC.unpack . rqPathInfo <$> getRequest
92− exists <- liftIO $ doesFileExist file
93− guard (exists && takeExtension file == ".elm")
94− onSuccess (compile file) (serve file)
95− where
96− compile file =
97− let elmArgs = [ "--make", "--runtime=" ++ runtimeName, file ]
98− in createProcess $ (proc "elm" elmArgs) { std_out = CreatePipe }
99−
100− serve file =
101− serveFileAs "text/html; charset=UTF-8" ("build" </> replaceExtension file "html")
102−
103−failure :: String -> Snap ()
104−failure msg =
105− do modifyResponse $ setResponseStatus 404 "Not found"
106− writeBS $ BSC.pack msg
107−
108−onSuccess :: IO (t, Maybe Handle, t1, ProcessHandle) -> Snap () -> Snap ()
109−onSuccess action success =
110− do (_, stdout, _, handle) <- liftIO action
111− exitCode <- liftIO $ waitForProcess handle
112− case (exitCode, stdout) of
113− (ExitFailure 127, _) ->
114− failure "Error: elm compiler not found in your path."
115−
116− (ExitFailure _, Just out) ->
117− failure =<< liftIO (hGetContents out)
118−
119− (ExitFailure _, Nothing) ->
120− failure "See command line for error message."
121−
122− (ExitSuccess, _) -> success
123−
124−{--
125−pageTitle :: String -> String
126−pageTitle = dropExtension . takeBaseName
127−--}
server/Setup.hsdeleted−2
@@ -1,2 +0,0 @@
1−import Distribution.Simple
2−main = defaultMain
server/elm-server.cabaldeleted−41
@@ -1,41 +0,0 @@
1−Name: elm-server
2−Version: 0.12
3−Synopsis: Server for developing Elm projects
4−Description: Provides a standalone Snap server that serves static files.
5− For Elm files, it recompiles them and serves them as HTML,
6− so you can just refresh to see the new version.
7−
8−Homepage: http://elm-lang.org
9−
10−License: BSD3
11−License-file: LICENSE
12−
13−Author: Evan Czaplicki
14−Maintainer: info@elm-lang.org
15−Copyright: Copyright: (c) 2011-2014 Evan Czaplicki
16−
17−Category: Compiler, Language
18−
19−Build-type: Simple
20−
21−--Extra-source-files: README.md
22−Cabal-version: >=1.8
23−
24−source-repository head
25− type: git
26− location: git://github.com/evancz/Elm.git
27−
28−Executable elm-server
29− Main-is: Server.hs
30− Build-depends: base >=4.2 && <5,
31− bytestring,
32− cmdargs,
33− containers >= 0.3,
34− directory,
35− filepath,
36− Elm >= 0.12,
37− snap-core,
38− snap-server,
39− mtl,
40− process,
41− unordered-containers
tests/elm/Test.elm+2−1
@@ -12,9 +12,10 @@ import IO.Runner as Run
12import Test.String as String
12import Test.String as String
13import Test.Trampoline as Trampoline
13import Test.Trampoline as Trampoline
14import Test.Array as Array
14import Test.Array as Array
15+import Test.Dict as Dict
15
16
16tests : [Test]
17tests : [Test]
17−tests = String.tests ++ Trampoline.tests ++ Array.tests
18+tests = String.tests ++ Trampoline.tests ++ Array.tests ++ Dict.tests
18
19
19console : IO ()
20console : IO ()
20console = runDisplay tests
21console = runDisplay tests
tests/elm/Test/Array.elm+44−37
@@ -8,40 +8,47 @@ import ElmTest.Test (..)
8
8
9tests : [Test]
9tests : [Test]
10tests =
10tests =
11+ let creationTests =
11− let creationTests = [ test "empty" <| assertEqual Array.empty (Array.fromList [])
12+ [ test "empty" <| assertEqual Array.empty (Array.fromList [])
12− , test "initialize" <| assertEqual (Array.initialize 4 id) (Array.fromList [0,1,2,3])
13+ , test "initialize" <| assertEqual (Array.initialize 4 id) (Array.fromList [0,1,2,3])
13− , test "initialize 2" <| assertEqual (Array.initialize 4 (\n -> n*n)) (Array.fromList [0,1,4,9])
14+ , test "initialize 2" <| assertEqual (Array.initialize 4 (\n -> n*n)) (Array.fromList [0,1,4,9])
14− , test "initialize 3" <| assertEqual (Array.initialize 4 (always 0)) (Array.fromList [0,0,0,0])
15+ , test "initialize 3" <| assertEqual (Array.initialize 4 (always 0)) (Array.fromList [0,0,0,0])
15− , test "initialize Empty" <| assertEqual (Array.initialize 0 id) Array.empty
16+ , test "initialize Empty" <| assertEqual (Array.initialize 0 id) Array.empty
16− , test "initialize 4" <| assertEqual (Array.initialize 2 (always 0)) (Array.fromList [0,0])
17+ , test "initialize 4" <| assertEqual (Array.initialize 2 (always 0)) (Array.fromList [0,0])
17− , test "repeat" <| assertEqual (Array.repeat 5 40) (Array.fromList [40,40,40,40,40])
18+ , test "repeat" <| assertEqual (Array.repeat 5 40) (Array.fromList [40,40,40,40,40])
18− , test "repeat 2" <| assertEqual (Array.repeat 5 0) (Array.fromList [0,0,0,0,0])
19+ , test "repeat 2" <| assertEqual (Array.repeat 5 0) (Array.fromList [0,0,0,0,0])
19− , test "repeat 3" <| assertEqual (Array.repeat 3 "cat") (Array.fromList ["cat","cat","cat"])
20+ , test "repeat 3" <| assertEqual (Array.repeat 3 "cat") (Array.fromList ["cat","cat","cat"])
20− , test "fromList" <| assertEqual (Array.fromList []) Array.empty
21+ , test "fromList" <| assertEqual (Array.fromList []) Array.empty
21− ]
22+ ]
23+ basicsTests =
22− basicsTests = [ test "length" <| assertEqual (Array.length (Array.fromList [1,2,3])) 3
24+ [ test "length" <| assertEqual (Array.length (Array.fromList [1,2,3])) 3
23− , test "length - Long" <| assertEqual (Array.length (Array.repeat 10000 0)) 10000
25+ , test "length - Long" <| assertEqual (Array.length (Array.repeat 10000 0)) 10000
24− , test "push" <| assertEqual (Array.push 3 (Array.fromList [1,2])) (Array.fromList [1,2,3])
26+ , test "push" <| assertEqual (Array.push 3 (Array.fromList [1,2])) (Array.fromList [1,2,3])
25− , test "append" <| assertEqual (Array.toList <| Array.append (Array.repeat 2 42) (Array.repeat 3 81)) ([42,42,81,81,81])
27+ , test "append" <| assertEqual (Array.toList <| Array.append (Array.repeat 2 42) (Array.repeat 3 81)) ([42,42,81,81,81])
26− ]
28+ ]
29+ getAndSetTests =
27− getAndSetTests = [ test "get" <| assertEqual (Array.get 2 (Array.fromList [3,2,1])) 1
30+ [ test "get" <| assertEqual (Array.get 1 (Array.fromList [3,2,1])) (Just 2)
28− , test "getMaybe" <| assertEqual (Array.getMaybe 1 (Array.fromList [3,2,1])) (Just 2)
29− , test "getMaybe 2" <| assertEqual (Array.getMaybe 5 (Array.fromList [3,2,1])) Nothing
31+ , test "get 2" <| assertEqual (Array.get 5 (Array.fromList [3,2,1])) Nothing
30− , test "getMaybe 3" <| assertEqual (Array.getMaybe -1 (Array.fromList [3,2,1])) Nothing
32+ , test "get 3" <| assertEqual (Array.get -1 (Array.fromList [3,2,1])) Nothing
31− , test "getWithDefault" <| assertEqual (Array.getWithDefault 0 2 (Array.fromList [3,2,1])) 1
33+ , test "getSafe 1" <| assertEqual (Array.getSafe 0 2 (Array.fromList [3,2,1])) 1
32− , test "getWithDefault 2" <| assertEqual (Array.getWithDefault 0 5 (Array.fromList [3,2,1])) 0
34+ , test "getSafe 2" <| assertEqual (Array.getSafe 0 5 (Array.fromList [3,2,1])) 0
35+ , test "getUnsafe" <| assertEqual (Array.getUnsafe 2 (Array.fromList [3,2,1])) 1
33− , test "set" <| assertEqual (Array.set 1 7 (Array.fromList [1,2,3])) (Array.fromList [1,7,3])
36+ , test "set" <| assertEqual (Array.set 1 7 (Array.fromList [1,2,3])) (Array.fromList [1,7,3])
34− ]
37+ ]
38+ takingArraysApartTests =
35− takingArraysApartTests = [ test "toList" <| assertEqual (Array.toList (Array.fromList [3,5,8])) [3,5,8]
39+ [ test "toList" <| assertEqual (Array.toList (Array.fromList [3,5,8])) [3,5,8]
36− , test "toIndexedList" <| assertEqual (Array.toIndexedList (Array.fromList ["cat","dog"])) [(0,"cat"), (1,"dog")]
40+ , test "toIndexedList" <| assertEqual (Array.toIndexedList (Array.fromList ["cat","dog"])) [(0,"cat"), (1,"dog")]
37− , test "slice" <| assertEqual (Array.slice 1 2 (Array.fromList [0,1,2,3,4])) (Array.fromList [1,2])
41+ , test "slice" <| assertEqual (Array.slice 1 2 (Array.fromList [0,1,2,3,4])) (Array.fromList [1,2])
38− , test "slice 2" <| assertEqual (Array.slice 1 -2 (Array.fromList [0,1,2,3,4])) (Array.fromList [1,2,3])
42+ , test "slice 2" <| assertEqual (Array.slice 1 -2 (Array.fromList [0,1,2,3,4])) (Array.fromList [1,2,3])
39− , test "slice 3" <| assertEqual (Array.slice -3 -2 (Array.fromList [0,1,2,3,4])) (Array.fromList [2,3])
43+ , test "slice 3" <| assertEqual (Array.slice -3 -2 (Array.fromList [0,1,2,3,4])) (Array.fromList [2,3])
40− ]
44+ ]
45+ mappingAndFoldingTests =
41− mappingAndFoldingTests = [ test "map" <| assertEqual (Array.map sqrt (Array.fromList [1,4,9])) (Array.fromList [1,2,3])
46+ [ test "map" <| assertEqual (Array.map sqrt (Array.fromList [1,4,9])) (Array.fromList [1,2,3])
42− , test "indexedMap" <| assertEqual (Array.indexedMap (*) (Array.fromList [5,5,5])) (Array.fromList [0,5,10])
47+ , test "indexedMap" <| assertEqual (Array.indexedMap (*) (Array.fromList [5,5,5])) (Array.fromList [0,5,10])
43− , test "foldl" <| assertEqual (Array.foldl (::) [] (Array.fromList [1,2,3])) [1,2,3]
48+ , test "foldl" <| assertEqual (Array.foldl (::) [] (Array.fromList [1,2,3])) [3,2,1]
44− , test "foldr" <| assertEqual (Array.foldr (+) 0 (Array.repeat 3 5)) 15
49+ , test "foldr" <| assertEqual (Array.foldr (+) 0 (Array.repeat 3 5)) 15
45− , test "foldl 2" <| assertEqual (Array.foldr (::) [] (Array.fromList [1,2,3])) [3,2,1]
50+ , test "foldr 2" <| assertEqual (Array.foldr (::) [] (Array.fromList [1,2,3])) [1,2,3]
51+ , test "filter" <| assertEqual (Array.filter (\x -> x `mod` 2 == 0) (Array.fromList [1..6])) (Array.fromList [2,4,6])
46− ]
52+ ]
53+ in
47− in List.concat [creationTests, basicsTests, getAndSetTests, takingArraysApartTests, mappingAndFoldingTests]
54+ List.concat [creationTests, basicsTests, getAndSetTests, takingArraysApartTests, mappingAndFoldingTests]
tests/elm/Test/Dict.elmnew file+26
@@ -0,0 +1,26 @@
1+module Test.Dict (tests) where
2+
3+import Dict
4+import List
5+
6+import ElmTest.Assertion (..)
7+import ElmTest.Test (..)
8+
9+animals : Dict.Dict String String
10+animals = Dict.fromList [ ("Tom", "cat"), ("Jerry", "mouse") ]
11+
12+tests : [Test]
13+tests =
14+ let getTests =
15+ [ test "get 1" <| assertEqual (Just "cat") (Dict.get "Tom" animals)
16+ , test "get 2" <| assertEqual Nothing (Dict.get "Spike" animals)
17+ , test "getSafe 1" <| assertEqual "mouse" (Dict.getSafe "dog" "Jerry" animals)
18+ , test "getSafe 2" <| assertEqual "dog" (Dict.getSafe "dog" "Spike" animals)
19+ , test "getUnsafe" <| assertEqual "cat" (Dict.getUnsafe "Tom" animals)
20+ ]
21+ filterTests =
22+ [ test "filter" <| assertEqual (Dict.singleton "Tom" "cat") (Dict.filter (\k v -> k == "Tom") animals)
23+ , test "partition" <| assertEqual (Dict.singleton "Tom" "cat", Dict.singleton "Jerry" "mouse") (Dict.partition (\k v -> k == "Tom") animals)
24+ ]
25+ in
26+ List.concat [ getTests, filterTests ]
tests/elm/Test/Set.elmnew file+26
@@ -0,0 +1,26 @@
1+module Test.Set (tests) where
2+
3+import Set
4+import List
5+
6+import ElmTest.Assertion (..)
7+import ElmTest.Test (..)
8+
9+set : Set
10+set = Set.fromList [1..100]
11+
12+setPart1 : Set
13+setPart1 = Set.fromList [1..50]
14+
15+setPart2 : Set
16+setPart2 = Set.fromList [51..100]
17+
18+pred : Int -> Bool
19+pred x = x <= 50
20+
21+tests : [Test]
22+tests =
23+ let
24+ filterTests = [assertEqual setPart1 <| filter pred set]
25+ partitionTests = [assertEqual (setPart1, setPart2) <| partition pred set]
26+ in List.concat [partitionTests, filterTests]