diff --git a/examples/gno.land/r/gnoland/events/errors.gno b/examples/gno.land/r/gnoland/events/errors.gno index 788115015a4..fb44d3c9f82 100644 --- a/examples/gno.land/r/gnoland/events/errors.gno +++ b/examples/gno.land/r/gnoland/events/errors.gno @@ -8,8 +8,8 @@ import ( var ( ErrEmptyName = errors.New("event name cannot be empty") ErrNoSuchID = errors.New("event with specified ID does not exist") - ErrWidgetMinAmt = errors.New("you need to request at least 1 event to render") - ErrWidgetMaxAmt = errors.New("maximum number of events in widget is" + strconv.Itoa(MaxWidgetSize)) + ErrMinWidgetSize = errors.New("you need to request at least 1 event to render") + ErrMaxWidgetSize = errors.New("maximum number of events in widget is" + strconv.Itoa(MaxWidgetSize)) ErrDescriptionTooLong = errors.New("event description is too long") ErrInvalidStartTime = errors.New("invalid start time format") ErrInvalidEndTime = errors.New("invalid end time format") diff --git a/examples/gno.land/r/gnoland/events/events_test.gno b/examples/gno.land/r/gnoland/events/events_test.gno index 1e5625e0b2c..953081331f3 100644 --- a/examples/gno.land/r/gnoland/events/events_test.gno +++ b/examples/gno.land/r/gnoland/events/events_test.gno @@ -7,6 +7,7 @@ import ( "time" "gno.land/p/demo/uassert" + "gno.land/p/demo/urequire" ) var ( @@ -159,3 +160,40 @@ func TestParseTimes(t *testing.T) { _, _, err = parseTimes("2009-02-10T23:30:30+02:00", "2009-02-13T21:30:33+05:00") uassert.ErrorContains(t, err, ErrStartEndTimezonemMismatch.Error()) } + +func TestRenderEventWidget(t *testing.T) { + events = nil // remove elements from previous tests - see issue #1982 + + // No events yet + out, err := RenderEventWidget(1) + uassert.NoError(t, err) + uassert.Equal(t, out, "No events.") + + // Too big of a widget + out, err = RenderEventWidget(MaxWidgetSize + 1) + uassert.ErrorIs(t, err, ErrMaxWidgetSize) + + out, err = RenderEventWidget(0) + uassert.ErrorIs(t, err, ErrMinWidgetSize) + + // Ordering & if requested amt is larger than the num of events that exist + e1Start := parsedTimeNow.Add(time.Hour * 24 * 5) + e1End := e1Start.Add(time.Hour * 4) + + e2Start := parsedTimeNow.Add(time.Hour * 24 * 10) // event 2 is after event 1 + e2End := e2Start.Add(time.Hour * 4) + + _, err = AddEvent("Event 1", "description", "gno.land", "loc", e1Start.Format(time.RFC3339), e1End.Format(time.RFC3339)) + urequire.NoError(t, err) + + _, err = AddEvent("Event 2", "description", "gno.land", "loc", e2Start.Format(time.RFC3339), e2End.Format(time.RFC3339)) + urequire.NoError(t, err) + + out, err = RenderEventWidget(MaxWidgetSize) + urequire.NoError(t, err) + + uniqueSequence := "- [" // sequence that is displayed once per each event to test against + uassert.Equal(t, 2, strings.Count(out, uniqueSequence)) + + uassert.True(t, strings.Index(out, "Event 1") > strings.Index(out, "Event 2")) +} diff --git a/examples/gno.land/r/gnoland/events/rendering.gno b/examples/gno.land/r/gnoland/events/rendering.gno index bde32065d27..d98879c68f6 100644 --- a/examples/gno.land/r/gnoland/events/rendering.gno +++ b/examples/gno.land/r/gnoland/events/rendering.gno @@ -19,11 +19,11 @@ func RenderEventWidget(eventsToRender int) (string, error) { } if eventsToRender > MaxWidgetSize { - return "", ErrWidgetMaxAmt + return "", ErrMaxWidgetSize } if eventsToRender < 1 { - return "", ErrWidgetMinAmt + return "", ErrMinWidgetSize } if eventsToRender > numOfEvents { @@ -32,7 +32,7 @@ func RenderEventWidget(eventsToRender int) (string, error) { output := "" - for _, event := range events[eventsToRender:] { + for _, event := range events[:eventsToRender] { output += ufmt.Sprintf("- [%s](%s)\n", event.name, event.link) }