lexer_test.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. package lexer
  2. import (
  3. "fgm/waiig15/token"
  4. "testing"
  5. )
  6. func TestNextToken(t *testing.T) {
  7. input := `
  8. let five = 5;
  9. let ten = 10;
  10. let add = fn(x, y) {
  11. x + y;
  12. }
  13. let result = add(five, ten);
  14. !-/*5;
  15. 5 < 10 > 5;
  16. if (5 < 10) {
  17. return true;
  18. }
  19. else {
  20. return false;
  21. }
  22. 10 == 10;
  23. 10 != 9;
  24. `
  25. tests := []struct {
  26. expectedType token.TokenType
  27. expectedLiteral string
  28. }{
  29. {token.LET, "let"},
  30. {token.IDENT, "five"},
  31. {token.ASSIGN, "="},
  32. {token.INT, "5"},
  33. {token.SEMICOLON, ";"},
  34. // 5
  35. {token.LET, "let"},
  36. {token.IDENT, "ten"},
  37. {token.ASSIGN, "="},
  38. {token.INT, "10"},
  39. {token.SEMICOLON, ";"},
  40. // 10
  41. {token.LET, "let"},
  42. {token.IDENT, "add"},
  43. {token.ASSIGN, "="},
  44. {token.FUNCTION, "fn"},
  45. {token.LPAREN, "("},
  46. {token.IDENT, "x"},
  47. {token.COMMA, ","},
  48. {token.IDENT, "y"},
  49. {token.RPAREN, ")"},
  50. {token.LBRACE, "{"},
  51. {token.IDENT, "x"},
  52. {token.PLUS, "+"},
  53. {token.IDENT, "y"},
  54. {token.SEMICOLON, ";"},
  55. {token.RBRACE, "}"},
  56. // 25
  57. {token.LET, "let"},
  58. {token.IDENT, "result"},
  59. {token.ASSIGN, "="},
  60. {token.IDENT, "add"},
  61. {token.LPAREN, "("},
  62. {token.IDENT, "five"},
  63. {token.COMMA, ","},
  64. {token.IDENT, "ten"},
  65. {token.RPAREN, ")"},
  66. {token.SEMICOLON, ";"},
  67. // 35
  68. {token.BANG, "!"},
  69. {token.MINUS, "-"},
  70. {token.SLASH, "/"},
  71. {token.ASTERISK, "*"},
  72. {token.INT, "5"},
  73. {token.SEMICOLON, ";"},
  74. // 41
  75. {token.INT, "5"},
  76. {token.LT, "<"},
  77. {token.INT, "10"},
  78. {token.GT, ">"},
  79. {token.INT, "5"},
  80. {token.SEMICOLON, ";"},
  81. // 47
  82. {token.IF, "if"},
  83. {token.LPAREN, "("},
  84. {token.INT, "5"},
  85. {token.LT, "<"},
  86. {token.INT, "10"},
  87. {token.RPAREN, ")"},
  88. {token.LBRACE, "{"},
  89. {token.RETURN, "return"},
  90. {token.TRUE, "true"},
  91. {token.SEMICOLON, ";"},
  92. {token.RBRACE, "}"},
  93. {token.ELSE, "else"},
  94. {token.LBRACE, "{"},
  95. {token.RETURN, "return"},
  96. {token.FALSE, "false"},
  97. {token.SEMICOLON, ";"},
  98. {token.RBRACE, "}"},
  99. // 64
  100. { token.INT, "10"},
  101. { token.EQ, "=="},
  102. { token.INT, "10"},
  103. { token.SEMICOLON, ";"},
  104. { token.INT, "10"},
  105. { token.NOT_EQ, "!="},
  106. { token.INT, "9"},
  107. { token.SEMICOLON, ";"},
  108. {token.EOF, ""},
  109. }
  110. l := New(input)
  111. for i, tt := range tests {
  112. tok := l.NextToken()
  113. if tok.Type != tt.expectedType {
  114. t.Fatalf("tests[%d] - tokentype wrong, expected %q, got %q",
  115. i, tt.expectedType, tok.Type)
  116. }
  117. if tok.Literal != tt.expectedLiteral {
  118. t.Fatalf("tests[%d] - literal wrong, expected %q, got %q",
  119. i, tt.expectedLiteral, tok.Literal)
  120. }
  121. }
  122. }