forked from awsdocs/aws-doc-sdk-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scenarios.go
83 lines (74 loc) · 2.37 KB
/
scenarios.go
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
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package testtools
import (
"bytes"
"errors"
"fmt"
"log"
"os"
"strings"
"testing"
)
// IScenarioTest defines callback functions that an individual scenario test must
// implement.
//
// SetupDataAndStubs sets up test data specific to the test and returns a list of stubs
// that specify the expected calls that are made during a scenario run.
//
// RunSubTest runs the actual code under test.
type IScenarioTest interface {
SetupDataAndStubs() []Stub
RunSubTest(stubber *AwsmStubber)
Cleanup()
}
// RunScenarioTests runs the scenario multiple times. The first time, it
// runs with no errors. In subsequent runs, it specifies that each stub in the sequence
// should raise an error, and it verifies the results.
func RunScenarioTests(scenarioTest IScenarioTest, t *testing.T) {
done := false
stubIndex := -1
for !done {
stubs := scenarioTest.SetupDataAndStubs()
if stubIndex == -1 {
t.Run("NoErrors", func(t *testing.T) { SubTestRunScenario(scenarioTest, stubs, nil, t) })
} else {
stub := &stubs[stubIndex]
if stub.Error == nil && !stub.SkipErrorTest {
errName := fmt.Sprintf("%vError", stub.OperationName)
stub.Error = &StubError{Err: errors.New(errName)}
t.Run(errName, func(t *testing.T) { SubTestRunScenario(scenarioTest, stubs, stub.Error, t) })
}
}
stubIndex++
done = stubIndex == len(stubs)
}
}
// SubTestRunScenario performs a single test run with a set of stubs that are set up to
// run with or without errors.
func SubTestRunScenario(scenarioTest IScenarioTest, stubs []Stub, stubErr *StubError, t *testing.T) {
stubber := NewStubber()
for _, stub := range stubs {
stubber.Add(stub)
}
log.SetFlags(0)
var buf bytes.Buffer
log.SetOutput(&buf)
scenarioTest.RunSubTest(stubber)
log.SetOutput(os.Stderr)
if stubErr == nil {
if !strings.Contains(buf.String(), "Thanks for watching") {
t.Errorf("didn't run to successful completion")
t.Logf("Here's the log: \n%v", buf.String())
} else if strings.Contains(buf.String(), "operation error") {
t.Errorf("got an unexpected error")
t.Logf("Here's the log: \n%v", buf.String())
}
} else {
if !strings.Contains(buf.String(), stubErr.Error()) {
t.Errorf("did not get expected error %v", stubErr)
t.Logf("Here's the log: \n%v", buf.String())
}
}
scenarioTest.Cleanup()
}