Add utils MustGetFileAsString and GetNumberOfLeftSpaces.
This commit is contained in:
@@ -2,9 +2,11 @@ package editorconfig
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
var filePathSeparatorRegex = regexp.QuoteMeta(string(filepath.Separator))
|
var filePathSeparatorRegex = regexp.QuoteMeta(string(filepath.Separator))
|
||||||
@@ -54,3 +56,16 @@ func GetErrorWithLineBreaksVisible(s string) string {
|
|||||||
s = crlfRegexp.ReplaceAllString(s, `\r\n`)
|
s = crlfRegexp.ReplaceAllString(s, `\r\n`)
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func MustGetFileAsString(path string) string {
|
||||||
|
inBytes, err := ioutil.ReadFile(path)
|
||||||
|
if err != nil {
|
||||||
|
ExitBecauseOfInternalError("Could not read file: " + path)
|
||||||
|
}
|
||||||
|
|
||||||
|
return string(inBytes)
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetNumberOfLeftSpaces(s string) int {
|
||||||
|
return len(s) - len(strings.TrimLeft(s, " "))
|
||||||
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package editorconfig
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"reflect"
|
"reflect"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
@@ -25,3 +26,18 @@ func TestSplitIntoLines(t *testing.T) {
|
|||||||
t.Error("Did not split string into lines correctly, got lines: " + strings.Join(result, ", "))
|
t.Error("Did not split string into lines correctly, got lines: " + strings.Join(result, ", "))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestMustGetFileAsString(t *testing.T) {
|
||||||
|
license := MustGetFileAsString("../LICENSE")
|
||||||
|
if !strings.Contains(license, "MIT License") || !strings.Contains(license, "THE SOFTWARE IS PROVIDED \"AS IS\"") {
|
||||||
|
t.Error("Could not read file")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGetNumberOfLeftSpaces(t *testing.T) {
|
||||||
|
for i := 0; i < 20; i++ {
|
||||||
|
if GetNumberOfLeftSpaces(strings.Repeat(" ", i)) != i {
|
||||||
|
t.Error("Wrong number of spaces returned when string starts with " + strconv.Itoa(i) + " spaces")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user