Search in sources :

Example 1 with OffsetSpan

use of com.amazon.ion.OffsetSpan in project ion-java by amzn.

the class RawValueSpanReaderTest method generateSpan.

private SpanTester generateSpan(Object expected, IonType type) {
    // This span includes the TID and length bytes
    Span span = seekableReader.currentSpan();
    // This gets just the value bytes
    byte[] expectedBytes = valueBytes((OffsetSpan) spanProvider.valueSpan());
    return new SpanTester(expected, type, expectedBytes, span);
}
Also used : OffsetSpan(com.amazon.ion.OffsetSpan) Span(com.amazon.ion.Span)

Example 2 with OffsetSpan

use of com.amazon.ion.OffsetSpan in project ion-java by amzn.

the class RawValueSpanReaderTest method assertSpan.

private void assertSpan(SpanTester tester) throws Exception {
    IonType type = tester.expectedType;
    // seeks back to the given span
    seekableReader.hoist(tester.span);
    assertEquals(type, reader.next());
    Object expected = tester.expected;
    if (reader.isNullValue()) {
        assertEquals(expected, null);
    } else {
        OffsetSpan valueSpan = (OffsetSpan) spanProvider.valueSpan();
        switch(type) {
            case BOOL:
                assertEquals(expected, reader.booleanValue());
                break;
            case INT:
                assertEquals(expected, reader.bigIntegerValue());
                break;
            case FLOAT:
                assertEquals(expected, reader.doubleValue());
                break;
            case DECIMAL:
                assertEquals(expected, reader.bigDecimalValue());
                break;
            case TIMESTAMP:
                assertEquals(expected, reader.timestampValue());
                break;
            case STRING:
                // A common use case will be to pass strings along without
                // decoding. This tests that case.
                assertArrayEquals(((String) expected).getBytes("UTF-8"), valueBytes(valueSpan));
                assertEquals(expected, reader.stringValue());
                break;
            case SYMBOL:
                // SymbolTokenImpl does not override .equals
                SymbolToken expectedToken = (SymbolToken) expected;
                SymbolToken actualToken = reader.symbolValue();
                assertEquals(expectedToken.getSid(), actualToken.getSid());
                assertEquals(expectedToken.getText(), actualToken.getText());
                break;
            case BLOB:
            case CLOB:
                assertArrayEquals((byte[]) expected, reader.newBytes());
                break;
            case STRUCT:
            case LIST:
            case SEXP:
                reader.stepIn();
                if (reader.next() != null) {
                    // The start position of the container's value span should
                    // be the same as the start position of its first element's
                    // seekable span.
                    long expectedValueStart = valueSpan.getStartOffset();
                    long expectedValueEnd = ((OffsetSpan) seekableReader.currentSpan()).getStartOffset();
                    // skips any nop pad to get at the actual value start
                    expectedValueStart += countNopPad((int) expectedValueStart);
                    if (reader.isInStruct()) {
                        // In structs, however, value spans will start before
                        // the first value's field name SID (VarUInt - 7
                        // bits per byte, hence division by 0x80).
                        expectedValueStart += (reader.getFieldNameSymbol().getSid() / 0x80) + 1;
                    }
                    assertEquals(expectedValueStart, expectedValueEnd);
                }
                reader.stepOut();
                break;
            default:
                throw new IllegalStateException("unexpected type: " + type);
        }
        assertArrayEquals(tester.expectedBytes, valueBytes(valueSpan));
        // All spans over the same value, no matter where they started, should finish at
        // the same position.
        assertEquals(((OffsetSpan) tester.span).getFinishOffset(), valueSpan.getFinishOffset());
    }
}
Also used : IonType(com.amazon.ion.IonType) SymbolToken(com.amazon.ion.SymbolToken) OffsetSpan(com.amazon.ion.OffsetSpan)

Example 3 with OffsetSpan

use of com.amazon.ion.OffsetSpan in project ion-java by amzn.

the class ReaderFacetTestCase method checkCurrentSpan.

protected void checkCurrentSpan(long start, long finish) {
    Span span = sp.currentSpan();
    OffsetSpan offsets = assumeFacet(OffsetSpan.class, span);
    checkSpan(start, finish, offsets);
    offsets = currentSpan(OffsetSpan.class, in);
    checkSpan(start, finish, offsets);
}
Also used : OffsetSpan(com.amazon.ion.OffsetSpan) TextSpan(com.amazon.ion.TextSpan) Spans.currentSpan(com.amazon.ion.util.Spans.currentSpan) Span(com.amazon.ion.Span) OffsetSpan(com.amazon.ion.OffsetSpan)

Aggregations

OffsetSpan (com.amazon.ion.OffsetSpan)3 Span (com.amazon.ion.Span)2 IonType (com.amazon.ion.IonType)1 SymbolToken (com.amazon.ion.SymbolToken)1 TextSpan (com.amazon.ion.TextSpan)1 Spans.currentSpan (com.amazon.ion.util.Spans.currentSpan)1