Skip to content

Commit

Permalink
Merge branch 'main' into dwedul/update-nhash-per-mil
Browse files Browse the repository at this point in the history
  • Loading branch information
SpicyLemon authored Feb 9, 2024
2 parents c1492e6 + 017bc7b commit 4ee095a
Show file tree
Hide file tree
Showing 15 changed files with 4,621 additions and 210 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
### Improvements

* Add new force_transfer access that is required for an account to do a forced transfer ([#1829](https://github.com/provenance-io/provenance/issues/1829)).
* Add exchange commitment stuff to CLI [PR 1830](https://github.com/provenance-io/provenance/pull/1830).
* Update the MsgFees Params to set the nhash per usd-mil to 40,000,000 ($0.025/hash) [#1833](https://github.com/provenance-io/provenance/pull/1833).

### API Breaking
Expand Down
87 changes: 87 additions & 0 deletions testutil/assertions/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package assertions

import (
"fmt"
"strings"

"github.com/stretchr/testify/assert"

Expand Down Expand Up @@ -35,6 +36,15 @@ func EventToStrings(event sdk.Event) []string {
return PrependToEach(event.Type, AttrsToStrings(event.Attributes))
}

// EventToString converts a single event to a one-line string.
func EventToString(event sdk.Event) string {
attrs := "(no attributes)"
if len(event.Attributes) > 0 {
attrs = strings.Join(AttrsToStrings(event.Attributes), ", ")
}
return fmt.Sprintf("%s: %s", event.Type, attrs)
}

// AttrsToStrings creates and returns a string for each attribute.
func AttrsToStrings(attrs []abci.EventAttribute) []string {
if len(attrs) == 0 {
Expand Down Expand Up @@ -83,3 +93,80 @@ func AssertEqualEventsf(t TB, expected, actual sdk.Events, msg string, args ...i
func RequireEqualEventsf(t TB, expected, actual sdk.Events, msg string, args ...interface{}) {
RequireEqualEvents(t, expected, actual, append([]interface{}{msg}, args...)...)
}

// AssertEventsContains asserts that each of the provided expected events is contained in the provided actual events.
//
// Returns success (true = they're all there, false = one or more is missing).
func AssertEventsContains(t TB, expected, actual sdk.Events, msgAndArgs ...interface{}) bool {
t.Helper()
if len(expected) == 0 {
return true
}

actStrs := make([]string, len(actual))
for i, event := range actual {
actStrs[i] = EventToString(event)
}

var notFound sdk.Events
for _, expEvent := range expected {
exp := EventToString(expEvent)
var found bool
for _, act := range actStrs {
if exp == act {
found = true
break
}
}
if !found {
notFound = append(notFound, expEvent)
}
}

if len(notFound) == 0 {
return true
}

var failureMsg strings.Builder
failureMsg.WriteString(fmt.Sprintf("%d (of %d) expected events missing from %d actual events", len(notFound), len(expected), len(actual)))
failureMsg.WriteString("\nActual:")
switch {
case actual == nil:
failureMsg.WriteString(" <nil>")
case len(actual) == 0:
failureMsg.WriteString(" sdk.Events{}")
default:
failureMsg.WriteByte('\n')
failureMsg.WriteString(strings.Join(PrependToEach("\n\t", EventsToStrings(actual)), ""))
}
failureMsg.WriteString("\nMissing:")
for i, event := range notFound {
for _, line := range EventToStrings(event) {
failureMsg.WriteString(fmt.Sprintf("\n\t%d: %s", i, line))
}
}
return assert.Fail(t, failureMsg.String(), msgAndArgs...)
}

// RequireEventsContains asserts that each of the provided expected events is contained in the provided actual events.
//
// Returns if they're all there, halts tests if not.
func RequireEventsContains(t TB, expected, actual sdk.Events, msgAndArgs ...interface{}) {
if !AssertEventsContains(t, expected, actual, msgAndArgs...) {
t.FailNow()
}
}

// AssertEventsContainsf asserts that each of the provided expected events is contained in the provided actual events.
//
// Returns success (true = they're all there, false = one or more is missing).
func AssertEventsContainsf(t TB, expected, actual sdk.Events, msg string, args ...interface{}) bool {
return AssertEventsContains(t, expected, actual, append([]interface{}{msg}, args...)...)
}

// RequireEventsContainsf asserts that each of the provided expected events is contained in the provided actual events.
//
// Returns if they're all there, halts tests if not.
func RequireEventsContainsf(t TB, expected, actual sdk.Events, msg string, args ...interface{}) {
RequireEventsContains(t, expected, actual, append([]interface{}{msg}, args...)...)
}
Loading

0 comments on commit 4ee095a

Please sign in to comment.