github.com/editorconfig-checker/editorconfig-checker@v0.0.0-20231102090242-ddae3e68851e/pkg/validation/validators/validators_test.go (about) 1 package validators 2 3 import ( 4 "errors" 5 "reflect" 6 "testing" 7 8 "github.com/editorconfig-checker/editorconfig-checker/pkg/config" 9 ) 10 11 func TestFinalNewline(t *testing.T) { 12 finalNewlineTests := []struct { 13 line string 14 insertFinalNewline string 15 lineEnding string 16 expected error 17 }{ 18 {"x\n", "true", "lf", nil}, 19 {"x\r", "true", "cr", nil}, 20 {"x\r\n", "true", "crlf", nil}, 21 22 {"x", "true", "lf", errors.New("Wrong line endings or no final newline")}, 23 {"x", "true", "cr", errors.New("Wrong line endings or no final newline")}, 24 {"x", "true", "crlf", errors.New("Wrong line endings or no final newline")}, 25 26 {"x\n", "true", "cr", errors.New("Wrong line endings or no final newline")}, 27 {"x\n", "true", "crlf", errors.New("Wrong line endings or no final newline")}, 28 29 {"x\r", "true", "lf", errors.New("Wrong line endings or no final newline")}, 30 {"x\r", "true", "crlf", errors.New("Wrong line endings or no final newline")}, 31 32 {"x\r\n", "true", "lf", errors.New("Wrong line endings or no final newline")}, 33 {"x\r\n", "true", "cr", errors.New("Wrong line endings or no final newline")}, 34 35 // insert_final_newline false 36 {"x", "false", "lf", nil}, 37 {"x\n", "false", "lf", errors.New("No final newline expected")}, 38 {"x\r", "false", "lf", errors.New("No final newline expected")}, 39 {"x\r\n", "false", "lf", errors.New("No final newline expected")}, 40 41 {"x", "false", "cr", nil}, 42 {"x\n", "false", "cr", errors.New("No final newline expected")}, 43 {"x\r", "false", "cr", errors.New("No final newline expected")}, 44 {"x\r\n", "false", "cr", errors.New("No final newline expected")}, 45 46 {"x", "false", "crlf", nil}, 47 {"x\n", "false", "crlf", errors.New("No final newline expected")}, 48 {"x\r", "false", "crlf", errors.New("No final newline expected")}, 49 {"x\r\n", "false", "crlf", errors.New("No final newline expected")}, 50 51 // insert_final_newline not set 52 {"x", "", "lf", nil}, 53 {"x", "", "cr", nil}, 54 {"x", "", "crlf", nil}, 55 {"x\n", "", "lf", nil}, 56 {"x\n", "", "cr", nil}, 57 {"x\n", "", "crlf", nil}, 58 {"x\r", "", "lf", nil}, 59 {"x\r", "", "cr", nil}, 60 {"x\r", "", "crlf", nil}, 61 {"x\r\n", "", "lf", nil}, 62 {"x\r\n", "", "cr", nil}, 63 {"x\r\n", "", "crlf", nil}, 64 65 // end_of_line not set 66 {"x", "true", "", errors.New("Final newline expected")}, 67 {"x", "false", "", nil}, 68 {"x\n", "true", "", nil}, 69 {"x\n", "false", "", errors.New("No final newline expected")}, 70 {"x\r", "true", "", nil}, 71 {"x\r", "false", "", errors.New("No final newline expected")}, 72 {"x\r\n", "true", "", nil}, 73 {"x\r\n", "false", "", errors.New("No final newline expected")}, 74 } 75 76 for _, tt := range finalNewlineTests { 77 actual := FinalNewline(tt.line, tt.insertFinalNewline, tt.lineEnding) 78 if !reflect.DeepEqual(actual, tt.expected) { 79 t.Errorf("FinalNewline(%s, %s, %s): expected: %v, got: %v", tt.line, tt.insertFinalNewline, tt.lineEnding, tt.expected, actual) 80 } 81 } 82 } 83 84 func TestLineEnding(t *testing.T) { 85 linedEndingTests := []struct { 86 line string 87 lineEnding string 88 expected error 89 }{ 90 {"x", "lf", nil}, 91 {"x\n", "lf", nil}, 92 {"x\r", "lf", errors.New("Not all lines have the correct end of line character")}, 93 {"x\r\n", "lf", errors.New("Not all lines have the correct end of line character")}, 94 {"x\ry\nz\n", "lf", errors.New("Not all lines have the correct end of line character")}, 95 96 {"x", "cr", nil}, 97 {"x\r", "cr", nil}, 98 {"x\n", "cr", errors.New("Not all lines have the correct end of line character")}, 99 {"x\r\n", "cr", errors.New("Not all lines have the correct end of line character")}, 100 {"x\ry\nz\n", "cr", errors.New("Not all lines have the correct end of line character")}, 101 102 {"x", "crlf", nil}, 103 {"x\r\n", "crlf", nil}, 104 {"x\r", "crlf", errors.New("Not all lines have the correct end of line character")}, 105 {"x\n", "crlf", errors.New("Not all lines have the correct end of line character")}, 106 {"x\ry\nz\n", "crlf", errors.New("Not all lines have the correct end of line character")}, 107 } 108 109 for _, tt := range linedEndingTests { 110 actual := LineEnding(tt.line, tt.lineEnding) 111 if !reflect.DeepEqual(actual, tt.expected) { 112 t.Errorf("LineEnding(%s, %s): expected: %v, got: %v", tt.line, tt.lineEnding, tt.expected, actual) 113 } 114 } 115 } 116 117 func TestIndentation(t *testing.T) { 118 configuration := config.Config{SpacesAftertabs: false} 119 120 indentationTests := []struct { 121 line string 122 indentStyle string 123 indenSize int 124 expected error 125 }{ 126 {" x", "space", 4, nil}, 127 {" x", "space", 4, errors.New("Wrong amount of left-padding spaces(want multiple of 4)")}, 128 {" x", "tab", 0, nil}, 129 {" x", "tab", 0, errors.New("Wrong indentation type(spaces instead of tabs)")}, 130 {" x", "x", 0, nil}, 131 {" x", "x", 0, nil}, 132 } 133 134 for _, tt := range indentationTests { 135 actual := Indentation(tt.line, tt.indentStyle, tt.indenSize, configuration) 136 if !reflect.DeepEqual(actual, tt.expected) { 137 t.Errorf("Indentation(%s, %s, %d, %+v): expected: %v, got: %v", tt.line, tt.indentStyle, tt.indenSize, configuration, tt.expected, actual) 138 } 139 } 140 } 141 142 func TestSpace(t *testing.T) { 143 enabledIndentSizeConfig := config.Config{} 144 disabled := config.DisabledChecks{IndentSize: true} 145 disabledIndentSizeConfig := config.Config{ 146 Disable: disabled, 147 } 148 149 spaceTests := []struct { 150 line string 151 indentSize int 152 config config.Config 153 expected error 154 }{ 155 {"", 4, enabledIndentSizeConfig, nil}, 156 {"x", 0, enabledIndentSizeConfig, nil}, 157 {"x", 4, enabledIndentSizeConfig, nil}, 158 {" x", 4, enabledIndentSizeConfig, nil}, 159 // 5 spaces 160 {" x", 4, enabledIndentSizeConfig, errors.New("Wrong amount of left-padding spaces(want multiple of 4)")}, 161 // 3 spaces 162 {" x", 4, enabledIndentSizeConfig, errors.New("Wrong amount of left-padding spaces(want multiple of 4)")}, 163 // correct indented block comment, empty and non empty 164 {" *", 4, enabledIndentSizeConfig, nil}, 165 {" * some comment", 4, enabledIndentSizeConfig, nil}, 166 {" ", 4, enabledIndentSizeConfig, nil}, 167 // disabled indent size 168 {"", 4, disabledIndentSizeConfig, nil}, 169 {" x ", 4, disabledIndentSizeConfig, nil}, 170 {" x ", 4, disabledIndentSizeConfig, nil}, 171 {" x ", 4, disabledIndentSizeConfig, nil}, 172 {" x ", 4, disabledIndentSizeConfig, nil}, 173 {" x ", 4, disabledIndentSizeConfig, errors.New("Wrong indent style found (tabs instead of spaces)")}, 174 {" x a", 4, disabledIndentSizeConfig, errors.New("Wrong indent style found (tabs instead of spaces)")}, 175 } 176 177 for _, tt := range spaceTests { 178 actual := Space(tt.line, tt.indentSize, tt.config) 179 if !reflect.DeepEqual(actual, tt.expected) { 180 t.Errorf("Space(%s, %d): expected: %v, got: %v", tt.line, tt.indentSize, tt.expected, actual) 181 } 182 } 183 } 184 185 func TestTab(t *testing.T) { 186 spacesAllowed := config.Config{SpacesAftertabs: true} 187 spacesForbidden := config.Config{SpacesAftertabs: false} 188 tabTests := []struct { 189 line string 190 config config.Config 191 expected error 192 }{ 193 {" x", spacesAllowed, nil}, 194 {" bla", spacesAllowed, nil}, 195 {" bla", spacesAllowed, nil}, 196 {" xx", spacesAllowed, nil}, 197 198 {"", spacesForbidden, nil}, 199 {"x", spacesForbidden, nil}, 200 {" x", spacesForbidden, nil}, 201 {" ", spacesForbidden, nil}, 202 {" x", spacesForbidden, nil}, 203 {" a", spacesForbidden, errors.New("Wrong indentation type(spaces instead of tabs)")}, 204 {" *", spacesForbidden, nil}, 205 {" *", spacesForbidden, nil}, 206 {" * some comment", spacesForbidden, nil}, 207 {" */", spacesForbidden, nil}, 208 {" */", spacesForbidden, nil}, 209 {" *", spacesForbidden, nil}, 210 } 211 212 for _, tt := range tabTests { 213 actual := Tab(tt.line, tt.config) 214 if !reflect.DeepEqual(actual, tt.expected) { 215 t.Errorf("Tab(%s, %+v): expected: %v, got: %v", tt.line, tt.config, tt.expected, actual) 216 } 217 } 218 } 219 220 func TestTrailingWhitespace(t *testing.T) { 221 trailingWhitespaceTests := []struct { 222 line string 223 trimTrailingWhitespace bool 224 expected error 225 }{ 226 {"", true, nil}, 227 {"", false, nil}, 228 {"x", true, nil}, 229 {"x", false, nil}, 230 231 // Spaces 232 {"x ", true, errors.New("Trailing whitespace")}, 233 {"x ", false, nil}, 234 {"x .", true, nil}, 235 {"x .", false, nil}, 236 237 // Tabs 238 {"x ", true, errors.New("Trailing whitespace")}, 239 {"x ", false, nil}, 240 {"x .", true, nil}, 241 {"x .", false, nil}, 242 } 243 244 for _, tt := range trailingWhitespaceTests { 245 actual := TrailingWhitespace(tt.line, tt.trimTrailingWhitespace) 246 if !reflect.DeepEqual(actual, tt.expected) { 247 t.Errorf("TrailingWhitespace(%s, %v): expected: %v, got: %v", tt.line, tt.trimTrailingWhitespace, tt.expected, actual) 248 } 249 } 250 } 251 252 func TestMaxLineLength(t *testing.T) { 253 maxLineLengthTest := []struct { 254 line string 255 maxLineLength int 256 charSet string 257 expected error 258 }{ 259 {"検索は次の", 5, "utf-8", nil}, 260 {"検索は次の", 2, "utf-8", errors.New("Line too long (5 instead of 2)")}, 261 {"\xEF\xBB\xBF検索は次の", 5, "utf-8-bom", nil}, 262 {"検索は次の", 5, "latin1", errors.New("Line too long (15 instead of 5)")}, 263 {"", 80, "latin1", nil}, 264 {"abc", 2, "latin1", errors.New("Line too long (3 instead of 2)")}, 265 {" ", 2, "latin1", errors.New("Line too long (3 instead of 2)")}, 266 {"xx", 2, "latin1", nil}, 267 } 268 269 for _, tt := range maxLineLengthTest { 270 actual := MaxLineLength(tt.line, tt.maxLineLength, tt.charSet) 271 if !reflect.DeepEqual(actual, tt.expected) { 272 t.Errorf("Max(%s, %v): expected: %v, got: %v", tt.line, tt.maxLineLength, tt.expected, actual) 273 } 274 } 275 }