sse-parse 0.1.0

Incremental, linear-time parser for Server-Sent Events byte streams, with a sans-IO core and a runtime-agnostic Stream adapter
Documentation
//! Conformance gate: every vector replays exactly, through both the
//! sans-IO parser and the stream adapter, under the declared chunking and
//! byte-at-a-time.

mod common;

use common::{load_vectors, observe_retry_updates, run_parser, run_stream, split_by_sizes};

#[test]
fn every_vector_replays_through_the_parser() {
    let vectors = load_vectors();
    for v in &vectors {
        let got = run_parser(&v.chunks);
        assert_eq!(got.events, v.events, "{}: events ({})", v.id, v.purpose);
        assert_eq!(
            got.last_event_id, v.final_last_event_id,
            "{}: final last event id",
            v.id
        );
        assert_eq!(
            got.retry,
            v.retry_updates.last().copied(),
            "{}: final retry",
            v.id
        );
        assert_eq!(
            got.retry_updates,
            v.retry_updates.len() as u64,
            "{}: retry update count",
            v.id
        );
    }
}

#[test]
fn every_vector_reports_retry_updates_in_order() {
    for v in &load_vectors() {
        let updates = observe_retry_updates(&v.stream_bytes());
        assert_eq!(updates, v.retry_updates, "{}: retry_updates", v.id);
    }
}

#[test]
fn every_vector_replays_byte_at_a_time() {
    for v in &load_vectors() {
        let chunks = split_by_sizes(&v.stream_bytes(), &[1]);
        let got = run_parser(&chunks);
        assert_eq!(got.events, v.events, "{}: events", v.id);
        assert_eq!(got.last_event_id, v.final_last_event_id, "{}", v.id);
    }
}

#[test]
fn every_vector_replays_through_the_stream_adapter() {
    for v in &load_vectors() {
        let got = run_stream(&v.chunks);
        assert_eq!(got.events, v.events, "{}: stream events", v.id);
        assert_eq!(got.last_event_id, v.final_last_event_id, "{}", v.id);
        assert_eq!(got.retry, v.retry_updates.last().copied(), "{}", v.id);
    }
}

#[test]
fn vector_file_is_the_frozen_vector_set() {
    let vectors = load_vectors();
    let ids: Vec<&str> = vectors.iter().map(|v| v.id.as_str()).collect();
    assert_eq!(ids[0], "v01-whatwg-stock-ticker");
    assert_eq!(ids[33], "v34-invalid-utf8-replacement");
    assert_eq!(ids[34], "g01-many-events-one-byte-chunks");
    assert_eq!(ids[35], "g02-single-huge-line-one-byte-chunks");
    assert_eq!(vectors.iter().filter(|v| v.is_generator).count(), 2);
}