Repository: editorconfig-checker/editorconfig-checker
Pull request #84 (2019-08-22 16:48:29)

Switch to table driven tests


Patch:
diff --git a/cmd/editorconfig-checker/main.go b/cmd/editorconfig-checker/main.go
index b7bfdb6..597a017 100644
--- a/cmd/editorconfig-checker/main.go
+++ b/cmd/editorconfig-checker/main.go
@@ -81,14 +81,19 @@ func main() {
 	currentConfig.Logger.Debug(currentConfig.GetAsString())
 	currentConfig.Logger.Verbose("Exclude Regexp: %s", currentConfig.GetExcludesAsRegularExpression())
 
-	// Check for returnworthy params
+	// Check for returnworthy arguments
 	shouldExit := ReturnableFlags(config)
 	if shouldExit {
 		os.Exit(0)
 	}
 
 	// contains all files which should be checked
-	filePaths := files.GetFiles(config)
+	filePaths, err := files.GetFiles(config)
+
+	if err != nil {
+		currentConfig.Logger.Error(err.Error())
+		os.Exit(1)
+	}
 
 	if currentConfig.DryRun {
 		for _, file := range filePaths {
diff --git a/pkg/files/files.go b/pkg/files/files.go
index 79cd2e6..d7ff010 100644
--- a/pkg/files/files.go
+++ b/pkg/files/files.go
@@ -27,22 +27,22 @@ type FileInformation struct {
 }
 
 // IsExcluded returns wether the file is excluded via arguments or config file
-func IsExcluded(filePath string, config config.Config) bool {
+func IsExcluded(filePath string, config config.Config) (bool, error) {
 	if len(config.Exclude) == 0 {
-		return false
+		return false, nil
 	}
 
 	relativeFilePath, err := GetRelativePath(filePath)
 	if err != nil {
-		panic(err)
+		return true, err
 	}
 
 	result, err := regexp.MatchString(config.GetExcludesAsRegularExpression(), relativeFilePath)
 	if err != nil {
-		panic(err)
+		return true, err
 	}
 
-	return result
+	return result, nil
 }
 
 // AddToFiles adds a file to a slice if it isn't already in there
@@ -57,7 +57,9 @@ func AddToFiles(filePaths []string, filePath string, config config.Config) []str
 		config.Logger.Error(err.Error())
 	}
 
-	if !IsExcluded(filePath, config) && IsAllowedContentType(contentType, config) {
+	isExcluded, err := IsExcluded(filePath, config)
+
+	if err == nil && !isExcluded && IsAllowedContentType(contentType, config) {
 		config.Logger.Verbose("Add %s to be checked", filePath)
 		return append(filePaths, filePath)
 	}
@@ -67,7 +69,7 @@ func AddToFiles(filePaths []string, filePath string, config config.Config) []str
 }
 
 // GetFiles returns all files which should be checked
-func GetFiles(config config.Config) []string {
+func GetFiles(config config.Config) ([]string, error) {
 	var filePaths []string
 
 	// Handle explicit passed files
@@ -86,7 +88,7 @@ func GetFiles(config config.Config) []string {
 			}
 		}
 
-		return filePaths
+		return filePaths, nil
 	}
 
 	byteArray, err := exec.Command("git", "ls-tree", "-r", "--name-only", "HEAD").Output()
@@ -94,7 +96,7 @@ func GetFiles(config config.Config) []string {
 		// It is not a git repository.
 		cwd, err := os.Getwd()
 		if err != nil {
-			panic("Could not get the current working directly")
+			return filePaths, err
 		}
 
 		_ = filepath.Walk(cwd, func(path string, fi os.FileInfo, err error) error {
@@ -120,7 +122,7 @@ func GetFiles(config config.Config) []string {
 		}
 	}
 
-	return filePaths
+	return filePaths, nil
 }
 
 // ReadLines returns the lines from a file as a slice
@@ -165,7 +167,7 @@ func GetContentType(path string) (string, error) {
 	// Reset the read pointer if necessary.
 	_, err = file.Seek(0, 0)
 	if err != nil {
-		panic(fmt.Sprintf("ERROR: %s", err))
+		panic(err)
 	}
 
 	// Always returns a valid content-type and "application/octet-stream" if no others seemed to match.
@@ -184,7 +186,7 @@ func PathExists(filePath string) bool {
 func GetRelativePath(filePath string) (string, error) {
 	cwd, err := os.Getwd()
 	if err != nil {
-		return "", fmt.Errorf("Could not get the current working directly")
+		return "", fmt.Errorf("Could not get the current working directory")
 	}
 
 	relativePath := strings.Replace(filePath, cwd, ".", 1)


Test patch:
diff --git a/pkg/files/files_test.go b/pkg/files/files_test.go
index 43ff277..8e0536c 100644
--- a/pkg/files/files_test.go
+++ b/pkg/files/files_test.go
@@ -1,8 +1,8 @@
 package files
 
 import (
-	"fmt"
 	"os"
+	"reflect"
 	"strings"
 	"testing"
 
@@ -41,28 +41,39 @@ func TestGetContentType(t *testing.T) {
 }
 
 func TestIsAllowedContentType(t *testing.T) {
-	config := config.Config{AllowedContentTypes: []string{"text/", "application/octet-stream"}}
-
-	if IsAllowedContentType("bla", config) {
-		t.Error("Bla shouldn't be an allowed contentType")
-	}
-
-	if !IsAllowedContentType("text/", config) {
-		t.Error("text/ shouldn't be an allowed contentType")
-	}
-
-	if !IsAllowedContentType("text/xml abc", config) {
-		t.Error("text/xml shouldn't be an allowed contentType")
+	configuration := config.Config{AllowedContentTypes: []string{"text/", "application/octet-stream"}}
+	isAllowedContentTypeTests := []struct {
+		contentType string
+		config      config.Config
+		expected    bool
+	}{
+		{"bla", configuration, false},
+		{"text/", configuration, true},
+		{"text/xml abc", configuration, true},
+	}
+
+	for _, tt := range isAllowedContentTypeTests {
+		actual := IsAllowedContentType(tt.contentType, tt.config)
+		if actual != tt.expected {
+			t.Errorf("IsAllowedContentType(%s, %+v): expected: %v, got: %v", tt.contentType, tt.config, tt.expected, actual)
+		}
 	}
 }
 
 func TestPathExists(t *testing.T) {
-	if !PathExists(".") {
-		t.Error("Expected . to be an existing path")
+	pathExistsTests := []struct {
+		path     string
+		expected bool
+	}{
+		{".", true},
+		{"notexisting", false},
 	}
 
-	if PathExists("notexisting") {
-		t.Error("Expected \"notexisting\" to not exist")
+	for _, tt := range pathExistsTests {
+		actual := PathExists(tt.path)
+		if actual != tt.expected {
+			t.Errorf("PathExists(%s): expected: %v, got: %v", tt.path, tt.expected, actual)
+		}
 	}
 }
 
@@ -79,17 +90,17 @@ func TestGetRelativePath(t *testing.T) {
 	os.Remove(DIR)
 	err := os.Mkdir(DIR, 0755)
 	if err != nil {
-		panic(fmt.Sprintf("ERROR: %s", err))
+		panic(err)
 	}
 
 	err = os.Chdir(DIR)
 	if err != nil {
-		panic(fmt.Sprintf("ERROR: %s", err))
+		panic(err)
 	}
 
 	err = os.Remove(DIR)
 	if err != nil {
-		panic(fmt.Sprintf("ERROR: %s", err))
+		panic(err)
 	}
 
 	_, err = GetRelativePath(cwd + filePath)
@@ -100,9 +111,66 @@ func TestGetRelativePath(t *testing.T) {
 }
 
 func TestIsExcluded(t *testing.T) {
-	result := IsExcluded("./cmd/editorconfig-checker/main.go", config.Config{})
+	isExcludedTests := []struct {
+		file          string
+		config        config.Config
+		expected      bool
+		errorExpected bool
+	}{
+		{"./cmd/editorconfig-checker/main.go", config.Config{}, false, false},
+		{"./cmd/editorconfig-checker/main.go", config.Config{Exclude: []string{"main"}}, true, true},
+	}
+
+	for _, tt := range isExcludedTests {
+		actual, err := IsExcluded(tt.file, tt.config)
+		if actual != tt.expected || (tt.errorExpected && err == nil || !tt.errorExpected && err != nil) {
+			t.Errorf("IsExcluded(%s, %+v): expected: %v, got: %v", tt.file, tt.config, tt.expected, actual)
+		}
+	}
+}
+
+func TestAddToFiles(t *testing.T) {
+	configuration := config.Config{}
+	excludedFileConfiguration := config.Config{Exclude: []string{"files"}}
+	addToFilesTests := []struct {
+		filePaths []string
+		filePath  string
+		config    config.Config
+		expected  []string
+	}{
+		{[]string{},
+			"./files.go",
+			excludedFileConfiguration,
+			[]string{}},
+		{[]string{"./files.go"},
+			"./files.go",
+			configuration,
+			[]string{"./files.go"}},
+	}
+
+	for _, tt := range addToFilesTests {
+		actual := AddToFiles(tt.filePaths, tt.filePath, tt.config)
+
+		if !reflect.DeepEqual(actual, tt.expected) {
+			t.Error(actual)
+			t.Error(tt.expected)
+			t.Errorf("AddToFiles(%s, %s, %+v): expected: %v, got: %v", tt.filePaths, tt.filePath, tt.config, tt.expected, actual)
+		}
+	}
+}
+
+func TestGetFiles(t *testing.T) {
+	configuration := config.Config{}
+	_, err := GetFiles(configuration)
+
+	if err == nil {
+		t.Errorf("Error expected")
+	}
+
+	configuration.PassedFiles = []string{"."}
+	files, err := GetFiles(configuration)
 
-	if result {
-		t.Error("Should return true if no excludes are given, got", result)
+	if len(files) > 0 && err != nil {
+		t.Errorf("Error expected")
 	}
 }
diff --git a/pkg/utils/utils_test.go b/pkg/utils/utils_test.go
index 0ce5286..3397f3e 100644
--- a/pkg/utils/utils_test.go
+++ b/pkg/utils/utils_test.go
@@ -5,57 +5,73 @@ import (
 )
 
 func TestGetEolChar(t *testing.T) {
-	if GetEolChar("lf") != "\n" {
-		t.Error("Expected end of line character to be \\n for \"lf\"")
+	getEolCharTests := []struct {
+		input    string
+		expected string
+	}{
+		{"lf", "\n"},
+		{"cr", "\r"},
+		{"crlf", "\r\n"},
+		{"", "\n"},
 	}
 
-	if GetEolChar("cr") != "\r" {
-		t.Error("Expected end of line character to be \\r for \"cr\"")
-	}
-
-	if GetEolChar("crlf") != "\r\n" {
-		t.Error("Expected end of line character to be \\r\\n for \"crlf\"")
-	}
-
-	if GetEolChar("") != "\n" {
-		t.Error("Expected end of line character to be \\n as a fallback")
+	for _, tt := range getEolCharTests {
+		actual := GetEolChar(tt.input)
+		if actual != tt.expected {
+			t.Errorf("GetEolChar(%s): expected: %v, got: %v", tt.input, tt.expected, actual)
+		}
 	}
 }
 
 func TestIsRegularFile(t *testing.T) {
-	if !IsRegularFile("./utils.go") {
-		t.Error("Expected utils.go to be a regular file")
+	isRegularFileTests := []struct {
+		input    string
+		expected bool
+	}{
+		{"./utils.go", true},
+		{"./notExisting.go", false},
+		{".", false},
 	}
 
-	if IsRegularFile("./notExisting.go") {
-		t.Error("Expected not existing file not to be a regular file")
-	}
-
-	if IsRegularFile(".") {
-		t.Error("Expected a directory not to be a regular file")
+	for _, tt := range isRegularFileTests {
+		actual := IsRegularFile(tt.input)
+		if actual != tt.expected {
+			t.Errorf("IsRegularFile(%s): expected: %v, got: %v", tt.input, tt.expected, actual)
+		}
 	}
 }
 
 func TestIsDirectory(t *testing.T) {
-	if !IsDirectory(".") {
-		t.Error("Expected the current directory to be a directory")
-	}
-
-	if IsDirectory("./notExisting") {
-		t.Error("Expected not existing directory not to be a directory")
+	isDirectoryTests := []struct {
+		input    string
+		expected bool
+	}{
+		{".", true},
+		{"./notExisting", false},
+		{"./utils.go", false},
 	}
 
-	if IsDirectory("./utils.go") {
-		t.Error("Expected a file not to be a directory")
+	for _, tt := range isDirectoryTests {
+		actual := IsDirectory(tt.input)
+		if actual != tt.expected {
+			t.Errorf("IsDirectory(%s): expected: %v, got: %v", tt.input, tt.expected, actual)
+		}
 	}
 }
 
 func TestFileExists(t *testing.T) {
-	if !FileExists("./utils.go") {
-		t.Error("./utils.go should exist")
+	fileExistsTests := []struct {
+		input    string
+		expected bool
+	}{
+		{"./utils.go", true},
+		{"./notExisting.go", false},
 	}
 
-	if FileExists("./notExisting.go") {
-		t.Error("./notExisting.go should not exist")
+	for _, tt := range fileExistsTests {
+		actual := FileExists(tt.input)
+		if actual != tt.expected {
+			t.Errorf("FileExists(%s): expected: %v, got: %v", tt.input, tt.expected, actual)
+		}
 	}
 }
diff --git a/pkg/validation/validation_test.go b/pkg/validation/validation_test.go
index bb38fdb..045411d 100644
--- a/pkg/validation/validation_test.go
+++ b/pkg/validation/validation_test.go
@@ -7,106 +7,106 @@ import (
 )
 
 func TestProcessValidation(t *testing.T) {
-	params := config.Config{Verbose: true}
+	configuration := config.Config{Verbose: true}
 
-	processValidationResult := ProcessValidation([]string{"./main.go"}, params)
+	processValidationResult := ProcessValidation([]string{"./main.go"}, configuration)
 	if len(processValidationResult) > 1 || len(processValidationResult[0].Errors) != 0 {
 		t.Error("Should not have errors when validating main.go, got", processValidationResult)
 	}
 
-	processValidationResult = ProcessValidation([]string{"./../../testfiles/disabled-file.ext"}, params)
+	processValidationResult = ProcessValidation([]string{"./../../testfiles/disabled-file.ext"}, configuration)
 	if len(processValidationResult) > 1 || len(processValidationResult[0].Errors) != 0 {
 		t.Error("Disabled file should have no errors, got", processValidationResult)
 	}
 
-	processValidationResult = ProcessValidation([]string{"./../../testfiles/empty-file.txt"}, params)
+	processValidationResult = ProcessValidation([]string{"./../../testfiles/empty-file.txt"}, configuration)
 	if len(processValidationResult) > 1 || len(processValidationResult[0].Errors) != 0 {
 		t.Error("Empty file should have no errors, got", processValidationResult)
 	}
 
-	processValidationResult = ProcessValidation([]string{"./../../testfiles/wrong-file.txt"}, params)
+	processValidationResult = ProcessValidation([]string{"./../../testfiles/wrong-file.txt"}, configuration)
 	if (len(processValidationResult) > 1) || (len(processValidationResult[0].Errors) != 1) {
 		t.Error("Wrong file should have errors, got", processValidationResult)
 	}
 }
 
 func TestValidateFile(t *testing.T) {
-	params := config.Config{Verbose: true}
+	configuration := config.Config{Verbose: true}
 
-	result := ValidateFile("./main.go", params)
+	result := ValidateFile("./main.go", configuration)
 	if len(result) != 0 {
 		t.Error("Should not have errors when validating main.go, got", result)
 	}
 
-	result = ValidateFile("./../../testfiles/wrong-file.txt", params)
+	result = ValidateFile("./../../testfiles/wrong-file.txt", configuration)
 	if len(result) != 1 {
 		t.Error("Should have errors when validating file with one error, got", result)
 	}
 
-	params.Disable.Indentation = true
-	result = ValidateFile("./../../testfiles/wrong-file.txt", params)
+	configuration.Disable.Indentation = true
+	result = ValidateFile("./../../testfiles/wrong-file.txt", configuration)
 	if len(result) != 0 {
 		t.Error("Should have no errors, got", result)
 	}
 
-	params = config.Config{SpacesAftertabs: true}
-	result = ValidateFile("./../../testfiles/spaces-after-tabs.txt", params)
+	configuration = config.Config{SpacesAftertabs: true}
+	result = ValidateFile("./../../testfiles/spaces-after-tabs.txt", configuration)
 	if len(result) != 0 {
 		t.Error("Should have no errors when validating valid file, got", result)
 	}
 
-	params = config.Config{SpacesAftertabs: false}
-	result = ValidateFile("./../../testfiles/zero-indent.txt", params)
+	configuration = config.Config{SpacesAftertabs: false}
+	result = ValidateFile("./../../testfiles/zero-indent.txt", configuration)
 	if len(result) != 0 {
 		t.Error("Should have no errors when validating valid file, got", result)
 	}
 
-	result = ValidateFile("./../../testfiles/disabled-line.txt", params)
+	result = ValidateFile("./../../testfiles/disabled-line.txt", configuration)
 	if len(result) != 0 {
 		t.Error("Should have no errors when validating valid file, got", result)
 	}
 
-	params = config.Config{SpacesAftertabs: false}
-	result = ValidateFile("./../../testfiles/spaces-after-tabs.txt", params)
+	configuration = config.Config{SpacesAftertabs: false}
+	result = ValidateFile("./../../testfiles/spaces-after-tabs.txt", configuration)
 	if len(result) != 1 {
 		t.Error("Should have one error, got", result)
 	}
 
-	params = config.Config{Verbose: true}
-	result = ValidateFile("./../../testfiles/trailing-whitespace.txt", params)
+	configuration = config.Config{Verbose: true}
+	result = ValidateFile("./../../testfiles/trailing-whitespace.txt", configuration)
 	if len(result) != 1 {
 		t.Error("Should have one error, got", result)
 	}
 
-	params = config.Config{Verbose: true}
-	params.Disable.TrimTrailingWhitespace = true
-	result = ValidateFile("./../../testfiles/trailing-whitespace.txt", params)
+	configuration = config.Config{Verbose: true}
+	configuration.Disable.TrimTrailingWhitespace = true
+	result = ValidateFile("./../../testfiles/trailing-whitespace.txt", configuration)
 	if len(result) != 0 {
 		t.Error("Should have no error, got", result)
 	}
 
-	params = config.Config{Verbose: true}
-	result = ValidateFile("./../../testfiles/final-newline-missing.txt", params)
+	configuration = config.Config{Verbose: true}
+	result = ValidateFile("./../../testfiles/final-newline-missing.txt", configuration)
 	if len(result) != 1 {
 		t.Error("Should have one error, got", result)
 	}
 
-	params = config.Config{Verbose: true}
-	params.Disable.InsertFinalNewline = true
-	result = ValidateFile("./../../testfiles/final-newline-missing.txt", params)
+	configuration = config.Config{Verbose: true}
+	configuration.Disable.InsertFinalNewline = true
+	result = ValidateFile("./../../testfiles/final-newline-missing.txt", configuration)
 	if len(result) != 0 {
 		t.Error("Should have no error, got", result)
 	}
 
-	params = config.Config{Verbose: true}
-	result = ValidateFile("./../../testfiles/wrong-line-ending.txt", params)
+	configuration = config.Config{Verbose: true}
+	result = ValidateFile("./../../testfiles/wrong-line-ending.txt", configuration)
 	if len(result) == 0 {
 		t.Error("Should have one error, got", result)
 	}
 
-	params = config.Config{Verbose: true}
-	params.Disable.EndOfLine = true
-	result = ValidateFile("./../../testfiles/wrong-line-ending.txt", params)
+	configuration = config.Config{Verbose: true}
+	configuration.Disable.EndOfLine = true
+	result = ValidateFile("./../../testfiles/wrong-line-ending.txt", configuration)
 	if len(result) != 0 {
 		t.Error("Should have no error, got", result)
 	}
diff --git a/pkg/validation/validators/validators_test.go b/pkg/validation/validators/validators_test.go
index 882e091..be498d6 100644
--- a/pkg/validation/validators/validators_test.go
+++ b/pkg/validation/validators/validators_test.go
@@ -1,409 +1,224 @@
 package validators
 
 import (
+	"errors"
+	"reflect"
 	"testing"
 
 	"github.com/editorconfig-checker/editorconfig-checker/pkg/config"
 )
 
-func TestFinalNewlineTrue(t *testing.T) {
-	if FinalNewline("x\n", "true", "lf") != nil {
-		t.Error("Expected FinalNewline to be true if insertFinalNewline is set to true and correct eol-char is used")
-	}
-
-	if FinalNewline("x", "true", "lf") == nil {
-		t.Error("Expected FinalNewline to be false if insertFinalNewline is set to true and no eol-char is used")
-	}
-
-	if FinalNewline("x\r", "true", "cr") != nil {
-		t.Error("Expected FinalNewline to be true if insertFinalNewline is set to true and correct eol-char is used")
-	}
-
-	if FinalNewline("x", "true", "cr") == nil {
-		t.Error("Expected FinalNewline to be false if insertFinalNewline is set to true and no eol-char is used")
-	}
-
-	if FinalNewline("x\r\n", "true", "crlf") != nil {
-		t.Error("Expected FinalNewline to be true if insertFinalNewline is set to true and correct eol-char is used")
-	}
-
-	if FinalNewline("x", "true", "crlf") == nil {
-		t.Error("Expected FinalNewline to be false if insertFinalNewline is set to true and no eol-char is used")
-	}
-
-	if FinalNewline("x\n", "true", "cr") == nil {
-		t.Error("Expected FinalNewline to be false if insertFinalNewline is set to true and the wrong eol-char is used")
-	}
-
-	if FinalNewline("x\n", "true", "crlf") == nil {
-		t.Error("Expected FinalNewline to be false if insertFinalNewline is set to true and the wrong eol-char is used")
-	}
-
-	if FinalNewline("x\r", "true", "lf") == nil {
-		t.Error("Expected FinalNewline to be false if insertFinalNewline is set to true and the wrong eol-char is used")
-	}
-
-	if FinalNewline("x\r", "true", "crlf") == nil {
-		t.Error("Expected FinalNewline to be false if insertFinalNewline is set to true and the wrong eol-char is used")
-	}
-
-	// TODO: This needs to be fixed
-	// if FinalNewline("x\r\n", "true", "lf") == nil {
-	// 	t.Error("Expected FinalNewline to be false if insertFinalNewline is set to true and the wrong eol-char is used")
-	// }
-
-	if FinalNewline("x\r\n", "true", "cr") == nil {
-		t.Error("Expected FinalNewline to be false if insertFinalNewline is set to true and the wrong eol-char is used")
-	}
-}
-
-func TestFinalNewlineFalse(t *testing.T) {
-	if FinalNewline("x", "false", "lf") != nil {
-		t.Error("Expected FinalNewline to be true if insertFinalNewline is set to false and no eol-char is used")
-	}
-
-	if FinalNewline("x\n", "false", "lf") == nil {
-		t.Error("Expected FinalNewline to be false if insertFinalNewline is set to false and lf is used as eol-char")
-	}
-
-	if FinalNewline("x\r", "false", "lf") == nil {
-		t.Error("Expected FinalNewline to be false if insertFinalNewline is set to false and cr is used as eol-char")
-	}
-
-	if FinalNewline("x\r\n", "false", "lf") == nil {
-		t.Error("Expected FinalNewline to be false if insertFinalNewline is set to false and crlf is used as eol-char")
-	}
-
-	if FinalNewline("x", "false", "cr") != nil {
-		t.Error("Expected FinalNewline to be true if insertFinalNewline is set to false and no eol-char is used")
-	}
-
-	if FinalNewline("x\n", "false", "cr") == nil {
-		t.Error("Expected FinalNewline to be false if insertFinalNewline is set to false and lf is used as eol-char")
-	}
-
-	if FinalNewline("x\r", "false", "cr") == nil {
-		t.Error("Expected FinalNewline to be false if insertFinalNewline is set to false and cr is used as eol-char")
-	}
-
-	if FinalNewline("x\r\n", "false", "cr") == nil {
-		t.Error("Expected FinalNewline to be false if insertFinalNewline is set to false and crlf is used as eol-char")
-	}
-
-	if FinalNewline("x", "false", "crlf") != nil {
-		t.Error("Expected FinalNewline to be true if insertFinalNewline is set to false and no eol-char is used")
-	}
-
-	if FinalNewline("x\n", "false", "crlf") == nil {
-		t.Error("Expected FinalNewline to be false if insertFinalNewline is set to false and lf is used as eol-char")
-	}
-
-	if FinalNewline("x\r", "false", "crlf") == nil {
-		t.Error("Expected FinalNewline to be false if insertFinalNewline is set to false and cr is used as eol-char")
-	}
-
-	if FinalNewline("x\r\n", "false", "crlf") == nil {
-		t.Error("Expected FinalNewline to be false if insertFinalNewline is set to false and crlf is used as eol-char")
-	}
-}
-
-func TestFinalNewlineNothing(t *testing.T) {
-	if FinalNewline("x", "", "lf") != nil {
-		t.Error("Expected FinalNewline to be true if nothing is set for insertfinalnewline")
-	}
-
-	if FinalNewline("x", "", "cr") != nil {
-		t.Error("Expected FinalNewline to be true if nothing is set for insertfinalnewline")
-	}
-
-	if FinalNewline("x", "", "crlf") != nil {
-		t.Error("Expected FinalNewline to be true if nothing is set for insertfinalnewline")
-	}
-
-	if FinalNewline("x\n", "", "lf") != nil {
-		t.Error("Expected FinalNewline to be true if nothing is set for insertfinalnewline")
-	}
-
-	if FinalNewline("x\n", "", "cr") != nil {
-		t.Error("Expected FinalNewline to be true if nothing is set for insertfinalnewline")
-	}
-
-	if FinalNewline("x\n", "", "crlf") != nil {
-		t.Error("Expected FinalNewline to be true if nothing is set for insertfinalnewline")
-	}
-
-	if FinalNewline("x\r", "", "lf") != nil {
-		t.Error("Expected FinalNewline to be true if nothing is set for insertfinalnewline")
-	}
-
-	if FinalNewline("x\r", "", "cr") != nil {
-		t.Error("Expected FinalNewline to be true if nothing is set for insertfinalnewline")
-	}
-
-	if FinalNewline("x\r", "", "crlf") != nil {
-		t.Error("Expected FinalNewline to be true if nothing is set for insertfinalnewline")
-	}
-
-	if FinalNewline("x\r\n", "", "lf") != nil {
-		t.Error("Expected FinalNewline to be true if nothing is set for insertfinalnewline")
-	}
-
-	if FinalNewline("x\r\n", "", "cr") != nil {
-		t.Error("Expected FinalNewline to be true if nothing is set for insertfinalnewline")
-	}
-
-	if FinalNewline("x\r\n", "", "crlf") != nil {
-		t.Error("Expected FinalNewline to be true if nothing is set for insertfinalnewline")
-	}
-}
-
-func TestLineEndingLf(t *testing.T) {
-	if LineEnding("x", "lf") != nil {
-		t.Error("Expected to return true for line without linebreak")
-	}
-
-	if LineEnding("x\n", "lf") != nil {
-		t.Error("Expected to return true for a valid file(lf)")
-	}
-
-	if LineEnding("x\r", "lf") == nil {
-		t.Error("Expected to return false for an invalid file(lf) with \\r")
-	}
-
-	if LineEnding("x\r\n", "lf") == nil {
-		t.Error("Expected to return false for an invalid file(lf) with \\r\\n")
-	}
-
-	if LineEnding("x\ry\nz\n", "lf") == nil {
-		t.Error("Expected to return false for mixed file(lf)")
-	}
-}
-
-func TestLineEndingCr(t *testing.T) {
-	if LineEnding("x", "cr") != nil {
-		t.Error("Expected to return true for line without linebreak")
-	}
-
-	if LineEnding("x\r", "cr") != nil {
-		t.Error("Expected to return true for a valid file(cr)")
-	}
-
-	if LineEnding("x\n", "cr") == nil {
-		t.Error("Expected to return false for an invalid file(cr) with \\n")
-	}
-
-	if LineEnding("x\r\n", "cr") == nil {
-		t.Error("Expected to return false for an invalid file(cr) with \\r\\n")
-	}
-
-	if LineEnding("x\ry\nz\n", "cr") == nil {
-		t.Error("Expected to return false for mixed file(lf)")
+func TestFinalNewline(t *testing.T) {
+	finalNewlineTests := []struct {
+		line               string
+		insertFinalNewline string
+		lineEnding         string
+		expected           error
+	}{
+		{"x\n", "true", "lf", nil},
+		{"x\r", "true", "cr", nil},
+		{"x\r\n", "true", "crlf", nil},
+
+		{"x", "true", "lf", errors.New("Wrong line endings or new final newline")},
+		{"x", "true", "cr", errors.New("Wrong line endings or new final newline")},
+		{"x", "true", "crlf", errors.New("Wrong line endings or new final newline")},
+
+		{"x\n", "true", "cr", errors.New("Wrong line endings or new final newline")},
+		{"x\n", "true", "crlf", errors.New("Wrong line endings or new final newline")},
+
+		{"x\r", "true", "lf", errors.New("Wrong line endings or new final newline")},
+		{"x\r", "true", "crlf", errors.New("Wrong line endings or new final newline")},
+
+		// TODO: Needs a fix (\n is the last char so it somehow matches)
+		// {"x\r\n", "true", "lf", errors.New("Wrong line endings or new final newline")},
+		{"x\r\n", "true", "cr", errors.New("Wrong line endings or new final newline")},
+
+		// insert_final_newline false
+		{"x", "false", "lf", nil},
+		{"x\n", "false", "lf", errors.New("No final newline expected")},
+		{"x\r", "false", "lf", errors.New("No final newline expected")},
+		{"x\r\n", "false", "lf", errors.New("No final newline expected")},
+
+		{"x", "false", "cr", nil},
+		{"x\n", "false", "cr", errors.New("No final newline expected")},
+		{"x\r", "false", "cr", errors.New("No final newline expected")},
+		{"x\r\n", "false", "cr", errors.New("No final newline expected")},
+
+		{"x", "false", "crlf", nil},
+		{"x\n", "false", "crlf", errors.New("No final newline expected")},
+		{"x\r", "false", "crlf", errors.New("No final newline expected")},
+		{"x\r\n", "false", "crlf", errors.New("No final newline expected")},
+
+		// insert_final_newline not set
+		{"x", "", "lf", nil},
+		{"x", "", "cr", nil},
+		{"x", "", "crlf", nil},
+		{"x\n", "", "lf", nil},
+		{"x\n", "", "cr", nil},
+		{"x\n", "", "crlf", nil},
+		{"x\r", "", "lf", nil},
+		{"x\r", "", "cr", nil},
+		{"x\r", "", "crlf", nil},
+		{"x\r\n", "", "lf", nil},
+		{"x\r\n", "", "cr", nil},
+		{"x\r\n", "", "crlf", nil},
+	}
+
+	for _, tt := range finalNewlineTests {
+		actual := FinalNewline(tt.line, tt.insertFinalNewline, tt.lineEnding)
+		if !reflect.DeepEqual(actual, tt.expected) {
+			t.Errorf("FinalNewline(%s, %s, %s): expected: %v, got: %v", tt.line, tt.insertFinalNewline, tt.lineEnding, tt.expected, actual)
+		}
 	}
 }
 
-func TestLineEndingCrlf(t *testing.T) {
-	if LineEnding("x", "crlf") != nil {
-		t.Error("Expected to return true for line without linebreak")
-	}
-
-	if LineEnding("x\r\n", "crlf") != nil {
-		t.Error("Expected to return true for a valid file(crlf)")
-	}
-
-	if LineEnding("x\n", "crlf") == nil {
-		t.Error("Expected to return false for an invalid file(crlf) with \\n")
-	}
-
-	if LineEnding("x\r", "crlf") == nil {
-		t.Error("Expected to return false for an invalid file(crlf) with \\r")
-	}
-
-	if LineEnding("x\ry\nz\n", "crlf") == nil {
-		t.Error("Expected to return false for mixed file(crlf)")
+func TestLineEnding(t *testing.T) {
+	linedEndingTests := []struct {
+		line       string
+		lineEnding string
+		expected   error
+	}{
+		{"x", "lf", nil},
+		{"x\n", "lf", nil},
+		{"x\r", "lf", errors.New("Not all lines have the correct end of line character")},
+		{"x\r\n", "lf", errors.New("Not all lines have the correct end of line character")},
+		{"x\ry\nz\n", "lf", errors.New("Not all lines have the correct end of line character")},
+
+		{"x", "cr", nil},
+		{"x\r", "cr", nil},
+		{"x\n", "cr", errors.New("Not all lines have the correct end of line character")},
+		{"x\r\n", "cr", errors.New("Not all lines have the correct end of line character")},
+		{"x\ry\nz\n", "cr", errors.New("Not all lines have the correct end of line character")},
+
+		{"x", "crlf", nil},
+		{"x\r\n", "crlf", nil},
+		{"x\r", "crlf", errors.New("Not all lines have the correct end of line character")},
+		{"x\n", "crlf", errors.New("Not all lines have the correct end of line character")},
+		{"x\ry\nz\n", "crlf", errors.New("Not all lines have the correct end of line character")},
+	}
+
+	for _, tt := range linedEndingTests {
+		actual := LineEnding(tt.line, tt.lineEnding)
+		if !reflect.DeepEqual(actual, tt.expected) {
+			t.Errorf("LineEnding(%s, %s): expected: %v, got: %v", tt.line, tt.lineEnding, tt.expected, actual)
+		}
 	}
 }
 
 func TestIndentation(t *testing.T) {
-	params := config.Config{SpacesAftertabs: false}
-	if (Indentation("    x", "space", 4, params)) != nil {
-		t.Error("Expected correctly indented line to return an nil")
-	}
-
-	if (Indentation("   x", "space", 4, params)) == nil {
-		t.Error("Expected wrong indented line to return an error")
-	}
-
-	if (Indentation("	x", "tab", 0, params)) != nil {
-		t.Error("Expected correctly indented line to return an nil")
-	}
-
-	if (Indentation("   x", "tab", 0, params)) == nil {
-		t.Error("Expected wrong indented line to return an error")
-	}
-
-	if (Indentation("	x", "x", 0, params)) != nil {
-		t.Error("Expected unknown indentation to return nil")
-	}
-
-	if (Indentation("   x", "x", 0, params)) != nil {
-		t.Error("Expected unknown indentation to return nil")
+	configuration := config.Config{SpacesAftertabs: false}
+
+	indentationTests := []struct {
+		line        string
+		indentStyle string
+		indenSize   int
+		expected    error
+	}{
+		{"    x", "space", 4, nil},
+		{"   x", "space", 4, errors.New("Wrong amount of left-padding spaces(want multiple of 4)")},
+		{"	x", "tab", 0, nil},
+		{"   x", "tab", 0, errors.New("Wrong indentation type(spaces instead of tabs)")},
+		{"	x", "x", 0, nil},
+		{"   x", "x", 0, nil},
+	}
+
+	for _, tt := range indentationTests {
+		actual := Indentation(tt.line, tt.indentStyle, tt.indenSize, configuration)
+		if !reflect.DeepEqual(actual, tt.expected) {
+			t.Errorf("Indentation(%s, %s, %d, %+v): expected: %v, got: %v", tt.line, tt.indentStyle, tt.indenSize, configuration, tt.expected, actual)
+		}
 	}
 }
 
 func TestSpace(t *testing.T) {
-	if Space("", 4) != nil {
-		t.Error("Expected empty line to return true regardless of parameter")
-	}
-
-	if Space("x", 0) != nil {
-		t.Error("Expected call with indentSize 0 to always return nil")
-	}
-
-	if Space("x", 4) != nil {
-		t.Error("Expected line which starts at the beginning to return true")
-	}
-
-	if Space("    x", 4) != nil {
-		t.Error("Expected correctly indented line to return true")
-	}
-
-	if Space("     x", 4) == nil {
-		t.Error("Expected falsy indented line to return false")
-	}
-
-	if Space("   x", 4) == nil {
-		t.Error("Expected falsy indented line to return false")
-	}
-
-	if Space("     *", 4) != nil {
-		t.Error("Expected correctly indented line to be true with empty block comment line")
-	}
-
-	if Space("     * some comment", 4) != nil {
-		t.Error("Expected correctly indented line to be true with block comment")
+	spaceTests := []struct {
+		line       string
+		indentSize int
+		expected   error
+	}{
+		{"", 4, nil},
+		{"x", 0, nil},
+		{"x", 4, nil},
+		{"    x", 4, nil},
+		// 5 spaces
+		{"     x", 4, errors.New("Wrong amount of left-padding spaces(want multiple of 4)")},
+		// 3 spaces
+		{"   x", 4, errors.New("Wrong amount of left-padding spaces(want multiple of 4)")},
+		// correct indented block comment, empty and non empty
+		{"     *", 4, nil},
+		{"     * some comment", 4, nil},
+	}
+
+	for _, tt := range spaceTests {
+		actual := Space(tt.line, tt.indentSize)
+		if !reflect.DeepEqual(actual, tt.expected) {
+			t.Errorf("Space(%s, %d): expected: %v, got: %v", tt.line, tt.indentSize, tt.expected, actual)
+		}
 	}
 }
 
-func TestTabSpacesAllowed(t *testing.T) {
+func TestTab(t *testing.T) {
 	spacesAllowed := config.Config{SpacesAftertabs: true}
-
-	if Tab(" x", spacesAllowed) != nil {
-		t.Error("Expected char after space to return nil")
-	}
-
-	if Tab("	   bla", spacesAllowed) != nil {
-		t.Error("Expected tab indented and with spaces aligned line to return nil")
-	}
-
-	if Tab("	 bla", spacesAllowed) != nil {
-		t.Error("Expected tab indented and with spaces aligned line to return nil")
-	}
-
-	if (Tab("		  xx", spacesAllowed)) != nil {
-		t.Error("starting with tabs and trailing spaces with at least one character after the spaces")
-	}
-}
-
-func TestTabSpacesForbidden(t *testing.T) {
 	spacesForbidden := config.Config{SpacesAftertabs: false}
-
-	if Tab("", spacesForbidden) != nil {
-		t.Error("Expected empty line to return true regardless of parameter")
-	}
-
-	if Tab("x", spacesForbidden) != nil {
-		t.Error("Expected line which starts at the beginning to return true")
-	}
-
-	if Tab("	x", spacesForbidden) != nil {
-		t.Error("Expected correctly indented line to return true")
-	}
-
-	if (Tab("		x", spacesForbidden)) != nil {
-		t.Error("starting with tabs and characters after that")
-	}
-
-	if (Tab("  	a", spacesForbidden)) == nil {
-		t.Error("Starting with two spaces, followed by a tab and followed by a non whitespace char should not return nil")
-	}
-
-	if (Tab(" *", spacesForbidden)) != nil {
-		t.Error("starting with a space and at leat one character after the space")
-	}
-
-	if Tab(" * some comment", spacesForbidden) != nil {
-		t.Error("Expected tab indented block comment without indentation and with comment to return nil")
-	}
-
-	if Tab("	 *", spacesForbidden) != nil {
-		t.Error("Expected tab indented block comment with indentation and without comment to return nil")
-	}
-
-	if Tab("	 * some comment", spacesForbidden) != nil {
-		t.Error("Expected tab indented block comment with indentation and with comment to return nil")
-	}
-
-	if Tab(" */", spacesForbidden) != nil {
-		t.Error("Expected tab indented block comment without indentation in the last line to return nil")
-	}
-
-	if Tab("	 */", spacesForbidden) != nil {
-		t.Error("Expected tab indented block comment with indentation in the last line to return nil")
-	}
-
-	if Tab(" *", spacesForbidden) != nil {
-		t.Error("Expected tab indented block comment without indentation to return nil")
+	tabTests := []struct {
+		line     string
+		config   config.Config
+		expected error
+	}{
+		{" x", spacesAllowed, nil},
+		{"	   bla", spacesAllowed, nil},
+		{"	 bla", spacesAllowed, nil},
+		{"		  xx", spacesAllowed, nil},
+
+		{"", spacesForbidden, nil},
+		{"x", spacesForbidden, nil},
+		{"	x", spacesForbidden, nil},
+		{"		x", spacesForbidden, nil},
+		{"  	a", spacesForbidden, errors.New("Wrong indentation type(spaces instead of tabs)")},
+		{" *", spacesForbidden, nil},
+		{"	 *", spacesForbidden, nil},
+		{"	 * some comment", spacesForbidden, nil},
+		{" */", spacesForbidden, nil},
+		{"	 */", spacesForbidden, nil},
+		{" *", spacesForbidden, nil},
+	}
+
+	for _, tt := range tabTests {
+		actual := Tab(tt.line, tt.config)
+		if !reflect.DeepEqual(actual, tt.expected) {
+			t.Errorf("Tab(%s, %+v): expected: %v, got: %v", tt.line, tt.config, tt.expected, actual)
+		}
 	}
 }
 
 func TestTrailingWhitespace(t *testing.T) {
-	if TrailingWhitespace("", true) != nil {
-		t.Error("Expected empty line to return true regardless of trimTrailingWhitespace parameter")
-	}
-
-	if TrailingWhitespace("", false) != nil {
-		t.Error("Expected empty line to return true regardless of trimTrailingWhitespace parameter")
-	}
-
-	if TrailingWhitespace("x", true) != nil {
-		t.Error("Expected line with no trailing space and trimTrailingWhitespace set to true to return true")
-	}
-
-	if TrailingWhitespace("x", false) != nil {
-		t.Error("Expected line with no trailing space and trimTrailingWhitespace set to false to return true")
-	}
-
-	// Spaces
-	if TrailingWhitespace("x ", true) == nil {
-		t.Error("Expected line with trailing space and trimTrailingWhitespace set to true to return false")
-	}
-
-	if TrailingWhitespace("x ", false) != nil {
-		t.Error("Expected line with trailing space and trimTrailingWhitespace set to false to return true")
-	}
-
-	if TrailingWhitespace("x .", true) != nil {
-		t.Error("Expected line with no trailing space and trimTrailingWhitespace set to true to return true")
-	}
-
-	if TrailingWhitespace("x .", false) != nil {
-		t.Error("Expected line with no trailing space and trimTrailingWhitespace set to false to return true")
-	}
-
-	// Tabs
-	if TrailingWhitespace("x	", true) == nil {
-		t.Error("Expected line with trailing space and trimTrailingWhitespace set to true to return false")
-	}
-
-	if TrailingWhitespace("x	", false) != nil {
-		t.Error("Expected line with trailing space and trimTrailingWhitespace set to false to return true")
-	}
-
-	if TrailingWhitespace("x	.", true) != nil {
-		t.Error("Expected line with no trailing space and trimTrailingWhitespace set to true to return true")
-	}
-
-	if TrailingWhitespace("x	.", false) != nil {
-		t.Error("Expected line with no trailing space and trimTrailingWhitespace set to false to return true")
+	trailingWhitespaceTests := []struct {
+		line                   string
+		trimTrailingWhitespace bool
+		expected               error
+	}{
+		{"", true, nil},
+		{"", false, nil},
+		{"x", true, nil},
+		{"x", false, nil},
+
+		// Spaces
+		{"x ", true, errors.New("Trailing whitespace")},
+		{"x ", false, nil},
+		{"x .", true, nil},
+		{"x .", false, nil},
+
+		// Tabs
+		{"x	", true, errors.New("Trailing whitespace")},
+		{"x	", false, nil},
+		{"x	.", true, nil},
+		{"x	.", false, nil},
+	}
+
+	for _, tt := range trailingWhitespaceTests {
+		actual := TrailingWhitespace(tt.line, tt.trimTrailingWhitespace)
+		if !reflect.DeepEqual(actual, tt.expected) {
+			t.Errorf("TrailingWhitespace(%s, %v): expected: %v, got: %v", tt.line, tt.trimTrailingWhitespace, tt.expected, actual)
+		}
 	}
 }