lexer_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. // 25
  58. {token.LET, "let"},
  59. {token.IDENT, "result"},
  60. {token.ASSIGN, "="},
  61. {token.IDENT, "add"},
  62. {token.LPAREN, "("},
  63. {token.IDENT, "five"},
  64. {token.COMMA, ","},
  65. {token.IDENT, "ten"},
  66. {token.RPAREN, ")"},
  67. {token.SEMICOLON, ";"},
  68. // 35
  69. {token.BANG, "!"},
  70. {token.MINUS, "-"},
  71. {token.SLASH, "/"},
  72. {token.ASTERISK, "*"},
  73. {token.INT, "5"},
  74. {token.SEMICOLON, ";"},
  75. // 41
  76. {token.INT, "5"},
  77. {token.LT, "<"},
  78. {token.INT, "10"},
  79. {token.GT, ">"},
  80. {token.INT, "5"},
  81. {token.SEMICOLON, ";"},
  82. // 47
  83. {token.IF, "if"},
  84. {token.LPAREN, "("},
  85. {token.INT, "5"},
  86. {token.LT, "<"},
  87. {token.INT, "10"},
  88. {token.RPAREN, ")"},
  89. {token.LBRACE, "{"},
  90. {token.RETURN, "return"},
  91. {token.TRUE, "true"},
  92. {token.SEMICOLON, ";"},
  93. {token.RBRACE, "}"},
  94. {token.ELSE, "else"},
  95. {token.LBRACE, "{"},
  96. {token.RETURN, "return"},
  97. {token.FALSE, "false"},
  98. {token.SEMICOLON, ";"},
  99. {token.RBRACE, "}"},
  100. // 64
  101. {token.INT, "10"},
  102. {token.EQ, "=="},
  103. {token.INT, "10"},
  104. {token.SEMICOLON, ";"},
  105. {token.INT, "10"},
  106. {token.NOT_EQ, "!="},
  107. {token.INT, "9"},
  108. {token.SEMICOLON, ";"},
  109. {token.ILLEGAL, "$"},
  110. {token.EOF, ""},
  111. }
  112. l := New(input)
  113. for i, tt := range tests {
  114. tok := l.NextToken()
  115. if tok.Type != tt.expectedType {
  116. t.Fatalf("tests[%d] - tokentype wrong, expected %q, got %q",
  117. i, tt.expectedType, tok.Type)
  118. }
  119. if tok.Literal != tt.expectedLiteral {
  120. t.Fatalf("tests[%d] - literal wrong, expected %q, got %q",
  121. i, tt.expectedLiteral, tok.Literal)
  122. }
  123. }
  124. }