1 package commontest 2 3 // TestContext is an abstraction used by ReaderTestSuite and WriterTestSuite. 4 type TestContext interface { 5 // JSONData returns either (for readers) the input data that was passed in when the TestContext 6 // was created, or (for writers) all of the output that has been produced so far. 7 JSONData() []byte 8 } 9 10 // Action is an arbitrary action that can be executed during a test. For readers, this normally 11 // consists of trying to read some type of value from the input, and asserting that no error occurred 12 // and that the expected value was found. For writers, it consists of trying to write something to 13 // the output. 14 // 15 // All test assertions should return early on any non-nil error. 16 type Action func(c TestContext) error 17 18 // PropertyAction is used in the context of a JSON object value, describing a property name and the 19 // Action for reading or writing the property value. 20 type PropertyAction struct { 21 Name string 22 Action Action 23 } 24 25 // ValueVariant is an optional identifier that ValueTestFactory can use to make the tests produce 26 // multiple variations of value tests. See ValueTestFactory.Variants. 27 type ValueVariant string 28 29 const ( 30 // This variant means that the reader will try to consume a JSON value without regard to its type, 31 // or the writer will write it as raw JSON data. 32 UntypedVariant ValueVariant = "any:" 33 34 // This variant means that the reader will try to recursively skip past a JSON value of any type. 35 SkipValueVariant ValueVariant = "skip:" 36 ) 37 38 // ValueTestFactory is an interface for producing specific reader/writer test actions. To test any 39 // reader or writer with ReaderTestSuite or WriterTestSuite, provide an implementation of this 40 // interface that performs the specified actions. 41 type ValueTestFactory interface { 42 EOF() Action 43 Value(value AnyValue, variant ValueVariant) Action 44 Variants(value AnyValue) []ValueVariant 45 } 46 47 // ReadErrorTestFactory is an interface for use with ReaderTestSuite to generate expectations about 48 // how errors are reported. 49 type ReadErrorTestFactory interface { 50 ExpectEOFError(err error) error 51 ExpectWrongTypeError(err error, expectedType ValueKind, variant ValueVariant, gotType ValueKind) error 52 ExpectSyntaxError(err error) error 53 } 54 55 type ValueKind int 56 57 const ( 58 NullValue ValueKind = iota 59 BoolValue ValueKind = iota 60 NumberValue ValueKind = iota 61 StringValue ValueKind = iota 62 ArrayValue ValueKind = iota 63 ObjectValue ValueKind = iota 64 ) 65 66 type AnyValue struct { 67 Kind ValueKind 68 Bool bool 69 Number float64 70 String string 71 Array []Action 72 Object []PropertyAction 73 } 74