...

Source file src/github.com/launchdarkly/go-jsonstream/v3/jwriter/writer_test.go

Documentation: github.com/launchdarkly/go-jsonstream/v3/jwriter

     1  package jwriter
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"strconv"
     7  	"testing"
     8  
     9  	"github.com/launchdarkly/go-jsonstream/v3/internal/commontest"
    10  )
    11  
    12  // This uses the framework defined in the commontest package to exercise Writer with a large
    13  // number of JSON output permutations, in conjunction with DefaultTokenWriter.
    14  
    15  type writerTestContext struct {
    16  	w *Writer
    17  }
    18  
    19  type writerValueTestFactory struct{}
    20  
    21  func TestWriter(t *testing.T) {
    22  	s := commontest.WriterTestSuite{
    23  		ContextFactory: func() commontest.TestContext {
    24  			w := NewWriter()
    25  			return &writerTestContext{
    26  				w: &w,
    27  			}
    28  		},
    29  		ValueTestFactory: writerValueTestFactory{},
    30  		EncodeAsHex:      tokenWriterWillEncodeAsHex,
    31  	}
    32  	s.Run(t)
    33  }
    34  
    35  func (c writerTestContext) JSONData() []byte { return c.w.tw.Bytes() }
    36  
    37  func (f writerValueTestFactory) EOF() commontest.Action {
    38  	return func(c commontest.TestContext) error {
    39  		return c.(*writerTestContext).w.Error()
    40  	}
    41  }
    42  
    43  func (f writerValueTestFactory) Variants(value commontest.AnyValue) []commontest.ValueVariant {
    44  	// Integer values can be written using either Int() or Float64().
    45  	if value.Kind == commontest.NumberValue && float64(int(value.Number)) == value.Number {
    46  		return variantsForWritingNumbers
    47  	}
    48  	if value.Kind != commontest.ArrayValue && value.Kind != commontest.ObjectValue {
    49  		return variantsForScalarValues
    50  	}
    51  	return nil
    52  }
    53  
    54  func (f writerValueTestFactory) Value(value commontest.AnyValue, variant commontest.ValueVariant) commontest.Action {
    55  	return func(c commontest.TestContext) error {
    56  		ctx := c.(*writerTestContext)
    57  		w := ctx.w
    58  
    59  		switch value.Kind {
    60  		case commontest.NullValue:
    61  			if variant == commontest.UntypedVariant {
    62  				w.Raw(json.RawMessage(`null`))
    63  				return w.Error()
    64  			}
    65  			w.Null()
    66  
    67  		case commontest.BoolValue:
    68  			if variant == commontest.UntypedVariant {
    69  				w.Raw(json.RawMessage(fmt.Sprintf("%t", value.Bool)))
    70  				return w.Error()
    71  			}
    72  			w.Bool(value.Bool)
    73  
    74  		case commontest.NumberValue:
    75  			if variant == commontest.UntypedVariant {
    76  				w.Raw(json.RawMessage(strconv.FormatFloat(value.Number, 'f', -1, 64)))
    77  				return w.Error()
    78  			}
    79  			if variant == writeNumberAsInt {
    80  				w.Int(int(value.Number))
    81  			} else {
    82  				w.Float64(value.Number)
    83  			}
    84  
    85  		case commontest.StringValue:
    86  			if variant == commontest.UntypedVariant {
    87  				// Use our own encoder to encode the string, but then write it with Raw()
    88  				tw1 := newTokenWriter()
    89  				_ = tw1.String(value.String)
    90  				w.Raw(json.RawMessage(tw1.Bytes()))
    91  				return w.Error()
    92  			}
    93  			w.String(value.String)
    94  
    95  		case commontest.ArrayValue:
    96  			arr := w.Array()
    97  			for _, e := range value.Array {
    98  				if err := e(ctx); err != nil {
    99  					return err
   100  				}
   101  			}
   102  			arr.End()
   103  
   104  		case commontest.ObjectValue:
   105  			obj := w.Object()
   106  			for _, p := range value.Object {
   107  				obj.Name(p.Name)
   108  				if err := p.Action(ctx); err != nil {
   109  					return err
   110  				}
   111  			}
   112  			obj.End()
   113  		}
   114  
   115  		return w.Error()
   116  	}
   117  }
   118  

View as plain text