github.com/MontFerret/ferret@v0.18.0/pkg/stdlib/strings/json_test.go (about)

     1  package strings_test
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	. "github.com/smartystreets/goconvey/convey"
     8  
     9  	"github.com/MontFerret/ferret/pkg/runtime/values"
    10  	"github.com/MontFerret/ferret/pkg/runtime/values/types"
    11  	"github.com/MontFerret/ferret/pkg/stdlib/strings"
    12  )
    13  
    14  func TestJSONParse(t *testing.T) {
    15  	Convey("When args are not passed", t, func() {
    16  		Convey("It should return an error", func() {
    17  			var err error
    18  			_, err = strings.JSONParse(context.Background())
    19  
    20  			So(err, ShouldBeError)
    21  		})
    22  	})
    23  
    24  	Convey("It should parse none", t, func() {
    25  		val := values.None
    26  
    27  		b, err := val.MarshalJSON()
    28  
    29  		So(err, ShouldBeNil)
    30  
    31  		out, err := strings.JSONParse(
    32  			context.Background(),
    33  			values.NewString(string(b)),
    34  		)
    35  
    36  		So(err, ShouldBeNil)
    37  		So(out.Type().Equals(types.None), ShouldBeTrue)
    38  	})
    39  
    40  	Convey("It should parse a string", t, func() {
    41  		val := values.NewString("foobar")
    42  
    43  		b, err := val.MarshalJSON()
    44  
    45  		So(err, ShouldBeNil)
    46  
    47  		out, err := strings.JSONParse(
    48  			context.Background(),
    49  			values.NewString(string(b)),
    50  		)
    51  
    52  		So(err, ShouldBeNil)
    53  		So(out.Type().Equals(types.String), ShouldBeTrue)
    54  	})
    55  
    56  	Convey("It should parse an int", t, func() {
    57  		val := values.NewInt(1)
    58  
    59  		b, err := val.MarshalJSON()
    60  
    61  		So(err, ShouldBeNil)
    62  
    63  		out, err := strings.JSONParse(
    64  			context.Background(),
    65  			values.NewString(string(b)),
    66  		)
    67  
    68  		So(err, ShouldBeNil)
    69  		So(out.Type().Equals(types.Float), ShouldBeTrue)
    70  	})
    71  
    72  	Convey("It should parse a float", t, func() {
    73  		val := values.NewFloat(1.1)
    74  
    75  		b, err := val.MarshalJSON()
    76  
    77  		So(err, ShouldBeNil)
    78  
    79  		out, err := strings.JSONParse(
    80  			context.Background(),
    81  			values.NewString(string(b)),
    82  		)
    83  
    84  		So(err, ShouldBeNil)
    85  		So(out.Type().Equals(types.Float), ShouldBeTrue)
    86  	})
    87  
    88  	Convey("It should parse a boolean", t, func() {
    89  		val := values.True
    90  
    91  		b, err := val.MarshalJSON()
    92  
    93  		So(err, ShouldBeNil)
    94  
    95  		out, err := strings.JSONParse(
    96  			context.Background(),
    97  			values.NewString(string(b)),
    98  		)
    99  
   100  		So(err, ShouldBeNil)
   101  		So(out.Type().Equals(types.Boolean), ShouldBeTrue)
   102  	})
   103  
   104  	Convey("It should parse an array", t, func() {
   105  		val := values.NewArrayWith(
   106  			values.Int(1),
   107  			values.Int(2),
   108  			values.Int(3),
   109  		)
   110  
   111  		b, err := val.MarshalJSON()
   112  
   113  		So(err, ShouldBeNil)
   114  
   115  		out, err := strings.JSONParse(
   116  			context.Background(),
   117  			values.NewString(string(b)),
   118  		)
   119  
   120  		So(err, ShouldBeNil)
   121  		So(out.Type().Equals(types.Array), ShouldBeTrue)
   122  		So(out.String(), ShouldEqual, "[1,2,3]")
   123  	})
   124  
   125  	Convey("It should parse an object", t, func() {
   126  		val := values.NewObject()
   127  		val.Set(values.NewString("foo"), values.NewString("bar"))
   128  
   129  		b, err := val.MarshalJSON()
   130  
   131  		So(err, ShouldBeNil)
   132  
   133  		out, err := strings.JSONParse(
   134  			context.Background(),
   135  			values.NewString(string(b)),
   136  		)
   137  
   138  		So(err, ShouldBeNil)
   139  		So(out.Type().Equals(types.Object), ShouldBeTrue)
   140  		So(out.String(), ShouldEqual, `{"foo":"bar"}`)
   141  	})
   142  }
   143  
   144  func TestJSONStringify(t *testing.T) {
   145  	Convey("When args are not passed", t, func() {
   146  		Convey("It should return an error", func() {
   147  			var err error
   148  			_, err = strings.JSONStringify(context.Background())
   149  
   150  			So(err, ShouldBeError)
   151  		})
   152  	})
   153  
   154  	Convey("It should serialize none", t, func() {
   155  		out, err := strings.JSONStringify(
   156  			context.Background(),
   157  			values.None,
   158  		)
   159  
   160  		So(err, ShouldBeNil)
   161  		So(out.String(), ShouldEqual, "null")
   162  	})
   163  
   164  	Convey("It should serialize boolean", t, func() {
   165  		out, err := strings.JSONStringify(
   166  			context.Background(),
   167  			values.False,
   168  		)
   169  
   170  		So(err, ShouldBeNil)
   171  		So(out.String(), ShouldEqual, "false")
   172  	})
   173  
   174  	Convey("It should serialize string", t, func() {
   175  		out, err := strings.JSONStringify(
   176  			context.Background(),
   177  			values.NewString("foobar"),
   178  		)
   179  
   180  		So(err, ShouldBeNil)
   181  		So(out.String(), ShouldEqual, `"foobar"`)
   182  	})
   183  
   184  	Convey("It should serialize int", t, func() {
   185  		out, err := strings.JSONStringify(
   186  			context.Background(),
   187  			values.NewInt(1),
   188  		)
   189  
   190  		So(err, ShouldBeNil)
   191  		So(out.String(), ShouldEqual, `1`)
   192  	})
   193  
   194  	Convey("It should serialize float", t, func() {
   195  		out, err := strings.JSONStringify(
   196  			context.Background(),
   197  			values.NewFloat(1.1),
   198  		)
   199  
   200  		So(err, ShouldBeNil)
   201  		So(out.String(), ShouldEqual, `1.1`)
   202  	})
   203  
   204  	Convey("It should serialize array", t, func() {
   205  		out, err := strings.JSONStringify(
   206  			context.Background(),
   207  			values.NewArrayWith(
   208  				values.NewString("foo"),
   209  				values.NewString("bar"),
   210  			),
   211  		)
   212  
   213  		So(err, ShouldBeNil)
   214  		So(out.String(), ShouldEqual, `["foo","bar"]`)
   215  	})
   216  
   217  	Convey("It should serialize object", t, func() {
   218  		obj := values.NewObject()
   219  		obj.Set(values.NewString("foo"), values.NewString("bar"))
   220  
   221  		out, err := strings.JSONStringify(
   222  			context.Background(),
   223  			obj,
   224  		)
   225  
   226  		So(err, ShouldBeNil)
   227  		So(out.String(), ShouldEqual, `{"foo":"bar"}`)
   228  	})
   229  
   230  	Convey("It should serialize datetime", t, func() {
   231  		obj, err := values.ParseDateTime("2006-01-02T15:04:05Z")
   232  
   233  		So(err, ShouldBeNil)
   234  
   235  		out, err := strings.JSONStringify(
   236  			context.Background(),
   237  			obj,
   238  		)
   239  
   240  		So(err, ShouldBeNil)
   241  		So(out.String(), ShouldEqual, `"2006-01-02T15:04:05Z"`)
   242  	})
   243  }