github.com/justinjmoses/evergreen@v0.0.0-20170530173719-1d50e381ff0d/rest/model/string_test.go (about)

     1  package model
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"testing"
     7  
     8  	. "github.com/smartystreets/goconvey/convey"
     9  )
    10  
    11  func TestStringMarshal(t *testing.T) {
    12  	Convey("When checking string", t, func() {
    13  		Convey("and marshalling", func() {
    14  			Convey("then empty string should produce null as output", func() {
    15  				str := APIString("")
    16  				data, err := json.Marshal(&str)
    17  				So(err, ShouldBeNil)
    18  				So(string(data), ShouldEqual, "null")
    19  			})
    20  			Convey("then non empty string should produce regular output", func() {
    21  				testStr := "testStr"
    22  				str := APIString(testStr)
    23  				data, err := json.Marshal(str)
    24  				So(err, ShouldBeNil)
    25  				So(string(data), ShouldEqual, fmt.Sprintf("\"%s\"", testStr))
    26  			})
    27  
    28  		})
    29  		Convey("and unmarshalling", func() {
    30  			Convey("then null should produce null as output", func() {
    31  				var res APIString
    32  				err := json.Unmarshal([]byte("null"), &res)
    33  				So(err, ShouldBeNil)
    34  				So(res, ShouldEqual, "")
    35  			})
    36  			Convey("then non empty string should produce regular output", func() {
    37  				var res APIString
    38  				testStr := "testStr"
    39  				str := fmt.Sprintf("\"%s\"", testStr)
    40  				err := json.Unmarshal([]byte(str), &res)
    41  				So(err, ShouldBeNil)
    42  				So(res, ShouldEqual, testStr)
    43  
    44  			})
    45  
    46  		})
    47  
    48  	})
    49  
    50  }