|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "io" |
| 6 | + "math" |
| 7 | + |
| 8 | + "github.com/cucumber/godog" |
| 9 | +) |
| 10 | + |
| 11 | +const ( |
| 12 | + passedEmoji = "✅" |
| 13 | + skippedEmoji = "➖" |
| 14 | + failedEmoji = "❌" |
| 15 | + undefinedEmoji = "❓" |
| 16 | + pendingEmoji = "🚧" |
| 17 | +) |
| 18 | + |
| 19 | +func init() { |
| 20 | + godog.Format("emoji", "Progress formatter with emojis", emojiFormatterFunc) |
| 21 | +} |
| 22 | + |
| 23 | +func emojiFormatterFunc(suite string, out io.Writer) godog.Formatter { |
| 24 | + return newEmojiFmt(suite, out) |
| 25 | +} |
| 26 | + |
| 27 | +func newEmojiFmt(suite string, out io.Writer) *emojiFmt { |
| 28 | + return &emojiFmt{ |
| 29 | + ProgressFmt: godog.NewProgressFmt(suite, out), |
| 30 | + out: out, |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +type emojiFmt struct { |
| 35 | + *godog.ProgressFmt |
| 36 | + |
| 37 | + out io.Writer |
| 38 | +} |
| 39 | + |
| 40 | +func (f *emojiFmt) TestRunStarted() {} |
| 41 | + |
| 42 | +func (f *emojiFmt) Passed(scenario *godog.Scenario, step *godog.Step, match *godog.StepDefinition) { |
| 43 | + f.ProgressFmt.Base.Passed(scenario, step, match) |
| 44 | + |
| 45 | + f.ProgressFmt.Base.Lock.Lock() |
| 46 | + defer f.ProgressFmt.Base.Lock.Unlock() |
| 47 | + |
| 48 | + f.step(step.Id) |
| 49 | +} |
| 50 | + |
| 51 | +func (f *emojiFmt) Skipped(scenario *godog.Scenario, step *godog.Step, match *godog.StepDefinition) { |
| 52 | + f.ProgressFmt.Base.Skipped(scenario, step, match) |
| 53 | + |
| 54 | + f.ProgressFmt.Base.Lock.Lock() |
| 55 | + defer f.ProgressFmt.Base.Lock.Unlock() |
| 56 | + |
| 57 | + f.step(step.Id) |
| 58 | +} |
| 59 | + |
| 60 | +func (f *emojiFmt) Undefined(scenario *godog.Scenario, step *godog.Step, match *godog.StepDefinition) { |
| 61 | + f.ProgressFmt.Base.Undefined(scenario, step, match) |
| 62 | + |
| 63 | + f.ProgressFmt.Base.Lock.Lock() |
| 64 | + defer f.ProgressFmt.Base.Lock.Unlock() |
| 65 | + |
| 66 | + f.step(step.Id) |
| 67 | +} |
| 68 | + |
| 69 | +func (f *emojiFmt) Failed(scenario *godog.Scenario, step *godog.Step, match *godog.StepDefinition, err error) { |
| 70 | + f.ProgressFmt.Base.Failed(scenario, step, match, err) |
| 71 | + |
| 72 | + f.ProgressFmt.Base.Lock.Lock() |
| 73 | + defer f.ProgressFmt.Base.Lock.Unlock() |
| 74 | + |
| 75 | + f.step(step.Id) |
| 76 | +} |
| 77 | + |
| 78 | +func (f *emojiFmt) Pending(scenario *godog.Scenario, step *godog.Step, match *godog.StepDefinition) { |
| 79 | + f.ProgressFmt.Base.Pending(scenario, step, match) |
| 80 | + |
| 81 | + f.ProgressFmt.Base.Lock.Lock() |
| 82 | + defer f.ProgressFmt.Base.Lock.Unlock() |
| 83 | + |
| 84 | + f.step(step.Id) |
| 85 | +} |
| 86 | + |
| 87 | +func (f *emojiFmt) Summary() { |
| 88 | + f.printSummaryLegend() |
| 89 | + f.ProgressFmt.Summary() |
| 90 | +} |
| 91 | + |
| 92 | +func (f *emojiFmt) printSummaryLegend() { |
| 93 | + fmt.Fprint(f.out, "\n\nOutput Legend:\n") |
| 94 | + fmt.Fprint(f.out, fmt.Sprintf("\t%s Passed\n", passedEmoji)) |
| 95 | + fmt.Fprint(f.out, fmt.Sprintf("\t%s Failed\n", failedEmoji)) |
| 96 | + fmt.Fprint(f.out, fmt.Sprintf("\t%s Skipped\n", skippedEmoji)) |
| 97 | + fmt.Fprint(f.out, fmt.Sprintf("\t%s Undefined\n", undefinedEmoji)) |
| 98 | + fmt.Fprint(f.out, fmt.Sprintf("\t%s Pending\n", pendingEmoji)) |
| 99 | +} |
| 100 | + |
| 101 | +func (f *emojiFmt) step(pickleStepID string) { |
| 102 | + pickleStepResult := f.Storage.MustGetPickleStepResult(pickleStepID) |
| 103 | + |
| 104 | + switch pickleStepResult.Status { |
| 105 | + case godog.StepPassed: |
| 106 | + fmt.Fprint(f.out, fmt.Sprintf(" %s", passedEmoji)) |
| 107 | + case godog.StepSkipped: |
| 108 | + fmt.Fprint(f.out, fmt.Sprintf(" %s", skippedEmoji)) |
| 109 | + case godog.StepFailed: |
| 110 | + fmt.Fprint(f.out, fmt.Sprintf(" %s", failedEmoji)) |
| 111 | + case godog.StepUndefined: |
| 112 | + fmt.Fprint(f.out, fmt.Sprintf(" %s", undefinedEmoji)) |
| 113 | + case godog.StepPending: |
| 114 | + fmt.Fprint(f.out, fmt.Sprintf(" %s", pendingEmoji)) |
| 115 | + } |
| 116 | + |
| 117 | + *f.Steps++ |
| 118 | + |
| 119 | + if math.Mod(float64(*f.Steps), float64(f.StepsPerRow)) == 0 { |
| 120 | + fmt.Fprintf(f.out, " %d\n", *f.Steps) |
| 121 | + } |
| 122 | +} |
0 commit comments