git.iko.soy/rowtype-yoga/purescript-vault
on master help sign in
21 files changed+504−107
since splitsnapshots
unifiedsplit
.editorconfig+13 .github/ISSUE_TEMPLATE/bug-report.md+23 .github/ISSUE_TEMPLATE/change-request.md+24 .github/ISSUE_TEMPLATE/config.yml+8 .github/PULL_REQUEST_TEMPLATE.md+12 .github/workflows/ci.yml+53 .gitignore+9−8 .tidyrc.json+10 CHANGELOG.md+34 CONTRIBUTING.md+50 LICENSE+30 README.md+67−9 bower.json+16−5 docs/README.md+3 packages.dhall+5 shell.nix+28 spago.dhall+12 src/Data/Vault.purs+27−22 src/Data/Vault/Internal.purs−27 test.dhall+6 test/Main.purs+74−36
.editorconfignew file+13
@@ -0,0 +1,13 @@
1+# https://editorconfig.org
2+root = true
3+
4+[*]
5+indent_style = space
6+indent_size = 2
7+end_of_line = lf
8+charset = utf-8
9+trim_trailing_whitespace = true
10+insert_final_newline = true
11+
12+[*.md]
13+trim_trailing_whitespace = false
.github/ISSUE_TEMPLATE/bug-report.mdnew file+23
@@ -0,0 +1,23 @@
1+---
2+name: Bug report
3+about: Report an issue
4+title: ""
5+labels: bug
6+assignees: ""
7+---
8+
9+**Describe the bug**
10+
11+A clear and concise description of the bug.
12+
13+**To Reproduce**
14+
15+A minimal code example (preferably a runnable example on [Try PureScript](https://try.purescript.org)!) or steps to reproduce the issue.
16+
17+**Expected behavior**
18+
19+A clear and concise description of what you expected to happen.
20+
21+**Additional context**
22+
23+Add any other context about the problem here.
.github/ISSUE_TEMPLATE/change-request.mdnew file+24
@@ -0,0 +1,24 @@
1+---
2+name: Change request
3+about: Propose an improvement to this library
4+title: ""
5+labels: ""
6+assignees: ""
7+---
8+
9+**Is your change request related to a problem? Please describe.**
10+
11+A clear and concise description of the problem.
12+
13+Examples:
14+
15+- It's frustrating to have to [...]
16+- I was looking for a function to [...]
17+
18+**Describe the solution you'd like**
19+
20+A clear and concise description of what a good solution to you looks like, including any solutions you've already considered.
21+
22+**Additional context**
23+
24+Add any other context about the change request here.
.github/ISSUE_TEMPLATE/config.ymlnew file+8
@@ -0,0 +1,8 @@
1+blank_issues_enabled: false
2+contact_links:
3+ - name: PureScript Discourse
4+ url: https://discourse.purescript.org/
5+ about: Ask and answer questions on the PureScript discussion forum.
6+ - name: PureScript Discord
7+ url: https://purescript.org/chat
8+ about: Ask and answer questions on the PureScript chat.
.github/PULL_REQUEST_TEMPLATE.mdnew file+12
@@ -0,0 +1,12 @@
1+**Description of the change**
2+
3+Clearly and concisely describe the purpose of the pull request. If this PR relates to an existing issue or change proposal, please link to it. Include any other background context that would help reviewers understand the motivation for this PR.
4+
5+---
6+
7+**Checklist:**
8+
9+- [ ] Added the change to the changelog's "Unreleased" section with a link to this PR and your username
10+- [ ] Linked any existing issues or proposals that this pull request should close
11+- [ ] Updated or added relevant documentation in the README and/or documentation directory
12+- [ ] Added a test for the contribution (if applicable)
.github/workflows/ci.ymlnew file+53
@@ -0,0 +1,53 @@
1+name: CI
2+
3+on:
4+ push:
5+ branches: [master]
6+ pull_request:
7+ branches: [master]
8+
9+jobs:
10+ build:
11+ runs-on: ubuntu-latest
12+
13+ steps:
14+ - uses: actions/checkout@v3
15+
16+ - name: Set up a PureScript toolchain
17+ uses: purescript-contrib/setup-purescript@main
18+ with:
19+ bower: "latest"
20+ purescript: "latest"
21+ psa: "latest"
22+ pulp: "latest"
23+ spago: "latest"
24+ purs-tidy: "latest"
25+
26+ - name: Check formatting
27+ run: purs-tidy check src test
28+
29+ - name: Cache PureScript dependencies
30+ uses: actions/cache@v2
31+ with:
32+ key: ${{ runner.os }}-spago-${{ hashFiles('**/*.dhall') }}
33+ path: |
34+ .spago
35+ output
36+
37+ - name: Install dependencies
38+ run: spago install
39+
40+ - name: Build source
41+ run: spago build --no-install --purs-args '--censor-lib --strict'
42+
43+ - name: Run tests
44+ run: spago -x test.dhall test
45+
46+ - name: Verify Bower & Pulp
47+ run: |
48+ npm install bower pulp@16.0.0-0
49+ npx bower install
50+ npx pulp build -- --censor-lib --strict
51+ if [ -d "test" ]; then
52+ npx pulp test
53+ fi
.gitignore+9−8
@@ -1,8 +1,9 @@
1−/bower_components/
2−/node_modules/
3−/.pulp-cache/
4−/output/
5−/generated-docs/
6−/.psc*
7−/.purs*
8−/.psa*
1+.*
2+!.gitignore
3+!.github
4+!.editorconfig
5+!.tidyrc.json
6+
7+output
8+generated-docs
9+bower_components
.tidyrc.jsonnew file+10
@@ -0,0 +1,10 @@
1+{
2+ "importSort": "source",
3+ "importWrap": "source",
4+ "indent": 2,
5+ "operatorsFile": null,
6+ "ribbon": 1,
7+ "typeArrowPlacement": "first",
8+ "unicode": "never",
9+ "width": null
10+}
CHANGELOG.mdnew file+34
@@ -0,0 +1,34 @@
1+# Changelog
2+
3+Notable changes to this project are documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
4+
5+## [Unreleased]
6+
7+Breaking changes:
8+
9+New features:
10+
11+Bugfixes:
12+
13+Other improvements:
14+- Updated the project for its new home in the Rowtype Yoga organization. ([c86a9a9](https://github.com/rowtype-yoga/purescript-vault/commit/c86a9a97840bb871e7b687f3056fedead99b925b) by @nsaunders)
15+
16+## [1.0.0] - 2023-03-09
17+
18+Breaking changes:
19+- Updated to v0.15 of the compiler, dropping support for previous versions. (#1 by @nsaunders)
20+
21+New features:
22+- Added the `adjust` function which allows the value corresponding to a given key to be modified. (#2 by @nsaunders)
23+
24+Bugfixes:
25+- Fixed `union` function which previously ignored the first argument. (#2 by @nsaunders)
26+
27+Other improvements:
28+- Updated the project to the standards of the [purescript-contrib](https://github.com/purescript-contrib) organization. ([713fd52](https://github.com/rowtype-yoga/purescript-vault/commit/713fd521362c2833cea5f9ef3afb1e600164b903) by @nsaunders)
29+- Updated the test suite to include better coverage. (#2 by @nsaunders)
30+- New implementation removes FFI code. (#3 by @nsaunders)
31+
32+## [0.1.0] - 2017-10-30
33+
34+Initial release
CONTRIBUTING.mdnew file+50
@@ -0,0 +1,50 @@
1+Contributing
2+============
3+
4+Welcome, and thanks for your interest in Vault. Whether you are new to PureScript or here to teach us new tricks, we're glad to have you and look forward to your contributions. Listed below are a few ways you can help.
5+
6+Asking questions
7+----------------
8+If you have a question, please [open an issue](https://github.com/rowtype-yoga/purescript-vault/issues/new?labels=question) to discuss it. This might help us to find a defect; reveal an opportunity to improve the documentation or developer experience; or help someone facing a similar issue in the future.
9+
10+Reporting bugs
11+--------------
12+Please [open an issue](https://github.com/rowtype-yoga/purescript-vault/issues/new?labels=bug&template=bug-report.md) to discuss any defects you find.
13+
14+Enhancements
15+------------
16+Please [open an issue](https://github.com/rowtype-yoga/purescript-vault/issues/new?labels=enhancement&template=change-request.md) to discuss potential enhancements to the library. When submitting design ideas, please note that, for the most part, this library is intended as a direct port of the Haskell [vault](https://hackage.haskell.org/package/vault) package as described in [this blog post](https://apfelmus.nfshost.com/blog/2011/09/04-vault.html).
17+
18+Improving documentation
19+-----------------------
20+Please share any suggestions for improving or adding to the documentation by [submitting a pull request](https://github.com/rowtype-yoga/purescript-vault/compare) or [opening an issue](https://github.com/rowtype-yoga/purescript-vault/issues/new?labels=documentation).
21+
22+Sharing resources
23+-----------------
24+If you have created a tutorial, auxiliary library, interesting code example, or other resource related to Vault, please [share it with the PureScript community](https://discourse.purescript.org). Increasing awareness will bring more users (and potential contributors) to the project.
25+
26+Submitting code
27+---------------
28+Pull requests are welcome, but we ask that you [open an issue](https://github.com/rowtype-yoga/purescript-vault/issues/new) to discuss your plans before making any significant investment of your valuable time.
29+
30+### Development environment
31+
32+You can use [Nix](https://github.com/NixOS/nix) to create a development environment with all required tooling. Simply run `nix-shell` in the root project directory, and you'll have everything you need to get started.
33+
34+### Making changes
35+
36+* After making any changes, run `purs-tidy format-in-place src test` to format the code.
37+* Unit test(s) should accompany each change.
38+
39+### Preparing your submission
40+
41+Please verify your changes before submitting a pull request using the following commands:
42+1. `purs-tidy check src test`, which ensures that modules in the `src` and `test` directories conform to the project's formatting standards; and
43+1. `spago -x test.dhall test`, which runs the unit tests.
44+
45+### Submitting a pull request
46+
47+When you are ready to [submit a pull request](https://github.com/rowtype-yoga/purescript-vault/compare), please make sure to include:
48+1. a brief description of the change;
49+1. a link to the related issue, if applicable; and
50+1. any design considerations you feel are important.
LICENSEnew file+30
@@ -0,0 +1,30 @@
1+Copyright (c)2011, Julien Chandra
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 Julien Chandra 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.
README.md+67−9
@@ -1,20 +1,78 @@
1−### Vault
1+# Vault
22
3−A typed, persistent store for values of arbitrary types. This is a port of the haskell [Vault](https://github.com/HeinrichApfelmus/vault) library Heinrich Apfelmus.
3+[![CI](https://github.com/rowtype-yoga/purescript-vault/workflows/CI/badge.svg?branch=master)](https://github.com/rowtype-yoga/purescript-vault/actions?query=workflow%3ACI+branch%3Amaster)
4+[![Release](https://img.shields.io/github/release/rowtype-yoga/purescript-vault.svg)](https://github.com/rowtype-yoga/purescript-vault/releases)
5+[![Pursuit](https://pursuit.purescript.org/packages/purescript-vault/badge)](https://pursuit.purescript.org/packages/purescript-vault)
6+[![Maintainer: nsaunders](https://img.shields.io/badge/maintainer-nsaunders-teal.svg)](https://github.com/nsaunders)
47
5−Think of it as ```Map``` that can store for values of arbitrary types, but it's type-safe.
6−You can store of any types by creating ```Key a``` first, this key used to parameterized the
7−type you are going to store in vault.
8+A typed, persistent store for values of arbitrary types, ported from the Haskell [Vault](https://github.com/HeinrichApfelmus/vault) library by Heinrich Apfelmus.
89
9−In summary a vault API:
10+Think of a vault like a map that can store any value in a type-safe manner. The first step is to generate a new `Key a` value using the `newKey` function. Then, use `insert`, `lookup`, `delete`, etc., just like their [Data.Map](https://pursuit.purescript.org/packages/purescript-ordered-collections/3.0.0/docs/Data.Map) analogs.
1011
11−```haskell
12+```purescript
1213data Vault
1314data Key a
1415
15−newKey :: forall e a. Eff (ref :: REF | e) (Key a)
16+newKey :: forall a. Effect (Key a)
1617empty :: Vault
17−lookup :: forall a. Key a -> Vault -> Maybe a
1818insert :: forall a. Key a -> a -> Vault -> Vault
19+lookup :: forall a. Key a -> Vault -> Maybe a
1920delete :: forall a. Key a -> Vault -> Vault
21+adjust :: forall a. (a -> a) -> Key a -> Vault -> Vault
22+```
23+
24+## Installation
25+
26+Install `vault` with [Spago](https://github.com/purescript/spago):
27+
28+```sh
29+spago install vault
2030```
31+
32+If `vault` is not included in your package set, include it in `packages.dhall`, e.g.
33+
34+```dhall
35+let upstream = -- <package set URL here>
36+in upstream
37+ with vault =
38+ { dependencies =
39+ [ "effect"
40+ , "maybe"
41+ , "ordered-collections"
42+ , "prelude"
43+ , "refs"
44+ , "unique"
45+ ]
46+ , repo =
47+ "https://github.com/rowtype-yoga/purescript-vault.git"
48+ , version =
49+ "v1.0.0"
50+ }
51+```
52+
53+## Quick start
54+
55+The quick start hasn't been written yet (contributions are welcome!). The quick start covers a common, minimal use case for the library, whereas longer examples and tutorials are kept in the [docs directory](./docs).
56+
57+## Documentation
58+
59+Vault documentation is stored in a few places:
60+
61+1. Module documentation is [published on Pursuit](https://pursuit.purescript.org/packages/purescript-vault).
62+2. Written documentation is kept in the [docs directory](./docs).
63+3. Usage examples can be found in [the test suite](./test).
64+
65+If you get stuck, there are several ways to get help:
66+
67+- [Open an issue](https://github.com/rowtype-yoga/purescript-vault/issues/new?labels=bug&template=bug-report.md) if you have encountered a bug or problem.
68+- Ask general questions on the [PureScript Discourse](https://discourse.purescript.org) forum or the [PureScript Discord](https://discord.com/invite/sMqwYUbvz6) chat.
69+
70+## Contributing
71+
72+You can contribute to Vault in several ways:
73+
74+1. If you encounter a problem or have a question, please [open an issue](https://github.com/rowtype-yoga/purescript-vault/issues). We'll do our best to work with you to resolve or answer it.
75+
76+2. If you would like to contribute code, tests, or documentation, please [read the contributor guide](./CONTRIBUTING.md). It's a short, helpful introduction to contributing to this library, including development instructions.
77+
78+3. If you have written a library, tutorial, guide, or other resource based on this package, please share it on the [PureScript Discourse](https://discourse.purescript.org)! Writing libraries and learning resources are a great way to help this library succeed.
bower.json+16−5
@@ -1,5 +1,13 @@
11{
22 "name": "purescript-vault",
3+ "license": "BSD-3-Clause",
4+ "authors": [
5+ "Julien Chandra"
6+ ],
7+ "repository": {
8+ "type": "git",
9+ "url": "git://github.com/rowtype-yoga/purescript-vault.git"
10+ },
311 "ignore": [
412 "**/.*",
513 "node_modules",
@@ -7,12 +15,15 @@
715 "output"
816 ],
917 "dependencies": {
10− "purescript-prelude": "^3.1.0",
11− "purescript-maps": "^3.5.1",
12− "purescript-refs": "^3.0.0"
18+ "purescript-prelude": "^6.0.0",
19+ "purescript-effect": "^4.0.0",
20+ "purescript-ordered-collections": "^3.0.0",
21+ "purescript-refs": "^6.0.0",
22+ "purescript-maybe": "^6.0.0",
23+ "purescript-unique": "^0.6.0"
1324 },
1425 "devDependencies": {
15− "purescript-psci-support": "^3.0.0",
16− "purescript-console": "^3.0.0"
26+ "purescript-aff": "^7.0.0",
27+ "purescript-spec": "^7.0.0"
1728 }
1829}
docs/README.mdnew file+3
@@ -0,0 +1,3 @@
1+# Vault Documentation
2+
3+This directory contains documentation for `vault`. If you are interested in contributing new documentation, please read the [contributor guidelines](../CONTRIBUTING.md) and [What Nobody Tells You About Documentation](https://documentation.divio.com) for help getting started.
packages.dhallnew file+5
@@ -0,0 +1,5 @@
1+let upstream =
2+ https://github.com/purescript/package-sets/releases/download/psc-0.15.7-20230306/packages.dhall
3+ sha256:0757626c7422b8b5b5b1d0df3d3628e5deac755d7f89c433a9bf89009787dcbd
4+
5+in upstream
shell.nixnew file+28
@@ -0,0 +1,28 @@
1+let
2+ pkgs = import (builtins.fetchTarball {
3+ url = "https://github.com/NixOS/nixpkgs/archive/21.11.tar.gz";
4+ }) {};
5+
6+ # To update to a newer version of easy-purescript-nix:
7+ # 1. Obtain the commit hash <rev> via `curl https://api.github.com/repos/justinwoo/easy-purescript-nix/commits/master`.
8+ # 2. Obtain the sha256 hash <sha256> via `nix-prefetch-url --unpack https://github.com/justinwoo/easy-purescript-nix/archive/<rev>.tar.gz`.
9+ # 3. Update the <rev> and <sha256> below.
10+ pursPkgs = import (pkgs.fetchFromGitHub {
11+ owner = "justinwoo";
12+ repo = "easy-purescript-nix";
13+ rev = "11d3bd58ce6e32703bf69cec04dc7c38eabe14ba";
14+ sha256 = "0q24hb4a3fvcizns17ddd4pshlbkfdq2m6pgcjfslwlvgnbrli5l";
15+ }) {inherit pkgs;};
16+in
17+ pkgs.stdenv.mkDerivation {
18+ name = "vault";
19+ buildInputs = with pursPkgs; [
20+ purs
21+ spago
22+ pulp
23+ purs-tidy
24+
25+ pkgs.nodejs-16_x
26+ pkgs.nodePackages.bower
27+ ];
28+ }
spago.dhallnew file+12
@@ -0,0 +1,12 @@
1+{ name = "vault"
2+, dependencies =
3+ [ "effect"
4+ , "maybe"
5+ , "ordered-collections"
6+ , "prelude"
7+ , "refs"
8+ , "unique"
9+ ]
10+, packages = ./packages.dhall
11+, sources = [ "src/**/*.purs" ]
12+}
src/Data/Vault.purs+27−22
@@ -1,6 +1,7 @@
11module Data.Vault
22 ( Vault
33 , Key
4+ , adjust
45 , empty
56 , newKey
67 , insert
@@ -11,53 +12,57 @@ module Data.Vault
1112
1213import Prelude
1314
14−import Control.Monad.Eff (Eff)
15−import Control.Monad.Eff.Unsafe (unsafePerformEff)
16−import Control.Monad.Eff.Ref (REF, Ref, newRef, writeRef, modifyRef')
15+import Effect (Effect)
16+import Effect.Unsafe (unsafePerformEffect)
17+import Effect.Ref (Ref)
18+import Effect.Ref as Ref
1719
18−import Data.Map as M
19−import Data.Maybe (Maybe(..))
20−import Data.Vault.Internal (Unique, newUnique)
20+import Data.Map (Map)
21+import Data.Map as Map
22+import Data.Maybe (Maybe(..), fromMaybe)
23+import Data.Unique (Unique, newUnique)
2124
2225-- | A persistent store for values of arbitrary types.
23−newtype Vault = Vault (M.Map Unique Item)
26+newtype Vault = Vault (Map Unique Item)
2427
2528-- | Key for an item in vault
2629data Key a = Key Unique (Item' a)
2730
28−type Item = Eff (ref :: REF) Unit
31+type Item = Effect Unit
2932
3033type Item' a = Ref (Maybe a)
3134
3235-- | The empty vault
3336empty :: Vault
34−empty = Vault M.empty
37+empty = Vault Map.empty
3538
3639-- | Create a new key for use with a vault.
37−newKey :: forall eff a. Eff (ref :: REF | eff) (Key a)
40+newKey :: forall a. Effect (Key a)
3841newKey = do
3942 k <- newUnique
40− ref <- newRef Nothing
43+ ref <- Ref.new Nothing
4144 pure $ Key k ref
4245
4346-- | Insert a value for a given key. Overwrites any previous value.
4447insert :: forall a. Key a -> a -> Vault -> Vault
45−insert (Key k ref) a (Vault m) = Vault (M.insert k (writeRef ref (Just a)) m)
48+insert (Key k ref) a (Vault m) = Vault $ Map.insert k (Ref.write (Just a) ref) m
49+
50+-- | Modify the value corresponding to the specified key by applying the specified function.
51+adjust :: forall a. (a -> a) -> Key a -> Vault -> Vault
52+adjust f k m = fromMaybe m $ lookup k m >>= \x -> pure $ insert k (f x) m
4653
4754-- | Delete a key from the vault.
4855delete :: forall a. Key a -> Vault -> Vault
49−delete (Key k _) (Vault m) = Vault (M.delete k m)
56+delete (Key k _) (Vault m) = Vault $ Map.delete k m
5057
5158-- | Lookup the value of a key in the vault.
5259lookup :: forall a. Key a -> Vault -> Maybe a
53−lookup (Key k ref) (Vault m) = case M.lookup k m of
54− Nothing -> Nothing
55− Just act -> unsafePerformEff do
56− _ <- act
57− modifyRef' ref rollItem
60+lookup (Key k ref) (Vault m) =
61+ Map.lookup k m >>= \item -> unsafePerformEffect do
62+ item
63+ maybeX <- Ref.read ref
64+ Ref.write Nothing ref
65+ pure maybeX
5866
5967union :: Vault -> Vault -> Vault
60−union (Vault m) (Vault n) = Vault (M.union m n)
61−
62−rollItem :: forall a. Maybe a -> { state :: Maybe a, value :: Maybe a }
63−rollItem a = { state: Nothing, value: a }
68+union (Vault m) (Vault n) = Vault $ Map.union m n
src/Data/Vault/Internal.pursdeleted−27
@@ -1,27 +0,0 @@
1−module Data.Vault.Internal
2− ( Unique
3− , newUnique
4− ) where
5−
6−import Prelude
7−
8−import Control.Monad.Eff (Eff)
9−import Control.Monad.Eff.Unsafe (unsafePerformEff)
10−import Control.Monad.Eff.Ref (REF, Ref, newRef, modifyRef')
11−
12−-- | Store as Number, so it can handle 53-bit signed integer
13−newtype Unique = Unique Number
14−
15−derive newtype instance eqUnique :: Eq Unique
16−derive newtype instance ordUnique :: Ord Unique
17−
18−uniqueSource :: Ref Number
19−uniqueSource = unsafePerformEff $ newRef 0.00
20−
21−incrementSource :: Number -> { state :: Number, value :: Number }
22−incrementSource x = { state: x + one, value: x}
23−
24−newUnique :: forall eff. Eff (ref :: REF | eff) Unique
25−newUnique = do
26− r <- modifyRef' uniqueSource incrementSource
27− pure $ Unique r
test.dhallnew file+6
@@ -0,0 +1,6 @@
1+let conf = ./spago.dhall
2+
3+in conf // {
4+ sources = conf.sources # ["test/**/*.purs"],
5+ dependencies = conf.dependencies # ["aff", "spec"]
6+}
test/Main.purs+74−36
@@ -1,42 +1,80 @@
11module Test.Main where
22
33import Prelude
4−import Control.Monad.Eff (Eff)
5−import Control.Monad.Eff.Console (CONSOLE, log)
6−import Control.Monad.Eff.Ref (REF)
74
8−import Data.Vault as V
95import Data.Maybe (Maybe(..))
6+import Data.Vault as Vault
7+import Effect (Effect)
8+import Effect.Aff (launchAff_)
9+import Effect.Class (liftEffect)
10+import Test.Spec (describe, it)
11+import Test.Spec.Assertions (shouldEqual)
12+import Test.Spec.Reporter (consoleReporter)
13+import Test.Spec.Runner (runSpec)
1014
11−itemKeyInt :: forall eff. Eff (ref :: REF | eff) (V.Key Int)
12−itemKeyInt = V.newKey
13−
14−itemKeyString :: forall eff. Eff (ref :: REF | eff) (V.Key String)
15−itemKeyString = V.newKey
16−
17−testCreateAndGet :: forall eff. Eff (console :: CONSOLE, ref :: REF | eff) Unit
18−testCreateAndGet = do
19− k1 <- itemKeyString
20− k2 <- itemKeyInt
21− let m1 = V.insert k1 "vault" V.empty
22− v1 = V.lookup k1 m1
23− v2 = V.lookup k2 m1
24− when (v1 /= Just "vault") (log "Failed getting value from Vault")
25− when (v2 /= Nothing) (log "Non existing value should return Nothing")
26−
27−testCreateAndDelete :: forall eff. Eff (console :: CONSOLE, ref :: REF | eff) Unit
28−testCreateAndDelete = do
29− k <- itemKeyInt
30− let m1 = V.insert k 1 V.empty
31− v1 = V.lookup k m1
32− m2 = V.delete k m1
33− v2 = V.lookup k m2
34− when (v1 == v2) $ log "Deleted item should return Nothing"
35−
36−main :: forall eff. Eff (console :: CONSOLE, ref :: REF | eff) Unit
37−main = do
38− log "Create new Vault Item"
39− testCreateAndGet
40−
41− log "Crate new Vault Item and Delete"
42− testCreateAndDelete
15+main :: Effect Unit
16+main =
17+ launchAff_ $
18+ runSpec [ consoleReporter ] do
19+ it "inserts an item" do
20+ let s0 = Vault.empty
21+ k <- liftEffect Vault.newKey
22+ let
23+ v = { foo: 1 }
24+ s1 = Vault.insert k v s0
25+ Vault.lookup k s1 `shouldEqual` pure v
26+ it "adjusts an item" do
27+ let s0 = Vault.empty
28+ k <- liftEffect Vault.newKey
29+ let
30+ v0 = { foo: 1 }
31+ s1 = Vault.insert k v0 s0
32+ s2 = Vault.adjust (\r -> r { foo = r.foo + 1 }) k s1
33+ Vault.lookup k s2 `shouldEqual` pure { foo: 2 }
34+ it "deletes an item" do
35+ let s0 = Vault.empty
36+ k <- liftEffect Vault.newKey
37+ let
38+ s1 = Vault.insert k 1 s0
39+ s2 = Vault.delete k s1
40+ Vault.lookup k s2 `shouldEqual` Nothing
41+ it "merges two vaults" do
42+ k <- liftEffect Vault.newKey
43+ l <- liftEffect Vault.newKey
44+ let
45+ a = Vault.insert k 1 Vault.empty
46+ b = Vault.empty # Vault.insert k 2 # Vault.insert l 3
47+ c = Vault.union a b
48+ Vault.lookup k c `shouldEqual` pure 1
49+ Vault.lookup l c `shouldEqual` pure 3
50+ describe "on insert" do
51+ it "does not affect other items" do
52+ k <- liftEffect Vault.newKey
53+ let
54+ s0 = Vault.empty
55+ s1 = Vault.insert k "foo" s0
56+ k' <- liftEffect Vault.newKey
57+ let s2 = Vault.insert k' 3 s1
58+ Vault.lookup k s2 `shouldEqual` pure "foo"
59+ describe "on adjust" do
60+ it "does not affect other items" do
61+ k <- liftEffect Vault.newKey
62+ let
63+ s0 = Vault.empty
64+ s1 = Vault.insert k "foo" s0
65+ k' <- liftEffect Vault.newKey
66+ let
67+ s2 = Vault.insert k' "hi" s1
68+ s3 = Vault.adjust (_ <> "!") k' s2
69+ Vault.lookup k s3 `shouldEqual` pure "foo"
70+ describe "on delete" do
71+ it "does not affect other items" do
72+ k <- liftEffect Vault.newKey
73+ let
74+ s0 = Vault.empty
75+ s1 = Vault.insert k "foo" s0
76+ k' <- liftEffect Vault.newKey
77+ let
78+ s2 = Vault.insert k' "hi" s1
79+ s3 = Vault.delete k' s2
80+ Vault.lookup k s3 `shouldEqual` pure "foo"