forked from mrcljx/rubocop-for-rubymine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRubocopResultTest.kt
136 lines (120 loc) · 4.91 KB
/
RubocopResultTest.kt
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
package io.github.sirlantis.rubymine.rubocop.model
import org.junit.Test
import java.io.InputStreamReader
import kotlin.test.assertTrue
import kotlin.test.assertFalse
import com.google.gson.stream.JsonReader
import kotlin.test.assertEquals
class RubocopResultTest {
fun String.reader(): InputStreamReader {
return InputStreamReader(byteInputStream())
}
fun String.jsonReader(): JsonReader {
return JsonReader(reader())
}
@Test fun testHelperFunctions() {
val s = "Hallo, ich hätte gerne ein Weißwurstbrötchen!"
assertEquals(s, s.reader().readText())
}
fun readFromString(s: String, stderr: String? = null): RubocopResult {
return RubocopResult.readFromJsonReader(s.jsonReader(), stderr?.reader())
}
@Test fun testEmpty() {
val result = readFromString("{}")
assertTrue(result.isEmpty())
}
@Test fun testWithoutEmptyFiles() {
val result = readFromString("""{"files":[]}""")
assertTrue(result.isEmpty())
}
@Test fun testWithConfigError() {
val result = readFromString("""{"files":[]}""", "Warning: unrecognized cop Style/CaseIndentation found in /project/.rubocop.yml")
assertEquals(1, result.warnings.count())
assertEquals("unrecognized cop Style/CaseIndentation found in /project/.rubocop.yml", result.warnings.first())
}
@Test fun testWithConfigErrors() {
val result = readFromString("""{"files":[]}""", "Warning: unrecognized cop Style/CaseIndentation found in /project/.rubocop.yml\nWarning: unrecognized foo\nWarning: unrecognized cop Whatever found in /somewhere/.rubocop.yml")
assertEquals(2, result.warnings.count())
}
@Test fun testWithFileWithoutOffenses() {
val result = readFromString("""{"files":[{"path":"test.rb","offenses":[]}]}""")
assertFalse(result.isEmpty())
val file = result.first()
assertEquals(file.path, "test.rb")
assertTrue(file.offenses.isEmpty())
}
@Test fun testMultipleFiles() {
val input = """
{
"metadata":{
"rubocop_version":"0.27.0",
"ruby_engine":"ruby",
"ruby_version":"2.1.3",
"ruby_patchlevel":"242",
"ruby_platform":"x86_64-darwin14.0"
},
"files":[
{
"path":"Gemfile",
"offenses":[]
}, {
"path":"test.rb",
"offenses":[
{
"severity":"convention",
"message":"Prefer single-quoted strings when you don't need string interpolation or special symbols.",
"cop_name":"Style/StringLiterals",
"corrected":null,
"location":{
"line":5,
"column":7,
"length":11
}
}, {
"severity":"warning",
"message":"`end` at 11, 6 is not aligned with `if` at 8, 4",
"cop_name":"Lint/EndAlignment",
"corrected":null,
"location":{
"line":11,
"column":7,
"length":3
}
}
]
}
],
"summary":{
"offense_count":2,
"target_file_count":2,
"inspected_file_count":2
}
}
"""
val result = readFromString(input)
assertFalse(result.isEmpty())
assertEquals(result.size, 2)
}
@Test fun testOffense() {
val offense = Offense.readFromJsonReader("""{
"severity":"convention",
"message":"Prefer single-quoted strings when you don't need string interpolation or special symbols.",
"cop_name":"Style/StringLiterals",
"corrected":null,
"location":{"line":5,"column":7,"length":11}}
""".jsonReader())
assertEquals(offense.severity, "convention")
assertEquals(offense.message, "Prefer single-quoted strings when you don't need string interpolation or special symbols.")
assertEquals(offense.cop, "Style/StringLiterals")
assertEquals(offense.location.line, 5)
assertEquals(offense.location.column, 7)
assertEquals(offense.location.length, 11)
}
@Test fun testOffenseLocation() {
val input = """{"line":42,"column":13,"length":"7"}"""
val location = OffenseLocation.readFromJsonReader(input.jsonReader())
assertEquals(location.line, 42)
assertEquals(location.column, 13)
assertEquals(location.length, 7)
}
}