]> git.lizzy.rs Git - micro.git/blob - cmd/micro/buffer_test.go
Fix -startpos flag being ignored (#1129)
[micro.git] / cmd / micro / buffer_test.go
1 package main
2
3 import (
4         "testing"
5 )
6
7 func TestGetBufferCursorLocationEmptyArgs(t *testing.T) {
8         buf := NewBufferFromString("this is my\nbuffer\nfile\nhello", "")
9
10         location, err := GetBufferCursorLocation(nil, buf)
11
12         assertEqual(t, 0, location.Y)
13         assertEqual(t, 0, location.X)
14
15         // an error is present due to the cursorLocation being nil
16         assertTrue(t, err != nil)
17
18 }
19
20 func TestGetBufferCursorLocationStartposFlag(t *testing.T) {
21         buf := NewBufferFromString("this is my\nbuffer\nfile\nhello", "")
22
23         *flagStartPos = "1,2"
24
25         location, err := GetBufferCursorLocation(nil, buf)
26
27         // note: 1 is subtracted from the line to get the correct index in the buffer
28         assertTrue(t, 0 == location.Y)
29         assertTrue(t, 2 == location.X)
30
31         // an error is present due to the cursorLocation being nil
32         assertTrue(t, err != nil)
33 }
34
35 func TestGetBufferCursorLocationInvalidStartposFlag(t *testing.T) {
36         buf := NewBufferFromString("this is my\nbuffer\nfile\nhello", "")
37
38         *flagStartPos = "apples,2"
39
40         location, err := GetBufferCursorLocation(nil, buf)
41         // expect to default to the start of the file, which is 0,0
42         assertEqual(t, 0, location.Y)
43         assertEqual(t, 0, location.X)
44
45         // an error is present due to the cursorLocation being nil
46         assertTrue(t, err != nil)
47 }
48 func TestGetBufferCursorLocationStartposFlagAndCursorPosition(t *testing.T) {
49         text := "this is my\nbuffer\nfile\nhello"
50         cursorPosition := []string{"3", "1"}
51
52         buf := NewBufferFromString(text, "")
53
54         *flagStartPos = "1,2"
55
56         location, err := GetBufferCursorLocation(cursorPosition, buf)
57         // expect to have the flag positions, not the cursor position
58         // note: 1 is subtracted from the line to get the correct index in the buffer
59         assertEqual(t, 0, location.Y)
60         assertEqual(t, 2, location.X)
61
62         assertTrue(t, err == nil)
63 }
64 func TestGetBufferCursorLocationCursorPositionAndInvalidStartposFlag(t *testing.T) {
65         text := "this is my\nbuffer\nfile\nhello"
66         cursorPosition := []string{"3", "1"}
67
68         buf := NewBufferFromString(text, "")
69
70         *flagStartPos = "apples,2"
71
72         location, err := GetBufferCursorLocation(cursorPosition, buf)
73         // expect to have the flag positions, not the cursor position
74         // note: 1 is subtracted from the line to get the correct index in the buffer
75         assertEqual(t, 2, location.Y)
76         assertEqual(t, 1, location.X)
77
78         // no errors this time as cursorPosition is not nil
79         assertTrue(t, err == nil)
80 }
81
82 func TestGetBufferCursorLocationNoErrorWhenOverflowWithStartpos(t *testing.T) {
83         text := "this is my\nbuffer\nfile\nhello"
84
85         buf := NewBufferFromString(text, "")
86
87         *flagStartPos = "50,50"
88
89         location, err := GetBufferCursorLocation(nil, buf)
90         // expect to have the flag positions, not the cursor position
91         assertEqual(t, buf.NumLines-1, location.Y)
92         assertEqual(t, 5, location.X)
93
94         // error is expected as cursorPosition is nil
95         assertTrue(t, err != nil)
96 }
97 func TestGetBufferCursorLocationNoErrorWhenOverflowWithCursorPosition(t *testing.T) {
98         text := "this is my\nbuffer\nfile\nhello"
99         cursorPosition := []string{"50", "2"}
100
101         *flagStartPos = ""
102
103         buf := NewBufferFromString(text, "")
104
105         location, err := GetBufferCursorLocation(cursorPosition, buf)
106         // expect to have the flag positions, not the cursor position
107         assertEqual(t, buf.NumLines-1, location.Y)
108         assertEqual(t, 2, location.X)
109
110         // error is expected as cursorPosition is nil
111         assertTrue(t, err == nil)
112 }
113
114 //func TestGetBufferCursorLocationColonArgs(t *testing.T) {
115 //      buf := new(Buffer)
116
117 //}