package core import ( "testing" "github.com/stretchr/testify/assert" "github.com/yaoapp/kun/any" "github.com/yaoapp/yao/config" "github.com/yaoapp/yao/test" ) func TestPageExec(t *testing.T) { prepare(t) defer clean() page := testPage(t) request := &Request{ URL: ReqeustURL{Path: "/test/path"}, Query: map[string][]string{"show": {"yes"}}, Locale: "zh-CN", Theme: "dark", } data, err := page.Exec(request) if err != nil { t.Fatalf("Exec error: %v", err) } assert.NotEmpty(t, data) res := any.Of(data).Map().Dot() assert.Equal(t, "yes", res.Get("array[3][0].query")) assert.Equal(t, "Article Search", res.Get("articles.data[0].description")) assert.Equal(t, "/test/path", res.Get("url.path")) } func prepare(t *testing.T) { test.Prepare(t, config.Conf, "YAO_TEST_BUILDER_APPLICATION") } func clean() { test.Clean() } func testPage(t *testing.T) *Page { page := &Page{ Name: "test", Route: "test", Document: []byte(` Document
{{ __page }}
`), Codes: SourceCodes{ HTML: Source{ File: "test.html", Code: `
For
{{ idx }} {{ article.title }}
{{ article.desc }}
{{ article.type == "article" ? "article" : "others"}}
IF
article
image
others
Nested
{{ imgIndex }} {{ image }}
IF
{{ len(articles) > 0 }} articles.length > 0
{{ length > 0 }} len > 0
hello space {{ P_('scripts.article.Space', 'hello') }}
showImage
noImage
autoImage
otherImage
Bind
{{ input.data }}
Import Page
Link Item Link2 Item2 `, }, DATA: Source{ File: "test.json", Code: `{ "$articles": "scripts.article.Search", "$showImage": { "process": "scripts.article.ShowImage", "args": ["$query.show"] }, "length": 20, "array": [ "item-1", "$scripts.article.Setting", {"$images": "scripts.article.Images"}, {"process": "scripts.article.Thumbs", "args": ["$query.show"], "__exec": true } ], "input": { "data": "hello world" }, "url": {"path":"$url.path"} } `, }, }, } return page }