Use server_name! macro in tests

This commit is contained in:
Jonas Platte 2021-09-23 20:33:45 +02:00
parent fe5d31a9a6
commit 8b44f279c8
No known key found for this signature in database
GPG Key ID: CC154DE0E30B7C67
6 changed files with 34 additions and 43 deletions

View File

@ -37,9 +37,9 @@ impl DerefMut for DirectEventContent {
#[cfg(test)]
mod tests {
use std::{collections::BTreeMap, convert::TryFrom};
use std::collections::BTreeMap;
use ruma_identifiers::{RoomId, ServerName, UserId};
use ruma_identifiers::{server_name, RoomId, UserId};
use serde_json::{from_value as from_json_value, json, to_value as to_json_value};
use super::{DirectEvent, DirectEventContent};
@ -47,9 +47,9 @@ mod tests {
#[test]
fn serialization() {
let mut content = DirectEventContent(BTreeMap::new());
let server_name = <&ServerName>::try_from("ruma.io").unwrap();
let alice = UserId::new(server_name);
let room = vec![RoomId::new(server_name)];
let server_name = server_name!("ruma.io");
let alice = UserId::new(&server_name);
let room = vec![RoomId::new(&server_name)];
content.insert(alice.clone(), room.clone());
@ -66,9 +66,9 @@ mod tests {
#[test]
fn deserialization() {
let server_name = <&ServerName>::try_from("ruma.io").unwrap();
let alice = UserId::new(server_name);
let rooms = vec![RoomId::new(server_name), RoomId::new(server_name)];
let server_name = server_name!("ruma.io");
let alice = UserId::new(&server_name);
let rooms = vec![RoomId::new(&server_name), RoomId::new(&server_name)];
let json_data = json!({
"content": {

View File

@ -27,10 +27,10 @@ impl PinnedEventsEventContent {
#[cfg(test)]
mod tests {
use std::convert::{TryFrom, TryInto};
use std::convert::TryInto;
use ruma_common::MilliSecondsSinceUnixEpoch;
use ruma_identifiers::{EventId, RoomId, ServerName, UserId};
use ruma_identifiers::{server_name, EventId, RoomId, UserId};
use super::PinnedEventsEventContent;
use crate::{StateEvent, Unsigned};
@ -38,18 +38,18 @@ mod tests {
#[test]
fn serialization_deserialization() {
let mut content: PinnedEventsEventContent = PinnedEventsEventContent { pinned: Vec::new() };
let server_name = <&ServerName>::try_from("example.com").unwrap();
let server_name = server_name!("example.com");
content.pinned.push(EventId::new(server_name));
content.pinned.push(EventId::new(server_name));
content.pinned.push(EventId::new(&server_name));
content.pinned.push(EventId::new(&server_name));
let event = StateEvent {
content: content.clone(),
event_id: EventId::new(server_name),
event_id: EventId::new(&server_name),
origin_server_ts: MilliSecondsSinceUnixEpoch(1_432_804_485_886_u64.try_into().unwrap()),
prev_content: None,
room_id: RoomId::new(server_name),
sender: UserId::new(server_name),
room_id: RoomId::new(&server_name),
sender: UserId::new(&server_name),
state_key: "".into(),
unsigned: Unsigned::default(),
};

View File

@ -141,11 +141,9 @@ mod tests {
#[cfg(feature = "rand")]
#[test]
fn generate_random_valid_event_id() {
use crate::ServerName;
use crate::server_name;
let server_name =
<&ServerName>::try_from("example.com").expect("Failed to parse ServerName");
let event_id = EventId::new(server_name);
let event_id = EventId::new(&server_name!("example.com"));
let id_str = event_id.as_str();
assert!(id_str.starts_with('$'));

View File

@ -117,12 +117,10 @@ impl serde::Serialize for MxcUri {
#[cfg(test)]
mod tests {
use std::convert::TryFrom;
use std::convert::TryInto;
use ruma_identifiers_validation::error::MxcUriError;
use crate::ServerName;
use super::MxcUri;
#[test]
@ -132,10 +130,7 @@ mod tests {
assert!(mxc.is_valid());
assert_eq!(
mxc.parts(),
Ok((
<&ServerName>::try_from("127.0.0.1").expect("Failed to create ServerName"),
"asd32asdfasdsd"
))
Ok(("127.0.0.1".try_into().expect("Failed to create ServerName"), "asd32asdfasdsd"))
);
}
@ -172,7 +167,7 @@ mod tests {
assert!(mxc.is_valid());
assert_eq!(
mxc.parts(),
Ok((<&ServerName>::try_from("server").expect("Failed to create ServerName"), "1234id"))
Ok(("server".try_into().expect("Failed to create ServerName"), "1234id"))
);
}
}

View File

@ -96,11 +96,9 @@ mod tests {
#[cfg(feature = "rand")]
#[test]
fn generate_random_valid_room_id() {
use crate::ServerName;
use crate::server_name;
let server_name =
<&ServerName>::try_from("example.com").expect("Failed to parse ServerName");
let room_id = RoomId::new(server_name);
let room_id = RoomId::new(&server_name!("example.com"));
let id_str = room_id.as_str();
assert!(id_str.starts_with('!'));

View File

@ -115,7 +115,7 @@ mod tests {
use std::convert::TryFrom;
use super::UserId;
use crate::{Error, ServerName};
use crate::{server_name, Error};
#[test]
fn valid_user_id_from_str() {
@ -128,8 +128,8 @@ mod tests {
#[test]
fn parse_valid_user_id() {
let server_name = <&ServerName>::try_from("example.com").unwrap();
let user_id = UserId::parse_with_server_name("@carl:example.com", server_name)
let server_name = server_name!("example.com");
let user_id = UserId::parse_with_server_name("@carl:example.com", &server_name)
.expect("Failed to create UserId.");
assert_eq!(user_id.as_ref(), "@carl:example.com");
assert_eq!(user_id.localpart(), "carl");
@ -139,9 +139,9 @@ mod tests {
#[test]
fn parse_valid_user_id_parts() {
let server_name = <&ServerName>::try_from("example.com").unwrap();
let server_name = server_name!("example.com");
let user_id =
UserId::parse_with_server_name("carl", server_name).expect("Failed to create UserId.");
UserId::parse_with_server_name("carl", &server_name).expect("Failed to create UserId.");
assert_eq!(user_id.as_ref(), "@carl:example.com");
assert_eq!(user_id.localpart(), "carl");
assert_eq!(user_id.server_name(), "example.com");
@ -159,8 +159,8 @@ mod tests {
#[test]
fn parse_valid_historical_user_id() {
let server_name = <&ServerName>::try_from("example.com").unwrap();
let user_id = UserId::parse_with_server_name("@a%b[irc]:example.com", server_name)
let server_name = server_name!("example.com");
let user_id = UserId::parse_with_server_name("@a%b[irc]:example.com", &server_name)
.expect("Failed to create UserId.");
assert_eq!(user_id.as_ref(), "@a%b[irc]:example.com");
assert_eq!(user_id.localpart(), "a%b[irc]");
@ -170,8 +170,8 @@ mod tests {
#[test]
fn parse_valid_historical_user_id_parts() {
let server_name = <&ServerName>::try_from("example.com").unwrap();
let user_id = UserId::parse_with_server_name("a%b[irc]", server_name)
let server_name = server_name!("example.com");
let user_id = UserId::parse_with_server_name("a%b[irc]", &server_name)
.expect("Failed to create UserId.");
assert_eq!(user_id.as_ref(), "@a%b[irc]:example.com");
assert_eq!(user_id.localpart(), "a%b[irc]");
@ -189,8 +189,8 @@ mod tests {
#[cfg(feature = "rand")]
#[test]
fn generate_random_valid_user_id() {
let server_name = <&ServerName>::try_from("example.com").unwrap();
let user_id = UserId::new(server_name);
let server_name = server_name!("example.com");
let user_id = UserId::new(&server_name);
assert_eq!(user_id.localpart().len(), 12);
assert_eq!(user_id.server_name(), "example.com");