File: prompt_test.go

package info (click to toggle)
golang-github-juju-cmd 3.0.14-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 424 kB
  • sloc: makefile: 7
file content (136 lines) | stat: -rw-r--r-- 3,254 bytes parent folder | download | duplicates (2)
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// Copyright 2017 Canonical Ltd.
// Licensed under the LGPLv3, see LICENSE file for details.

package cmdtesting_test

import (
	"fmt"
	"io"
	"strconv"
	"strings"

	"github.com/juju/errors"
	"github.com/juju/testing"
	jc "github.com/juju/testing/checkers"
	gc "gopkg.in/check.v1"

	"github.com/juju/cmd/v3/cmdtesting"
)

type prompterSuite struct {
	testing.IsolationSuite
}

var _ = gc.Suite(&prompterSuite{})

func (*prompterSuite) TestPrompter(c *gc.C) {
	noPrompt := func(p string) (string, error) {
		c.Fatalf("unpexected prompt (text %q)", p)
		panic("unreachable")
	}
	promptFn := noPrompt
	p := cmdtesting.NewPrompter(func(p string) (string, error) {
		return promptFn(p)
	})

	promptText := "hello: "
	promptReply := "reply\n"

	fmt.Fprint(p, promptText)
	promptFn = func(p string) (string, error) {
		c.Assert(p, gc.Equals, promptText)
		return promptReply, nil
	}
	c.Assert(readStr(c, p, 20), gc.Equals, promptReply)

	promptText = "some text\ngoodbye: "
	promptReply = "again\n"
	fmt.Fprint(p, promptText[0:10])
	fmt.Fprint(p, promptText[10:])

	c.Assert(readStr(c, p, 3), gc.Equals, promptReply[0:3])
	c.Assert(readStr(c, p, 20), gc.Equals, promptReply[3:])

	fmt.Fprint(p, "final text\n")

	c.Assert(p.Tail(), gc.Equals, "final text\n")
	c.Assert(p.HasUnread(), gc.Equals, false)
}

func (*prompterSuite) TestUnreadInput(c *gc.C) {
	p := cmdtesting.NewPrompter(func(s string) (string, error) {
		return "hello world", nil
	})
	c.Assert(readStr(c, p, 3), gc.Equals, "hel")

	c.Assert(p.HasUnread(), gc.Equals, true)
}

func (*prompterSuite) TestError(c *gc.C) {
	expectErr := errors.New("something")
	p := cmdtesting.NewPrompter(func(s string) (string, error) {
		return "", expectErr
	})
	buf := make([]byte, 3)
	n, err := p.Read(buf)
	c.Assert(n, gc.Equals, 0)
	c.Assert(err, gc.Equals, expectErr)
}

func (*prompterSuite) TestSeqPrompter(c *gc.C) {
	p := cmdtesting.NewSeqPrompter(c, "»", `
hello: »reply
some text
goodbye: »again
final
`[1:])
	fmt.Fprint(p, "hello: ")
	c.Assert(readStr(c, p, 1), gc.Equals, "r")
	c.Assert(readStr(c, p, 20), gc.Equals, "eply\n")
	fmt.Fprint(p, "some text\n")
	fmt.Fprint(p, "goodbye: ")
	c.Assert(readStr(c, p, 20), gc.Equals, "again\n")
	fmt.Fprint(p, "final\n")
	p.AssertDone()
}

func (*prompterSuite) TestSeqPrompterEOF(c *gc.C) {
	p := cmdtesting.NewSeqPrompter(c, "»", `
hello: »»
final
`[1:])
	fmt.Fprint(p, "hello: ")
	n, err := p.Read(make([]byte, 10))
	c.Assert(n, gc.Equals, 0)
	c.Assert(err, gc.Equals, io.EOF)
	fmt.Fprint(p, "final\n")
	p.AssertDone()
}

func (*prompterSuite) TestNewIOChecker(c *gc.C) {
	checker := cmdtesting.NewSeqPrompter(c, "»", `What is your name: »Bob
»more
And your age: »148
You're .* old, Bob
more!
`)
	fmt.Fprintf(checker, "What is your name: ")
	buf := make([]byte, 100)
	n, _ := checker.Read(buf)
	name := strings.TrimSpace(string(buf[0:n]))
	fmt.Fprintf(checker, "And your age: ")
	n, _ = checker.Read(buf)
	age, err := strconv.Atoi(strings.TrimSpace(string(buf[0:n])))
	c.Assert(err, gc.IsNil)
	if age > 90 {
		fmt.Fprintf(checker, "You're very old, %s!\n", name)
	}
	checker.CheckDone()
}

func readStr(c *gc.C, r io.Reader, nb int) string {
	buf := make([]byte, nb)
	n, err := r.Read(buf)
	c.Assert(err, jc.ErrorIsNil)
	return string(buf[0:n])
}