From 5ab28d87496711fe133389d3cff99d78da8345f0 Mon Sep 17 00:00:00 2001 From: Jordan Martinez Date: Fri, 30 Jun 2023 15:03:44 -0500 Subject: [PATCH 1/3] Format code via purs-tidy --- src/Node/Buffer/Immutable.purs | 8 +- src/Node/Buffer/Internal.purs | 4 +- src/Node/Buffer/Types.purs | 16 +- src/Node/Encoding.purs | 30 +-- test/Test/Node/Buffer/Class.purs | 268 ++++++++++++++------------- test/Test/Node/Buffer/Immutable.purs | 65 +++---- test/Test/Node/Buffer/ST.purs | 2 +- 7 files changed, 200 insertions(+), 193 deletions(-) diff --git a/src/Node/Buffer/Immutable.purs b/src/Node/Buffer/Immutable.purs index a284ec3..9d5d731 100644 --- a/src/Node/Buffer/Immutable.purs +++ b/src/Node/Buffer/Immutable.purs @@ -73,8 +73,8 @@ foreign import readImpl :: String -> Offset -> ImmutableBuffer -> Number readString :: Encoding -> Offset -> Offset -> ImmutableBuffer -> String readString = readStringImpl <<< encodingToNode -foreign import readStringImpl :: - String -> Offset -> Offset -> ImmutableBuffer -> String +foreign import readStringImpl + :: String -> Offset -> Offset -> ImmutableBuffer -> String -- | Reads the buffer as a string with the specified encoding. toString :: Encoding -> ImmutableBuffer -> String @@ -92,8 +92,8 @@ foreign import toArrayBuffer :: ImmutableBuffer -> ArrayBuffer getAtOffset :: Offset -> ImmutableBuffer -> Maybe Octet getAtOffset = getAtOffsetImpl Just Nothing -foreign import getAtOffsetImpl :: - (Octet -> Maybe Octet) -> Maybe Octet -> Offset -> ImmutableBuffer -> Maybe Octet +foreign import getAtOffsetImpl + :: (Octet -> Maybe Octet) -> Maybe Octet -> Offset -> ImmutableBuffer -> Maybe Octet -- | Concatenates a list of buffers. foreign import concat :: Array ImmutableBuffer -> ImmutableBuffer diff --git a/src/Node/Buffer/Internal.purs b/src/Node/Buffer/Internal.purs index 427b6ac..94b40ff 100644 --- a/src/Node/Buffer/Internal.purs +++ b/src/Node/Buffer/Internal.purs @@ -82,8 +82,8 @@ foreign import writeInternal :: forall buf m. String -> Number -> Offset -> buf writeString :: forall buf m. Monad m => Encoding -> Offset -> Int -> String -> buf -> m Int writeString = writeStringInternal <<< encodingToNode -foreign import writeStringInternal :: - forall buf m. String -> Offset -> Int -> String -> buf -> m Int +foreign import writeStringInternal + :: forall buf m. String -> Offset -> Int -> String -> buf -> m Int toArray :: forall buf m. Monad m => buf -> m (Array Octet) toArray = usingFromImmutable Immutable.toArray diff --git a/src/Node/Buffer/Types.purs b/src/Node/Buffer/Types.purs index c6b2489..3dc7c8c 100644 --- a/src/Node/Buffer/Types.purs +++ b/src/Node/Buffer/Types.purs @@ -31,17 +31,17 @@ data BufferValueType | DoubleBE instance showBufferValueType :: Show BufferValueType where - show UInt8 = "UInt8" + show UInt8 = "UInt8" show UInt16LE = "UInt16LE" show UInt16BE = "UInt16BE" show UInt32LE = "UInt32LE" show UInt32BE = "UInt32BE" - show Int8 = "Int8" - show Int16LE = "Int16LE" - show Int16BE = "Int16BE" - show Int32LE = "Int32LE" - show Int32BE = "Int32BE" - show FloatLE = "FloatLE" - show FloatBE = "FloatBE" + show Int8 = "Int8" + show Int16LE = "Int16LE" + show Int16BE = "Int16BE" + show Int32LE = "Int32LE" + show Int32BE = "Int32BE" + show FloatLE = "FloatLE" + show FloatBE = "FloatBE" show DoubleLE = "DoubleLE" show DoubleBE = "DoubleBE" diff --git a/src/Node/Encoding.purs b/src/Node/Encoding.purs index b760603..86c53c5 100644 --- a/src/Node/Encoding.purs +++ b/src/Node/Encoding.purs @@ -1,5 +1,5 @@ module Node.Encoding - ( Encoding (..) + ( Encoding(..) , encodingToNode , byteLength ) where @@ -17,26 +17,26 @@ data Encoding | Hex instance showEncoding :: Show Encoding where - show ASCII = "ASCII" - show UTF8 = "UTF8" + show ASCII = "ASCII" + show UTF8 = "UTF8" show UTF16LE = "UTF16LE" - show UCS2 = "UCS2" - show Base64 = "Base64" - show Latin1 = "Latin1" - show Binary = "Binary" - show Hex = "Hex" + show UCS2 = "UCS2" + show Base64 = "Base64" + show Latin1 = "Latin1" + show Binary = "Binary" + show Hex = "Hex" -- | Convert an `Encoding` to a `String` in the format expected by Node.js -- | APIs. encodingToNode :: Encoding -> String -encodingToNode ASCII = "ascii" -encodingToNode UTF8 = "utf8" +encodingToNode ASCII = "ascii" +encodingToNode UTF8 = "utf8" encodingToNode UTF16LE = "utf16le" -encodingToNode UCS2 = "ucs2" -encodingToNode Base64 = "base64" -encodingToNode Latin1 = "latin1" -encodingToNode Binary = "binary" -encodingToNode Hex = "hex" +encodingToNode UCS2 = "ucs2" +encodingToNode Base64 = "base64" +encodingToNode Latin1 = "latin1" +encodingToNode Binary = "binary" +encodingToNode Hex = "hex" foreign import byteLengthImpl :: String -> String -> Int diff --git a/test/Test/Node/Buffer/Class.purs b/test/Test/Node/Buffer/Class.purs index be8ca0d..7ebd53e 100644 --- a/test/Test/Node/Buffer/Class.purs +++ b/test/Test/Node/Buffer/Class.purs @@ -13,8 +13,12 @@ import Node.Encoding (Encoding(..)) import Test.Assert (assertEqual) import Type.Proxy (Proxy) -testMutableBuffer :: forall buf m. MutableBuffer buf m => - Proxy buf -> (forall a. m a -> Effect a) -> Effect Unit +testMutableBuffer + :: forall buf m + . MutableBuffer buf m + => Proxy buf + -> (forall a. m a -> Effect a) + -> Effect Unit testMutableBuffer _ run = do log " - create" @@ -63,136 +67,136 @@ testMutableBuffer _ run = do testGetAtOffset where - testCreate :: Effect Unit - testCreate = do - buf <- run ((create 3 :: m buf) >>= toArray) - assertEqual {expected: [0, 0, 0], actual: buf} - - testFreeze :: Effect Unit - testFreeze = do - buf <- Immutable.toArray <$> run ((fromArray [1, 2, 3] :: m buf) >>= freeze) - assertEqual {expected: [1, 2, 3], actual: buf} - - testThaw :: Effect Unit - testThaw = do - buf <- run ((thaw (Immutable.fromArray [1, 2, 3]) :: m buf) >>= toArray) - assertEqual {expected: [1, 2, 3], actual: buf} - - testReadWrite :: Effect Unit - testReadWrite = do - let val = 42.0 - readVal <- run do - buf <- create 1 :: m buf - write UInt8 val 0 buf - read UInt8 0 buf - - assertEqual {expected: val, actual: readVal} - - testFromArray :: Effect Unit - testFromArray = do - readVal <- run do - buf <- fromArray [1,2,3,4,5] :: m buf - read UInt8 2 buf - - assertEqual {expected: 3.0, actual: readVal} - - testToArray :: Effect Unit - testToArray = do - let val = [1,2,67,3,3,7,8,3,4,237] - valOut <- run do - buf <- fromArray val :: m buf - toArray buf - - assertEqual {expected: val, actual: valOut} - - testFromString :: Effect Unit - testFromString = do - let str = "hello, world" - val <- run do - buf <- fromString str ASCII :: m buf - read UInt8 6 buf - - assertEqual {expected: 32.0, actual: val} -- ASCII space - - testToFromArrayBuffer :: Effect Unit - testToFromArrayBuffer = do - buf <- run $ - fromArray [1, 2, 3] + testCreate :: Effect Unit + testCreate = do + buf <- run ((create 3 :: m buf) >>= toArray) + assertEqual { expected: [ 0, 0, 0 ], actual: buf } + + testFreeze :: Effect Unit + testFreeze = do + buf <- Immutable.toArray <$> run ((fromArray [ 1, 2, 3 ] :: m buf) >>= freeze) + assertEqual { expected: [ 1, 2, 3 ], actual: buf } + + testThaw :: Effect Unit + testThaw = do + buf <- run ((thaw (Immutable.fromArray [ 1, 2, 3 ]) :: m buf) >>= toArray) + assertEqual { expected: [ 1, 2, 3 ], actual: buf } + + testReadWrite :: Effect Unit + testReadWrite = do + let val = 42.0 + readVal <- run do + buf <- create 1 :: m buf + write UInt8 val 0 buf + read UInt8 0 buf + + assertEqual { expected: val, actual: readVal } + + testFromArray :: Effect Unit + testFromArray = do + readVal <- run do + buf <- fromArray [ 1, 2, 3, 4, 5 ] :: m buf + read UInt8 2 buf + + assertEqual { expected: 3.0, actual: readVal } + + testToArray :: Effect Unit + testToArray = do + let val = [ 1, 2, 67, 3, 3, 7, 8, 3, 4, 237 ] + valOut <- run do + buf <- fromArray val :: m buf + toArray buf + + assertEqual { expected: val, actual: valOut } + + testFromString :: Effect Unit + testFromString = do + let str = "hello, world" + val <- run do + buf <- fromString str ASCII :: m buf + read UInt8 6 buf + + assertEqual { expected: 32.0, actual: val } -- ASCII space + + testToFromArrayBuffer :: Effect Unit + testToFromArrayBuffer = do + buf <- run $ + fromArray [ 1, 2, 3 ] >>= (toArrayBuffer :: buf -> m ArrayBuffer) >>= (fromArrayBuffer :: ArrayBuffer -> m buf) >>= toArray - assertEqual {expected: [1, 2, 3], actual: buf} - - testToString :: Effect Unit - testToString = do - let str = "hello, world" - strOut <- run do - buf <- fromString str ASCII :: m buf - toString ASCII buf - - assertEqual {expected: str, actual: strOut} - - testReadString :: Effect Unit - testReadString = do - let str = "hello, world" - strOut <- run do - buf <- fromString str ASCII :: m buf - readString ASCII 7 12 buf - - assertEqual {expected: "world", actual: strOut} - - testSlice :: Effect Unit - testSlice = do - {bufArr, bufSliceArr} <- run do - buf <- fromArray [1, 2, 3, 4] :: m buf - let bufSlice = slice 1 3 buf - setAtOffset 42 1 bufSlice - bufArr <- toArray buf - bufSliceArr <- toArray bufSlice - pure {bufArr, bufSliceArr} - - assertEqual {expected: [1, 2, 42, 4], actual: bufArr} - assertEqual {expected: [2, 42], actual: bufSliceArr} - - testCopy :: Effect Unit - testCopy = do - {copied, out} <- run do - buf1 <- fromArray [1,2,3,4,5] :: m buf - buf2 <- fromArray [10,9,8,7,6] - copied <- copy 0 3 buf1 2 buf2 - out <- toArray buf2 - pure {copied, out} - - assertEqual {expected: 3, actual: copied} - assertEqual {expected: [10,9,1,2,3], actual: out} - - testFill :: Effect Unit - testFill = do - out <- run do - buf <- fromArray [1,1,1,1,1] :: m buf - fill 42 2 4 buf - toArray buf - - assertEqual {expected: [1,1,42,42,1], actual: out} - - testConcat' :: Effect Unit - testConcat' = do - out <- run do - bufs <- traverse (fromArray :: Array Int -> m buf) $ map (\x -> [x, x+1, x+2]) [0,3,6,9,12] - buf <- concat' bufs 15 - toArray buf - - assertEqual {expected: [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14], actual: out} - - testGetAtOffset :: Effect Unit - testGetAtOffset = do - {o1, o4, om1} <- run do - buf <- fromArray [1, 2, 3, 4] :: m buf - o1 <- getAtOffset 1 buf - o4 <- getAtOffset 4 buf - om1 <- getAtOffset (-1) buf - pure {o1, o4, om1} - - assertEqual {expected: Just 2, actual: o1} - assertEqual {expected: Nothing, actual: o4} - assertEqual {expected: Nothing, actual: om1} + assertEqual { expected: [ 1, 2, 3 ], actual: buf } + + testToString :: Effect Unit + testToString = do + let str = "hello, world" + strOut <- run do + buf <- fromString str ASCII :: m buf + toString ASCII buf + + assertEqual { expected: str, actual: strOut } + + testReadString :: Effect Unit + testReadString = do + let str = "hello, world" + strOut <- run do + buf <- fromString str ASCII :: m buf + readString ASCII 7 12 buf + + assertEqual { expected: "world", actual: strOut } + + testSlice :: Effect Unit + testSlice = do + { bufArr, bufSliceArr } <- run do + buf <- fromArray [ 1, 2, 3, 4 ] :: m buf + let bufSlice = slice 1 3 buf + setAtOffset 42 1 bufSlice + bufArr <- toArray buf + bufSliceArr <- toArray bufSlice + pure { bufArr, bufSliceArr } + + assertEqual { expected: [ 1, 2, 42, 4 ], actual: bufArr } + assertEqual { expected: [ 2, 42 ], actual: bufSliceArr } + + testCopy :: Effect Unit + testCopy = do + { copied, out } <- run do + buf1 <- fromArray [ 1, 2, 3, 4, 5 ] :: m buf + buf2 <- fromArray [ 10, 9, 8, 7, 6 ] + copied <- copy 0 3 buf1 2 buf2 + out <- toArray buf2 + pure { copied, out } + + assertEqual { expected: 3, actual: copied } + assertEqual { expected: [ 10, 9, 1, 2, 3 ], actual: out } + + testFill :: Effect Unit + testFill = do + out <- run do + buf <- fromArray [ 1, 1, 1, 1, 1 ] :: m buf + fill 42 2 4 buf + toArray buf + + assertEqual { expected: [ 1, 1, 42, 42, 1 ], actual: out } + + testConcat' :: Effect Unit + testConcat' = do + out <- run do + bufs <- traverse (fromArray :: Array Int -> m buf) $ map (\x -> [ x, x + 1, x + 2 ]) [ 0, 3, 6, 9, 12 ] + buf <- concat' bufs 15 + toArray buf + + assertEqual { expected: [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 ], actual: out } + + testGetAtOffset :: Effect Unit + testGetAtOffset = do + { o1, o4, om1 } <- run do + buf <- fromArray [ 1, 2, 3, 4 ] :: m buf + o1 <- getAtOffset 1 buf + o4 <- getAtOffset 4 buf + om1 <- getAtOffset (-1) buf + pure { o1, o4, om1 } + + assertEqual { expected: Just 2, actual: o1 } + assertEqual { expected: Nothing, actual: o4 } + assertEqual { expected: Nothing, actual: om1 } diff --git a/test/Test/Node/Buffer/Immutable.purs b/test/Test/Node/Buffer/Immutable.purs index 5093bdb..522f299 100644 --- a/test/Test/Node/Buffer/Immutable.purs +++ b/test/Test/Node/Buffer/Immutable.purs @@ -55,76 +55,79 @@ test = do testSize buffer123 :: ImmutableBuffer -buffer123 = Immutable.fromArray [1, 2, 3] +buffer123 = Immutable.fromArray [ 1, 2, 3 ] testShow :: Effect Unit testShow = do - assertEqual {expected: "", actual: show buffer123} + assertEqual { expected: "", actual: show buffer123 } testEq :: Effect Unit testEq = do assertTrue $ buffer123 == buffer123 - assertTrue $ buffer123 == Immutable.fromArray [1, 2, 3] - assertTrue $ buffer123 /= Immutable.fromArray [1, 2, 4] - assertTrue $ buffer123 /= Immutable.fromArray [1, 2] + assertTrue $ buffer123 == Immutable.fromArray [ 1, 2, 3 ] + assertTrue $ buffer123 /= Immutable.fromArray [ 1, 2, 4 ] + assertTrue $ buffer123 /= Immutable.fromArray [ 1, 2 ] testCompare :: Effect Unit testCompare = do - assertEqual {expected: EQ, actual: compare buffer123 buffer123} - assertEqual {expected: LT, actual: compare buffer123 $ Immutable.fromArray [3, 2, 1]} - assertEqual {expected: GT, actual: compare buffer123 $ Immutable.fromArray [0, 1, 2]} + assertEqual { expected: EQ, actual: compare buffer123 buffer123 } + assertEqual { expected: LT, actual: compare buffer123 $ Immutable.fromArray [ 3, 2, 1 ] } + assertEqual { expected: GT, actual: compare buffer123 $ Immutable.fromArray [ 0, 1, 2 ] } testCreate :: Effect Unit testCreate = do - assertEqual {expected: Immutable.fromArray [], actual: Immutable.create 0} - assertEqual {expected: Immutable.fromArray [0, 0, 0], actual: Immutable.create 3} + assertEqual { expected: Immutable.fromArray [], actual: Immutable.create 0 } + assertEqual { expected: Immutable.fromArray [ 0, 0, 0 ], actual: Immutable.create 3 } testFromString :: Effect Unit testFromString = do let buf = Immutable.fromString "hello, world" ASCII - assertEqual {expected: 32.0, actual: Immutable.read UInt8 6 buf} + assertEqual { expected: 32.0, actual: Immutable.read UInt8 6 buf } testToString :: Effect Unit testToString = do - let str = "hello, world" - str' = Immutable.toString ASCII $ Immutable.fromString str ASCII - assertEqual {expected: str, actual: str'} + let + str = "hello, world" + str' = Immutable.toString ASCII $ Immutable.fromString str ASCII + assertEqual { expected: str, actual: str' } testToArray :: Effect Unit testToArray = do - assertEqual {expected: [1, 2, 3], actual: Immutable.toArray buffer123} + assertEqual { expected: [ 1, 2, 3 ], actual: Immutable.toArray buffer123 } testReadString :: Effect Unit testReadString = do - let str = "hello, world" - str' = Immutable.readString ASCII 7 12 $ Immutable.fromString str ASCII - assertEqual {expected: "world", actual: str'} + let + str = "hello, world" + str' = Immutable.readString ASCII 7 12 $ Immutable.fromString str ASCII + assertEqual { expected: "world", actual: str' } testGetAtOffset :: Effect Unit testGetAtOffset = do - assertEqual {expected: Just 2, actual: Immutable.getAtOffset 1 buffer123} - assertEqual {expected: Nothing, actual: Immutable.getAtOffset 99 buffer123} - assertEqual {expected: Nothing, actual: Immutable.getAtOffset (-1) buffer123} + assertEqual { expected: Just 2, actual: Immutable.getAtOffset 1 buffer123 } + assertEqual { expected: Nothing, actual: Immutable.getAtOffset 99 buffer123 } + assertEqual { expected: Nothing, actual: Immutable.getAtOffset (-1) buffer123 } testToFromArrayBuffer :: Effect Unit testToFromArrayBuffer = do - assertEqual {expected: buffer123, actual: Immutable.fromArrayBuffer $ Immutable.toArrayBuffer buffer123} + assertEqual { expected: buffer123, actual: Immutable.fromArrayBuffer $ Immutable.toArrayBuffer buffer123 } testConcat' :: Effect Unit testConcat' = do - let bufs = map (\x -> Immutable.fromArray [x, x+1, x+2]) [0,3,6,9,12] - buf = Immutable.concat' bufs 15 - out = Immutable.toArray buf + let + bufs = map (\x -> Immutable.fromArray [ x, x + 1, x + 2 ]) [ 0, 3, 6, 9, 12 ] + buf = Immutable.concat' bufs 15 + out = Immutable.toArray buf - assertEqual {expected: [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14], actual: out} + assertEqual { expected: [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 ], actual: out } testSlice :: Effect Unit testSlice = do - assertEqual {expected: buffer123, actual: Immutable.slice 0 3 buffer123} - assertEqual {expected: buffer123, actual: Immutable.slice 0 4 buffer123} - assertEqual {expected: Immutable.fromArray [2], actual: Immutable.slice 1 2 buffer123} + assertEqual { expected: buffer123, actual: Immutable.slice 0 3 buffer123 } + assertEqual { expected: buffer123, actual: Immutable.slice 0 4 buffer123 } + assertEqual { expected: Immutable.fromArray [ 2 ], actual: Immutable.slice 1 2 buffer123 } testSize :: Effect Unit testSize = do - assertEqual {expected: 0, actual: Immutable.size $ Immutable.fromArray []} - assertEqual {expected: 3, actual: Immutable.size buffer123} + assertEqual { expected: 0, actual: Immutable.size $ Immutable.fromArray [] } + assertEqual { expected: 3, actual: Immutable.size buffer123 } diff --git a/test/Test/Node/Buffer/ST.purs b/test/Test/Node/Buffer/ST.purs index 783f835..399b715 100644 --- a/test/Test/Node/Buffer/ST.purs +++ b/test/Test/Node/Buffer/ST.purs @@ -23,4 +23,4 @@ test = do testRun :: Effect Unit testRun = do let buf = Immutable.toArray $ run (create 3) - assertEqual {expected: [0, 0, 0], actual: buf} + assertEqual { expected: [ 0, 0, 0 ], actual: buf } From 47219f3e1ef09510fe114fdd7ff4b5aca6b58827 Mon Sep 17 00:00:00 2001 From: Jordan Martinez Date: Fri, 30 Jun 2023 15:03:49 -0500 Subject: [PATCH 2/3] Add formatting check --- .github/workflows/ci.yml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 06ed895..8c3a618 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -15,6 +15,7 @@ jobs: - uses: purescript-contrib/setup-purescript@main with: purescript: "unstable" + purs-tidy: "latest" - uses: actions/setup-node@v2 with: @@ -33,3 +34,7 @@ jobs: run: | bower install npm run-script test --if-present + + - name: Check formatting + run: | + purs-tidy check src test From bd3b575b239d4326cb060d06e2cacfc00bb803ac Mon Sep 17 00:00:00 2001 From: Jordan Martinez Date: Fri, 30 Jun 2023 15:04:50 -0500 Subject: [PATCH 3/3] Update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index a8b5d0c..bc79822 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ New features: Bugfixes: Other improvements: +- Format code with `purs-tidy`; enforce in CI (#52 by @JordanMartinez) ## [v8.0.0](https://github.com/purescript-node/purescript-node-buffer/releases/tag/v8.0.0) - 2022-04-27