Skip to content

Commit

Permalink
Introduce purs-tidy formatter (#53)
Browse files Browse the repository at this point in the history
* Add purs-tidy formatter

* Run purs-tidy
  • Loading branch information
thomashoneyman authored Nov 11, 2021
1 parent 24a9a3b commit 5cbdf37
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 55 deletions.
9 changes: 7 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ jobs:

- name: Set up PureScript toolchain
uses: purescript-contrib/setup-purescript@main
with:
purs-tidy: "latest"

- name: Cache PureScript dependencies
uses: actions/cache@v2
Expand All @@ -25,9 +27,9 @@ jobs:
output
- name: Set up Node toolchain
uses: actions/setup-node@v1
uses: actions/setup-node@v2
with:
node-version: "12.x"
node-version: "14.x"

- name: Cache NPM dependencies
uses: actions/cache@v2
Expand All @@ -49,3 +51,6 @@ jobs:

- name: Run tests
run: npm run test

- name: Check formatting
run: purs-tidy check src test
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
!.gitignore
!.github
!.editorconfig
!.tidyrc.json
!.eslintrc.json

output
Expand Down
10 changes: 10 additions & 0 deletions .tidyrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"importSort": "source",
"importWrap": "source",
"indent": 2,
"operatorsFile": null,
"ribbon": 1,
"typeArrowPlacement": "first",
"unicode": "never",
"width": null
}
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ New features:
Bugfixes:

Other improvements:
- Added `purs-tidy` formatter (#53 by @thomashoneyman)
* Fixed readme bug where `jsonParser` was imported from `Data.Argonaut.Core` instead of `Data.Argonaut.Parser` (#50 by @flip111)

## [v6.0.0](https://github.com/purescript-contrib/purescript-argonaut-core/releases/tag/v6.0.0) - 2021-02-26
Expand Down
21 changes: 11 additions & 10 deletions src/Data/Argonaut/Core.purs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ caseJson
-> (String -> a)
-> (Array Json -> a)
-> (Object Json -> a)
-> Json -> a
-> Json
-> a
caseJson a b c d e f json = runFn7 _caseJson a b c d e f json

-- | A simpler version of `caseJson` which accepts a callback for when the
Expand Down Expand Up @@ -229,7 +230,7 @@ jsonEmptyObject = fromObject Obj.empty

-- | Constructs a `Json` array value containing only the provided value
jsonSingletonArray :: Json -> Json
jsonSingletonArray j = fromArray [j]
jsonSingletonArray j = fromArray [ j ]

-- | Constructs a `Json` object value containing only the provided key and value
jsonSingletonObject :: String -> Json -> Json
Expand All @@ -247,13 +248,13 @@ foreign import stringifyWithIndent :: Int -> Json -> String
foreign import _caseJson
:: forall z
. Fn7
(Unit -> z)
(Boolean -> z)
(Number -> z)
(String -> z)
(Array Json -> z)
(Object Json -> z)
Json
z
(Unit -> z)
(Boolean -> z)
(Number -> z)
(String -> z)
(Array Json -> z)
(Object Json -> z)
Json
z

foreign import _compare :: Fn5 Ordering Ordering Ordering Json Json Ordering
2 changes: 1 addition & 1 deletion src/Data/Argonaut/Gen.purs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ genJson = Gen.resize (min 5) $ Gen.sized genJson'
| otherwise = genLeaf

genLeaf :: m J.Json
genLeaf = Gen.oneOf $ pure J.jsonNull :| [genJBoolean, genJNumber, genJString]
genLeaf = Gen.oneOf $ pure J.jsonNull :| [ genJBoolean, genJNumber, genJString ]

genJArray :: m J.Json
genJArray = J.fromArray <$> Gen.unfoldable (defer \_ -> genJson)
Expand Down
104 changes: 62 additions & 42 deletions test/Test/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ foldTest = do

foldFn :: Json -> String
foldFn = caseJson
(const "null")
(const "boolean")
(const "number")
(const "string")
(const "array")
(const "object")
(const "null")
(const "boolean")
(const "number")
(const "string")
(const "array")
(const "object")

cases :: Array Json
cases =
Expand All @@ -64,44 +64,63 @@ cases =

foldXXX :: Effect Unit
foldXXX = do
assert ((caseJsonNull "not null" (const "null") <$> cases) ==
["null", "not null", "not null", "not null", "not null", "not null"] <?>
"Error in caseJsonNull")
assert ((caseJsonBoolean "not boolean" (const "boolean") <$> cases) ==
["not boolean", "boolean", "not boolean", "not boolean", "not boolean", "not boolean"] <?>
"Error in caseJsonBoolean")
assert ((caseJsonNumber "not number" (const "number") <$> cases) ==
["not number", "not number", "number", "not number", "not number", "not number"] <?>
"Error in caseJsonNumber")

assert ((caseJsonString "not string" (const "string") <$> cases) ==
["not string", "not string", "not string", "string", "not string", "not string"] <?>
"Error in caseJsonString")

assert ((caseJsonArray "not array" (const "array") <$> cases) ==
["not array", "not array", "not array", "not array", "array", "not array"] <?>
"Error in caseJsonArray")
assert ((caseJsonObject "not object" (const "object") <$> cases) ==
["not object", "not object", "not object", "not object", "not object", "object"] <?>
"Error in caseJsonObject")

assert
( (caseJsonNull "not null" (const "null") <$> cases) ==
[ "null", "not null", "not null", "not null", "not null", "not null" ] <?>
"Error in caseJsonNull"
)
assert
( (caseJsonBoolean "not boolean" (const "boolean") <$> cases) ==
[ "not boolean", "boolean", "not boolean", "not boolean", "not boolean", "not boolean" ] <?>
"Error in caseJsonBoolean"
)
assert
( (caseJsonNumber "not number" (const "number") <$> cases) ==
[ "not number", "not number", "number", "not number", "not number", "not number" ] <?>
"Error in caseJsonNumber"
)

assert
( (caseJsonString "not string" (const "string") <$> cases) ==
[ "not string", "not string", "not string", "string", "not string", "not string" ] <?>
"Error in caseJsonString"
)

assert
( (caseJsonArray "not array" (const "array") <$> cases) ==
[ "not array", "not array", "not array", "not array", "array", "not array" ] <?>
"Error in caseJsonArray"
)
assert
( (caseJsonObject "not object" (const "object") <$> cases) ==
[ "not object", "not object", "not object", "not object", "not object", "object" ] <?>
"Error in caseJsonObject"
)

fromTest :: Effect Unit
fromTest = do
assert ((caseJsonNull false (const true) jsonNull) <?> "Error in fromNull")
quickCheck (\bool -> caseJsonBoolean Nothing Just (fromBoolean bool) == Just bool <?> "Error in fromBoolean")
quickCheck (\num -> caseJsonNumber Nothing Just (fromNumber num) == Just num <?> "Error in fromNumber")
quickCheck (\str -> caseJsonString Nothing Just (fromString str) == Just str <?> "Error in fromString")
quickCheck (\num ->
let arr :: Array Json
arr = A.singleton (fromNumber num)
in (caseJsonArray Nothing Just (fromArray arr) == Just arr)
<?> "Error in fromArray")
quickCheck (\(Tuple str num) ->
let sm :: Obj.Object Json
sm = Obj.singleton str (fromNumber num)
in (caseJsonObject Nothing Just (fromObject sm) == Just sm)
<?> "Error in fromObject")
quickCheck
( \num ->
let
arr :: Array Json
arr = A.singleton (fromNumber num)
in
(caseJsonArray Nothing Just (fromArray arr) == Just arr)
<?> "Error in fromArray"
)
quickCheck
( \(Tuple str num) ->
let
sm :: Obj.Object Json
sm = Obj.singleton str (fromNumber num)
in
(caseJsonObject Nothing Just (fromObject sm) == Just sm)
<?> "Error in fromObject"
)

toTest :: Effect Unit
toTest = do
Expand All @@ -114,10 +133,11 @@ toTest = do
where
assertion :: forall a. (Eq a) => (Json -> Maybe a) -> Json -> String -> Result
assertion fn j msg =
let forCases = A.catMaybes (fn <$> cases)
exact = A.singleton $ unsafePartial fromJust $ fn j
in forCases == exact <?> msg

let
forCases = A.catMaybes (fn <$> cases)
exact = A.singleton $ unsafePartial fromJust $ fn j
in
forCases == exact <?> msg

parserTest :: Effect Unit
parserTest = do
Expand All @@ -128,7 +148,7 @@ parserTest = do
roundtripTest = do
json <- Gen.resize (const 5) genJson
let parsed = jsonParser (stringify json)
pure $ parsed == Right json <?> show (stringify <$> parsed) <> " /= " <> stringify json
pure $ parsed == Right json <?> show (stringify <$> parsed) <> " /= " <> stringify json

assert :: forall prop. Testable prop => prop -> Effect Unit
assert = quickCheck' 1
Expand Down

0 comments on commit 5cbdf37

Please sign in to comment.