lex-map_editor_std 0.2.0

standard types and parser for lex-map_editor
Documentation
use crate::types::*;
use crate::map::Map;

#[test]
fn block_from_str() {
	let block_data = "block,dirt,2,5,7";
	let block_made = Block {
		string_id: "dirt".to_owned(),
		u_id: 2,
		x: 5,
		y: 7
	};
	let block = Block::from_str(block_data);
	assert_eq!(block.unwrap(),block_made);
}

#[test]
fn block_to_string() {
	let block_data = "block,dirt,3,5,7".to_owned();
	let block_made = Block {
		string_id: "dirt".to_owned(),
		u_id: 3,
		x: 5,
		y: 7
	};
	assert_eq!(block_made.to_string(),block_data);
}


#[test]
fn object_from_str() {
	let object_data = "object,glass,89,5,7,1,3";
	let object_made = Object {
		string_id: "glass".to_owned(),
		u_id: 89,
		x: 5,
		y: 7,
		w: 1,
		h: 3
	};
	let object = Object::from_str(object_data);
	assert_eq!(object.unwrap(),object_made);
}

#[test]
fn object_to_string() {
	let object_data = "object,glass,89,5,7,1,3".to_owned();
	let object_made = Object {
		string_id: "glass".to_owned(),
		u_id: 89,
		x: 5,
		y: 7,
		w: 1,
		h: 3
	};
	assert_eq!(object_made.to_string(), object_data);
}


#[test]
fn entity_from_str() {
	let entity_data = "entity,bird,1,5,7,not a car";
	let entity_made = Entity {
		string_id: "bird".to_owned(),
		u_id: 1,
		x: 5,
		y: 7,
		extra: "not a car".to_owned()
	};
	let entity = Entity::from_str(entity_data);
	assert_eq!(entity.unwrap(),entity_made);
}

#[test]
fn entity_to_string() {
	let entity_data = "entity,bird,1,5,7,not a car".to_owned();
	let entity_made = Entity {
		string_id: "bird".to_owned(),
		u_id: 1,
		x: 5,
		y: 7,
		extra: "not a car".to_owned()
	};
	assert_eq!(entity_made.to_string(),entity_data);
}

#[test]
fn map_from_file() {
	let map_made = Map{
		version: env!("CARGO_PKG_VERSION").to_owned(),
		name: "test_file".to_owned(),
		unit:96,
		size_x: 3072,
		size_y: 512,
		blocks: vec![
			Block::from_str("block,dirt,6,6,4").unwrap(),
			Block::from_str("block,grass,2,2048,510").unwrap(),
			Block::from_str("block,stone,1,7,2").unwrap(),
		],
		objects: vec![
			Object::from_str("object,house,5,5,8,34,40").unwrap(),
			Object::from_str("object,glass_water_45,8,9,3,2,4").unwrap(),
		],
		entities: vec![
			Entity::from_str("entity,null,3,5,8,").unwrap(),
			Entity::from_str("entity,Chip,0,8,9,cargo run is overrated").unwrap(),
		]
	};
	let mut test_file = match std::fs::File::open("test_file"){
		Ok(f) => f,
		Err(e) => panic!("{}",e)
	};
	let map = Map::from_file(&mut test_file);
	assert_eq!(map.unwrap(), map_made);
}

#[test]
fn map_to_file() {
	let mut test_file = std::fs::File::open("test_file").unwrap();
	let mut test_file_out = std::fs::File::create("test_file_out").unwrap();
	let map = Map::from_file(&mut test_file).unwrap();
	map.to_file(&mut test_file_out).unwrap()
}

#[test]
fn map_from_to_file() {
	use std::io::Read;
	let mut test_file = std::fs::File::open("test_file").unwrap();
	let mut test_file_out = std::fs::File::create("test_file_out").unwrap();
	let map = Map::from_file(&mut test_file).unwrap();
	map.to_file(&mut test_file_out).unwrap();
	
	let mut test_file = std::fs::File::open("test_file").unwrap();
	let mut test_file_out = std::fs::File::open("test_file_out").unwrap();
	
	let mut test_file_string = String::new();
	let mut test_file_out_string = String::new();
	
	test_file.read_to_string(&mut test_file_string).unwrap();
	test_file_out.read_to_string(&mut test_file_out_string).unwrap();
	
	assert_eq!(test_file_string, test_file_out_string);
}