lexer_test.go 2.5 KB

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