...

Source file src/github.com/launchdarkly/go-jsonstream/v3/internal/commontest/reader_test_suite.go

Documentation: github.com/launchdarkly/go-jsonstream/v3/internal/commontest

     1  package commontest
     2  
     3  import (
     4  	"strings"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/require"
     8  )
     9  
    10  // ReaderTestSuite runs a standard set of tests against some implementation of JSON reading.
    11  // This allows us to test both jreader.Reader and the low-level tokenizer jreader.tokenReader
    12  // with many permutations of valid and invalid input.
    13  type ReaderTestSuite struct {
    14  	// ContextFactory must be provided by the caller to create an implementation of TestContext for
    15  	// running a parsing test on the specified JSON input. This should include whatever parser
    16  	// object will be used by the Actions that the ValueTestFactory creates.
    17  	ContextFactory func(input []byte) TestContext
    18  
    19  	// ValueTestFactory must be provided by the caller to create implementations of Action for
    20  	// various JSON value types.
    21  	ValueTestFactory ValueTestFactory
    22  
    23  	// ReadErrorTestFactory must be provided by the caller to define expectations about error
    24  	// reporting for invalid input.
    25  	ReadErrorTestFactory ReadErrorTestFactory
    26  }
    27  
    28  // Run runs the test suite.
    29  func (s ReaderTestSuite) Run(t *testing.T) {
    30  	tf := testFactory{
    31  		valueTestFactory:     s.ValueTestFactory,
    32  		readErrorTestFactory: s.ReadErrorTestFactory,
    33  		encodingBehavior: encodingBehavior{
    34  			forParsing: true,
    35  		},
    36  	}
    37  	var testDefs testDefs
    38  	testDefs = append(testDefs, tf.MakeAllValueTests()...)
    39  	testDefs = append(testDefs, tf.MakeAllReadErrorTests()...)
    40  	whitespaceOptions := MakeWhitespaceOptions()
    41  	whitespaceOptions[""] = ""
    42  	for _, td := range testDefs {
    43  		for wsName, wsValue := range whitespaceOptions {
    44  			testName := td.name
    45  			if wsName != "" {
    46  				testName += " [with whitespace: " + wsName + "]"
    47  			}
    48  			t.Run(testName, func(t *testing.T) {
    49  				input := wsValue + strings.Join(td.encoding, wsValue) + wsValue
    50  				t.Cleanup(func() {
    51  					if t.Failed() {
    52  						t.Logf("JSON input was: `%s`", input)
    53  					}
    54  				})
    55  				c := s.ContextFactory([]byte(input))
    56  				require.NoError(t, td.action(c))
    57  			})
    58  		}
    59  	}
    60  }
    61  

View as plain text