Skip to content
This repository has been archived by the owner on Aug 29, 2022. It is now read-only.

Commit

Permalink
Support regex
Browse files Browse the repository at this point in the history
  • Loading branch information
nhatthm committed Apr 19, 2021
1 parent eb44ec7 commit 43aabb7
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 1 deletion.
13 changes: 13 additions & 0 deletions assertions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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")

Expand Down
44 changes: 43 additions & 1 deletion features/console.feature
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Feature: Console

Scenario: Captured Output
Scenario: Text
When I write to console:
"""
Hello World!
Expand All @@ -14,3 +14,45 @@ Feature: Console
I'm John
"""

Scenario: JSON
When I write to console:
"""
[
{
"username": "[email protected]",
"password": "123456"
}
]
"""

Then console output is:
"""
[
{
"username": "[email protected]",
"password": "123456"
}
]
"""

Scenario: Regex
When I write to console:
"""
[
{
"username": "[email protected]",
"password": "123456"
}
]
"""

Then console output matches:
"""
\[
{
"username": "[email protected]",
"password": "[0-9]+"
}
\]
"""
10 changes: 10 additions & 0 deletions manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 43aabb7

Please sign in to comment.