mirror of
https://github.com/rowtype-yoga/purescript-parsing-dataview.git
synced 2024-11-22 04:05:39 +03:00
Test UTF-8
This commit is contained in:
parent
0c2843f776
commit
b6c6a8feb7
15
README.md
15
README.md
@ -64,22 +64,21 @@ import Control.Monad.Except (ExceptT)
|
||||
import Data.ArrayBuffer.Cast (toUint8Array)
|
||||
import Effect.Exception (catchException, message)
|
||||
import Parsing (runParserT, liftExceptT)
|
||||
import Parsing.DataView (anyUint32be, takeN)
|
||||
import Data.UInt (toInt)
|
||||
import Parsing.DataView (anyInt32be, takeN)
|
||||
import Web.Encoding.TextDecoder as TextDecoder
|
||||
import Web.Encoding.UtfLabel as UtfLabel
|
||||
|
||||
do
|
||||
textDecoder <- TextDecoder.new UtfLabel.utf8
|
||||
|
||||
result :: <- runParserT dataview do
|
||||
-- Parse a 32-bit big-endian length prefix for the length
|
||||
result <- runParserT dataview do
|
||||
-- First parse a 32-bit big-endian length prefix for the length
|
||||
-- of the UTF-8 string in bytes.
|
||||
length <- anyUint32be
|
||||
stringview <- takeN $ toInt length
|
||||
length <- anyInt32be
|
||||
stringview <- takeN length
|
||||
stringarray <- lift $ liftEffect $ toUint8Array stringview
|
||||
liftExceptT $ ExceptT $ catchException (Left <<< message) do
|
||||
Right <$> TextDecoder.decode stringarray
|
||||
liftExceptT $ ExceptT $ catchException (pure <<< Left <<< message) do
|
||||
Right <$> TextDecoder.decode stringarray textDecoder
|
||||
```
|
||||
|
||||
## Serialization
|
||||
|
@ -117,8 +117,8 @@ let additions =
|
||||
-------------------------------
|
||||
-}
|
||||
let upstream =
|
||||
https://github.com/purescript/package-sets/releases/download/psc-0.15.4-20221110/packages.dhall
|
||||
sha256:55be93ee309eeb1b3a1d30c7b9fa5d18ffefa67f5fbeec1566b7b6a70b0ac218
|
||||
https://github.com/purescript/package-sets/releases/download/psc-0.15.4-20221202/packages.dhall
|
||||
sha256:4e2f2e060ab73b119eeb55fe9bc75168b380fc5e0696d2cc2941ac674b32cb3c
|
||||
|
||||
let overrides = {=}
|
||||
|
||||
|
@ -18,7 +18,8 @@ in conf //
|
||||
, "console"
|
||||
, "foldable-traversable"
|
||||
, "lists"
|
||||
, "psci-support"
|
||||
, "either"
|
||||
, "web-encoding"
|
||||
, "exceptions"
|
||||
]
|
||||
}
|
@ -2,8 +2,10 @@ module Test.Main where
|
||||
|
||||
import Prelude
|
||||
|
||||
import Control.Monad.Except (ExceptT(..))
|
||||
import Control.Monad.Trans.Class (lift)
|
||||
import Data.ArrayBuffer.ArrayBuffer (empty) as AB
|
||||
import Data.ArrayBuffer.ArrayBuffer as AB
|
||||
import Data.ArrayBuffer.Cast (fromUint8Array, toUint8Array)
|
||||
import Data.ArrayBuffer.DataView as DataView
|
||||
import Data.Either (Either(..))
|
||||
import Data.List (fromFoldable)
|
||||
@ -13,12 +15,17 @@ import Data.Traversable (for_)
|
||||
import Data.Tuple (Tuple(..))
|
||||
import Data.UInt (fromInt)
|
||||
import Effect (Effect)
|
||||
import Effect.Class (liftEffect)
|
||||
import Effect.Console (logShow)
|
||||
import Parsing (ParserT, Position(..), fail, parseErrorPosition, runParserT)
|
||||
import Effect.Exception (catchException, message)
|
||||
import Parsing (ParserT, Position(..), fail, liftExceptT, parseErrorPosition, runParserT)
|
||||
import Parsing.Combinators (many, manyTill)
|
||||
import Parsing.DataView (anyInt8)
|
||||
import Parsing.DataView as DV
|
||||
import Test.Assert (assert', assertEqual')
|
||||
import Web.Encoding.TextDecoder as TextDecoder
|
||||
import Web.Encoding.TextEncoder as TextEncoder
|
||||
import Web.Encoding.UtfLabel as UtfLabel
|
||||
|
||||
parseTestT :: forall s a. Show a => Eq a => s -> a -> ParserT s Effect a -> Effect Unit
|
||||
parseTestT input expected p = do
|
||||
@ -138,3 +145,27 @@ main = do
|
||||
{ actual: x
|
||||
, expected: Right 200000
|
||||
}
|
||||
|
||||
do
|
||||
let
|
||||
teststring = "test string 👓"
|
||||
|
||||
textDecoder <- TextDecoder.new UtfLabel.utf8
|
||||
|
||||
textEncoder <- TextEncoder.new
|
||||
|
||||
let testarray = TextEncoder.encode teststring textEncoder
|
||||
|
||||
testview <- fromUint8Array testarray
|
||||
|
||||
result <- runParserT testview do
|
||||
let length = DataView.byteLength testview
|
||||
stringview <- DV.takeN length
|
||||
stringarray <- lift $ liftEffect $ toUint8Array stringview
|
||||
liftExceptT $ ExceptT $ catchException (pure <<< Left <<< message) do
|
||||
Right <$> TextDecoder.decode stringarray textDecoder
|
||||
|
||||
assertEqual' "UTF-8 decoding example"
|
||||
{ expected: Right teststring
|
||||
, actual: result
|
||||
}
|
Loading…
Reference in New Issue
Block a user