| 41 | } |
| 42 | |
| 43 | func TestParseTruncateFunction(t *testing.T) { |
| 44 | source := "tupx5xzf6hvsrhnruz5cr8gwp" |
| 45 | |
| 46 | testCases := []struct { |
| 47 | template string |
| 48 | expected string |
| 49 | }{ |
| 50 | { |
| 51 | template: `{{truncate . 5}}`, |
| 52 | expected: "tupx5", |
| 53 | }, |
| 54 | { |
| 55 | template: `{{truncate . 25}}`, |
| 56 | expected: "tupx5xzf6hvsrhnruz5cr8gwp", |
| 57 | }, |
| 58 | { |
| 59 | template: `{{truncate . 30}}`, |
| 60 | expected: "tupx5xzf6hvsrhnruz5cr8gwp", |
| 61 | }, |
| 62 | { |
| 63 | template: `{{pad . 3 3}}`, |
| 64 | expected: " tupx5xzf6hvsrhnruz5cr8gwp ", |
| 65 | }, |
| 66 | } |
| 67 | |
| 68 | for _, tc := range testCases { |
| 69 | tm, err := Parse(tc.template) |
| 70 | assert.NilError(t, err) |
| 71 | |
| 72 | t.Run("Non Empty Source Test with template: "+tc.template, func(t *testing.T) { |
| 73 | var b bytes.Buffer |
| 74 | assert.NilError(t, tm.Execute(&b, source)) |
| 75 | assert.Check(t, is.Equal(tc.expected, b.String())) |
| 76 | }) |
| 77 | |
| 78 | t.Run("Empty Source Test with template: "+tc.template, func(t *testing.T) { |
| 79 | var c bytes.Buffer |
| 80 | assert.NilError(t, tm.Execute(&c, "")) |
| 81 | assert.Check(t, is.Equal("", c.String())) |
| 82 | }) |
| 83 | |
| 84 | t.Run("Nil Source Test with template: "+tc.template, func(t *testing.T) { |
| 85 | var c bytes.Buffer |
| 86 | assert.Check(t, tm.Execute(&c, nil) != nil) |
| 87 | assert.Check(t, is.Equal("", c.String())) |
| 88 | }) |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | func TestHeaderFunctions(t *testing.T) { |
| 93 | const source = "hello world" |