test and lint
This commit is contained in:
		
							parent
							
								
									f28c9fee83
								
							
						
					
					
						commit
						b6d54d086e
					
				
							
								
								
									
										11
									
								
								src/main.rs
									
									
									
									
									
								
							
							
						
						
									
										11
									
								
								src/main.rs
									
									
									
									
									
								
							| @ -20,20 +20,18 @@ fn main() { | ||||
|         let code_generator = Arc::clone(&code_generator); | ||||
|         let rooms = Arc::clone(&rooms); | ||||
|         thread::spawn (move || { | ||||
|             let mut ws = tungstenite::accept(stream.unwrap()).unwrap(); | ||||
|             println!("New client!"); | ||||
|             let ws = tungstenite::accept(stream.unwrap()).unwrap(); | ||||
|             let mut ws = WebsocketWrapper::new(ws); | ||||
|             ws.send(msg!("HOSTJOIN")); | ||||
|             
 | ||||
|             ws.send(msg!("HOSTJOIN")); | ||||
|             let msg = ws.recv(); | ||||
|             match msg {                
 | ||||
|                 None => (), | ||||
| 
 | ||||
|                 Some(msg) => match msg.command.as_str() { | ||||
|                     "HOST" => { | ||||
|                         let code = code_generator.lock().unwrap().generate(); | ||||
|                         let mut room = Room::default(); | ||||
|                         let player = Player { name: "Guest".to_string() }; | ||||
|                         let player = Player {}; | ||||
|                         room.players.push(player); | ||||
|                         rooms.lock().unwrap().insert(code.clone(), room); | ||||
|                         ws.send(msg!("CODE", code)); | ||||
| @ -41,14 +39,15 @@ fn main() { | ||||
|                     _ => unimplemented!(), | ||||
|                 } | ||||
|             } | ||||
| 
 | ||||
|         }); | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| #[derive(Default)] | ||||
| struct Room { | ||||
|     players: Vec<Player>, | ||||
| } | ||||
| 
 | ||||
| struct Player { | ||||
|     name: String, | ||||
| } | ||||
|  | ||||
| @ -10,13 +10,9 @@ pub type Result<T> = std::result::Result<T, Error>; | ||||
| #[derive(PartialEq, Debug)] | ||||
| pub enum Error { | ||||
|     BadParse, | ||||
|     UnknownCommand, | ||||
| } | ||||
| 
 | ||||
| impl Message { | ||||
|     pub fn new(command: String, args: Vec<String>) -> Self { | ||||
| 	Message { command, args } | ||||
|     } | ||||
|     pub fn parse(text: String) -> Result<Message> { | ||||
| 	let re = regex::Regex::new(r"^([A-Z_]+):\s*(.*)").unwrap(); | ||||
| 	match re.captures(text.as_str()) { | ||||
| @ -49,10 +45,7 @@ macro_rules! msg { | ||||
|     ( $command:expr, $( $arg:expr ),*) => { | ||||
| 	{ | ||||
| 	    let command = $command.to_string(); | ||||
| 	    let mut args = Vec::new(); | ||||
| 	    $( | ||||
| 		args.push($arg.to_string()); | ||||
| 	    )* | ||||
| 	    let args = vec![$($arg.to_string()),*]; | ||||
| 	    Message { command, args } | ||||
| 	} | ||||
|     }; | ||||
| @ -61,8 +54,12 @@ macro_rules! msg { | ||||
| 
 | ||||
| impl std::fmt::Display for Message { | ||||
|     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> { | ||||
|         if self.args.is_empty() { | ||||
|             write!(f, "{}:", self.command) | ||||
|         } else { | ||||
| 	    write!(f, "{}: {}", self.command, self.args.as_slice().join(", ")) | ||||
|         } | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| 
 | ||||
| @ -72,19 +69,13 @@ mod test { | ||||
|     #[test] | ||||
|     fn test_parse() -> Result<()> { | ||||
| 	let text = "COMMAND: arg1, arg2"; | ||||
| 	let msg = Message::parse(text)?; | ||||
| 	assert_eq!(Message { | ||||
| 	    command: "COMMAND", | ||||
| 	    args: vec!["arg1", "arg2"], | ||||
| 	}, msg); | ||||
| 	let msg = Message::parse(text.to_string())?; | ||||
| 	assert_eq!(msg!("COMMAND", "arg1", "arg2"), msg); | ||||
| 	Ok(()) | ||||
|     } | ||||
|     #[test] | ||||
|     fn test_to_string() { | ||||
| 	let msg = Message { | ||||
| 	    command: "COMMAND", | ||||
| 	    args: vec!["arg1", "arg2"], | ||||
| 	}; | ||||
|         let msg = msg!("COMMAND", "arg1", "arg2"); | ||||
| 	assert_eq!(msg.to_string(), "COMMAND: arg1, arg2".to_string()); | ||||
|     } | ||||
| } | ||||
|  | ||||
| @ -21,12 +21,12 @@ impl WebsocketWrapper { | ||||
|             Ok(WsMessage::Text(text)) => match Message::parse(text) { | ||||
|                 Ok(msg) => Some(msg), | ||||
|                 Err(_) => { | ||||
|                     self.websocket.write_message(WsMessage::Text("ERROR: bad_format".to_string())); | ||||
|                     self.websocket.write_message(WsMessage::Text("ERROR: bad_format".to_string())).unwrap(); | ||||
|                     self.recv() | ||||
|                 } | ||||
|             } | ||||
|             _ => { | ||||
|                 self.websocket.write_message(WsMessage::Text("ERROR: bad_command".to_string())); | ||||
|                 self.websocket.write_message(WsMessage::Text("ERROR: bad_command".to_string())).unwrap(); | ||||
|                 self.recv() | ||||
|             } | ||||
|         } | ||||
|  | ||||
| @ -8,7 +8,7 @@ async def test(): | ||||
|         assert(response == "HOSTJOIN:") | ||||
|         await ws.send("HOST:") | ||||
|         response = await ws.recv() | ||||
|         assert(re.match(r"(\d|[a-f]){6}", response)) | ||||
|         assert(re.match(r"CODE: [a-f\d]{6}", response)) | ||||
|          | ||||
| asyncio.get_event_loop().run_until_complete(test()) | ||||
| print("All tests passed") | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user