github.com/MontFerret/ferret@v0.18.0/pkg/stdlib/io/fs/read_test.go (about)

     1  package fs_test
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/MontFerret/ferret/pkg/runtime/core"
     8  	"github.com/MontFerret/ferret/pkg/runtime/values"
     9  	"github.com/MontFerret/ferret/pkg/runtime/values/types"
    10  	"github.com/MontFerret/ferret/pkg/stdlib/io/fs"
    11  
    12  	. "github.com/smartystreets/goconvey/convey"
    13  )
    14  
    15  func TestRead(t *testing.T) {
    16  
    17  	Convey("Arguments passed", t, func() {
    18  
    19  		Convey("No arguments passed", func() {
    20  			out, err := fs.Read(context.Background())
    21  
    22  			So(out, ShouldEqual, values.None)
    23  			So(err, ShouldBeError)
    24  		})
    25  
    26  		Convey("Passed not a string", func() {
    27  			args := []core.Value{values.NewInt(0)}
    28  			out, err := fs.Read(context.Background(), args...)
    29  
    30  			So(out, ShouldEqual, values.None)
    31  			So(err, ShouldBeError)
    32  		})
    33  
    34  		Convey("Passed more that one argument", func() {
    35  			args := []core.Value{
    36  				values.NewString("filepath"),
    37  				values.NewInt(0),
    38  			}
    39  			out, err := fs.Read(context.Background(), args...)
    40  
    41  			So(out, ShouldEqual, values.None)
    42  			So(err, ShouldBeError)
    43  		})
    44  	})
    45  
    46  	Convey("Read from file", t, func() {
    47  
    48  		Convey("File exists", func() {
    49  			file, delFile := tempFile()
    50  			defer delFile()
    51  
    52  			text := "s string"
    53  			file.WriteString(text)
    54  
    55  			fname := values.NewString(file.Name())
    56  
    57  			out, err := fs.Read(context.Background(), fname)
    58  			So(err, ShouldBeNil)
    59  
    60  			So(out.Type().ID(), ShouldEqual, types.Binary.ID())
    61  			So(out.String(), ShouldEqual, text)
    62  		})
    63  
    64  		Convey("File does not exist", func() {
    65  			fname := values.NewString("not_exist.file")
    66  
    67  			out, err := fs.Read(context.Background(), fname)
    68  			So(out, ShouldEqual, values.None)
    69  			So(err, ShouldBeError)
    70  		})
    71  	})
    72  }