diff --git a/assertions.go b/assertions.go index 192419d..e9d3117 100644 --- a/assertions.go +++ b/assertions.go @@ -21,6 +21,8 @@ type tError struct { err error } +func (t *tError) Helper() {} + func (t *tError) Errorf(format string, args ...interface{}) { t.err = fmt.Errorf(format, args...) // nolint: goerr113 } @@ -44,6 +46,17 @@ func AssertState(t assert.TestingT, state *vt10x.State, expected string) bool { return assert.Equal(t, expected, actual) } +// AssertStateRegex asserts console state. +func AssertStateRegex(t assert.TestingT, state *vt10x.State, expected string) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + + actual := trimTailingSpaces(expect.StripTrailingEmptyLines(state.String())) + + return assert.Regexp(t, expected, actual) +} + func trimTailingSpaces(out string) string { lines := strings.Split(out, "\n") diff --git a/features/console.feature b/features/console.feature index 5da8ea2..e1ef875 100644 --- a/features/console.feature +++ b/features/console.feature @@ -1,6 +1,6 @@ Feature: Console - Scenario: Captured Output + Scenario: Text When I write to console: """ Hello World! @@ -14,3 +14,45 @@ Feature: Console I'm John """ + + Scenario: JSON + When I write to console: + """ + [ + { + "username": "user@example.org", + "password": "123456" + } + ] + """ + + Then console output is: + """ + [ + { + "username": "user@example.org", + "password": "123456" + } + ] + """ + + Scenario: Regex + When I write to console: + """ + [ + { + "username": "user@example.org", + "password": "123456" + } + ] + """ + + Then console output matches: + """ + \[ + { + "username": "user@example.org", + "password": "[0-9]+" + } + \] + """ diff --git a/manager.go b/manager.go index 8ae7553..2c7456e 100644 --- a/manager.go +++ b/manager.go @@ -53,6 +53,7 @@ func (m *Manager) RegisterContext(ctx *godog.ScenarioContext) { }) ctx.Step(`console output is:`, m.isConsoleOutput) + ctx.Step(`console output matches:`, m.matchConsoleOutput) } func (m *Manager) session() *session { @@ -129,6 +130,15 @@ func (m *Manager) isConsoleOutput(expected *godog.DocString) error { return t.LastError() } +func (m *Manager) matchConsoleOutput(expected *godog.DocString) error { + m.Flush() + + t := teeError() + AssertStateRegex(t, m.session().state, expected.Content) + + return t.LastError() +} + // WithStarter adds a Starter to Manager. func (m *Manager) WithStarter(s Starter) *Manager { m.starters = append(m.starters, s)