-
Notifications
You must be signed in to change notification settings - Fork 34
/
registry_test.exs
93 lines (72 loc) · 3.43 KB
/
registry_test.exs
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
defmodule Makeup.RegistryTest do
use ExUnit.Case
alias Makeup.Registry
doctest Registry
setup do
Registry.clean_name_registry()
Registry.clean_extension_registry()
:ok
end
describe "register lexer by name" do
test "register a lexer by name and fetch it afterwards" do
# Register the lexer
Registry.register_lexer_with_name("crazy_lexer", {CrazyLexer, [opt1: 1, opt2: "2"]})
# Fetch the lexer
assert {:ok, {CrazyLexer, [opt1: 1, opt2: "2"]}} ==
Registry.fetch_lexer_by_name("crazy_lexer")
assert {CrazyLexer, [opt1: 1, opt2: "2"]} == Registry.fetch_lexer_by_name!("crazy_lexer")
end
test "fail to get a lexer that hasn't been registered by name" do
assert nil == Registry.get_lexer_by_extension("ext_that_doesnt_exist")
assert :error == Registry.fetch_lexer_by_name("lexer_that_doesnt_exist")
assert_raise KeyError, fn ->
Registry.fetch_lexer_by_name!("lexer_that_doesnt_exist")
end
end
end
describe "register lexer by file extension" do
test "register a lexer by extension and fetch it afterwards" do
# Register the lexer
Registry.register_lexer_with_extension("cz", {CrazierLexer, [opt1: 2, opt2: "3"]})
# Fetch the lexer
assert {:ok, {CrazierLexer, [opt1: 2, opt2: "3"]}} ==
Registry.fetch_lexer_by_extension("cz")
assert {CrazierLexer, [opt1: 2, opt2: "3"]} == Registry.fetch_lexer_by_extension!("cz")
assert {CrazierLexer, [opt1: 2, opt2: "3"]} == Registry.get_lexer_by_extension("cz")
end
test "fail to get a lexer that hasn't been registered by extension" do
assert nil == Registry.get_lexer_by_extension("ext_that_doesnt_exist")
assert :error == Registry.fetch_lexer_by_extension("ext_that_doesnt_exist")
assert_raise KeyError, fn ->
Registry.fetch_lexer_by_extension!("ext_that_doesnt_exist")
end
end
end
describe "register lexer using the high level API" do
test "register and fetch a lexer" do
Registry.register_lexer(MadLexer,
options: [opt1: "a", opt2: "b"],
names: ["mad_lexer1", "mad_lexer2"],
extensions: ["ml1", "ml2"]
)
# Fetching by name
assert {:ok, {MadLexer, [opt1: "a", opt2: "b"]}} ==
Registry.fetch_lexer_by_name("mad_lexer1")
assert {:ok, {MadLexer, [opt1: "a", opt2: "b"]}} ==
Registry.fetch_lexer_by_name("mad_lexer2")
assert {MadLexer, [opt1: "a", opt2: "b"]} = Registry.fetch_lexer_by_name!("mad_lexer1")
assert {MadLexer, [opt1: "a", opt2: "b"]} = Registry.fetch_lexer_by_name!("mad_lexer2")
assert {MadLexer, [opt1: "a", opt2: "b"]} = Registry.get_lexer_by_name("mad_lexer1")
assert {MadLexer, [opt1: "a", opt2: "b"]} = Registry.get_lexer_by_name("mad_lexer2")
# Fetching by extension
assert {:ok, {MadLexer, [opt1: "a", opt2: "b"]}} ==
Registry.fetch_lexer_by_extension("ml1")
assert {:ok, {MadLexer, [opt1: "a", opt2: "b"]}} ==
Registry.fetch_lexer_by_extension("ml2")
assert {MadLexer, [opt1: "a", opt2: "b"]} = Registry.fetch_lexer_by_extension!("ml1")
assert {MadLexer, [opt1: "a", opt2: "b"]} = Registry.fetch_lexer_by_extension!("ml2")
assert {MadLexer, [opt1: "a", opt2: "b"]} = Registry.get_lexer_by_extension("ml1")
assert {MadLexer, [opt1: "a", opt2: "b"]} = Registry.get_lexer_by_extension("ml2")
end
end
end