|
| 1 | +package otf |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/real-logic/simple-binary-encoding/json" |
| 5 | + "github.com/real-logic/simple-binary-encoding/otf" |
| 6 | + "github.com/real-logic/simple-binary-encoding/otf/test/code_generation_test" |
| 7 | + "github.com/stretchr/testify/assert" |
| 8 | + "testing" |
| 9 | +) |
| 10 | + |
| 11 | +func TestJson(t *testing.T) { |
| 12 | + assert := assert.New(t) |
| 13 | + |
| 14 | + ir := otf.NewIrDecoder() |
| 15 | + if ir.DecodeFile(SchemaFilename) < 0 { |
| 16 | + t.Errorf("Error decoding %s", SchemaFilename) |
| 17 | + t.Fail() |
| 18 | + } |
| 19 | + printer, err := json.NewJsonPrinter(ir) |
| 20 | + assert.NoErrorf(err, "Error creating JsonPrinter: %v", err) |
| 21 | + assert.NotNil(printer) |
| 22 | + |
| 23 | + var car code_generation_test.Car |
| 24 | + buffer := make([]byte, MsgBufferCapacity) |
| 25 | + encodeCar(&car, buffer) |
| 26 | + |
| 27 | + headerDecoder, _ := otf.NewOtfHeaderDecoder(ir.Header()) |
| 28 | + bufferOffset := 0 |
| 29 | + schemaId, _ := headerDecoder.SchemaId(buffer[bufferOffset:]) |
| 30 | + |
| 31 | + if uint16(schemaId) != car.SbeSchemaId() { |
| 32 | + t.Errorf("Invalid schema id: %d", schemaId) |
| 33 | + } |
| 34 | + |
| 35 | + str, err := printer.PrintJson(buffer) |
| 36 | + assert.NoErrorf(err, "Error printing JSON: %v", err) |
| 37 | + expected := `{ |
| 38 | + "serialNumber": 1234, |
| 39 | + "modelYear": 2013, |
| 40 | + "available": "T", |
| 41 | + "code": "A", |
| 42 | + "someNumbers": [0, 1, 2, 3, 4], |
| 43 | + "vehicleCode": ["a", "b", "c", "d", "e", "f"], |
| 44 | + "extras": { "sunRoof": false, "sportsPack": false, "cruiseControl": false }, |
| 45 | + "discountedModel": "C", |
| 46 | + "Engine": |
| 47 | + { |
| 48 | + "capacity": 2000, |
| 49 | + "numCylinders": 4, |
| 50 | + "maxRpm": "9000", |
| 51 | + "manufacturerCode": ["1", "2", "3"], |
| 52 | + "fuel": "Petrol", |
| 53 | + "booster": |
| 54 | + { |
| 55 | + "BoostType": "NITROUS", |
| 56 | + "horsePower": 200 |
| 57 | + } |
| 58 | + }, |
| 59 | + "fuelFigures": [ |
| 60 | + { |
| 61 | + "speed": 30, |
| 62 | + "mpg": 35.900001525878906, |
| 63 | + "usageDescription": "Urban Cycle" |
| 64 | + }, |
| 65 | + { |
| 66 | + "speed": 55, |
| 67 | + "mpg": 49, |
| 68 | + "usageDescription": "Combined Cycle" |
| 69 | + }, |
| 70 | + { |
| 71 | + "speed": 75, |
| 72 | + "mpg": 40, |
| 73 | + "usageDescription": "Highway Cycle" |
| 74 | + }], |
| 75 | + "performanceFigures": [ |
| 76 | + { |
| 77 | + "octaneRating": 95, |
| 78 | + "acceleration": [ |
| 79 | + { |
| 80 | + "mph": 30, |
| 81 | + "seconds": 4 |
| 82 | + }, |
| 83 | + { |
| 84 | + "mph": 60, |
| 85 | + "seconds": 7.5 |
| 86 | + }, |
| 87 | + { |
| 88 | + "mph": 100, |
| 89 | + "seconds": 12.199999809265137 |
| 90 | + }] |
| 91 | + }, |
| 92 | + { |
| 93 | + "octaneRating": 99, |
| 94 | + "acceleration": [ |
| 95 | + { |
| 96 | + "mph": 30, |
| 97 | + "seconds": 3.799999952316284 |
| 98 | + }, |
| 99 | + { |
| 100 | + "mph": 60, |
| 101 | + "seconds": 7.099999904632568 |
| 102 | + }, |
| 103 | + { |
| 104 | + "mph": 100, |
| 105 | + "seconds": 11.800000190734863 |
| 106 | + }] |
| 107 | + }], |
| 108 | + "manufacturer": "Honda", |
| 109 | + "model": "Civic VTi", |
| 110 | + "activationCode": "abcdef", |
| 111 | + "color": "" |
| 112 | +}` |
| 113 | + assert.EqualValues(expected, str, "Message printer") |
| 114 | +} |
0 commit comments