forked from open2b/scriggo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
examples_test.go
112 lines (100 loc) · 2.21 KB
/
examples_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Copyright 2021 The Scriggo Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package scriggo_test
import (
"fmt"
"log"
"os"
"github.com/open2b/scriggo"
"github.com/open2b/scriggo/native"
)
func ExampleBuild() {
fsys := scriggo.Files{
"main.go": []byte(`
package main
func main() { }
`),
}
_, err := scriggo.Build(fsys, nil)
if err != nil {
log.Fatal(err)
}
// Output:
}
func ExampleProgram_Run() {
fsys := scriggo.Files{
"main.go": []byte(`
package main
import "fmt"
func main() {
fmt.Println("Hello, I'm Scriggo!")
}
`),
}
opts := &scriggo.BuildOptions{
Packages: native.Packages{
"fmt": native.Package{
Name: "fmt",
Declarations: native.Declarations{
"Println": fmt.Println,
},
},
},
}
program, err := scriggo.Build(fsys, opts)
if err != nil {
log.Fatal(err)
}
err = program.Run(nil)
if err != nil {
log.Fatal(err)
}
// Output:
// Hello, I'm Scriggo!
}
func ExampleBuildTemplate() {
fsys := scriggo.Files{
"index.html": []byte(`{% name := "Scriggo" %}Hello, {{ name }}!`),
}
_, err := scriggo.BuildTemplate(fsys, "index.html", nil)
if err != nil {
log.Fatal(err)
}
// Output:
}
func ExampleTemplate_Run() {
fsys := scriggo.Files{
"index.html": []byte(`{% name := "Scriggo" %}Hello, {{ name }}!`),
}
template, err := scriggo.BuildTemplate(fsys, "index.html", nil)
if err != nil {
log.Fatal(err)
}
err = template.Run(os.Stdout, nil, nil)
if err != nil {
log.Fatal(err)
}
// Output:
// Hello, Scriggo!
}
func ExampleHTMLEscape() {
fmt.Println(scriggo.HTMLEscape("Rock & Roll!"))
// Output:
// Rock & Roll!
}
func ExampleBuildError() {
fsys := scriggo.Files{
"index.html": []byte(`{{ 42 + "hello" }}`),
}
_, err := scriggo.BuildTemplate(fsys, "index.html", nil)
if err != nil {
fmt.Printf("Error has type %T\n", err)
fmt.Printf("Error message is: %s\n", err.(*scriggo.BuildError).Message())
fmt.Printf("Error path is: %s\n", err.(*scriggo.BuildError).Path())
}
// Output:
// Error has type *scriggo.BuildError
// Error message is: invalid operation: 42 + "hello" (mismatched types int and string)
// Error path is: index.html
}