Welcome to mirror list, hosted at ThFree Co, Russian Federation.

json.rs « tests « json « text - gitlab.freedesktop.org/gstreamer/gst-plugins-rs.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1bfe702a0e95ce6e6be51d4cf31fb4a529c961d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Copyright (C) 2020 Mathieu Duponchelle <mathieu@centricular.com>
//
// This Source Code Form is subject to the terms of the Mozilla Public License, v2.0.
// If a copy of the MPL was not distributed with this file, You can obtain one at
// <https://mozilla.org/MPL/2.0/>.
//
// SPDX-License-Identifier: MPL-2.0

#![allow(clippy::single_match)]

fn init() {
    use std::sync::Once;
    static INIT: Once = Once::new();

    INIT.call_once(|| {
        gst::init().unwrap();
        gstrsjson::plugin_register_static().expect("json test");
    });
}

#[test]
fn test_enc() {
    init();

    let input = "{\"foo\":42}";

    let mut h = gst_check::Harness::new("jsongstenc");

    h.set_src_caps_str("application/x-json, format=test");

    let buf = {
        let mut buf = gst::Buffer::from_mut_slice(Vec::from(input));
        let buf_ref = buf.get_mut().unwrap();
        buf_ref.set_pts(gst::ClockTime::from_seconds(0));
        buf_ref.set_duration(gst::ClockTime::from_seconds(2));
        buf
    };

    assert_eq!(h.push(buf), Ok(gst::FlowSuccess::Ok));

    let buf = h.pull().expect("Couldn't pull buffer");
    let map = buf.map_readable().expect("Couldn't map buffer readable");
    assert_eq!(
        std::str::from_utf8(map.as_ref()),
        Ok("{\"Header\":{\"format\":\"test\"}}\n"),
    );

    let buf = h.pull().expect("Couldn't pull buffer");
    assert_eq!(buf.pts(), Some(gst::ClockTime::ZERO));
    assert_eq!(buf.duration(), Some(2 * gst::ClockTime::SECOND));
    let map = buf.map_readable().expect("Couldn't map buffer readable");
    assert_eq!(
        std::str::from_utf8(map.as_ref()),
        Ok("{\"Buffer\":{\"pts\":0,\"duration\":2000000000,\"data\":{\"foo\":42}}}\n")
    );
}

#[test]
fn test_parse() {
    init();

    let input = [
        "{\"Header\":{\"format\":\"test\"}}\n",
        "{\"Buffer\":{\"pts\":0,\"duration\":2000000000,\"data\":{\"foo\":42}}}\n",
    ];

    let mut h = gst_check::Harness::new("jsongstparse");

    h.set_src_caps_str("text/x-raw");

    for input in &input {
        let buf = gst::Buffer::from_mut_slice(Vec::from(&input[..]));
        assert_eq!(h.push(buf), Ok(gst::FlowSuccess::Ok));
    }

    while h.events_in_queue() > 0 {
        use gst::EventView;

        let ev = h.pull_event().unwrap();

        match ev.view() {
            EventView::Caps(ev) => {
                assert!(ev.caps().is_strictly_equal(
                    &gst::Caps::builder("application/x-json")
                        .field("format", "test")
                        .build()
                ));
            }
            _ => (),
        }
    }

    let buf = h.pull().expect("Couldn't pull buffer");
    let map = buf.map_readable().expect("Couldn't map buffer readable");
    assert_eq!(buf.pts(), Some(gst::ClockTime::ZERO));
    assert_eq!(buf.duration(), Some(2 * gst::ClockTime::SECOND));
    assert_eq!(std::str::from_utf8(map.as_ref()), Ok("{\"foo\":42}"));
}