File: readline_test.go

package info (click to toggle)
golang-github-ergochat-readline 0.1.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 336 kB
  • sloc: makefile: 17
file content (68 lines) | stat: -rw-r--r-- 1,233 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package readline

import (
	"testing"
	"time"
)

func TestRace(t *testing.T) {
	rl, err := NewFromConfig(&Config{})
	if err != nil {
		t.Fatal(err)
		return
	}

	go func() {
		for range time.Tick(time.Millisecond) {
			rl.SetPrompt("hello")
		}
	}()

	go func() {
		time.Sleep(100 * time.Millisecond)
		rl.Close()
	}()

	rl.Readline()
}

func TestParseCPRResponse(t *testing.T) {
	badResponses := []string{
		"",
		";",
		"\x00",
		"\x00;",
		";\x00",
		"x",
		"1;a",
		"a;1",
		"a;1;",
		"1;1;",
		"1;1;1",
	}
	for _, response := range badResponses {
		if _, err := parseCPRResponse([]byte(response)); err == nil {
			t.Fatalf("expected parsing of `%s` to fail, but did not", response)
		}
	}

	goodResponses := []struct {
		input  string
		output cursorPosition
	}{
		{"1;2", cursorPosition{1, 2}},
		{"0;2", cursorPosition{0, 2}},
		{"0;0", cursorPosition{0, 0}},
		{"48378;9999999", cursorPosition{48378, 9999999}},
	}

	for _, response := range goodResponses {
		got, err := parseCPRResponse([]byte(response.input))
		if err != nil {
			t.Fatalf("could not parse `%s`: %v", response.input, err)
		}
		if got != response.output {
			t.Fatalf("expected %s to parse to %#v, got %#v", response.input, response.output, got)
		}
	}
}