Skip to content
This repository has been archived by the owner on Sep 29, 2021. It is now read-only.

Commit

Permalink
docs: add example
Browse files Browse the repository at this point in the history
  • Loading branch information
suzuki-shunsuke committed Oct 15, 2020
1 parent 8c16141 commit 04d01c6
Show file tree
Hide file tree
Showing 11 changed files with 132 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .cmdx.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,8 @@ tasks:
description: tengo-tester run for test
usage: tengo-tester run for test
script: "go run ./cmd/tengo-tester run"
- name: test-example
short: te
description: test examples
usage: test examples
script: "bash scripts/test-example.sh"
3 changes: 3 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ jobs:
CC_TEST_REPORTER_ID: ${{ secrets.CC_TEST_REPORTER_ID }}
run: |
bash ci/test.sh tengo-tester
- name: test examples
run: |
bash scripts/test-example.sh
- name: remove changes
# Sometimes it is failed to release by goreleaser due to changes of go.sum
Expand Down
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,20 @@ FATA[0000]
exit status 1
```

## Examples

Please see [examples](examples).

On the directory, there are some examples of configuration files.

We can execute the test with them.

For example,

```
$ tengo-tester run -c examples/hello_world.yaml
```

## Configuration Reference

```yaml
Expand Down
5 changes: 5 additions & 0 deletions examples/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module examples

go 1.15

require gotest.tools/v3 v3.0.3
16 changes: 16 additions & 0 deletions examples/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
github.com/google/go-cmp v0.4.0 h1:xsAVV57WRhGj6kEIi8ReJzQlHHqcBYCElAvkovg3B/4=
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20190624222133-a101b041ded4/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gotest.tools v1.4.0 h1:BjtEgfuw8Qyd+jPvQz8CfoxiO/UjFEidWinwEXZiWv0=
gotest.tools v2.2.0+incompatible h1:VsBPFP1AI068pPrMxtb/S8Zkgf9xEmTLJjfM+P5UIEo=
gotest.tools/v3 v3.0.3 h1:4AuOwCGf4lLR9u3YOe2awrHygurzhO/HeQ6laiA6Sx0=
gotest.tools/v3 v3.0.3/go.mod h1:Z7Lb0S5l+klDB31fvDQX8ss/FlKDxtlFlw3Oa8Ymbl8=
1 change: 1 addition & 0 deletions examples/hello_world.tengo
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
result := 1
33 changes: 33 additions & 0 deletions examples/hello_world.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---
entries:
- name: main
script_file: hello_world.tengo
tests:
- name: test
var_name: result
equal: 1
- name: params
script_file: params.tengo
params:
value:
message: hello
tests:
- name: test
var_name: result
equal:
foo:
message: hello
- name: use Tengo script for test
script: |
fmt := import("fmt")
err_msg := ""
exp := {
foo: {
message: "hello"
}
}
if result.result != exp {
err_msg = fmt.sprintf("the variable 'value' is invalid: wanted %+v, got %+v", exp, result.result)
}
- name: read Tengo script from a file
script_file: params_test.tengo
27 changes: 27 additions & 0 deletions examples/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package examples_test

import (
"testing"

"gotest.tools/v3/icmd"
)

func TestBuildflow(t *testing.T) { //nolint:funlen
data := []struct {
title string
file string
exp icmd.Expected
}{
{
title: "hello world",
file: "hello_world.yaml",
},
}
for _, d := range data {
d := d
t.Run(d.title, func(t *testing.T) {
result := icmd.RunCmd(icmd.Command("tengo-tester", "run", "-c", d.file))
result.Assert(t, d.exp)
})
}
}
5 changes: 5 additions & 0 deletions examples/params.tengo
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
result := func(value) {
return {
"foo": value
}
}(value)
10 changes: 10 additions & 0 deletions examples/params_test.tengo
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
fmt := import("fmt")
err_msg := ""
exp := {
foo: {
message: "hello"
}
}
if result.result != exp {
err_msg = fmt.sprintf("the variable 'value' is invalid: wanted %+v, got %+v", exp, result.result)
}
13 changes: 13 additions & 0 deletions scripts/test-example.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env bash

set -eu

cd "$(dirname "$0")/.."

tempdir=$(mktemp -d)
go build -o "$tempdir/tengo-tester" ./cmd/tengo-tester
export PATH="$tempdir:$PATH"
cd examples
# command -v tengo-tester
go test -race -covermode=atomic ./...
rm -R "$tempdir"

0 comments on commit 04d01c6

Please sign in to comment.