我有以下代码生成一些字符串输出:
package formatter
import (
"bytes"
"log"
"text/template"
"github.com/foo/bar/internal/mapper"
)
// map of template functions that enable us to identify the final item within a
// collection being iterated over.
var fns = template.FuncMap{
"plus1": func(x int) int {
return x + 1
},
}
// Dot renders our results in dot format for use with graphviz
func Dot(results []mapper.Page) string {
dotTmpl := `digraph sitemap { {{range .}}
"{{.URL}}"
-> { {{$n := len .Anchors}}{{range $i, $v := .Anchors}}
"{{.}}"{{if eq (plus1 $i) $n}}{{else}},{{end}}{{end}}
} {{end}}
}`
tmpl, err := template.New("digraph").Funcs(fns).Parse(dotTmpl)
if err != nil {
log.Fatal(err)
}
var output bytes.Buffer
if err := tmpl.Execute(&output, results); err != nil {
log.Fatal(err)
}
return output.String()
}
它生成的输出如下:
digraph sitemap {
"http://www.example.com/"
-> {
"http://www.example.com/foo",
"http://www.example.com/bar",
"http://www.example.com/baz"
}
}
以下是此功能的测试...
package formatter
import (
"testing"
"github.com/foo/bar/internal/mapper"
)
func TestDot(t *testing.T) {
input := []mapper.Page{
mapper.Page{
URL: "http://www.example.com/",
Anchors: []string{
"http://www.example.com/foo",
"http://www.example.com/bar",
"http://www.example.com/baz",
},
Links: []string{
"http://www.example.com/foo.css",
"http://www.example.com/bar.css",
"http://www.example.com/baz.css",
},
Scripts: []string{
"http://www.example.com/foo.js",
"http://www.example.com/bar.js",
"http://www.example.com/baz.js",
},
},
}
output := `digraph sitemap {
"http://www.example.com/"
-> {
"http://www.example.com/foo",
"http://www.example.com/bar",
"http://www.example.com/baz"
}
}`
actual := Dot(input)
if actual != output {
t.Errorf("expected: %s\ngot: %s", output, actual)
}
}
哪个失败,出现以下错误(与输出的格式间距有关)......
--- FAIL: TestDot (0.00s)
format_test.go:43: expected: digraph sitemap {
"http://www.example.com/"
-> {
"http://www.example.com/foo",
"http://www.example.com/bar",
"http://www.example.com/baz"
}
}
got: digraph sitemap {
"http://www.example.com/"
-> {
"http://www.example.com/foo",
"http://www.example.com/bar",
"http://www.example.com/baz"
}
}
我已经尝试调整我的测试output
变量,以便间距与真实代码实际输出的内容一致。那没用。
我也尝试在我的输出变量和实际输出内容上使用strings.Replace()
,奇怪的是我的函数输出(即使它通过strings.Replace
传递仍然是多行的(因此测试会失败)?
任何人都有任何想法如何使代码验证的输出一致?
谢谢。
我尝试了@icza建议的方法,它仍然没有通过测试,虽然测试中的输出看起来更像是预期的:
=== RUN TestDot
--- FAIL: TestDot (0.00s)
format_test.go:65: expected: digraph sitemap {
"http://www.example.com/"
-> {
"http://www.example.com/foo",
"http://www.example.com/bar",
"http://www.example.com/baz"
}
}
got: digraph sitemap {
"http://www.example.com/"
-> {
"http://www.example.com/foo",
"http://www.example.com/bar",
"http://www.example.com/baz"
}
}
如果要忽略格式,可以使用strings.Fields
。
output := strings.Fields(`digraph sitemap {
"http://www.example.com/"
-> {
"http://www.example.com/foo",
"http://www.example.com/bar",
"http://www.example.com/baz"
}
}`)
actual := strings.Fields(Dot(input))
if !equal(output,actual) {
// ...
}
其中,equal是一个比较两个切片的简单函数。
最简单的解决方案是在指定预期输出时使用相同的缩进(与模板中使用的相同)。
你有:
output := `digraph sitemap {
"http://www.example.com/"
-> {
"http://www.example.com/foo",
"http://www.example.com/bar",
"http://www.example.com/baz"
}
}`
将其更改为:
output := `digraph sitemap {
"http://www.example.com/"
-> {
"http://www.example.com/foo",
"http://www.example.com/bar",
"http://www.example.com/baz"
}
}`
请注意,例如最后一行不缩进。使用原始字符串文字时,包含缩进字符的每个字符都是字面值的一部分。
毕竟,这完全是一个非编码问题,而是编辑器自动格式化和定义原始字符串文字的问题。一个简单的方法是首先编写一个空的原始字符串文字,向其添加一个空行并清除编辑器插入的自动缩进:
output := `
`
如果你有这个,在关闭反引号之前复制粘贴正确的输入,例如:
output := `
digraph sitemap {
"http://www.example.com/"
-> {
"http://www.example.com/foo",
"http://www.example.com/bar",
"http://www.example.com/baz"
}
}`
最后一步,从原始字符串文字的第一行中删除换行符,并且您拥有正确的原始字符串文字:
output := `digraph sitemap {
"http://www.example.com/"
-> {
"http://www.example.com/foo",
"http://www.example.com/bar",
"http://www.example.com/baz"
}
}`
一旦你有了这个,运行gofmt
或编辑器的自动格式化将不再混乱它。
更新:
我检查了你更新的测试结果,在得到的结果中,第一行后面有一个空格:digraph sitemap {
,第三行后面还有一个空格:-> {
,但是你没有将它们添加到你的预期输出中。也可以将它们添加到预期的输出中,或者从模板中删除这些空格!比较字符串时,它们按字节进行比较,每个字符(包括空格)都很重要。
要从模板中删除这些额外的空格:
dotTmpl := `digraph sitemap { {{- range .}}
"{{.URL}}"
-> { {{- $n := len .Anchors}}{{range $i, $v := .Anchors}}
"{{.}}"{{if eq (plus1 $i) $n}}{{else}},{{end}}{{end}}
} {{end}}
}`
注意使用{{-
。这是trim spaces around template actions,这是在Go 1.6添加。
问题是有一个额外的空间。在{
之后的格式化文本中,这似乎是你的问题。您可以通过将格式字符串更改为此来修复它
`digraph sitemap {{{range .}}
"{{.URL}}"
-> {{{$n := len .Anchors}}{{range $i, $v := .Anchors}}
"{{.}}"{{if eq (plus1 $i) $n}}{{else}},{{end}}{{end}}
}{{end}}
}`