// 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 . use crate::{Hook, Issue, Push}; use serde::Deserialize; /// Deserializes the payload into a struct fn parse(payload: &str) -> Result> 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(payload: &str) -> () where for<'a> Output: Into + Deserialize<'a> + PartialEq, { let res1: Result = parse(&payload); match res1 { Ok(_) => (), Err(err) => panic!("Error: {err:?}"), } let res2: Result = parse(&payload); match res2 { Ok(_) => (), Err(err) => panic!("Error: {err:?}"), } let hook1: Output = res1.unwrap(); let hook2: Hook = res2.unwrap(); assert_eq!(Into::::into(hook1), hook2); } #[test] fn push_payload() { let payload = std::fs::read_to_string("src/tests/push.json").unwrap(); let _ = parse_roundtrip::(&payload); } #[test] fn issue_new() { let payload = std::fs::read_to_string("src/tests/issue-new.json").unwrap(); let _ = parse_roundtrip::(&payload); } #[test] fn issue_with_assignee() { let payload = std::fs::read_to_string("src/tests/issue-with-assignee.json").unwrap(); let _ = parse_roundtrip::(&payload); } #[test] fn issue_edited() { let payload = std::fs::read_to_string("src/tests/issue-edited.json").unwrap(); let _ = parse_roundtrip::(&payload); }