git.iko.soy/rowtype-yoga/purescript-protobuf
on v1.7.0 help sign in
115 lines 3.3 kB history raw
1module Test.Main where
3import Prelude
5import Data.Array (catMaybes)
6import Data.ArrayBuffer.Builder (execPut)
7import Data.ArrayBuffer.DataView as DV
8import Data.ArrayBuffer.Typed as TA
9import Data.Either (Either(..))
10import Data.Float32 as Float32
11import Data.Long.Internal as Long
12import Data.Maybe (Maybe(..))
13import Data.UInt as UInt
14import Data.Unfoldable (replicate)
15import Effect (Effect)
16import Pack.Msg1 as Pack1
17import Pack.Msg2 as Pack2
18import Pack3.Msg3 as Pack3
19import Pack4.Msg4 as Pack4
20import Pack5.Msg5 as Pack5
21import Test.Assert (assert')
22import Text.Parsing.Parser (runParserT)
23import Data.TextEncoding (encodeUtf8)
24import Protobuf.Common (Bytes(..))
26billion' :: Int
27billion' = -1000000000
28billion :: UInt.UInt
29billion = UInt.fromInt 1000000000
30billion2' :: Long.Long Long.Signed
31billion2' = Long.fromLowHighBits billion' billion'
32billion2 :: Long.Long Long.Unsigned
33billion2 = Long.fromLowHighBits 1000000000 1000000000
35main :: Effect Unit
36main = do
38 let somebytes = TA.buffer $ encodeUtf8 "some bytes"
40 let msgxx = Pack1.mkMsg1 { f1: Just 1.0 }
42 let msg4 = (Pack4.mkMsg2 -- this is just a compile test
43 { f1: Just $ Pack4.mkMsg1_Msg2 { nested: Just "nested" }
44 })
46 let msg6 = (Pack5.mkMsg2 -- this is just a compile test
47 { sum1: Just $ Pack5.Msg2_Sum1_F1 $ Pack5.mkMsg1
48 { sum1: Just $ Pack5.Msg1_Sum1_F3 billion'
49 }
50 })
52 let msg1 = (Pack1.mkMsg1
53 { f1: Just 1234.5
54 , f2: Float32.fromNumber 345.6
55 , f3: Just billion'
56 , f4: Just billion2'
57 , f5: Just billion
58 , f6: Just billion2
59 , f7: Just billion'
60 , f8: Just billion2'
61 , f9: Just billion
62 , f10: Just billion2
63 , f11: Just billion2'
64 , f12: Just true
65 , f13: Just "๐Ÿ"
66 , f14: Just $ Bytes somebytes
67 , f15: Just Pack1.Msg1_E1_Three
68 })
70 ab1 <- execPut $ Pack1.putMsg1 msg1
71 let dv1 = DV.whole ab1
72 parseResult1 <- runParserT dv1 $ Pack1.parseMsg1 $ DV.byteLength dv1
73 case parseResult1 of
74 Left err -> assert' ("msg1 parse " <> show err) false
75 Right msg1' -> assert' "msg1 roundtrip" $ msg1 == msg1'
77 let msg2 = (Pack2.mkMsg2
78 { f1: replicate 3 1234.5
79 , f2: catMaybes [Float32.fromNumber 345.6, Float32.fromNumber 345.6]
80 , f3: replicate 3 billion'
81 , f4: replicate 3 billion2'
82 , f5: replicate 3 billion
83 , f6: replicate 3 billion2
84 , f7: replicate 3 billion'
85 , f8: replicate 3 billion2'
86 , f9: replicate 3 billion
87 , f10: replicate 3 billion2
88 , f11: replicate 3 billion2'
89 , f12: replicate 3 true
90 , f13: replicate 3 "๐Ÿ"
91 , f14: replicate 3 $ Bytes somebytes
92 , f15: replicate 3 Pack2.Msg2_E1_Three
93 })
95 ab2 <- execPut $ Pack2.putMsg2 msg2
96 let dv2 = DV.whole ab2
97 parseResult2 <- runParserT dv2 $ Pack2.parseMsg2 $ DV.byteLength dv2
98 case parseResult2 of
99 Left err -> assert' ("msg2 parse " <> show err) false
100 Right msg2' -> assert' "msg2 roundtrip" $ msg2 == msg2'
102 let msg3 = (Pack3.mkMsg3
103 { f1: replicate 3 msg1
104 , f2: replicate 3 msg2
105 , f4: replicate 3 (Pack3.mkMsg3_Msg4 { f1: Just 1 })
106 })
108 ab3 <- execPut $ Pack3.putMsg3 msg3
109 let dv3 = DV.whole ab3
110 parseResult3 <- runParserT dv3 $ Pack3.parseMsg3 $ DV.byteLength dv3
111 case parseResult3 of
112 Left err -> assert' ("msg3 parse " <> show err) false
113 Right msg3' -> assert' "msg3 roundtrip" $ msg3 == msg3'