2022-07-23 17:15:08 +00:00
|
|
|
defmodule MfmParser.LexerTest do
|
|
|
|
use ExUnit.Case
|
2022-07-23 18:17:34 +00:00
|
|
|
|
2022-07-23 17:15:08 +00:00
|
|
|
alias MfmParser.Lexer
|
|
|
|
|
2022-07-24 05:55:55 +00:00
|
|
|
alias MfmParser.Token.MFM
|
2022-07-23 18:17:34 +00:00
|
|
|
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
|
2022-07-24 05:55:55 +00:00
|
|
|
assert Lexer.peek("$[ola puerca]") == %MFM.Open{content: "$[ola "}
|
|
|
|
assert Lexer.next("$[ola puerca]") == {%MFM.Open{content: "$[ola "}, "puerca]"}
|
2022-07-23 18:17:34 +00:00
|
|
|
|
|
|
|
assert Lexer.next("$[ola.x,speed=5s puerca]") ==
|
2022-07-24 05:55:55 +00:00
|
|
|
{%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
|
2022-07-24 05:55:55 +00:00
|
|
|
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
|
2022-07-24 05:55:55 +00:00
|
|
|
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]") ==
|
2022-07-24 05:55:55 +00:00
|
|
|
%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]") ==
|
2022-07-24 05:55:55 +00:00
|
|
|
{%Text{content: "Tu abuela ve anime y no se lava el "}, "$[spin culo]"}
|
2022-07-23 17:15:08 +00:00
|
|
|
|
2022-07-24 05:55:55 +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
|
|
|
|
2022-07-24 05:55:55 +00:00
|
|
|
assert Lexer.peek("Eyes like $$") == %Text{content: "Eyes like $$"}
|
|
|
|
assert Lexer.next("Eyes like $$") == {%Text{content: "Eyes like $$"}, ""}
|
2022-07-24 23:21:02 +00:00
|
|
|
|
|
|
|
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
|
2022-07-24 05:55:55 +00:00
|
|
|
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
|
2022-07-24 05:55:55 +00:00
|
|
|
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") ==
|
2022-07-24 05:55:55 +00:00
|
|
|
{%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
|
2022-07-24 05:55:55 +00:00
|
|
|
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
|
2022-07-24 05:55:55 +00:00
|
|
|
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
|