const ( ExampleStructStringFieldName = "string" ExampleStructIntFieldName = "int" ExampleStructOptBoolAsInterfaceFieldName = "optBool" )
var ( ExampleStructData = []byte(`{"string":"s","int":3,"optBool":true}`) ExampleStructValue = ExampleStruct{StringField: "s", IntField: 3, OptBoolAsInterfaceField: true} ExampleStructRequiredFieldNames = []string{ExampleStructStringFieldName, ExampleStructIntFieldName} )
func AssertEqual(expected, actual interface{}) error
func AssertNoErrors(errs ...error) error
func AssertTrue(value bool, failureMessage string) error
func MakeBools() []bool
func MakeBoolsJSON(bools []bool) []byte
func MakeStrings() []string
func MakeStringsJSON(strings []string) []byte
func MakeStructsJSON(structs []ExampleStruct) []byte
func MakeWhitespaceOptions() map[string]string
Action is an arbitrary action that can be executed during a test. For readers, this normally consists of trying to read some type of value from the input, and asserting that no error occurred and that the expected value was found. For writers, it consists of trying to write something to the output.
All test assertions should return early on any non-nil error.
type Action func(c TestContext) error
type AnyValue struct { Kind ValueKind Bool bool Number float64 String string Array []Action Object []PropertyAction }
type ExampleStruct struct { StringField string `json:"string"` IntField int `json:"int"` OptBoolAsInterfaceField interface{} `json:"optBool"` }
func MakeStructs() []ExampleStruct
PropertyAction is used in the context of a JSON object value, describing a property name and the Action for reading or writing the property value.
type PropertyAction struct { Name string Action Action }
ReadErrorTestFactory is an interface for use with ReaderTestSuite to generate expectations about how errors are reported.
type ReadErrorTestFactory interface { ExpectEOFError(err error) error ExpectWrongTypeError(err error, expectedType ValueKind, variant ValueVariant, gotType ValueKind) error ExpectSyntaxError(err error) error }
ReaderTestSuite runs a standard set of tests against some implementation of JSON reading. This allows us to test both jreader.Reader and the low-level tokenizer jreader.tokenReader with many permutations of valid and invalid input.
type ReaderTestSuite struct { // ContextFactory must be provided by the caller to create an implementation of TestContext for // running a parsing test on the specified JSON input. This should include whatever parser // object will be used by the Actions that the ValueTestFactory creates. ContextFactory func(input []byte) TestContext // ValueTestFactory must be provided by the caller to create implementations of Action for // various JSON value types. ValueTestFactory ValueTestFactory // ReadErrorTestFactory must be provided by the caller to define expectations about error // reporting for invalid input. ReadErrorTestFactory ReadErrorTestFactory }
func (s ReaderTestSuite) Run(t *testing.T)
Run runs the test suite.
TestContext is an abstraction used by ReaderTestSuite and WriterTestSuite.
type TestContext interface { // JSONData returns either (for readers) the input data that was passed in when the TestContext // was created, or (for writers) all of the output that has been produced so far. JSONData() []byte }
type ValueKind int
const ( NullValue ValueKind = iota BoolValue ValueKind = iota NumberValue ValueKind = iota StringValue ValueKind = iota ArrayValue ValueKind = iota ObjectValue ValueKind = iota )
ValueTestFactory is an interface for producing specific reader/writer test actions. To test any reader or writer with ReaderTestSuite or WriterTestSuite, provide an implementation of this interface that performs the specified actions.
type ValueTestFactory interface { EOF() Action Value(value AnyValue, variant ValueVariant) Action Variants(value AnyValue) []ValueVariant }
ValueVariant is an optional identifier that ValueTestFactory can use to make the tests produce multiple variations of value tests. See ValueTestFactory.Variants.
type ValueVariant string
const ( // This variant means that the reader will try to consume a JSON value without regard to its type, // or the writer will write it as raw JSON data. UntypedVariant ValueVariant = "any:" // This variant means that the reader will try to recursively skip past a JSON value of any type. SkipValueVariant ValueVariant = "skip:" )
WriterTestSuite runs a standard set of tests against some implementation of JSON writing. This allows us to test both jwriter.Writer and the low-level JSON formatter jwriter.tokenWriter with many permutations of output data.
type WriterTestSuite struct { // ContextFactory must be provided by the caller to create an implementation of TestContext for // running a writing test on some set of JSON data. This should include whatever writer object // will be used by the Actions that the ValueTestFactory creates. ContextFactory func() TestContext // ValueTestFactory must be provided by the caller to create implementations of Action for // various JSON value types. ValueTestFactory ValueTestFactory // EncodeAsHex must be provided by the caller to define expectations about whether this writer // will use a \uNNNN escape sequence for the specified Unicode character. There is no single // correct answer for all implementations, since JSON allows characters to be escaped in // several ways and also allows unescaped multi-byte characters. EncodeAsHex func(rune) bool }
func (s WriterTestSuite) Run(t *testing.T)
Run runs the test suite.