File: eval_examples_test.go

package info (click to toggle)
elvish 0.21.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,372 kB
  • sloc: javascript: 236; sh: 130; python: 104; makefile: 88; xml: 9
file content (71 lines) | stat: -rw-r--r-- 1,763 bytes parent folder | download
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
package eval_test

import (
	"fmt"

	"src.elv.sh/pkg/eval"
	"src.elv.sh/pkg/eval/vals"
	"src.elv.sh/pkg/parse"
)

func ExampleEvaler_Eval_usingPortsFromStdFiles() {
	ev := eval.NewEvaler()
	// These ports are connected to the process's stdin, stdout and stderr. The
	// "> " part is the prefix to use for value outputs.
	ports, cleanup := eval.PortsFromStdFiles("> ")
	defer cleanup()

	ev.Eval(
		parse.Source{Name: "example 1", Code: "echo Hello Elvish!"},
		eval.EvalCfg{Ports: ports})

	// Value outputs are written with the prefix we specified earlier
	ev.Eval(
		parse.Source{Name: "example 2", Code: "put [&foo=bar]"},
		eval.EvalCfg{Ports: ports})

	// Output:
	// Hello Elvish!
	// > [&foo=bar]
}

func ExampleEvaler_Eval_capturingValueOutputs() {
	ev := eval.NewEvaler()
	// The stdout port captures all values written to it, which can be retrieved
	// with the returned get function.
	stdout, get, err := eval.ValueCapturePort()
	if err != nil {
		panic(err)
	}

	ev.Eval(
		parse.Source{Name: "example 1", Code: "put [&foo=bar] [a b] data"},
		eval.EvalCfg{Ports: []*eval.Port{eval.DummyInputPort, stdout, eval.DummyOutputPort}})

	values := get()
	for i, value := range values {
		fmt.Printf("#%d: %s: %s\n", i, vals.Kind(value), vals.ReprPlain(value))
	}

	// Output:
	// #0: map: [&foo=bar]
	// #1: list: [a b]
	// #2: string: data
}

func ExampleEvaler_Eval_inspectingGlobal() {
	ev := eval.NewEvaler()
	ev.Eval(
		parse.Source{Name: "example 1", Code: "var map = [&foo=bar]"},
		// Omitting the ports connects all of them to "dummy" IO ports.
		eval.EvalCfg{})

	m, ok := ev.Global().Index("map")
	if !ok {
		fmt.Println("$map not found")
	}
	fmt.Printf("$map: %s: %s\n", vals.Kind(m), vals.ReprPlain(m))

	// Output:
	// $map: map: [&foo=bar]
}