This repository has been archived by the owner on Feb 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
/
TestParser.fs
1085 lines (936 loc) · 36.5 KB
/
TestParser.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
namespace TestParser
open Xunit
open FsUnit.Xunit
open System
open FParsec
open Froto.Parser
open Froto.Parser.Ast
open Froto.Parser.Parse.Parsers
[<Xunit.Trait("Kind", "Unit")>]
module Identifiers =
[<Fact>]
let ``Identifier can be parsed`` () =
Parse.fromStringWithParser pIdent_ws "test" |> should equal "test"
[<Fact>]
let ``Identifier must start with a letter`` () =
fun () -> Parse.fromStringWithParser pIdent_ws "1st" |> ignore
|> should throw typeof<System.FormatException>
[<Fact>]
let ``Identifier can contain A-Za-z0-9_`` () =
let ident = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_"
Parse.fromStringWithParser pIdent_ws ident |> should equal ident
[<Fact>]
let ``Identifier cannot contain '.'`` () =
fun () -> Parse.fromStringWithParser pIdent_ws "Test.Value" |> ignore
|> should throw typeof<System.FormatException>
[<Fact>]
let ``Full identifier can contain '.'`` () =
let fullIdent = "abc.def"
Parse.fromStringWithParser pFullIdent_ws fullIdent
|> should equal fullIdent
[<Fact>]
let ``Message Type can start with '.'`` () =
let fullyQualified = ".full.message.type"
Parse.fromStringWithParser pMessageType_ws fullyQualified
|> should equal fullyQualified
[<Fact>]
let ``Group name can be parsed`` () =
let groupName = "MyGroup"
Parse.fromStringWithParser pGroupName_ws groupName
|> should equal groupName
[<Fact>]
let ``Group name must begin with upper`` () =
fun () -> Parse.fromStringWithParser pGroupName_ws "myGroup" |> ignore
|> should throw typeof<System.FormatException>
[<Xunit.Trait("Kind", "Unit")>]
module Literals =
[<Fact>]
let ``Given dec, can parse intLit`` () =
Parse.fromStringWithParser pNumLit "123"
|> should equal (TIntLit 123)
[<Fact>]
let ``Given hex, can parse intLit`` () =
Parse.fromStringWithParser pNumLit "0x80"
|> should equal (TIntLit 128)
[<Fact>]
let ``Given oct, can parse intLit`` () =
Parse.fromStringWithParser pNumLit "0777"
|> should equal (TIntLit 511)
[<Fact>]
let ``Verify negative intLit is supported`` () =
Parse.fromStringWithParser pNumLit "-123"
|> should equal (TIntLit -123)
[<Fact>]
let ``Verify fieldNum < 1 is unsupported`` () =
fun () -> Parse.fromStringWithParser pFieldNum "0" |> ignore
|> should throw typeof<System.FormatException>
fun () -> Parse.fromStringWithParser pFieldNum "-123" |> ignore
|> should throw typeof<System.FormatException>
[<Fact>]
let ``Verify fieldNum >= 2^29 is unsupported`` () =
fun () -> Parse.fromStringWithParser pFieldNum "536870912" |> ignore
|> should throw typeof<System.FormatException>
[<Fact>]
let ``Given float, can parse floatLit`` () =
Parse.fromStringWithParser pNumLit "123.456"
|> should equal (TFloatLit 123.456)
[<Fact>]
let ``Given float with exp, can parse floatLit`` () =
Parse.fromStringWithParser pNumLit "1.23456E2"
|> should equal (TFloatLit 123.456)
[<Fact>]
let ``Verify negative floatLit is supported`` () =
Parse.fromStringWithParser pNumLit "-123.456"
|> should equal (TFloatLit -123.456)
[<Fact>]
let ``Verify negative exp is supported`` () =
Parse.fromStringWithParser pNumLit "123456e-3"
|> should equal (TFloatLit 123.456)
[<Fact>]
let ``Given list of bools, can parse boolLit`` () =
[ "true", (TBoolLit true)
"false", (TBoolLit false) ]
|> List.iter ( fun (given, expected) ->
Parse.fromStringWithParser pBoolLit given
|> should equal expected
)
[<Fact>]
let ``Simple string literal can be parsed`` () =
Parse.fromStringWithParser pStrLit "\"Test\""
|> should equal (TStrLit "Test")
[<Fact>]
let ``Simple string between squotes can be parsed`` () =
Parse.fromStringWithParser pStrLit "'Test'"
|> should equal (TStrLit "Test")
[<Fact>]
let ``String with quote between squotes can be parsed`` () =
Parse.fromStringWithParser pStrLit "'Te\"st'"
|> should equal (TStrLit "Te\"st")
[<Fact>]
let ``String with squote between quotes can be parsed`` () =
Parse.fromStringWithParser pStrLit "\"Test's\""
|> should equal (TStrLit "Test's")
[<Fact>]
let ``Escaped characters can be parsed`` () =
Parse.fromStringWithParser pStrLit ("\"" + @"\a\b\f\n\r\t\v\\\'\" + "\"\"")
|> should equal (TStrLit "\a\b\f\n\r\t\v\\\'\"")
Parse.fromStringWithParser pStrLit (@"'\a\b\f\n\r\t\v\\\'\" + "\"'")
|> should equal (TStrLit "\a\b\f\n\r\t\v\\\'\"")
[<Fact>]
let ``Hex escape can be parsed`` () =
Parse.fromStringWithParser pStrLit @"'\x30\x31\x32'"
|> should equal (TStrLit "012")
[<Fact>]
let ``Hex throws on invalid digit`` () =
fun () -> Parse.fromStringWithParser pStrLit @"'\xFG'" |> ignore
|> should throw typeof<System.FormatException>
[<Fact>]
let ``Octal escape can be parsed`` () =
Parse.fromStringWithParser pStrLit @"'\101\040\102'"
|> should equal (TStrLit "A B")
[<Fact>]
let ``Octal throws on invalid digit`` () =
fun () -> Parse.fromStringWithParser pStrLit @"'\008'" |> ignore
|> should throw typeof<System.FormatException>
fun () -> Parse.fromStringWithParser pStrLit @"'\009'" |> ignore
|> should throw typeof<System.FormatException>
[<Xunit.Trait("Kind", "Unit")>]
module SyntaxStatement =
[<Fact>]
let ``Syntax 'proto2' parses`` () =
Parse.fromStringWithParser pSyntax @"syntax = 'proto2';"
|> should equal (TSyntax TProto2)
[<Fact>]
let ``Syntax 'proto3' parses`` () =
Parse.fromStringWithParser pSyntax @"syntax = 'proto3';"
|> should equal (TSyntax TProto3)
[<Fact>]
let ``Invalid syntax throws`` () =
fun () -> Parse.fromStringWithParser pSyntax @"syntax = 'protox';" |> ignore
|> should throw typeof<System.FormatException>
[<Xunit.Trait("Kind", "Unit")>]
module ImportStatement =
[<Fact>]
let ``Import parses`` () =
Parse.fromStringWithParser pImport @"import 'test.proto';"
|> should equal (TImport ("test.proto", TNormal))
[<Fact>]
let ``Import public parses`` () =
Parse.fromStringWithParser pImport @"import public 'test.proto';"
|> should equal (TImport ("test.proto", TPublic))
[<Fact>]
let ``Import weak parses`` () =
Parse.fromStringWithParser pImport @"import weak 'test.proto';"
|> should equal (TImport ("test.proto", TWeak))
[<Xunit.Trait("Kind", "Unit")>]
module PackageStatement =
[<Fact>]
let ``Package parses`` () =
Parse.fromStringWithParser pPackage @"package abc.def;"
|> should equal (TPackage "abc.def")
[<Xunit.Trait("Kind", "Unit")>]
module OptionStatement =
[<Fact>]
let ``Simple option parses`` () =
Parse.fromStringWithParser pOptionStatement @"option test=1;"
|> should equal (TOption ("test", TIntLit 1))
[<Fact>]
let ``Option handles int literal`` () =
Parse.fromStringWithParser pOptionStatement @"option test = 1;"
|> should equal (TOption ("test", TIntLit 1))
[<Fact>]
let ``Option handles float literal`` () =
Parse.fromStringWithParser pOptionStatement @"option test = 1.2;"
|> should equal (TOption ("test", TFloatLit 1.2))
[<Fact>]
let ``Option handles bool literal`` () =
Parse.fromStringWithParser pOptionStatement @"option test = false;"
|> should equal (TOption ("test", TBoolLit false))
[<Fact>]
let ``Option handles string literal`` () =
Parse.fromStringWithParser pOptionStatement @"option test = 'Hello!';"
|> should equal (TOption ("test", TStrLit "Hello!"))
[<Fact>]
let ``Option handles enumeration value (literal)`` () =
Parse.fromStringWithParser pOptionStatement @"option test = MY_ENUM_VALUE;"
|> should equal (TOption ("test", TEnumLit "MY_ENUM_VALUE"))
[<Fact>]
let ``Option handles qualified enumeration value (literal)`` () =
Parse.fromStringWithParser pOptionStatement @"option test = Fully.Qualified.MY_ENUM_VALUE;"
|> should equal (TOption ("test", TEnumLit "Fully.Qualified.MY_ENUM_VALUE"))
[<Fact>]
let ``Custom option parses`` () =
Parse.fromStringWithParser pOptionStatement @"option (test)=true;"
|> should equal (TOption ("test", TBoolLit true))
[<Fact>]
let ``Custom option field parses`` () =
Parse.fromStringWithParser pOptionStatement @"option (test).field=true;"
|> should equal (TOption ("test.field", TBoolLit true))
[<Fact>]
let ``Custom option dotted field parses`` () =
Parse.fromStringWithParser pOptionStatement @"option (test).field.more = true;"
|> should equal (TOption ("test.field.more", TBoolLit true))
[<Fact>]
let ``Alternate option syntax parses`` () =
let option = """option (google.api.http) = { put: "/v1/{name=projects/*/subscriptions/*}" body: "*" };"""
let result = Parse.fromStringWithParser Parse.Parsers.pOptionStatement option
let expectedResults =
[ ("put", TStrLit "/v1/{name=projects/*/subscriptions/*}");
("body", TStrLit "*") ]
let expectedResult = TOption( "google.api.http", PConstant.TAggregateOptionsLit expectedResults )
result
|> should equal (expectedResult)
[<Fact>]
let ``Numeric option parses`` () =
let option = """option (google.api.http) = { put: "/v1/{name=projects/*/subscriptions/*}" body: 1 };"""
let result = Parse.fromStringWithParser Parse.Parsers.pOptionStatement option
let expectedResults =
[ ("put", TStrLit "/v1/{name=projects/*/subscriptions/*}");
("body", TIntLit 1) ]
let expectedResult = TOption( "google.api.http", PConstant.TAggregateOptionsLit expectedResults )
result
|> should equal (expectedResult)
[<Fact>]
let ``Recursive option parses`` () =
let option = """option (google.api.http) = {
get: "/v1/messages/{message_id}"
additional_bindings {
get: "/v1/users/{user_id}/messages/{message_id}"
}
};"""
let result = Parse.fromStringWithParser Parse.Parsers.pOptionStatement option
let expectedResults =
[ ("get", TStrLit "/v1/messages/{message_id}");
("additional_bindings", TAggregateOptionsLit [ ("get", TStrLit "/v1/users/{user_id}/messages/{message_id}") ]) ]
let expectedResult = TOption( "google.api.http", PConstant.TAggregateOptionsLit expectedResults )
result
|> should equal (expectedResult)
[<Fact>]
let ``Two recursive options parses`` () =
let option = """option (google.api.http) = {
get: "/v1/messages/{message_id}"
additional_bindings {
get: "/v1/users/{user_id}/messages/{message_id}"
}
additional_bindings {
put: "/v1/users/{user_id}/messages/{message_id}"
}
};"""
let result = Parse.fromStringWithParser Parse.Parsers.pOptionStatement option
let expectedResults =
[ ("get", TStrLit "/v1/messages/{message_id}");
("additional_bindings", TAggregateOptionsLit [ ("get", TStrLit "/v1/users/{user_id}/messages/{message_id}") ])
("additional_bindings", TAggregateOptionsLit [ ("put", TStrLit "/v1/users/{user_id}/messages/{message_id}") ]) ]
let expectedResult = TOption( "google.api.http", PConstant.TAggregateOptionsLit expectedResults )
result
|> should equal (expectedResult)
[<Fact>]
let ``Two recursive options with empty statements parses`` () =
let option = """option (google.api.http) = {
;
get: "/v1/messages/{message_id}"
;
additional_bindings {
get: "/v1/users/{user_id}/messages/{message_id}"
}
;
;
additional_bindings {
put: "/v1/users/{user_id}/messages/{message_id}"
};
};"""
let result = Parse.fromStringWithParser Parse.Parsers.pOptionStatement option
let expectedResults =
[ ("get", TStrLit "/v1/messages/{message_id}");
("additional_bindings", TAggregateOptionsLit [ ("get", TStrLit "/v1/users/{user_id}/messages/{message_id}") ])
("additional_bindings", TAggregateOptionsLit [ ("put", TStrLit "/v1/users/{user_id}/messages/{message_id}") ]) ]
let expectedResult = TOption( "google.api.http", PConstant.TAggregateOptionsLit expectedResults )
result
|> should equal (expectedResult)
[<Fact>]
let ``Option with only empty statement parses`` () =
let option = """option (google.api.http) = {
;
};"""
let result = Parse.fromStringWithParser Parse.Parsers.pOptionStatement option
let expectedResults =
List.Empty
let expectedResult = TOption( "google.api.http", PConstant.TAggregateOptionsLit expectedResults )
result
|> should equal (expectedResult)
[<Xunit.Trait("Kind", "Unit")>]
module Message =
[<Fact>]
let ``Label parses`` () =
[ "required", TRequired; "optional", TOptional; "repeated", TRepeated]
|> List.iter (fun (given,expected) ->
Parse.fromStringWithParser pLabel given
|> should equal expected)
[<Fact>]
let ``Field parses`` () =
Parse.fromStringWithParser pField @"required int32 test = 1 ;"
|> should equal <| TField ("test", TRequired, TInt32, 1u, [])
[<Fact>]
let ``Field with options parses`` () =
Parse.fromStringWithParser pField @"repeated sint32 samples=2 [packed=true,(custom).option=2];"
|> should equal
<| TField( "samples",
TRepeated,
TSInt32,
2u,
[ ("packed", TBoolLit(true)); ("custom.option", TIntLit(2)) ]
)
[<Fact>]
let ``Field with alt syntax options parses`` () =
Parse.fromStringWithParser pField @"repeated sint32 samples=2 [(custom) = { option:2 another:false }];"
|> should equal
(TField( "samples",
TRepeated,
TSInt32,
2u,
[ "custom",TAggregateOptionsLit( [ ("option",TIntLit 2); ("another",TBoolLit false) ] ) ]
))
[<Fact>]
let ``Parse simple message`` () =
Parse.fromStringWithParser (ws >>. pMessage .>> ws)
"""message Echo {
required string msg=1;
required bytes blob=2 [(myopt)="yes"] ;
}"""
|> should equal
<| TMessage ("Echo",
[ TField( "msg", TRequired, TString, 1u, [] )
TField( "blob", TRequired, TBytes, 2u, [ ("myopt",TStrLit("yes")) ])
])
[<Fact>]
let ``Parse simple message with option`` () =
Parse.fromStringWithParser (ws >>. pMessage .>> ws)
"""message Echo {
option (foo) = "bar";
required string msg=1;
required bytes blob=2 [(myopt)="yes"] ;
}"""
|> should equal
<| TMessage ("Echo",
[ TMessageOption( "foo", TStrLit "bar")
TField( "msg", TRequired, TString, 1u, [] )
TField( "blob", TRequired, TBytes, 2u, [ ("myopt",TStrLit("yes")) ])
])
[<Fact>]
let ``Parse group`` () =
Parse.fromStringWithParser (ws >>. pGroup) """
optional group MyGroup = 41 {
optional string abc = 42;
}"""
|> should equal
<| TGroup( "MyGroup", TOptional, 41u,
[ TField( "abc", TOptional, TString, 42u, [] ) ]
)
[<Fact>]
let ``Parse group with option`` () =
Parse.fromStringWithParser (ws >>. pGroup) """
optional group MyGroup = 41 {
optional string abc = 42;
option (foo) = "bar";
}"""
|> should equal
<| TGroup( "MyGroup", TOptional, 41u,
[ TField( "abc", TOptional, TString, 42u, [] )
TMessageOption( "foo", TStrLit "bar" )
]
)
[<Fact>]
let ``Parse oneof`` () =
Parse.fromStringWithParser (ws >>. pOneOf) """
oneof MyOneof {
string name = 1;
}"""
|> should equal
<| TOneOf("MyOneof", [ TOneOfField("name",TString,1u,[]) ])
[<Fact>]
let ``Parse oneof with field option`` () =
Parse.fromStringWithParser (ws >>. pOneOf) """
oneof MyOneof {
string name = 1 [(foo)="bar"];
}"""
|> should equal
<| TOneOf("MyOneof", [ TOneOfField("name",TString,1u,[("foo",TStrLit("bar"))]) ])
[<Fact>]
let ``Parse oneof with multiple cases`` () =
Parse.fromStringWithParser (ws >>. pOneOf) """
oneof test_oneof {
string name = 4;
int32 age = 9;
}
"""
|> should equal
<| TOneOf("test_oneof", [ TOneOfField("name",TString,4u,[]); TOneOfField("age",TInt32,9u,[]) ])
[<Fact>]
let ``Parse map`` () =
Parse.fromStringWithParser (ws >>. pMap) """
map<string,Project> projects = 3;"""
|> should equal
<| TMap ("projects", TKString, TIdent("Project"), 3u, [])
[<Fact>]
let ``Parse map with option`` () =
Parse.fromStringWithParser (ws >>. pMap) """
map<string,Project> projects = 3 [(foo)="bar"];"""
|> should equal
<| TMap ("projects", TKString, TIdent("Project"), 3u, ["foo",TStrLit("bar")])
[<Fact>]
let ``Parse extensions`` () =
Parse.fromStringWithParser (ws >>. pExtensions) """
extensions 100 to 199;"""
|> should equal
<| TExtensions [ (100u, Some(199u)) ]
Parse.fromStringWithParser (ws >>. pExtensions) """
extensions 42, 999 to max;"""
|> should equal
<| TExtensions [
42u, None
999u, Some(UInt32.MaxValue)]
[<Fact>]
let ``Parse reserved ranges`` () =
Parse.fromStringWithParser (ws >>. pReserved) """
reserved 42, 100 to 199, 999 to max;"""
|> should equal
<| TReservedRanges [
42u, None
100u, Some(199u)
999u, Some(UInt32.MaxValue) ]
[<Fact>]
let ``Parse reserved names`` () =
Parse.fromStringWithParser (ws >>. pReserved) """
reserved "foo", "bar";"""
|> should equal
<| TReservedNames [
"foo"
"bar" ]
[<Fact>]
let ``Parse enum with option`` () =
Parse.fromStringWithParser (ws >>. pMessageEnum) """
enum EnumAllowingAlias {
option allow_alias = true;
UNKNOWN = 0;
STARTED = 1;
RUNNING = 2 [(custom_option) = "hello world"];
;
}"""
|> should equal (
TMessageEnum ("EnumAllowingAlias",
[
TEnumOption ("allow_alias", TBoolLit(true) )
TEnumField ("UNKNOWN", 0, [])
TEnumField ("STARTED", 1, [])
TEnumField ("RUNNING", 2, [ ("custom_option", TStrLit "hello world") ])
]))
[<Fact>]
let ``Parse extend`` () =
Parse.fromStringWithParser (ws >>. pExtend) """
extend Foo {
optional int32 bar = 126;
}"""
|> should equal (
TExtend("Foo",
[TExtendField("bar", TOptional, TInt32, 126u, List.empty)]))
[<Fact>]
let ``Parse group in message`` () =
Parse.fromStringWithParser (ws >>. pMessage) """
message foo {
optional group GroupMessage = 1 {
optional int32 a = 2;
}
}"""
|> should equal (
TMessage("foo",
[
TGroup("GroupMessage", TOptional, 1u,
[
TField("a", TOptional, TInt32, 2u, [])
])
]))
[<Xunit.Trait("Kind", "Unit")>]
module Service =
[<Fact>]
let ``Parse rpc`` () =
Parse.fromStringWithParser (ws >>. pRpc) """
rpc TestMethod (outer) returns (foo);"""
|> should equal (TRpc ("TestMethod", "outer", false, "foo", false, []))
[<Fact>]
let ``Parse rpc with optional option syntax`` () =
let expectedResults =
[ ("get", TStrLit "/v1/{name=projects/*/subscriptions/*}") ]
let actual =
Parse.fromStringWithParser pRpc ("""
rpc TestMethod (outer) returns (foo) {
option (google.api.http) = { get: "/v1/{name=projects/*/subscriptions/*}" };
}
""".Trim())
let expected = TRpc ("TestMethod", "outer", false, "foo", false,
[ "google.api.http", PConstant.TAggregateOptionsLit expectedResults ])
actual |> should equal expected
[<Fact>]
let ``Parse service`` () =
Parse.fromStringWithParser (ws >>. pService) """
service TestService {
rpc TestMethod (outer) returns (foo);
}"""
|> should equal (
TService ("TestService",
[
TRpc ("TestMethod", "outer", false, "foo", false, [])
]))
[<Fact>]
let ``Parse service with rpc, options, and empty statements`` () =
Parse.fromStringWithParser (ws >>. pService) """
service TestService {
; // empty
option (foo).bar = "fee";
rpc TestMethod (outer) returns (foo) {
; // empty
option (foo.baz) = "fie";
; // empty
option foo = { bat : "foe" qux : "foo" }
; // empty
}
; // empty
}"""
|> should equal (
TService ("TestService",
[
TServiceOption ( "foo.bar", TStrLit "fee")
TRpc ("TestMethod", "outer", false, "foo", false,
[
"foo.baz", TStrLit "fie"
"foo", TAggregateOptionsLit [
"bat", TStrLit "foe"
"qux", TStrLit "foo"
]
])
]))
[<Xunit.Trait("Kind", "Unit")>]
module Proto =
[<Fact>]
let ``Parse proto syntax, import, and option`` () =
Parse.fromStringWithParser pProto """
/* Comment */
/* Followed by comment */
syntax = "proto2";
import public "other.proto";
option java_package = "com.example.foo";
"""
|> should equal (
[
TSyntax TProto2
TImport ("other.proto", TPublic)
TOption ("java_package", TStrLit "com.example.foo")
]
)
[<Fact>]
let ``Parse proto enum`` () =
Parse.fromStringWithParser pProto """
enum EnumAllowingAlias {
option allow_alias = true;
UNKNOWN = 0;
STARTED = 1;
RUNNING = 2 [(custom_option) = "hello world"];
}
"""
|> should equal (
[
TEnum ("EnumAllowingAlias",
[
TEnumOption ( "allow_alias", TBoolLit true)
TEnumField ( "UNKNOWN", 0, [])
TEnumField ( "STARTED", 1, [])
TEnumField ( "RUNNING", 2, [ ("custom_option", TStrLit "hello world") ])
])
]
)
[<Fact>]
let ``Parse proto message`` () =
Parse.fromStringWithParser pProto """
message outer {
option (my_option).a = true;
message inner { // Level 2
required int64 ival = 1;
}
repeated inner inner_message = 2;
optional EnumAllowingAlias enum_field =3;
map<int32, string> my_map = 4;
extensions 20 to 30;
}
"""
|> should equal (
[
TMessage ("outer",
[
TMessageOption ("my_option.a", TBoolLit true)
TMessageMessage ( "inner", [ TField ("ival", TRequired, TInt64, 1u, [] ) ])
TField ("inner_message", TRepeated, TIdent "inner", 2u, [])
TField ("enum_field", TOptional, TIdent "EnumAllowingAlias", 3u, [])
TMap ("my_map", TKInt32, TString, 4u, [])
TExtensions ( [ (20u,Some(30u)) ])
])
]
)
[<Fact>]
let ``Parse proto group`` () =
Parse.fromStringWithParser pProto """
message foo {
optional group GroupMessage = 1 {
optional int32 a = 2;
}
}
"""
|> should equal (
[
TMessage ( "foo",
[
TGroup("GroupMessage", TOptional, 1u,
[
TField ("a", TOptional, TInt32, 2u, [])
])
])
]
)
[<Fact>]
let ``Parse proto full`` () =
Parse.fromStringWithParser pProto """
/* Comment */
/* Followed by comment */
syntax = "proto2";
import public "other.proto";
option java_package = "com.example.foo";
enum EnumAllowingAlias {
option allow_alias = true;
UNKNOWN = 0;
STARTED = 1;
RUNNING = 2 [(custom_option) = "hello world"];
}
message outer {
option (my_option).a = true;
message inner { // Level 2
required int64 ival = 1;
}
repeated inner inner_message = 2;
optional EnumAllowingAlias enum_field =3;
map<int32, string> my_map = 4;
extensions 20 to 30;
}
message foo {
optional group GroupMessage = 1 {
optional int32 a = 2;
}
}
service TestService {
rpc TestMethod (outer) returns (foo);
}
"""
|> should equal (
[
TSyntax TProto2
TImport ("other.proto", TPublic)
TOption ("java_package", TStrLit "com.example.foo")
TEnum ("EnumAllowingAlias",
[
TEnumOption ( "allow_alias", TBoolLit true)
TEnumField ( "UNKNOWN", 0, [])
TEnumField ( "STARTED", 1, [])
TEnumField ( "RUNNING", 2, [ ("custom_option", TStrLit "hello world") ])
])
TMessage ("outer",
[
TMessageOption ("my_option.a", TBoolLit true)
TMessageMessage ( "inner", [ TField ("ival", TRequired, TInt64, 1u, [] ) ])
TField ("inner_message", TRepeated, TIdent "inner", 2u, [])
TField ("enum_field", TOptional, TIdent "EnumAllowingAlias", 3u, [])
TMap ("my_map", TKInt32, TString, 4u, [])
TExtensions ( [ (20u,Some(30u)) ])
])
TMessage ( "foo",
[
TGroup("GroupMessage", TOptional, 1u,
[
TField ("a", TOptional, TInt32, 2u, [])
])
])
TService ("TestService",
[
TRpc ("TestMethod", "outer", false, "foo", false, [])
])
]
)
[<Fact>]
let ``Parse proto with optional option syntax`` () =
let expectedResults =
[ ("put", TStrLit "/v1/{name=projects/*/subscriptions/*}");
("body", TStrLit "*") ]
Parse.fromStringWithParser pProto """
syntax = "proto3";
service TestService {
rpc TestMethod (outer) returns (foo) {
option (google.api.http) = { put: "/v1/{name=projects/*/subscriptions/*}" body: "*" };
}
}
"""
|> should equal (
[
TSyntax TProto3
TService ("TestService",
[
TRpc ("TestMethod", "outer", false, "foo", false,
[
"google.api.http", PConstant.TAggregateOptionsLit expectedResults
])
])
]
)
open System.IO
/// gets the path for a test file based on the relative path from the executing assembly
let getTestFile file =
let codeBase = Reflection.Assembly.GetExecutingAssembly().CodeBase
let assemblyPath = DirectoryInfo (Uri codeBase).LocalPath
let solutionPath = (assemblyPath.Parent.Parent.Parent.Parent.Parent).FullName
Path.Combine(solutionPath, Path.Combine("test",file))
[<Fact>]
let ``Parse Google protobuf 'descriptor.proto' without error`` () =
Parse.fromFile <| getTestFile "google/protobuf/descriptor.proto"
|> ignore
[<Xunit.Trait("Kind", "Unit")>]
module Proto3 =
[<Fact>]
let ``Proto3 fields parse without optional`` () =
Parse.fromStringWithParser pProto """
syntax = "proto3";
message Test {
int32 field1 = 1;
string field2 = 2;
repeated bool field3 = 3;
}"""
|> should equal (
[
TSyntax TProto3
TMessage ("Test",
[
TField ("field1", TOptional, TInt32, 1u, [])
TField ("field2", TOptional, TString, 2u, [])
TField ("field3", TRepeated, TBool, 3u, [])
])
])
[<Fact>]
let ``Proto3 accepts stream keyword on RPC`` () =
Parse.fromStringWithParser pProto """
syntax="proto3";
service S {
rpc M (stream A) returns (stream B);
}"""
|> should equal (
[
TSyntax TProto3
TService( "S",
[
TRpc ("M", "A", true, "B", true, [])
])
])
[<Fact>]
let ``Proto3 rejects optional field`` () =
fun () -> Parse.fromStringWithParser pProto """
syntax = "proto3";
message Test {
optional int32 field1 = 1;
}""" |> ignore
|> should throw typeof<System.FormatException>
[<Fact>]
let ``Proto3 rejects required field`` () =
fun () -> Parse.fromStringWithParser pProto """
syntax = "proto3";
message Test {
required int32 field1 = 1;
}""" |> ignore
|> should throw typeof<System.FormatException>
[<Fact>]
let ``Proto3 rejects message group`` () =
fun () -> Parse.fromStringWithParser pProto """
syntax = "proto3";
message Test {
repeated group MyGroup = 1 {
repeated int32 a = 2;
}
}""" |> ignore
|> should throw typeof<System.FormatException>
[<Fact>]
let ``Proto3 rejects message extensions`` () =
fun () -> Parse.fromStringWithParser pProto """
syntax = "proto3";
message Test {
extensions 1 to 10;
}""" |> ignore
|> should throw typeof<System.FormatException>
[<Fact>]
let ``Proto3 rejects message extend`` () =
fun () -> Parse.fromStringWithParser pProto """
syntax = "proto3";
message Test {
extensions 1;
extend Test {
optional bool b = 1;
}
}
""" |> ignore
|> should throw typeof<System.FormatException>
[<Fact>]
let ``Proto3 rejects top-level extend`` () =
fun () -> Parse.fromStringWithParser pProto """
syntax = "proto3";
message Test {
extensions 1;
}
extend Test {
optional bool b = 1;
}
""" |> ignore
|> should throw typeof<System.FormatException>
[<Xunit.Trait("Kind", "Unit")>]
module Proto2 =
[<Fact>]
let ``Proto2 fields don't parse without optional`` () =
fun () -> Parse.fromStringWithParser pProto """
syntax = "proto2";
message Test {
int32 field1 = 1;
string field2 = 2;
repeated bool field3 = 3;
}""" |> ignore
|> should throw typeof<System.FormatException>
[<Fact>]
let ``Proto2 rejects stream keyword on RPC`` () =
fun () -> Parse.fromStringWithParser pProto """
syntax="proto2";
service S {
rpc M (stream A) returns (stream B);
}""" |> ignore
|> should throw typeof<System.FormatException>
[<Fact>]
let ``Proto2 accepts optional field`` () =
fun () -> Parse.fromStringWithParser pProto """
syntax = "proto2";
message Test {
optional int32 field1 = 1;
}""" |> ignore