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