forgejo-hooks: Add failing tests
Signed-off-by: pep <pep@bouah.net>
This commit is contained in:
parent
979dc50644
commit
25eeacde7c
4 changed files with 285 additions and 14 deletions
54
forgejo-hooks/src/tests/mod.rs
Normal file
54
forgejo-hooks/src/tests/mod.rs
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
// 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, 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);
|
||||
}
|
||||
Loading…
Reference in a new issue