|
1 | 1 | package recurparse
|
2 | 2 |
|
3 | 3 | import (
|
4 |
| - "embed" |
5 | 4 | "html/template"
|
| 5 | + "os" |
6 | 6 | "strings"
|
7 | 7 | "testing"
|
| 8 | + "testing/fstest" |
8 | 9 | )
|
9 | 10 |
|
10 |
| -//go:embed testdata/simple/*.txt |
11 |
| -var simple embed.FS |
| 11 | +func ExampleHTMLParseFS() { |
| 12 | + fsTest := fstest.MapFS{ |
| 13 | + "data.txt": &fstest.MapFile{ |
| 14 | + Data: []byte("super simple {{.Foo}}"), |
| 15 | + }, |
| 16 | + "some/deep/file.txt": &fstest.MapFile{ |
| 17 | + Data: []byte("other simple {{.Foo}}"), |
| 18 | + }, |
| 19 | + } |
12 | 20 |
|
13 |
| -func TestSimpleNil(t *testing.T) { |
14 | 21 | tmpl, err := HTMLParseFS(
|
15 | 22 | nil,
|
16 |
| - simple, |
| 23 | + fsTest, |
17 | 24 | "*.txt",
|
18 | 25 | )
|
19 | 26 | if err != nil {
|
20 | 27 | panic(err)
|
21 | 28 | }
|
22 | 29 |
|
23 |
| - b := &strings.Builder{} |
24 |
| - err = tmpl.ExecuteTemplate(b, "testdata/simple/simple.txt", struct{ Foo string }{Foo: "bar"}) |
| 30 | + err = tmpl.ExecuteTemplate(os.Stdout, "some/deep/file.txt", struct{ Foo string }{Foo: "bar"}) |
25 | 31 | if err != nil {
|
26 | 32 | panic(err)
|
27 | 33 | }
|
28 |
| - if b.String() != "super simple bar" { |
29 |
| - t.Error("not equal super simple bar") |
30 |
| - } |
31 | 34 |
|
| 35 | + // Output: other simple bar |
32 | 36 | }
|
33 | 37 |
|
34 | 38 | func TestSimpleExisting(t *testing.T) {
|
| 39 | + fsTest := fstest.MapFS{ |
| 40 | + "data.txt": &fstest.MapFile{ |
| 41 | + Data: []byte("super simple {{.Foo}}"), |
| 42 | + }, |
| 43 | + "some/deep/file.txt": &fstest.MapFile{ |
| 44 | + Data: []byte("other simple {{.Foo}}"), |
| 45 | + }, |
| 46 | + } |
| 47 | + |
35 | 48 | existing, err := template.New("existing").Parse("existing {{.Foo}}")
|
36 | 49 | if err != nil {
|
37 | 50 | panic(err)
|
38 | 51 | }
|
39 | 52 |
|
40 | 53 | tmpl, err := HTMLParseFS(
|
41 | 54 | existing,
|
42 |
| - simple, |
| 55 | + fsTest, |
43 | 56 | "*.txt",
|
44 | 57 | )
|
45 | 58 | if err != nil {
|
46 | 59 | panic(err)
|
47 | 60 | }
|
48 | 61 |
|
49 | 62 | b := &strings.Builder{}
|
50 |
| - err = tmpl.ExecuteTemplate(b, "testdata/simple/simple.txt", struct{ Foo string }{Foo: "bar"}) |
| 63 | + err = tmpl.ExecuteTemplate(b, "some/deep/file.txt", struct{ Foo string }{Foo: "bar"}) |
51 | 64 | if err != nil {
|
52 | 65 | panic(err)
|
53 | 66 | }
|
54 |
| - if b.String() != "super simple bar" { |
55 |
| - t.Error("not equal super simple bar") |
| 67 | + if b.String() != "other simple bar" { |
| 68 | + t.Error("not equal other simple bar") |
56 | 69 | }
|
57 | 70 |
|
58 | 71 | b = &strings.Builder{}
|
|
0 commit comments