1package parser
2
3import (
4 "testing"
5
6 "github.com/stretchr/testify/assert"
7)
8
9func TestTitleParser(t *testing.T) {
10 type args struct {
11 diff string
12 }
13 type want struct {
14 title string
15 }
16 tests := []struct {
17 name string
18 args args
19 want want
20 }{
21 {
22 name: "simple addition (html)",
23 args: args{
24 diff: `<p>changed title from <code class="idiff">simple title</code> to <code class="idiff">simple title<span class="idiff left addition"> addition</span></code></p>`,
25 },
26 want: want{
27 title: "simple title addition",
28 },
29 },
30 {
31 name: "simple addition (markdown)",
32 args: args{
33 diff: `changed title from **simple title** to **simple title{+ addition+}**`,
34 },
35 want: want{
36 title: "simple title addition",
37 },
38 },
39 {
40 name: "simple deletion (html)",
41 args: args{
42 diff: `<p>changed title from <code class="idiff">simple<span class="idiff left right deletion"> deleted</span> title</code> to <code class="idiff">simple title</code></p>`,
43 },
44 want: want{
45 title: "simple title",
46 },
47 },
48 {
49 name: "simple deletion (markdown)",
50 args: args{
51 diff: `changed title from **simple{- deleted-} title** to **simple title**`,
52 },
53 want: want{
54 title: "simple title",
55 },
56 },
57 {
58 name: "tail replacement (html)",
59 args: args{
60 diff: `<p>changed title from <code class="idiff">tail <span class="idiff left right deletion">title</span></code> to <code class="idiff">tail <span class="idiff left addition">replacement</span></code></p>`,
61 },
62 want: want{
63 title: "tail replacement",
64 },
65 },
66 {
67 name: "tail replacement (markdown)",
68 args: args{
69 diff: `changed title from **tail {-title-}** to **tail {+replacement+}**`,
70 },
71 want: want{
72 title: "tail replacement",
73 },
74 },
75 {
76 name: "head replacement (html)",
77 args: args{
78 diff: `<p>changed title from <code class="idiff"><span class="idiff left right deletion">title</span> replacement</code> to <code class="idiff"><span class="idiff left addition">head</span> replacement</code></p>`,
79 },
80 want: want{
81 title: "head replacement",
82 },
83 },
84 {
85 name: "head replacement (markdown)",
86 args: args{
87 diff: `changed title from **{-title-} replacement** to **{+head+} replacement**`,
88 },
89 want: want{
90 title: "head replacement",
91 },
92 },
93 {
94 name: "complex multi-section diff (html)",
95 args: args{
96 diff: `<p>changed title from <code class="idiff">this <span class="idiff left right deletion">is</span> an <span class="idiff left right deletion">issue</span></code> to <code class="idiff">this <span class="idiff left addition">may be</span> an <span class="idiff left right addition">amazing bug</span></code></p>`,
97 },
98 want: want{
99 title: "this may be an amazing bug",
100 },
101 },
102 {
103 name: "complex multi-section diff (markdown)",
104 args: args{
105 diff: `changed title from **this {-is-} an {-issue-}** to **this {+may be+} an {+amazing bug+}**`,
106 },
107 want: want{
108 title: "this may be an amazing bug",
109 },
110 },
111 }
112
113 for _, tt := range tests {
114 t.Run(tt.name, func(t *testing.T) {
115 title, err := NewWithInput(TitleParser, tt.args.diff).Parse()
116 if err != nil {
117 t.Error(err)
118 }
119 assert.Equal(t, tt.want.title, title)
120 })
121 }
122}