Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test Coverage #18 #53

Merged
merged 2 commits into from
Mar 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/block_pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,11 @@ mod tests {
let block_patterns = BlockPatterns::default();
assert_eq!(block_patterns.len(), 7);
}

#[test]
fn test_random() {
let block_patterns = BlockPatterns::default();

assert!(block_patterns.random().is_some());
}
}
11 changes: 11 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,12 @@ mod tests {
#[case::normal("Test Window", 800, 600)]
#[case::min_1("T", 1, 1)]
#[case::max_1000("12345678901234567890123456789012345678901234567890", 1000, 1000)]
#[should_panic]
#[case::panic_title("", 1, 1)]
#[should_panic]
#[case::panic_width("T", 0, 1)]
#[should_panic]
#[case::panic_height("T", 1, 0)]
fn test_window_config_creation(#[case] title: &str, #[case] width: u32, #[case] height: u32) {
let window_config = WindowConfig::new(title, width, height).unwrap();

Expand All @@ -132,4 +138,9 @@ mod tests {
expected_size
);
}

#[test]
fn test_create_window_config() {
create_window_config().unwrap();
}
}
2 changes: 2 additions & 0 deletions src/input.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use bevy::{app::AppExit, prelude::*};

#[cfg_attr(coverage_nightly, coverage(off))]
pub fn keyboard_input_system(
mut text: Query<&mut Text>,
keyboard_input: Res<ButtonInput<KeyCode>>,
Expand All @@ -13,6 +14,7 @@ pub fn keyboard_input_system(
}
}

#[cfg_attr(coverage_nightly, coverage(off))]
pub fn check_esc_to_exit(
keyboard_input: Res<ButtonInput<KeyCode>>,
mut app_exit_events: EventWriter<AppExit>,
Expand Down
4 changes: 4 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
#![cfg_attr(coverage_nightly, feature(coverage_attribute))]

pub mod block_pattern;
mod camera;
mod color_resources;
mod config;
mod constants;
mod input;
pub mod my_app;
mod position;
mod setup;
mod sprite;
mod text;
Expand All @@ -13,6 +16,7 @@ mod utils;
mod validator;
mod window;

#[cfg_attr(coverage_nightly, coverage(off))]
pub fn run() {
my_app::create_app().run();
}
3 changes: 3 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#![cfg_attr(coverage_nightly, feature(coverage_attribute))]

extern crate my_bevy_game;

#[cfg_attr(coverage_nightly, coverage(off))]
fn main() {
my_bevy_game::run();
}
6 changes: 6 additions & 0 deletions src/my_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ pub fn setup(app: &mut App) {
.add_systems(Update, sprite::spawn_block);
}

#[cfg_attr(coverage_nightly, coverage(off))]
pub fn create_app() -> App {
let mut app = App::new();
let plugin = DefaultPlugins.set(init_window_plugin());
Expand All @@ -48,6 +49,11 @@ mod tests {

use super::*;

#[test]
fn test_init_window_plugin() {
init_window_plugin();
}

#[test]
fn test_create_app() {
let mut app = App::new();
Expand Down
59 changes: 59 additions & 0 deletions src/position.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
use bevy::prelude::*;

use crate::utils;

#[derive(Component)]
pub struct Position {
x: i32,
y: i32,
}

impl Position {
pub fn new(x: i32, y: i32) -> Self {
Self { x, y }
}

pub fn vec2(&self) -> Vec2 {
Vec2::new(self.x as f32, self.y as f32)
}

pub fn translation(&self, origin: Vec2) -> Vec3 {
let position_vec2 = self.vec2();
let translation_vec2 = position_vec2 * utils::unit_size() + origin;

Vec3::new(translation_vec2.x, translation_vec2.y, 0.0)
}
}
#[cfg(test)]
mod tests {
use crate::utils::unit_size;

use super::*;

#[test]
fn test_position_new() {
let position = Position::new(10, 20);
assert_eq!(position.x, 10);
assert_eq!(position.y, 20);
}

#[test]
fn test_position_vec2() {
let position: Position = Position::new(10, 20);
let vec2 = position.vec2();
assert_eq!(vec2, Vec2::new(10.0, 20.0));
}

#[test]
fn test_position_translation() {
let position = Position::new(10, 20);
let origin = Vec2::new(-100.0, 5.0);
let translation = position.translation(origin);

let expected_x = unit_size().x * position.x as f32 + origin.x;
let expected_y = unit_size().y * position.y as f32 + origin.y;

let expected_translation = Vec3::new(expected_x, expected_y, 0.0);
assert_eq!(translation, expected_translation);
}
}
43 changes: 9 additions & 34 deletions src/sprite.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,6 @@
use bevy::prelude::*;

use crate::{block_pattern::BlockPatterns, constants, utils, window};

#[derive(Component)]
pub struct Position {
x: i32,
y: i32,
}

impl Position {
pub fn vec2(&self) -> Vec2 {
Vec2::new(self.x as f32, self.y as f32)
}
}

fn unit_size() -> Vec2 {
utils::vec2_from_tuple(constants::UNIT_SIZE)
}

pub fn calculate_translation(origin: Vec2, position: Vec2) -> Vec3 {
let translation_vec2 = position * utils::unit_size() + origin;

Vec3::new(translation_vec2.x, translation_vec2.y, 0.0)
}
use crate::{block_pattern::BlockPatterns, constants, position::Position, utils, window};

// pub fn spawn_sprite_at(commands: &mut Commands, windows: Query<&Window>, position: Vec2) {
// let sprite = Sprite {
Expand All @@ -44,6 +22,7 @@ pub fn calculate_translation(origin: Vec2, position: Vec2) -> Vec3 {
// commands.spawn(sprite_bundle);
// }

#[cfg_attr(coverage_nightly, coverage(off))]
fn spawn_block_element(commands: &mut Commands, position: Position, color: Color) {
commands
.spawn(SpriteBundle {
Expand All @@ -53,17 +32,15 @@ fn spawn_block_element(commands: &mut Commands, position: Position, color: Color
.insert(position);
}

#[cfg_attr(coverage_nightly, coverage(off))]
pub fn spawn_block(mut commands: Commands, block_patterns: Res<BlockPatterns>) {
let block = block_patterns.random().unwrap();

let initial_x = constants::UNIT_LENGTH.0 / 2;
let initial_y = constants::UNIT_LENGTH.1 - 4;
let initial_x = (constants::UNIT_LENGTH.0 / 2) as i32;
let initial_y = (constants::UNIT_LENGTH.1 - 4) as i32;

block.positions.iter().for_each(|(x, y)| {
let position = Position {
x: x + initial_x as i32,
y: y + initial_y as i32,
};
let position = Position::new(x + initial_x, y + initial_y);

spawn_block_element(&mut commands, position, block.color);
});
Expand All @@ -86,6 +63,7 @@ pub fn spawn_block(mut commands: Commands, block_patterns: Res<BlockPatterns>) {
// });
// }

#[cfg_attr(coverage_nightly, coverage(off))]
pub fn position_transform(
windows: Query<&Window>,
mut position_query: Query<(&Position, &mut Transform, &mut Sprite)>,
Expand All @@ -96,10 +74,7 @@ pub fn position_transform(
position_query
.iter_mut()
.for_each(|(position, mut transform, mut sprite)| {
let position_vec2 = position.vec2();
let translation = calculate_translation(origin, position_vec2);

transform.translation = translation;
sprite.custom_size = Some(unit_size());
transform.translation = position.translation(origin);
sprite.custom_size = Some(utils::unit_size());
});
}
1 change: 1 addition & 0 deletions src/text.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::text_resources::TextResources;
use bevy::prelude::*;

#[cfg_attr(coverage_nightly, coverage(off))]
pub fn spawn_initial_text(mut commands: Commands, asset_server: Res<AssetServer>) {
let text_resources = TextResources::new(&asset_server);
let initial_text = "テトリスみたいなものを作ってみてる\n「A」押したら文字が出る\n";
Expand Down
4 changes: 4 additions & 0 deletions src/text_resources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ pub struct TextResources {
}

impl TextResources {
#[cfg_attr(coverage_nightly, coverage(off))]
pub fn new(asset_server: &Res<AssetServer>) -> Self {
let font = asset_server.load("fonts/NotoSerifJP-Medium.otf");
Self { font }
}

#[cfg_attr(coverage_nightly, coverage(off))]
pub fn create_text_style(&self) -> TextStyle {
TextStyle {
font: self.font.clone(),
Expand All @@ -18,6 +20,7 @@ impl TextResources {
}
}

#[cfg_attr(coverage_nightly, coverage(off))]
pub fn create_text_bundle(&self, text: &str) -> TextBundle {
TextBundle::from_section(text, self.create_text_style()).with_style(Style {
align_self: AlignSelf::Center,
Expand All @@ -29,6 +32,7 @@ impl TextResources {
})
}

#[cfg_attr(coverage_nightly, coverage(off))]
pub fn spawn_text_entity(&self, commands: &mut Commands, text: &str) {
let text_bundle = self.create_text_bundle(text);
commands.spawn(text_bundle);
Expand Down
39 changes: 39 additions & 0 deletions src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,42 @@ pub fn calculate_origin(window: &Window) -> Vec2 {

half_unit_size - half_window_size
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_create_window() {
let title = "Test Window";
let width = 800;
let height = 600;
let window = create_window(title, width, height);

assert_eq!(window.title, title);
assert_eq!(window.width(), width as f32);
assert_eq!(window.height(), height as f32);
}

#[test]
fn test_init_window() {
let window = init_window();

assert!(!window.title.is_empty());
assert!(window.width() > 0.0);
assert!(window.height() > 0.0);
}

#[test]
fn test_calculate_origin() {
let window = Window {
title: "Test".to_string(),
resolution: (800.0, 600.0).into(),
..default()
};
let origin = calculate_origin(&window);

assert_eq!(origin.x, unit_size().x / 2.0 - 400.0);
assert_eq!(origin.y, unit_size().y / 2.0 - 300.0);
}
}
1 change: 1 addition & 0 deletions tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ fn init_game_app() -> App {

app.add_plugins(MinimalPlugins);
my_app::setup(&mut app);
// app.update();

app
}
Expand Down