Allows opening files using full path on Windows (#1126)

* Now can open Windows full-path from command line arg

Example that now works: micro.exe D:\myfile.txt

* Now correctly retrieves the path from the input path string. Except for single-letter filenames

* Fixed line/cols, need to make the code prettier

* Fixed path matching with regex by @Pariador

* Fixed not stripping the line/col args from file path

* Added tests for ParseCursorLocation
This commit is contained in:
Dimitar Borislavov Tasev
2018-06-03 22:13:03 +01:00
committed by Zachary Yedidia
parent d7f7d845b9
commit efe343b37c
3 changed files with 290 additions and 40 deletions

View File

@@ -103,3 +103,239 @@ func TestWidthOfLargeRunes(t *testing.T) {
t.Error("WidthOfLargeRunes 5 Failed. Got", w)
}
}
func assertEqual(t *testing.T, expected interface{}, result interface{}) {
if expected != result {
t.Fatalf("Expected: %d != Got: %d", expected, result)
}
}
func assertTrue(t *testing.T, condition bool) {
if !condition {
t.Fatalf("Condition was not true. Got false")
}
}
func TestGetPathRelativeWithDot(t *testing.T) {
path, cursorPosition := GetPathAndCursorPosition("./myfile:10:5")
assertEqual(t, path, "./myfile")
assertEqual(t, "10", cursorPosition[0])
assertEqual(t, "5", cursorPosition[1])
}
func TestGetPathRelativeWithDotWindows(t *testing.T) {
path, cursorPosition := GetPathAndCursorPosition(".\\myfile:10:5")
assertEqual(t, path, ".\\myfile")
assertEqual(t, "10", cursorPosition[0])
assertEqual(t, cursorPosition[1], "5")
}
func TestGetPathRelativeNoDot(t *testing.T) {
path, cursorPosition := GetPathAndCursorPosition("myfile:10:5")
assertEqual(t, path, "myfile")
assertEqual(t, "10", cursorPosition[0])
assertEqual(t, cursorPosition[1], "5")
}
func TestGetPathAbsoluteWindows(t *testing.T) {
path, cursorPosition := GetPathAndCursorPosition("C:\\myfile:10:5")
assertEqual(t, path, "C:\\myfile")
assertEqual(t, "10", cursorPosition[0])
assertEqual(t, cursorPosition[1], "5")
path, cursorPosition = GetPathAndCursorPosition("C:/myfile:10:5")
assertEqual(t, path, "C:/myfile")
assertEqual(t, "10", cursorPosition[0])
assertEqual(t, cursorPosition[1], "5")
}
func TestGetPathAbsoluteUnix(t *testing.T) {
path, cursorPosition := GetPathAndCursorPosition("/home/user/myfile:10:5")
assertEqual(t, path, "/home/user/myfile")
assertEqual(t, "10", cursorPosition[0])
assertEqual(t, cursorPosition[1], "5")
}
func TestGetPathRelativeWithDotWithoutLineAndColumn(t *testing.T) {
path, cursorPosition := GetPathAndCursorPosition("./myfile")
assertEqual(t, path, "./myfile")
assertEqual(t, "0", cursorPosition[0])
assertEqual(t, "0", cursorPosition[1])
}
func TestGetPathRelativeWithDotWindowsWithoutLineAndColumn(t *testing.T) {
path, cursorPosition := GetPathAndCursorPosition(".\\myfile")
assertEqual(t, path, ".\\myfile")
assertEqual(t, "0", cursorPosition[0])
assertEqual(t, "0", cursorPosition[1])
}
func TestGetPathRelativeNoDotWithoutLineAndColumn(t *testing.T) {
path, cursorPosition := GetPathAndCursorPosition("myfile")
assertEqual(t, path, "myfile")
assertEqual(t, "0", cursorPosition[0])
assertEqual(t, "0", cursorPosition[1])
}
func TestGetPathAbsoluteWindowsWithoutLineAndColumn(t *testing.T) {
path, cursorPosition := GetPathAndCursorPosition("C:\\myfile")
assertEqual(t, path, "C:\\myfile")
assertEqual(t, "0", cursorPosition[0])
assertEqual(t, "0", cursorPosition[1])
path, cursorPosition = GetPathAndCursorPosition("C:/myfile")
assertEqual(t, path, "C:/myfile")
assertEqual(t, "0", cursorPosition[0])
assertEqual(t, "0", cursorPosition[1])
}
func TestGetPathAbsoluteUnixWithoutLineAndColumn(t *testing.T) {
path, cursorPosition := GetPathAndCursorPosition("/home/user/myfile")
assertEqual(t, path, "/home/user/myfile")
assertEqual(t, "0", cursorPosition[0])
assertEqual(t, "0", cursorPosition[1])
}
func TestGetPathSingleLetterFileRelativePath(t *testing.T) {
path, cursorPosition := GetPathAndCursorPosition("a:5:6")
assertEqual(t, path, "a")
assertEqual(t, "5", cursorPosition[0])
assertEqual(t, "6", cursorPosition[1])
}
func TestGetPathSingleLetterFileAbsolutePathWindows(t *testing.T) {
path, cursorPosition := GetPathAndCursorPosition("C:\\a:5:6")
assertEqual(t, path, "C:\\a")
assertEqual(t, "5", cursorPosition[0])
assertEqual(t, "6", cursorPosition[1])
path, cursorPosition = GetPathAndCursorPosition("C:/a:5:6")
assertEqual(t, path, "C:/a")
assertEqual(t, "5", cursorPosition[0])
assertEqual(t, "6", cursorPosition[1])
}
func TestGetPathSingleLetterFileAbsolutePathUnix(t *testing.T) {
path, cursorPosition := GetPathAndCursorPosition("/home/user/a:5:6")
assertEqual(t, path, "/home/user/a")
assertEqual(t, "5", cursorPosition[0])
assertEqual(t, "6", cursorPosition[1])
}
func TestGetPathSingleLetterFileAbsolutePathWindowsWithoutLineAndColumn(t *testing.T) {
path, cursorPosition := GetPathAndCursorPosition("C:\\a")
assertEqual(t, path, "C:\\a")
assertEqual(t, "0", cursorPosition[0])
assertEqual(t, "0", cursorPosition[1])
path, cursorPosition = GetPathAndCursorPosition("C:/a")
assertEqual(t, path, "C:/a")
assertEqual(t, "0", cursorPosition[0])
assertEqual(t, "0", cursorPosition[1])
}
func TestGetPathSingleLetterFileAbsolutePathUnixWithoutLineAndColumn(t *testing.T) {
path, cursorPosition := GetPathAndCursorPosition("/home/user/a")
assertEqual(t, path, "/home/user/a")
assertEqual(t, "0", cursorPosition[0])
assertEqual(t, "0", cursorPosition[1])
}
// TODO test for only line without a column
func TestGetPathRelativeWithDotOnlyLine(t *testing.T) {
path, cursorPosition := GetPathAndCursorPosition("./myfile:10")
assertEqual(t, path, "./myfile")
assertEqual(t, "10", cursorPosition[0])
assertEqual(t, "0", cursorPosition[1])
}
func TestGetPathRelativeWithDotWindowsOnlyLine(t *testing.T) {
path, cursorPosition := GetPathAndCursorPosition(".\\myfile:10")
assertEqual(t, path, ".\\myfile")
assertEqual(t, "10", cursorPosition[0])
assertEqual(t, "0", cursorPosition[1])
}
func TestGetPathRelativeNoDotOnlyLine(t *testing.T) {
path, cursorPosition := GetPathAndCursorPosition("myfile:10")
assertEqual(t, path, "myfile")
assertEqual(t, "10", cursorPosition[0])
assertEqual(t, "0", cursorPosition[1])
}
func TestGetPathAbsoluteWindowsOnlyLine(t *testing.T) {
path, cursorPosition := GetPathAndCursorPosition("C:\\myfile:10")
assertEqual(t, path, "C:\\myfile")
assertEqual(t, "10", cursorPosition[0])
assertEqual(t, "0", cursorPosition[1])
path, cursorPosition = GetPathAndCursorPosition("C:/myfile:10")
assertEqual(t, path, "C:/myfile")
assertEqual(t, "10", cursorPosition[0])
assertEqual(t, "0", cursorPosition[1])
}
func TestGetPathAbsoluteUnixOnlyLine(t *testing.T) {
path, cursorPosition := GetPathAndCursorPosition("/home/user/myfile:10")
assertEqual(t, path, "/home/user/myfile")
assertEqual(t, "10", cursorPosition[0])
assertEqual(t, "0", cursorPosition[1])
}
func TestParseCursorLocationOneArg(t *testing.T) {
location, err := ParseCursorLocation([]string{"3"})
assertEqual(t, 3, location.Y)
assertEqual(t, 0, location.X)
assertEqual(t, nil, err)
}
func TestParseCursorLocationTwoArgs(t *testing.T) {
location, err := ParseCursorLocation([]string{"3", "15"})
assertEqual(t, 3, location.Y)
assertEqual(t, 15, location.X)
assertEqual(t, nil, err)
}
func TestParseCursorLocationNoArgs(t *testing.T) {
location, err := ParseCursorLocation([]string{})
// the expected result is the start position - 0, 0
assertEqual(t, 0, location.Y)
assertEqual(t, 0, location.X)
assertEqual(t, nil, err)
}
func TestParseCursorLocationFirstArgNotValidNumber(t *testing.T) {
// the messenger is necessary as ParseCursorLocation
// puts a message in it on error
messenger = new(Messenger)
_, err := ParseCursorLocation([]string{"apples", "1"})
// the expected result is the start position - 0, 0
assertTrue(t, messenger.hasMessage)
assertTrue(t, err != nil)
}
func TestParseCursorLocationSecondArgNotValidNumber(t *testing.T) {
// the messenger is necessary as ParseCursorLocation
// puts a message in it on error
messenger = new(Messenger)
_, err := ParseCursorLocation([]string{"1", "apples"})
// the expected result is the start position - 0, 0
assertTrue(t, messenger.hasMessage)
assertTrue(t, err != nil)
}