72 lines
2.2 KiB
Rust
72 lines
2.2 KiB
Rust
// Copyright (C) 2024-2099 The crate authors.
|
|
//
|
|
// This program is free software: you can redistribute it and/or modify it
|
|
// under the terms of the GNU Affero General Public License as published by the
|
|
// Free Software Foundation, either version 3 of the License, or (at your
|
|
// option) any later version.
|
|
//
|
|
// This program is distributed in the hope that it will be useful, but WITHOUT
|
|
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
|
|
// for more details.
|
|
//
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
use crate::{Hook, Issue, Push};
|
|
use serde::Deserialize;
|
|
|
|
/// Deserializes the payload into a struct
|
|
fn parse<Output>(payload: &str) -> Result<Output, serde_path_to_error::Error<serde_json::Error>>
|
|
where
|
|
for<'a> Output: Deserialize<'a>,
|
|
{
|
|
let mut json = serde_json::Deserializer::from_str(payload);
|
|
serde_path_to_error::deserialize(&mut json)
|
|
}
|
|
|
|
/// Tries to parse once into a specific type, then parses into Hook and ensure both match
|
|
fn parse_roundtrip<Output>(payload: &str) -> ()
|
|
where
|
|
for<'a> Output: Into<Hook> + Deserialize<'a> + PartialEq,
|
|
{
|
|
let res1: Result<Output, _> = parse(&payload);
|
|
match res1 {
|
|
Ok(_) => (),
|
|
Err(err) => panic!("Error: {err:?}"),
|
|
}
|
|
|
|
let res2: Result<Hook, _> = parse(&payload);
|
|
match res2 {
|
|
Ok(_) => (),
|
|
Err(err) => panic!("Error: {err:?}"),
|
|
}
|
|
|
|
let hook1: Output = res1.unwrap();
|
|
let hook2: Hook = res2.unwrap();
|
|
assert_eq!(Into::<Hook>::into(hook1), hook2);
|
|
}
|
|
|
|
#[test]
|
|
fn push_payload() {
|
|
let payload = std::fs::read_to_string("src/tests/push.json").unwrap();
|
|
let _ = parse_roundtrip::<Push>(&payload);
|
|
}
|
|
|
|
#[test]
|
|
fn issue_new() {
|
|
let payload = std::fs::read_to_string("src/tests/issue-new.json").unwrap();
|
|
let _ = parse_roundtrip::<Issue>(&payload);
|
|
}
|
|
|
|
#[test]
|
|
fn issue_with_assignee() {
|
|
let payload = std::fs::read_to_string("src/tests/issue-with-assignee.json").unwrap();
|
|
let _ = parse_roundtrip::<Issue>(&payload);
|
|
}
|
|
|
|
#[test]
|
|
fn issue_edited() {
|
|
let payload = std::fs::read_to_string("src/tests/issue-edited.json").unwrap();
|
|
let _ = parse_roundtrip::<Issue>(&payload);
|
|
}
|