helper_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. * Public Domain Software
  3. *
  4. * I (Matthias Ladkau) am the author of the source code in this file.
  5. * I have placed the source code in this file in the public domain.
  6. *
  7. * For further information see: http://creativecommons.org/publicdomain/zero/1.0/
  8. */
  9. package parser
  10. import (
  11. "testing"
  12. )
  13. func TestLABuffer(t *testing.T) {
  14. buf := NewLABuffer(Lex("test", "1 2 3 4 5 6 7 8 9"), 3)
  15. if token, ok := buf.Next(); token.Val != "1" || !ok {
  16. t.Error("Unexpected result: ", token, ok)
  17. return
  18. }
  19. if token, ok := buf.Next(); token.Val != "2" || !ok {
  20. t.Error("Unexpected result: ", token, ok)
  21. return
  22. }
  23. // Check Peek
  24. if token, ok := buf.Peek(0); token.Val != "3" || !ok {
  25. t.Error("Unexpected result: ", token, ok)
  26. return
  27. }
  28. if token, ok := buf.Peek(1); token.Val != "4" || !ok {
  29. t.Error("Unexpected result: ", token, ok)
  30. return
  31. }
  32. if token, ok := buf.Peek(2); token.Val != "5" || !ok {
  33. t.Error("Unexpected result: ", token, ok)
  34. return
  35. }
  36. if token, ok := buf.Peek(3); token.ID != TokenEOF || ok {
  37. t.Error("Unexpected result: ", token, ok)
  38. return
  39. }
  40. // Continue
  41. if token, ok := buf.Next(); token.Val != "3" || !ok {
  42. t.Error("Unexpected result: ", token, ok)
  43. return
  44. }
  45. if token, ok := buf.Next(); token.Val != "4" || !ok {
  46. t.Error("Unexpected result: ", token, ok)
  47. return
  48. }
  49. if token, ok := buf.Next(); token.Val != "5" || !ok {
  50. t.Error("Unexpected result: ", token, ok)
  51. return
  52. }
  53. if token, ok := buf.Next(); token.Val != "6" || !ok {
  54. t.Error("Unexpected result: ", token, ok)
  55. return
  56. }
  57. if token, ok := buf.Next(); token.Val != "7" || !ok {
  58. t.Error("Unexpected result: ", token, ok)
  59. return
  60. }
  61. if token, ok := buf.Next(); token.Val != "8" || !ok {
  62. t.Error("Unexpected result: ", token, ok)
  63. return
  64. }
  65. // Check Peek
  66. if token, ok := buf.Peek(0); token.Val != "9" || !ok {
  67. t.Error("Unexpected result: ", token, ok)
  68. return
  69. }
  70. if token, ok := buf.Peek(1); token.ID != TokenEOF || !ok {
  71. t.Error("Unexpected result: ", token, ok)
  72. return
  73. }
  74. if token, ok := buf.Peek(2); token.ID != TokenEOF || ok {
  75. t.Error("Unexpected result: ", token, ok)
  76. return
  77. }
  78. // Continue
  79. if token, ok := buf.Next(); token.Val != "9" || !ok {
  80. t.Error("Unexpected result: ", token, ok)
  81. return
  82. }
  83. // Check Peek
  84. if token, ok := buf.Peek(0); token.ID != TokenEOF || !ok {
  85. t.Error("Unexpected result: ", token, ok)
  86. return
  87. }
  88. if token, ok := buf.Peek(1); token.ID != TokenEOF || ok {
  89. t.Error("Unexpected result: ", token, ok)
  90. return
  91. }
  92. // Continue
  93. if token, ok := buf.Next(); token.ID != TokenEOF || !ok {
  94. t.Error("Unexpected result: ", token, ok)
  95. return
  96. }
  97. // New Buffer
  98. buf = NewLABuffer(Lex("test", "1 2 3"), 3)
  99. if token, ok := buf.Next(); token.Val != "1" || !ok {
  100. t.Error("Unexpected result: ", token, ok)
  101. return
  102. }
  103. if token, ok := buf.Next(); token.Val != "2" || !ok {
  104. t.Error("Unexpected result: ", token, ok)
  105. return
  106. }
  107. // Check Peek
  108. if token, ok := buf.Peek(0); token.Val != "3" || !ok {
  109. t.Error("Unexpected result: ", token, ok)
  110. return
  111. }
  112. if token, ok := buf.Peek(1); token.ID != TokenEOF || !ok {
  113. t.Error("Unexpected result: ", token, ok)
  114. return
  115. }
  116. if token, ok := buf.Peek(2); token.ID != TokenEOF || ok {
  117. t.Error("Unexpected result: ", token, ok)
  118. return
  119. }
  120. if token, ok := buf.Next(); token.Val != "3" || !ok {
  121. t.Error("Unexpected result: ", token, ok)
  122. return
  123. }
  124. if token, ok := buf.Next(); token.ID != TokenEOF || !ok {
  125. t.Error("Unexpected result: ", token, ok)
  126. return
  127. }
  128. // New Buffer - test edge case
  129. buf = NewLABuffer(Lex("test", ""), 0)
  130. if token, ok := buf.Peek(0); token.ID != TokenEOF || !ok {
  131. t.Error("Unexpected result: ", token, ok)
  132. return
  133. }
  134. if token, ok := buf.Next(); token.ID != TokenEOF || !ok {
  135. t.Error("Unexpected result: ", token, ok)
  136. return
  137. }
  138. if token, ok := buf.Peek(0); token.ID != TokenEOF || ok {
  139. t.Error("Unexpected result: ", token, ok)
  140. return
  141. }
  142. if token, ok := buf.Next(); token.ID != TokenEOF || ok {
  143. t.Error("Unexpected result: ", token, ok)
  144. return
  145. }
  146. }