protocol_tests.rs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. use btproto::protocol;
  2. struct End;
  3. struct Ping;
  4. #[test]
  5. fn minimal_syntax() {
  6. // This is the token stream that should be passed to the protocol macro:
  7. // Ident { ident: "let", span: #0 bytes(91..94) }
  8. // Ident { ident: "name", span: #0 bytes(95..99) }
  9. // Punct { ch: '=', spacing: Alone, span: #0 bytes(100..101) }
  10. // Ident { ident: "Minimal", span: #0 bytes(102..109) }
  11. // Punct { ch: ';', spacing: Alone, span: #0 bytes(109..110) }
  12. // Ident { ident: "let", span: #0 bytes(119..122) }
  13. // Ident { ident: "states", span: #0 bytes(123..129) }
  14. // Punct { ch: '=', spacing: Alone, span: #0 bytes(130..131) }
  15. // Group { delimiter: Bracket,
  16. // stream: TokenStream [Ident { ident: "Init", span: #0 bytes(133..137) }],
  17. // span: #0 bytes(132..138)
  18. // }
  19. // Punct { ch: ';', spacing: Alone, span: #0 bytes(138..139) }
  20. // Ident { ident: "Init", span: #0 bytes(148..152) }
  21. // Punct { ch: '?', spacing: Alone, span: #0 bytes(152..153) }
  22. // Ident { ident: "Activate", span: #0 bytes(153..161) }
  23. // Punct { ch: '-', spacing: Joint, span: #0 bytes(162..163) }
  24. // Punct { ch: '>', spacing: Alone, span: #0 bytes(163..164) }
  25. // Ident { ident: "End", span: #0 bytes(165..168) }
  26. // Punct { ch: ';', spacing: Alone, span: #0 bytes(168..169) }
  27. protocol! {
  28. let name = Minimal;
  29. let states = [Init];
  30. Init?Activate -> End;
  31. }
  32. }
  33. #[test]
  34. fn reply() {
  35. // Ident { ident: "let", span: #0 bytes(1529..1532) }
  36. // Ident { ident: "name", span: #0 bytes(1533..1537) }
  37. // Punct { ch: '=', spacing: Alone, span: #0 bytes(1538..1539) }
  38. // Ident { ident: "Reply", span: #0 bytes(1540..1545) }
  39. // Punct { ch: ';', spacing: Alone, span: #0 bytes(1545..1546) }
  40. // Ident { ident: "let", span: #0 bytes(1555..1558) }
  41. // Ident { ident: "states", span: #0 bytes(1559..1565) }
  42. // Punct { ch: '=', spacing: Alone, span: #0 bytes(1566..1567) }
  43. // Group {
  44. // delimiter: Bracket,
  45. // stream: TokenStream [
  46. // Ident { ident: "ServerInit", span: #0 bytes(1582..1592) },
  47. // Punct { ch: ',', spacing: Alone, span: #0 bytes(1592..1593) },
  48. // Ident { ident: "Listening", span: #0 bytes(1594..1603) },
  49. // Punct { ch: ',', spacing: Alone, span: #0 bytes(1603..1604) },
  50. // Ident { ident: "Client", span: #0 bytes(1617..1623) },
  51. // Punct { ch: ',', spacing: Alone, span: #0 bytes(1623..1624) },
  52. // Ident { ident: "Waiting", span: #0 bytes(1625..1632) },
  53. // Punct { ch: ',', spacing: Alone, span: #0 bytes(1632..1633) }
  54. // ],
  55. // span: #0 bytes(1568..1643)
  56. // }
  57. // Punct { ch: ';', spacing: Alone, span: #0 bytes(1643..1644) }
  58. // Ident { ident: "ServerInit", span: #0 bytes(1653..1663) }
  59. // Punct { ch: '?', spacing: Alone, span: #0 bytes(1663..1664) }
  60. // Ident { ident: "Activate", span: #0 bytes(1664..1672) }
  61. // Punct { ch: '-', spacing: Joint, span: #0 bytes(1673..1674) }
  62. // Punct { ch: '>', spacing: Alone, span: #0 bytes(1674..1675) }
  63. // Ident { ident: "Listening", span: #0 bytes(1676..1685) }
  64. // Punct { ch: ';', spacing: Alone, span: #0 bytes(1685..1686) }
  65. // Ident { ident: "Client", span: #0 bytes(1695..1701) }
  66. // Punct { ch: '-', spacing: Joint, span: #0 bytes(1702..1703) }
  67. // Punct { ch: '>', spacing: Alone, span: #0 bytes(1703..1704) }
  68. // Ident { ident: "Waiting", span: #0 bytes(1705..1712) }
  69. // Punct { ch: ',', spacing: Alone, span: #0 bytes(1712..1713) }
  70. // Punct { ch: '>', spacing: Alone, span: #0 bytes(1714..1715) }
  71. // Ident { ident: "service", span: #0 bytes(1715..1722) }
  72. // Group {
  73. // delimiter: Parenthesis,
  74. // stream: TokenStream [
  75. // Ident { ident: "Listening", span: #0 bytes(1723..1732) }
  76. // ],
  77. // span: #0 bytes(1722..1733)
  78. // }
  79. // Punct { ch: '!', spacing: Alone, span: #0 bytes(1733..1734) }
  80. // Ident { ident: "Ping", span: #0 bytes(1734..1738) }
  81. // Punct { ch: ';', spacing: Alone, span: #0 bytes(1738..1739) }
  82. // Ident { ident: "Listening", span: #0 bytes(1748..1757) }
  83. // Punct { ch: '?', spacing: Alone, span: #0 bytes(1757..1758) }
  84. // Ident { ident: "Ping", span: #0 bytes(1758..1762) }
  85. // Punct { ch: '-', spacing: Joint, span: #0 bytes(1763..1764) }
  86. // Punct { ch: '>', spacing: Alone, span: #0 bytes(1764..1765) }
  87. // Ident { ident: "Listening", span: #0 bytes(1766..1775) }
  88. // Punct { ch: ',', spacing: Alone, span: #0 bytes(1775..1776) }
  89. // Punct { ch: '>', spacing: Alone, span: #0 bytes(1777..1778) }
  90. // Ident { ident: "Waiting", span: #0 bytes(1778..1785) }
  91. // Punct { ch: '!', spacing: Alone, span: #0 bytes(1785..1786) }
  92. // Ident { ident: "Ping", span: #0 bytes(1786..1790) }
  93. // Punct { ch: ':', spacing: Joint, span: #0 bytes(1790..1791) }
  94. // Punct { ch: ':', spacing: Alone, span: #0 bytes(1791..1792) }
  95. // Ident { ident: "Reply", span: #0 bytes(1792..1797) }
  96. // Punct { ch: ';', spacing: Alone, span: #0 bytes(1797..1798) }
  97. // Ident { ident: "Waiting", span: #0 bytes(1807..1814) }
  98. // Punct { ch: '?', spacing: Alone, span: #0 bytes(1814..1815) }
  99. // Ident { ident: "Ping", span: #0 bytes(1815..1819) }
  100. // Punct { ch: ':', spacing: Joint, span: #0 bytes(1819..1820) }
  101. // Punct { ch: ':', spacing: Alone, span: #0 bytes(1820..1821) }
  102. // Ident { ident: "Reply", span: #0 bytes(1821..1826) }
  103. // Punct { ch: '-', spacing: Joint, span: #0 bytes(1827..1828) }
  104. // Punct { ch: '>', spacing: Alone, span: #0 bytes(1828..1829) }
  105. // Ident { ident: "End", span: #0 bytes(1830..1833) }
  106. // Punct { ch: ';', spacing: Alone, span: #0 bytes(1833..1834) }
  107. protocol! {
  108. let name = Reply;
  109. let states = [
  110. ServerInit, Listening,
  111. Client, Waiting,
  112. ];
  113. ServerInit?Activate -> Listening;
  114. Client -> Waiting, >service(Listening)!Ping;
  115. Listening?Ping -> Listening, >Waiting!Ping::Reply;
  116. Waiting?Ping::Reply -> End;
  117. }
  118. }