|
1 | 1 | package cmd |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "io/ioutil" |
4 | 5 | "os/exec" |
5 | 6 | "testing" |
6 | 7 |
|
| 8 | + "github.com/stretchr/testify/assert" |
7 | 9 | "github.com/stretchr/testify/require" |
8 | 10 | ) |
9 | 11 |
|
@@ -66,3 +68,64 @@ func Test_snippetCreate_Global(t *testing.T) { |
66 | 68 |
|
67 | 69 | require.Contains(t, string(b), "https://gitlab.com/snippets/") |
68 | 70 | } |
| 71 | + |
| 72 | +func Test_snipMsg(t *testing.T) { |
| 73 | + title, desc, err := snipMsg(nil, "snip title\nthis should be dropped") |
| 74 | + if err != nil { |
| 75 | + t.Fatal(err) |
| 76 | + } |
| 77 | + // This title was defaulted from the snippet contents/code because no |
| 78 | + // msgs -m title was provided |
| 79 | + assert.Equal(t, "snip title", title) |
| 80 | + // This is the body created in during editing or with provided msgs -m |
| 81 | + assert.Equal(t, "", desc) |
| 82 | +} |
| 83 | + |
| 84 | +func Test_snipCode(t *testing.T) { |
| 85 | + err := ioutil.WriteFile("./testfile", []byte("test file contents"), 0644) |
| 86 | + if err != nil { |
| 87 | + t.Fatal(err) |
| 88 | + } |
| 89 | + |
| 90 | + tests := []struct { |
| 91 | + Name string |
| 92 | + Path string |
| 93 | + ExpectedCode string |
| 94 | + }{ |
| 95 | + { |
| 96 | + Name: "From File", |
| 97 | + Path: "./testfile", |
| 98 | + ExpectedCode: "test file contents", |
| 99 | + }, |
| 100 | + { |
| 101 | + Name: "From Editor", |
| 102 | + Path: "", |
| 103 | + ExpectedCode: "\n\n", |
| 104 | + }, |
| 105 | + } |
| 106 | + for _, test := range tests { |
| 107 | + t.Run(test.Name, func(t *testing.T) { |
| 108 | + test := test |
| 109 | + t.Parallel() |
| 110 | + code, err := snipCode(test.Path) |
| 111 | + if err != nil { |
| 112 | + t.Fatal(err) |
| 113 | + } |
| 114 | + require.Equal(t, test.ExpectedCode, code) |
| 115 | + }) |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +func Test_snipText(t *testing.T) { |
| 120 | + var tmpl = "foo" + ` |
| 121 | +{{.CommentChar}} In this mode you are writing a snippet from scratch |
| 122 | +{{.CommentChar}} The first block is the title and the rest is the contents.` |
| 123 | + text, err := snipText(tmpl) |
| 124 | + if err != nil { |
| 125 | + t.Fatal(err) |
| 126 | + } |
| 127 | + require.Equal(t, `foo |
| 128 | +# In this mode you are writing a snippet from scratch |
| 129 | +# The first block is the title and the rest is the contents.`, text) |
| 130 | + |
| 131 | +} |
0 commit comments