1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
|
-module(tokenizer_test).
-include_lib("eunit/include/eunit.hrl").
tokenize(String) ->
tokenize(String, []).
tokenize(String, Opts) ->
{ok, _Line, _Column, _Warnings, Result, []} = elixir_tokenizer:tokenize(String, 1, Opts),
lists:reverse(Result).
tokenize_error(String) ->
{error, Error, _, _, _} = elixir_tokenizer:tokenize(String, 1, []),
Error.
type_test() ->
[{int, {1, 1, 1}, "1"},
{type_op, {1, 3, nil}, '::'},
{int, {1, 6, 3}, "3"}] = tokenize("1 :: 3"),
[{'true', {1, 1, nil}},
{type_op, {1, 5, nil}, '::'},
{int, {1, 7, 3}, "3"}] = tokenize("true::3"),
[{identifier, {1, 1, _}, name},
{'.', {1, 5, nil}},
{paren_identifier, {1, 6, _}, '::'},
{'(', {1, 8, nil}},
{int, {1, 9, 3}, "3"},
{')', {1, 10, nil}}] = tokenize("name.::(3)").
arithmetic_test() ->
[{int, {1, 1, 1}, "1"},
{dual_op, {1, 3, nil}, '+'},
{int, {1, 5, 2}, "2"},
{dual_op, {1, 7, nil}, '+'},
{int, {1, 9, 3}, "3"}] = tokenize("1 + 2 + 3").
op_kw_test() ->
[{atom, {1, 1, _}, foo},
{dual_op, {1, 5, nil}, '+'},
{atom, {1, 6, _}, bar}] = tokenize(":foo+:bar").
scientific_test() ->
[{flt, {1, 1, 0.1}, "1.0e-1"}] = tokenize("1.0e-1"),
[{flt, {1, 1, 0.1}, "1.0E-1"}] = tokenize("1.0E-1"),
[{flt, {1, 1, 1.2345678e-7}, "1_234.567_8e-10"}] = tokenize("1_234.567_8e-10"),
{[{line, 1}, {column, 1}], "invalid float number ", "1.0e309"} = tokenize_error("1.0e309").
hex_bin_octal_test() ->
[{int, {1, 1, 255}, "0xFF"}] = tokenize("0xFF"),
[{int, {1, 1, 255}, "0xF_F"}] = tokenize("0xF_F"),
[{int, {1, 1, 63}, "0o77"}] = tokenize("0o77"),
[{int, {1, 1, 63}, "0o7_7"}] = tokenize("0o7_7"),
[{int, {1, 1, 3}, "0b11"}] = tokenize("0b11"),
[{int, {1, 1, 3}, "0b1_1"}] = tokenize("0b1_1").
unquoted_atom_test() ->
[{atom, {1, 1, _}, '+'}] = tokenize(":+"),
[{atom, {1, 1, _}, '-'}] = tokenize(":-"),
[{atom, {1, 1, _}, '*'}] = tokenize(":*"),
[{atom, {1, 1, _}, '/'}] = tokenize(":/"),
[{atom, {1, 1, _}, '='}] = tokenize(":="),
[{atom, {1, 1, _}, '&&'}] = tokenize(":&&").
quoted_atom_test() ->
[{atom_quoted, {1, 1, $"}, 'foo bar'}] = tokenize(":\"foo bar\"").
oversized_atom_test() ->
OversizedAtom = string:copies("a", 256),
{[{line, 1}, {column, 1}], "atom length must be less than system limit: ", OversizedAtom} =
tokenize_error([$: | OversizedAtom]).
op_atom_test() ->
[{atom, {1, 1, _}, f0_1}] = tokenize(":f0_1").
kw_test() ->
[{kw_identifier, {1, 1, _}, do}] = tokenize("do: "),
[{kw_identifier, {1, 1, _}, a@}] = tokenize("a@: "),
[{kw_identifier, {1, 1, _}, 'A@'}] = tokenize("A@: "),
[{kw_identifier, {1, 1, _}, a@b}] = tokenize("a@b: "),
[{kw_identifier, {1, 1, _}, 'A@!'}] = tokenize("A@!: "),
[{kw_identifier, {1, 1, _}, 'a@!'}] = tokenize("a@!: "),
[{kw_identifier, {1, 1, _}, foo}, {bin_string, {1, 6, nil}, [<<"bar">>]}] = tokenize("foo: \"bar\""),
[{kw_identifier, {1, 1, _}, '+'}, {bin_string, {1, 6, nil}, [<<"bar">>]}] = tokenize("\"+\": \"bar\"").
int_test() ->
[{int, {1, 1, 123}, "123"}] = tokenize("123"),
[{int, {1, 1, 123}, "123"}, {';', {1, 4, 0}}] = tokenize("123;"),
[{eol, {1, 1, 2}}, {int, {3, 1, 123}, "123"}] = tokenize("\n\n123"),
[{int, {1, 3, 123}, "123"}, {int, {1, 8, 234}, "234"}] = tokenize(" 123 234 "),
[{int, {1, 1, 7}, "007"}] = tokenize("007"),
[{int, {1, 1, 100000}, "0100000"}] = tokenize("0100000").
float_test() ->
[{flt, {1, 1, 12.3}, "12.3"}] = tokenize("12.3"),
[{flt, {1, 1, 12.3}, "12.3"}, {';', {1, 5, 0}}] = tokenize("12.3;"),
[{eol, {1, 1, 2}}, {flt, {3, 1, 12.3}, "12.3"}] = tokenize("\n\n12.3"),
[{flt, {1, 3, 12.3}, "12.3"}, {flt, {1, 9, 23.4}, "23.4"}] = tokenize(" 12.3 23.4 "),
[{flt, {1, 1, 12.3}, "00_12.3_00"}] = tokenize("00_12.3_00"),
OversizedFloat = string:copies("9", 310) ++ ".0",
{[{line, 1}, {column, 1}], "invalid float number ", OversizedFloat} = tokenize_error(OversizedFloat).
identifier_test() ->
[{identifier, {1, 1, _}, abc}] = tokenize("abc "),
[{identifier, {1, 1, _}, 'abc?'}] = tokenize("abc?"),
[{identifier, {1, 1, _}, 'abc!'}] = tokenize("abc!"),
[{identifier, {1, 1, _}, 'a0c!'}] = tokenize("a0c!"),
[{paren_identifier, {1, 1, _}, 'a0c'}, {'(', {1, 4, nil}}, {')', {1, 5, nil}}] = tokenize("a0c()"),
[{paren_identifier, {1, 1, _}, 'a0c!'}, {'(', {1, 5, nil}}, {')', {1, 6, nil}}] = tokenize("a0c!()").
module_macro_test() ->
[{identifier, {1, 1, _}, '__MODULE__'}] = tokenize("__MODULE__").
dot_test() ->
[{identifier, {1, 1, _}, foo},
{'.', {1, 4, nil}},
{identifier, {1, 5, _}, bar},
{'.', {1, 8, nil}},
{identifier, {1, 9, _}, baz}] = tokenize("foo.bar.baz").
dot_keyword_test() ->
[{identifier, {1, 1, _}, foo},
{'.', {1, 4, nil}},
{identifier, {1, 5, _}, do}] = tokenize("foo.do").
newline_test() ->
[{identifier, {1, 1, _}, foo},
{'.', {2, 1, nil}},
{identifier, {2, 2, _}, bar}] = tokenize("foo\n.bar"),
[{int, {1, 1, 1}, "1"},
{concat_op, {2, 1, 1}, '++'},
{int, {2, 3, 2}, "2"}] = tokenize("1\n++2").
dot_newline_operator_test() ->
[{identifier, {1, 1, _}, foo},
{'.', {1, 4, nil}},
{identifier, {2, 1, _}, '+'},
{int, {2, 2, 1}, "1"}] = tokenize("foo.\n+1"),
[{identifier, {1, 1, _}, foo},
{'.', {1, 4, nil}},
{identifier, {2, 1, _}, '+'},
{int, {2, 2, 1}, "1"}] = tokenize("foo.#bar\n+1").
dot_call_operator_test() ->
[{identifier, {1, 1, _}, f},
{dot_call_op, {1, 2, nil}, '.'},
{'(', {1, 3, nil}},
{')', {1, 4, nil}}] = tokenize("f.()").
aliases_test() ->
[{'alias', {1, 1, _}, 'Foo'}] = tokenize("Foo"),
[{'alias', {1, 1, _}, 'Foo'},
{'.', {1, 4, nil}},
{'alias', {1, 5, _}, 'Bar'},
{'.', {1, 8, nil}},
{'alias', {1, 9, _}, 'Baz'}] = tokenize("Foo.Bar.Baz").
string_test() ->
[{bin_string, {1, 1, nil}, [<<"foo">>]}] = tokenize("\"foo\""),
[{bin_string, {1, 1, nil}, [<<"f\"">>]}] = tokenize("\"f\\\"\"").
heredoc_test() ->
[{bin_heredoc, {1, 1, nil}, 0, [<<"heredoc\n">>]}] = tokenize("\"\"\"\nheredoc\n\"\"\""),
[{bin_heredoc, {1, 1, nil}, 1, [<<"heredoc\n">>]}, {';', {3, 5, 0}}] = tokenize("\"\"\"\n heredoc\n \"\"\";").
empty_string_test() ->
[{bin_string, {1, 1, nil}, [<<>>]}] = tokenize("\"\"").
concat_test() ->
[{identifier, {1, 1, _}, x},
{concat_op, {1, 3, nil}, '++'},
{identifier, {1, 6, _}, y}] = tokenize("x ++ y"),
[{identifier, {1, 1, _}, x},
{concat_op, {1, 3, nil}, '+++'},
{identifier, {1, 7, _}, y}] = tokenize("x +++ y").
space_test() ->
[{op_identifier, {1, 1, _}, foo},
{dual_op, {1, 5, nil}, '-'},
{int, {1, 6, 2}, "2"}] = tokenize("foo -2"),
[{op_identifier, {1, 1, _}, foo},
{dual_op, {1, 6, nil}, '-'},
{int, {1, 7, 2}, "2"}] = tokenize("foo -2").
chars_test() ->
[{char, {1, 1, "?a"}, 97}] = tokenize("?a"),
[{char, {1, 1, "?c"}, 99}] = tokenize("?c"),
[{char, {1, 1, "?\\0"}, 0}] = tokenize("?\\0"),
[{char, {1, 1, "?\\a"}, 7}] = tokenize("?\\a"),
[{char, {1, 1, "?\\n"}, 10}] = tokenize("?\\n"),
[{char, {1, 1, "?\\\\"}, 92}] = tokenize("?\\\\").
interpolation_test() ->
[{bin_string, {1, 1, nil}, [<<"f">>, {{1, 3, nil},{1, 7, nil}, [{identifier, {1, 5, _}, oo}]}]},
{concat_op, {1, 10, nil}, '<>'},
{bin_string, {1, 13, nil}, [<<>>]}] = tokenize("\"f#{oo}\" <> \"\"").
escaped_interpolation_test() ->
[{bin_string, {1, 1, nil}, [<<"f#{oo}">>]},
{concat_op, {1, 11, nil}, '<>'},
{bin_string, {1, 14, nil}, [<<>>]}] = tokenize("\"f\\#{oo}\" <> \"\"").
capture_test() ->
% Parens precedence
[{capture_op, {1, 1, nil}, '&'},
{unary_op, {1, 2, nil}, 'not'},
{int, {1, 6, 1}, "1"},
{',', {1, 7, 0}},
{int, {1, 9, 2}, "2"}] = tokenize("¬ 1, 2"),
% Operators
[{capture_op, {1, 1, nil}, '&'},
{identifier, {1, 2, _}, '||'},
{mult_op, {1, 4, nil}, '/'},
{int, {1, 5, 2}, "2"}] = tokenize("&||/2"),
[{capture_op, {1, 1, nil}, '&'},
{identifier, {1, 2, _}, 'or'},
{mult_op, {1, 4, nil}, '/'},
{int, {1, 5, 2}, "2"}] = tokenize("&or/2"),
[{capture_op,{1,1,nil},'&'},
{identifier,{1,3,_},'+'},
{mult_op,{1,4,nil},'/'},
{int,{1,5,1},"1"}] = tokenize("& +/1"),
[{capture_op,{1,1,nil},'&'},
{identifier,{1,3,_},'&'},
{mult_op,{1,4,nil},'/'},
{int,{1,5,1},"1"}] = tokenize("& &/1"),
[{capture_op,{1,1,nil},'&'},
{identifier,{1,3,_},'..//'},
{mult_op,{1,7,nil},'/'},
{int,{1,8,3},"3"}] = tokenize("& ..///3"),
[{capture_op, {1,1,nil}, '&'},
{identifier, {1,3,_}, '/'},
{mult_op, {1,5,nil}, '/'},
{int, {1,6,2}, "2"}] = tokenize("& / /2"),
[{capture_op, {1,1,nil}, '&'},
{identifier, {1,2,_}, '/'},
{mult_op, {1,4,nil}, '/'},
{int, {1,5,2}, "2"}] = tokenize("&/ /2"),
% Only operators
[{identifier,{1,1,_},'&'},
{mult_op,{1,2,nil},'/'},
{int,{1,3,1},"1"}] = tokenize("&/1"),
[{identifier,{1,1,_},'+'},
{mult_op,{1,2,nil},'/'},
{int,{1,3,1},"1"}] = tokenize("+/1"),
[{identifier, {1,1,_}, '/'},
{mult_op, {1,3,nil}, '/'},
{int, {1,4,2}, "2"}] = tokenize("/ /2"),
[{identifier, {1,1,_}, '..//'},
{mult_op, {1,5,nil}, '/'},
{int, {1,6,3}, "3"}] = tokenize("..///3").
vc_merge_conflict_test() ->
{[{line, 1}, {column, 1}], "found an unexpected version control marker, please resolve the conflicts: ", "<<<<<<< HEAD"} =
tokenize_error("<<<<<<< HEAD\n[1, 2, 3]").
sigil_terminator_test() ->
[{sigil, {1, 1, nil}, sigil_r, [<<"foo">>], "", nil, <<"/">>}] = tokenize("~r/foo/"),
[{sigil, {1, 1, nil}, sigil_r, [<<"foo">>], "", nil, <<"[">>}] = tokenize("~r[foo]"),
[{sigil, {1, 1, nil}, sigil_r, [<<"foo">>], "", nil, <<"\"">>}] = tokenize("~r\"foo\""),
[{sigil, {1, 1, nil}, sigil_r, [<<"foo">>], "", nil, <<"/">>},
{comp_op, {1, 9, nil}, '=='},
{identifier, {1, 12, _}, bar}] = tokenize("~r/foo/ == bar"),
[{sigil, {1, 1, nil}, sigil_r, [<<"foo">>], "iu", nil, <<"/">>},
{comp_op, {1, 11, nil}, '=='},
{identifier, {1, 14, _}, bar}] = tokenize("~r/foo/iu == bar"),
[{sigil, {1, 1, nil}, sigil_M, [<<"1 2 3">>], "u8", nil, <<"[">>}] = tokenize("~M[1 2 3]u8").
sigil_heredoc_test() ->
[{sigil, {1, 1, nil}, sigil_S, [<<"sigil heredoc\n">>], "", 0, <<"\"\"\"">>}] = tokenize("~S\"\"\"\nsigil heredoc\n\"\"\""),
[{sigil, {1, 1, nil}, sigil_S, [<<"sigil heredoc\n">>], "", 0, <<"'''">>}] = tokenize("~S'''\nsigil heredoc\n'''"),
[{sigil, {1, 1, nil}, sigil_S, [<<"sigil heredoc\n">>], "", 2, <<"\"\"\"">>}] = tokenize("~S\"\"\"\n sigil heredoc\n \"\"\""),
[{sigil, {1, 1, nil}, sigil_s, [<<"sigil heredoc\n">>], "", 2, <<"\"\"\"">>}] = tokenize("~s\"\"\"\n sigil heredoc\n \"\"\"").
invalid_sigil_delimiter_test() ->
{[{line, 1}, {column, 1}], "invalid sigil delimiter: ", Message} = tokenize_error("~s\\"),
true = lists:prefix("\"\\\" (column 3, code point U+005C)", lists:flatten(Message)).
|