54 lines
1.4 KiB
Go
54 lines
1.4 KiB
Go
package builtins_test
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"github.com/stretchr/testify/assert"
|
|
"strings"
|
|
"testing"
|
|
"testing/fstest"
|
|
"ucl.lmika.dev/ucl"
|
|
"ucl.lmika.dev/ucl/builtins"
|
|
)
|
|
|
|
var testCsvFS = fstest.MapFS{
|
|
"test.csv": &fstest.MapFile{
|
|
Data: []byte(strings.Join([]string{
|
|
"wind,dir,bearing",
|
|
"north,N,0",
|
|
"south,S,180",
|
|
"east,E,90",
|
|
"west,W,270",
|
|
}, "\n")),
|
|
},
|
|
}
|
|
|
|
func TestCSV_ReadRecord(t *testing.T) {
|
|
tests := []struct {
|
|
descr string
|
|
eval string
|
|
wantOut string
|
|
}{
|
|
{descr: "read csv 1", eval: `csv:each-record "test.csv" { |r h| echo $r.(0) }`, wantOut: "north\nsouth\neast\nwest\n"},
|
|
{descr: "read csv 2", eval: `csv:each-record "test.csv" { |r h| echo $r.($h.dir) }`, wantOut: "N\nS\nE\nW\n"},
|
|
{descr: "read csv 3", eval: `csv:each-record "test.csv" { |r h| echo $r.($h.bearing) "-" $r.($h.dir) }`, wantOut: "0-N\n180-S\n90-E\n270-W\n"},
|
|
{descr: "read csv 4", eval: `csv:each-record "test.csv" { |r h| echo $h.bearing }`, wantOut: "2\n2\n2\n2\n"},
|
|
{descr: "read csv 5", eval: `csv:each-record "test.csv" {}`, wantOut: ""},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.descr, func(t *testing.T) {
|
|
var bfr bytes.Buffer
|
|
|
|
inst := ucl.New(
|
|
ucl.WithModule(builtins.CSV(testCsvFS)),
|
|
ucl.WithOut(&bfr),
|
|
)
|
|
|
|
_, err := inst.EvalString(context.Background(), tt.eval)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, tt.wantOut, bfr.String())
|
|
})
|
|
}
|
|
}
|