mfm-parser/test/lexer_test.exs

98 lines
3.2 KiB
Elixir
Raw Normal View History

2022-07-23 17:15:08 +00:00
defmodule MfmParser.LexerTest do
use ExUnit.Case
2022-07-23 17:15:08 +00:00
alias MfmParser.Lexer
alias MfmParser.Token.MFM
alias MfmParser.Token.Newline
alias MfmParser.Token.Text
2022-07-23 17:15:08 +00:00
describe "eof" do
test "peek/1 handles eof" do
assert Lexer.peek("") == :eof
end
test "next/1 handles eof" do
assert Lexer.next("") == :eof
end
end
describe "mfm $[ token" do
test "it ends with a space" do
assert Lexer.peek("$[ola puerca]") == %MFM.Open{content: "$[ola "}
assert Lexer.next("$[ola puerca]") == {%MFM.Open{content: "$[ola "}, "puerca]"}
assert Lexer.next("$[ola.x,speed=5s puerca]") ==
{%MFM.Open{content: "$[ola.x,speed=5s "}, "puerca]"}
2022-07-23 17:15:08 +00:00
end
test "it doesn't crash if the token can't be completed" do
Lexer.peek("$[ola")
Lexer.next("$[ola")
end
end
describe "] token" do
test "it handles ] as a token" do
assert Lexer.peek("]ve anime") == %MFM.Close{content: "]"}
assert Lexer.next("]ve anime") == {%MFM.Close{content: "]"}, "ve anime"}
2022-07-23 17:15:08 +00:00
end
test "it works at the eof" do
assert Lexer.peek("]") == %MFM.Close{content: "]"}
assert Lexer.next("]") == {%MFM.Close{content: "]"}, ""}
2022-07-23 17:15:08 +00:00
end
end
describe "text token" do
test "it ends when a mfm token opens while a $ alone doesn't end the text token" do
assert Lexer.peek("Tu abuela ve anime y no se lava el $[spin culo]") ==
%Text{content: "Tu abuela ve anime y no se lava el "}
2022-07-23 17:15:08 +00:00
assert Lexer.next("Tu abuela ve anime y no se lava el $[spin culo]") ==
{%Text{content: "Tu abuela ve anime y no se lava el "}, "$[spin culo]"}
2022-07-23 17:15:08 +00:00
assert Lexer.peek("A $2 chocolatine") == %Text{content: "A $2 chocolatine"}
assert Lexer.next("A $2 chocolatine") == {%Text{content: "A $2 chocolatine"}, ""}
2022-07-23 17:15:08 +00:00
assert Lexer.peek("Eyes like $$") == %Text{content: "Eyes like $$"}
assert Lexer.next("Eyes like $$") == {%Text{content: "Eyes like $$"}, ""}
assert Lexer.peek("$2 chocolatine") == %Text{content: "$2 chocolatine"}
assert Lexer.next("$2 chocolatine") == {%Text{content: "$2 chocolatine"}, ""}
assert Lexer.peek("$2") == %Text{content: "$2"}
assert Lexer.next("$2") == {%Text{content: "$2"}, ""}
assert Lexer.peek("$") == %Text{content: "$"}
assert Lexer.next("$") == {%Text{content: "$"}, ""}
2022-07-23 17:15:08 +00:00
end
test "it ends when a mfm token closes" do
assert Lexer.peek("el culo]") == %Text{content: "el culo"}
assert Lexer.next("el culo]") == {%Text{content: "el culo"}, "]"}
2022-07-23 17:15:08 +00:00
end
test "it ends when the eof is reached" do
assert Lexer.peek("Tu abuela ve anime y no se lava el culo") == %Text{
content: "Tu abuela ve anime y no se lava el culo"
}
2022-07-23 17:15:08 +00:00
assert Lexer.next("Tu abuela ve anime y no se lava el culo") ==
{%Text{content: "Tu abuela ve anime y no se lava el culo"}, ""}
2022-07-23 17:15:08 +00:00
end
end
describe "newline token" do
test "it handles \n as a token" do
assert Lexer.peek("\nchocolat") == %Newline{content: "\n"}
assert Lexer.next("\nchocolat") == {%Newline{content: "\n"}, "chocolat"}
2022-07-23 17:15:08 +00:00
end
test "it works at the eof" do
assert Lexer.peek("\n") == %Newline{content: "\n"}
assert Lexer.next("\n") == {%Newline{content: "\n"}, ""}
2022-07-23 17:15:08 +00:00
end
end
end