github.com/zaquestion/lab@v0.25.1/cmd/edit_common_test.go (about) 1 package cmd 2 3 import ( 4 "path/filepath" 5 "testing" 6 7 "github.com/stretchr/testify/assert" 8 "github.com/stretchr/testify/require" 9 ) 10 11 func Test_editDescription(t *testing.T) { 12 repo := copyTestRepo(t) 13 14 type fakeGLObj struct { 15 Title string 16 Description string 17 } 18 19 type funcArgs struct { 20 Msgs []string 21 Filename string 22 } 23 24 tests := []struct { 25 Name string 26 GLObj fakeGLObj 27 Args funcArgs 28 ExpectedTitle string 29 ExpectedDescription string 30 }{ 31 { 32 Name: "Using messages", 33 GLObj: fakeGLObj{ 34 Title: "old title", 35 Description: "old body", 36 }, 37 Args: funcArgs{ 38 Msgs: []string{"new title", "new body 1", "new body 2"}, 39 Filename: "", 40 }, 41 ExpectedTitle: "new title", 42 ExpectedDescription: "new body 1\n\nnew body 2", 43 }, 44 { 45 Name: "Using a single message", 46 GLObj: fakeGLObj{ 47 Title: "old title", 48 Description: "old body", 49 }, 50 Args: funcArgs{ 51 Msgs: []string{"new title"}, 52 Filename: "", 53 }, 54 ExpectedTitle: "new title", 55 ExpectedDescription: "old body", 56 }, 57 { 58 Name: "From Editor", 59 GLObj: fakeGLObj{ 60 Title: "old title", 61 Description: "old body", 62 }, 63 Args: funcArgs{ 64 Msgs: nil, 65 Filename: "", 66 }, 67 ExpectedTitle: "old title", 68 ExpectedDescription: "old body", 69 }, 70 { 71 Name: "From file", 72 GLObj: fakeGLObj{ 73 Title: "old title", 74 Description: "old body", 75 }, 76 Args: funcArgs{ 77 Msgs: nil, 78 Filename: filepath.Join(repo, "testedit"), 79 }, 80 ExpectedTitle: "new title", 81 ExpectedDescription: "\nnew body\n", 82 }, 83 } 84 85 for _, test := range tests { 86 t.Run(test.Name, func(t *testing.T) { 87 test := test 88 t.Parallel() 89 title, body, err := editDescription(test.GLObj.Title, 90 test.GLObj.Description, test.Args.Msgs, test.Args.Filename) 91 if err != nil { 92 t.Fatal(err) 93 } 94 95 assert.Equal(t, test.ExpectedTitle, title) 96 assert.Equal(t, test.ExpectedDescription, body) 97 }) 98 } 99 } 100 101 func Test_editText(t *testing.T) { 102 t.Parallel() 103 text, err := editText("old title", "old body") 104 if err != nil { 105 t.Fatal(err) 106 } 107 require.Equal(t, `old title 108 109 old body 110 111 # Edit the title and/or description. The first block of text 112 # is the title and the rest is the description.`, text) 113 114 }