aboutsummaryrefslogtreecommitdiffstats
path: root/bridge/gitlab/event_test.go
blob: 860570d1f09b464129bf3cd23d95ead23c80cc71 (plain) (blame)
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
84
85
86
87
88
89
90
91
92
93
94
95
package gitlab

import (
	"testing"
	"time"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

func TestGetNewTitle(t *testing.T) {
	type args struct {
		diff string
	}
	type want struct {
		title string
	}
	tests := []struct {
		name string
		args args
		want want
	}{
		{
			name: "addition diff",
			args: args{
				diff: "**first issue** to **first issue{+ edited+}**",
			},
			want: want{
				title: "first issue edited",
			},
		},
		{
			name: "deletion diff",
			args: args{
				diff: "**first issue{- edited-}** to **first issue**",
			},
			want: want{
				title: "first issue",
			},
		},
		{
			name: "mixed diff",
			args: args{
				diff: "**first {-issue-}** to **first {+bug+}**",
			},
			want: want{
				title: "first bug",
			},
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			title := getNewTitle(tt.args.diff)
			assert.Equal(t, tt.want.title, title)
		})
	}
}

var _ Event = mockEvent(0)

type mockEvent int64

func (m mockEvent) ID() string           { panic("implement me") }
func (m mockEvent) UserID() int          { panic("implement me") }
func (m mockEvent) Kind() EventKind      { panic("implement me") }
func (m mockEvent) CreatedAt() time.Time { return time.Unix(int64(m), 0) }

func TestSortedEvents(t *testing.T) {
	makeInput := func(times ...int64) chan Event {
		out := make(chan Event)
		go func() {
			for _, t := range times {
				out <- mockEvent(t)
			}
			close(out)
		}()
		return out
	}

	sorted := SortedEvents(
		makeInput(),
		makeInput(1, 7, 9, 19),
		makeInput(2, 8, 23),
		makeInput(35, 48, 59, 64, 721),
	)

	var previous Event
	for event := range sorted {
		if previous != nil {
			require.True(t, previous.CreatedAt().Before(event.CreatedAt()))
		}
		previous = event
	}
}