Exemplo n.º 1
0
func (s *formSuite) TestIOPrompter(c *gc.C) {
	for i, test := range ioPrompterTests {
		func() {
			c.Logf("%d. %s", i, test.about)
			for k, v := range test.environment {
				defer testing.PatchEnvironment(k, v)()
			}
			outBuf := new(bytes.Buffer)
			prompter := form.IOPrompter{
				In:  strings.NewReader(test.input),
				Out: outBuf,
			}
			if test.title != "" {
				err := prompter.ShowTitle(test.title)
				c.Assert(err, gc.IsNil)
			}
			for j, p := range test.prompts {
				result, err := prompter.Prompt(p.name, p.attr)
				if err != nil {
					c.Assert(err, gc.ErrorMatches, test.expectError)
					c.Assert(outBuf.String(), gc.Equals, test.expectOutput)
					return
				}
				c.Assert(result, gc.Equals, test.expect[j])
			}
			c.Assert(test.expectError, gc.Equals, "", gc.Commentf("did not reveive expected error"))
			c.Assert(outBuf.String(), gc.Equals, test.expectOutput)
		}()
	}
}
Exemplo n.º 2
0
func (s *formSuite) TestIOPrompterWriteErrors(c *gc.C) {
	prompter := form.IOPrompter{
		In:  nil,
		Out: errorWriter{},
	}
	err := prompter.ShowTitle("title")
	c.Assert(err, gc.ErrorMatches, "cannot show title: test")
	_, err = prompter.Prompt("attr", environschema.Attr{
		Description: "A",
	})
	c.Assert(err, gc.ErrorMatches, "cannot write prompt: test")
}