-
Notifications
You must be signed in to change notification settings - Fork 0
/
examples_test.go
188 lines (154 loc) · 4.29 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package main
import (
"bytes"
"fmt"
"log"
"os"
"os/exec"
)
// Example of using the list command in a folder with slang files.
func Example_listCmd() {
// Prepare the command
cmd := exec.Command("go", "run", "main.go", "list", "contracts")
// Capture the output
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
// Check for errors
if err != nil {
log.Println("Command execution failed:", err)
return
}
// Output the results
output := out.String()
fmt.Print(output)
// Output:
// Listing contracts in folder: contracts
// Found file: env (Path: contracts/test/env.slang)
// Found file: execute_zencode (Path: contracts/test/execute_zencode.slang)
// Found file: hello (Path: contracts/test/hello.slang)
// Found file: param (Path: contracts/test/param.slang)
// Found file: stdin (Path: contracts/test/stdin.slang)
// Found file: test (Path: contracts/test/test.slang)
}
// Example of using the run command to execute a specific slangroom file.
func Example_runCmd() {
// Prepare the command to run the slang file
cmd := exec.Command("go", "run", "main.go", "test", "hello")
// Capture the output
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
// Check for errors
if err != nil {
log.Println("Command execution failed:", err)
return
}
// Output the results
fmt.Print(out.String())
// Output:
// {"output":["Hello_from_embedded!"]}
}
func Example_runCmdWithExtraData() {
// Prepare the command to run the slang file
cmd := exec.Command("go", "run", "main.go", "test", "execute_zencode")
// Capture the output
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
// Check for errors
if err != nil {
log.Println("Command execution failed:", err)
return
}
// Output the results
fmt.Print(out.String())
// Output:
// {"result":{"ecdh_public_key":"BLOYXryyAI7rPuyNbI0/1CfLFd7H/NbX+osqyQHjPR9BPK1lYSPOixZQWvFK+rkkJ+aJbYp6kii2Y3+fZ5vl2MA="}}
}
func Example_runCmdWithParam() {
// Prepare the command to run the slang file
cmd := exec.Command("go", "run", "main.go", "test", "param", "username", "-n", "testname", "-D", "small")
// Capture the output
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
// Check for errors
if err != nil {
log.Println("Command execution failed:", err)
return
}
// Output the results
fmt.Print(out.String())
// Output:
// {"drink":"small","name":"testname","timeout":60,"username":"username"}
}
func Example_runCmdWithEnvVariable() {
// Prepare the command to run the slang file
cmd := exec.Command("go", "run", "main.go", "test", "env")
err := os.Setenv("FILE_CONTENT", "The enviroment variable is set correctly")
if err != nil {
log.Println("Command execution failed:", err)
return
}
// Capture the output
var out bytes.Buffer
cmd.Stdout = &out
err = cmd.Run()
// Check for errors
if err != nil {
log.Println("Command execution failed:", err)
return
}
// Output the results
fmt.Print(out.String())
// Output:
//{"content":"The enviroment variable is set correctly"}
}
func Example_runCmdWithStdinInput() {
// Prepare the command to run the slang file
cmd1 := exec.Command("cat", "contracts/test/hello.txt")
cmd2 := exec.Command("go", "run", "main.go", "test", "stdin", "-f", "-")
pipe, err := cmd1.StdoutPipe()
if err != nil {
log.Println("Command execution failed:", err)
}
var out bytes.Buffer
cmd2.Stdin = pipe
cmd2.Stdout = &out
// Step 4: Start cmd1 and cmd2
if err := cmd1.Start(); err != nil {
log.Println("Command execution failed:", err)
}
if err := cmd2.Start(); err != nil {
log.Println("Command execution failed:", err)
}
// Step 5: Wait for both commands to finish
if err := cmd1.Wait(); err != nil {
log.Println("Command execution failed:", err)
}
if err := cmd2.Wait(); err != nil {
log.Println("Command execution failed:", err)
}
// Output the results
fmt.Print(out.String())
// Output:
//{"file":"Hello World!"}
}
func Example_runCmdWithFilePath() {
// Prepare the command to run the slang file
cmd := exec.Command("go", "run", "main.go", "test", "stdin", "-f", "contracts/test/hello.txt")
// Capture the output
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
// Check for errors
if err != nil {
log.Println("Command execution failed:", err)
return
}
// Output the results
fmt.Print(out.String())
// Output:
//{"file":"Hello World!"}
}