/
heredocs_test.exs
155 lines (134 loc) · 3.61 KB
/
heredocs_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
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
defmodule Credo.Code.HeredocsTest do
use Credo.TestHelper
alias Credo.Code.Heredocs
test "it should return the source without string literals 2" do
source = """
@moduledoc \"\"\"
this is an example # TODO: and this is no actual comment
\"\"\"
x = ~s{also: # TODO: no comment here}
?" # TODO: this is the third
# "
"also: # TODO: no comment here as well"
"""
expected =
"""
@moduledoc \"\"\"
@@EMPTY_STRING@@
\"\"\"
x = ~s{also: # TODO: no comment here}
?" # TODO: this is the third
# "
"also: # TODO: no comment here as well"
"""
|> String.replace(
"@@EMPTY_STRING@@",
" "
)
assert expected == source |> Heredocs.replace_with_spaces()
end
test "it should return the source without string sigils 2" do
source = """
should "not error for a quote in a heredoc" do
errors = ~s(
\"\"\"
this is an example " TODO: and this is no actual comment
\"\"\") |> lint
assert [] == errors
end
"""
result = source |> Heredocs.replace_with_spaces()
assert source != result
assert String.length(source) == String.length(result)
refute String.contains?(result, "example")
refute String.contains?(result, "TODO:")
end
test "it should return the source without string literals 3" do
source = """
x = "↑ ↗ →"
x = ~s|text|
x = ~s"text"
x = ~s'text'
x = ~s(text)
x = ~s[text]
x = ~s{text}
x = ~s<text>
x = ~S|text|
x = ~S"text"
x = ~S'text'
x = ~S(text)
x = ~S[text]
x = ~S{text}
x = ~S<text>
?" # <-- this is not a string
"""
assert source == source |> Heredocs.replace_with_spaces()
end
test "it should return the source without string sigils and replace the contents" do
source = """
t = ~S\"\"\"
abc
\"\"\"
"""
expected = """
t = ~S\"\"\"
...
\"\"\"
"""
result = source |> Heredocs.replace_with_spaces(".")
assert expected == result
end
test "it should return the source without string sigils and replace the contents including interpolation" do
source = """
def fun() do
a = \"\"\"
MyModule.\#{fun(Module.value() + 1)}.SubModule.\#{name}"
\"\"\"
end
"""
expected = """
def fun() do
a = \"\"\"
........................................................
..\"\"\"
end
"""
result = source |> Heredocs.replace_with_spaces(".")
assert expected == result
end
test "it should not modify commented out code" do
source = """
defmodule Foo do
defmodule Bar do
# @doc \"\"\"
# Reassign a student to a discussion group.
# This will un-assign student from the current discussion group
# \"\"\"
# def assign_group(leader = %User{}, student = %User{}) do
# cond do
# leader.role == :student ->
# {:error, :invalid}
#
# student.role != :student ->
# {:error, :invalid}
#
# true ->
# Repo.transaction(fn ->
# {:ok, _} = unassign_group(student)
#
# %Group{}
# |> Group.changeset(%{})
# |> put_assoc(:leader, leader)
# |> put_assoc(:student, student)
# |> Repo.insert!()
# end)
# end
# end
def baz, do: 123
end
end
"""
expected = source
assert expected == source |> Heredocs.replace_with_spaces(".")
end
end