github.com/goplus/yap@v0.8.1/internal/templ/template_test.go (about) 1 /* 2 * Copyright (c) 2023 The GoPlus Authors (goplus.org). All rights reserved. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package templ 18 19 import ( 20 "bytes" 21 "testing" 22 ) 23 24 const yapScriptIn = ` 25 <html> 26 <body> 27 <pre>{{ 28 $name := .Name 29 $base := .Base 30 range .Items 31 }} 32 <a href="{{.URL}}">{{.Name}}</a> (pv: {{.Meta.ViewCount}} nps: {{.NPS | printf "%.3f"}} by: {{.Meta.User}}) (<a href="/metadata/{{.ID}}">meta</a>){{ 33 if $base 34 if .Ann}} (annotation: <a href="/annotation/{{.Ann}}?p=1">{{.Ann}}</a>){{ 35 end 36 end 37 with .Meta.Categories 38 $n := len . 39 if gt $n $base}} (under:{{ 40 range . 41 if ne . $name}} <a href="/category/{{.}}?p=1">{{.}}</a>{{ 42 end 43 end 44 }}){{ 45 end 46 end 47 end 48 }} 49 </pre> 50 </body> 51 </html> 52 ` 53 54 const yapScriptOut = ` 55 <html> 56 <body> 57 <pre>{{ 58 $name := .Name}}{{ 59 $base := .Base}}{{ 60 range .Items 61 }} 62 <a href="{{.URL}}">{{.Name}}</a> (pv: {{.Meta.ViewCount}} nps: {{.NPS | printf "%.3f"}} by: {{.Meta.User}}) (<a href="/metadata/{{.ID}}">meta</a>){{ 63 if $base}}{{ 64 if .Ann}} (annotation: <a href="/annotation/{{.Ann}}?p=1">{{.Ann}}</a>){{ 65 end}}{{ 66 end}}{{ 67 with .Meta.Categories}}{{ 68 $n := len .}}{{ 69 if gt $n $base}} (under:{{ 70 range .}}{{ 71 if ne . $name}} <a href="/category/{{.}}?p=1">{{.}}</a>{{ 72 end}}{{ 73 end 74 }}){{ 75 end}}{{ 76 end}}{{ 77 end 78 }} 79 </pre> 80 </body> 81 </html> 82 ` 83 84 func TestTranslate(t *testing.T) { 85 if ret := Translate(yapScriptIn); ret != yapScriptOut { 86 t.Fatal("TestTranslate:", len(ret), len(yapScriptOut), len(yapScriptIn)+11*4, ret) 87 } 88 noScript := "abc" 89 if Translate(noScript) != noScript { 90 t.Fatal("translate(noScript)") 91 } 92 noScriptEnd := `{{abc 93 efg 94 ` 95 noScriptEndOut := `{{abc}}{{ 96 efg 97 ` 98 if ret := Translate(noScriptEnd); ret != noScriptEndOut { 99 t.Fatal("translate(noScriptEnd):", ret) 100 } 101 var b bytes.Buffer 102 TranslateEx(&b, yapScriptIn, "{{", "}}") 103 if b.String() != yapScriptOut { 104 t.Fatal("TranslateEx:", b.String()) 105 } 106 }