跳到主要内容

Claude Cage · 面向 Claude 的隔离开发环境,支持桌面工作台、多账号、远程部署与移动端连接。上游 Lee-xeo/Claude-Cage 完整源码镜像,非灵栈原创;主许可 MIT,第三方组件遵循各自许可。来源:https://github.com/Lee-xeo/Claude-Cage 。手动同步,不附带 Release 安装包。

浏览文件

textutil_test.go

62 行 · 1.8 KB历史下载
1// SPDX-FileCopyrightText: Copyright The Lima Authors
2// SPDX-License-Identifier: Apache-2.0
3
4package textutil
5
6import (
7 "bytes"
8 "testing"
9 "text/template"
10
11 "gotest.tools/v3/assert"
12)
13
14func TestPrefixString(t *testing.T) {
15 assert.Equal(t, "", PrefixString("- ", ""))
16 assert.Equal(t, "\n", PrefixString("- ", "\n"))
17 assert.Equal(t, "- foo", PrefixString("- ", "foo"))
18 assert.Equal(t, "- foo\n- bar\n", PrefixString("- ", "foo\nbar\n"))
19 assert.Equal(t, "- foo\n\n- bar\n", PrefixString("- ", "foo\n\nbar\n"))
20}
21
22func TestIndentString(t *testing.T) {
23 assert.Equal(t, " foo", IndentString(2, "foo"))
24 assert.Equal(t, " foo\n bar\n", IndentString(2, "foo\nbar\n"))
25}
26
27func TestMissingString(t *testing.T) {
28 assert.Equal(t, "no", MissingString("no", ""))
29 assert.Equal(t, "msg", MissingString("no", "msg"))
30}
31
32func TestTemplateFuncs(t *testing.T) {
33 type X struct {
34 Foo int `json:"foo" yaml:"foo"`
35 Bar string `json:"bar" yaml:"bar"`
36 Message string `json:"message,omitempty" yaml:"message,omitempty"`
37 Missing string `json:"missing,omitempty" yaml:"missing,omitempty"`
38 }
39 x := X{Foo: 42, Bar: "hello", Message: "One\nTwo\nThree", Missing: ""}
40
41 testCases := map[string]string{
42 "{{json .}}": `{"foo":42,"bar":"hello","message":"One\nTwo\nThree"}`,
43 "{{yaml .}}": `---
44foo: 42
45bar: hello
46message: |-
47 One
48 Two
49 Three`,
50 `{{.Bar}}{{"\n"}}{{.Message | missing "<no message>" | indent 2}}`: "hello\n One\n Two\n Three",
51 `{{.Message | indent}}`: " One\n Two\n Three",
52 `{{.Missing | missing}}`: "<missing>",
53 }
54
55 for format, expected := range testCases {
56 tmpl, err := template.New("format").Funcs(TemplateFuncMap).Parse(format)
57 assert.NilError(t, err)
58 var b bytes.Buffer
59 assert.NilError(t, tmpl.Execute(&b, x))
60 assert.Equal(t, expected, b.String())
61 }
62}