1package gitlab
 2
 3import (
 4	"testing"
 5	"time"
 6
 7	"github.com/stretchr/testify/assert"
 8	"github.com/stretchr/testify/require"
 9)
10
11func TestGetNewTitle(t *testing.T) {
12	type args struct {
13		diff string
14	}
15	type want struct {
16		title string
17	}
18	tests := []struct {
19		name string
20		args args
21		want want
22	}{
23		{
24			name: "addition diff",
25			args: args{
26				diff: "**first issue** to **first issue{+ edited+}**",
27			},
28			want: want{
29				title: "first issue edited",
30			},
31		},
32		{
33			name: "deletion diff",
34			args: args{
35				diff: "**first issue{- edited-}** to **first issue**",
36			},
37			want: want{
38				title: "first issue",
39			},
40		},
41		{
42			name: "mixed diff",
43			args: args{
44				diff: "**first {-issue-}** to **first {+bug+}**",
45			},
46			want: want{
47				title: "first bug",
48			},
49		},
50	}
51
52	for _, tt := range tests {
53		t.Run(tt.name, func(t *testing.T) {
54			title := getNewTitle(tt.args.diff)
55			assert.Equal(t, tt.want.title, title)
56		})
57	}
58}
59
60var _ Event = mockEvent(0)
61
62type mockEvent int64
63
64func (m mockEvent) ID() string           { panic("implement me") }
65func (m mockEvent) UserID() int          { panic("implement me") }
66func (m mockEvent) Kind() EventKind      { panic("implement me") }
67func (m mockEvent) CreatedAt() time.Time { return time.Unix(int64(m), 0) }
68
69func TestSortedEvents(t *testing.T) {
70	makeInput := func(times ...int64) chan Event {
71		out := make(chan Event)
72		go func() {
73			for _, t := range times {
74				out <- mockEvent(t)
75			}
76			close(out)
77		}()
78		return out
79	}
80
81	sorted := SortedEvents(
82		makeInput(),
83		makeInput(1, 7, 9, 19),
84		makeInput(2, 8, 23),
85		makeInput(35, 48, 59, 64, 721),
86	)
87
88	var previous Event
89	for event := range sorted {
90		if previous != nil {
91			require.True(t, previous.CreatedAt().Before(event.CreatedAt()))
92		}
93		previous = event
94	}
95}