Add caret tests

This commit is contained in:
Adrian Wozniak 2019-12-22 21:32:44 +01:00
parent 7c64c20caf
commit eed6b72bb0
4 changed files with 109 additions and 134 deletions

View File

@ -123,63 +123,8 @@ pub fn move_caret_up(file_editor: &mut FileEditor) -> Option<TextCharacter> {
#[cfg(test)]
mod test_move_right {
use super::*;
use crate::renderer::managers::FontDetails;
use crate::renderer::managers::TextDetails;
use crate::renderer::renderer::Renderer;
use crate::tests::support;
use rider_config::config::Config;
use rider_config::ConfigHolder;
use sdl2::rect::Rect;
use sdl2::render::Texture;
use sdl2::ttf::Font;
use std::rc::Rc;
use std::sync::Arc;
use std::sync::RwLock;
struct RendererMock {
pub config: Arc<RwLock<Config>>,
}
impl RendererMock {
pub fn new(config: Arc<RwLock<Config>>) -> Self {
Self { config }
}
}
impl Renderer for RendererMock {
#[cfg_attr(tarpaulin, skip)]
fn load_font(&mut self, _details: FontDetails) -> Rc<Font> {
unimplemented!()
}
fn load_text_tex(
&mut self,
_details: &mut TextDetails,
_font_details: FontDetails,
) -> Result<Rc<Texture>, String> {
Err("skip render character".to_owned())
}
#[cfg_attr(tarpaulin, skip)]
fn load_image(&mut self, _path: String) -> Result<Rc<Texture>, String> {
unimplemented!()
}
}
impl ConfigHolder for RendererMock {
fn config(&self) -> &Arc<RwLock<Config>> {
&self.config
}
}
impl CharacterSizeManager for RendererMock {
fn load_character_size(&mut self, c: char) -> Rect {
match c {
'\n' => Rect::new(0, 0, 12, 13),
_ => Rect::new(0, 0, 14, 15),
}
}
}
use crate::tests::support::SimpleRendererMock;
#[test]
fn assert_move_with_no_file() {
@ -192,13 +137,12 @@ mod test_move_right {
#[test]
fn assert_move_caret_with_empty_file() {
let config = support::build_config();
let mut renderer = RendererMock::new(config.clone());
let mut renderer = SimpleRendererMock::new(config.clone());
let mut editor = FileEditor::new(config.clone());
let mut file = EditorFile::new("test.txt".to_owned(), "".to_owned(), config);
file.prepare_ui(&mut renderer);
editor.open_file(file);
editor.prepare_ui(&mut renderer);
editor.move_caret(MoveDirection::Left);
assert_eq!(move_caret_right(&mut editor).is_some(), false);
}
@ -206,13 +150,12 @@ mod test_move_right {
#[test]
fn assert_move_caret_with_filled_file() {
let config = support::build_config();
let mut renderer = RendererMock::new(config.clone());
let mut renderer = SimpleRendererMock::new(config.clone());
let mut editor = FileEditor::new(config.clone());
let mut file = EditorFile::new("test.txt".to_owned(), "hello".to_owned(), config);
file.prepare_ui(&mut renderer);
editor.open_file(file);
editor.prepare_ui(&mut renderer);
editor.move_caret(MoveDirection::Left);
assert_eq!(move_caret_right(&mut editor).is_some(), true);
}
@ -221,63 +164,8 @@ mod test_move_right {
#[cfg(test)]
mod test_move_left {
use super::*;
use crate::renderer::managers::FontDetails;
use crate::renderer::managers::TextDetails;
use crate::renderer::renderer::Renderer;
use crate::tests::support;
use rider_config::config::Config;
use rider_config::ConfigHolder;
use sdl2::rect::Rect;
use sdl2::render::Texture;
use sdl2::ttf::Font;
use std::rc::Rc;
use std::sync::Arc;
use std::sync::RwLock;
struct RendererMock {
pub config: Arc<RwLock<Config>>,
}
impl RendererMock {
pub fn new(config: Arc<RwLock<Config>>) -> Self {
Self { config }
}
}
impl Renderer for RendererMock {
#[cfg_attr(tarpaulin, skip)]
fn load_font(&mut self, _details: FontDetails) -> Rc<Font> {
unimplemented!()
}
#[cfg_attr(tarpaulin, skip)]
fn load_image(&mut self, _path: String) -> Result<Rc<Texture>, String> {
unimplemented!()
}
fn load_text_tex(
&mut self,
_details: &mut TextDetails,
_font_details: FontDetails,
) -> Result<Rc<Texture>, String> {
Err("skip render character".to_owned())
}
}
impl ConfigHolder for RendererMock {
fn config(&self) -> &Arc<RwLock<Config>> {
&self.config
}
}
impl CharacterSizeManager for RendererMock {
fn load_character_size(&mut self, c: char) -> Rect {
match c {
'\n' => Rect::new(0, 0, 12, 13),
_ => Rect::new(0, 0, 14, 15),
}
}
}
use crate::tests::support::SimpleRendererMock;
#[test]
fn assert_move_caret_without_file() {
@ -290,13 +178,12 @@ mod test_move_left {
#[test]
fn assert_move_caret_with_empty_file() {
let config = support::build_config();
let mut renderer = RendererMock::new(config.clone());
let mut renderer = SimpleRendererMock::new(config.clone());
let mut editor = FileEditor::new(config.clone());
let mut file = EditorFile::new("test.txt".to_owned(), "".to_owned(), config);
file.prepare_ui(&mut renderer);
editor.open_file(file);
editor.prepare_ui(&mut renderer);
editor.move_caret(MoveDirection::Right);
assert_eq!(move_caret_left(&mut editor).is_some(), false);
}
@ -304,14 +191,102 @@ mod test_move_left {
#[test]
fn assert_move_caret_with_filled_file() {
let config = support::build_config();
let mut renderer = RendererMock::new(config.clone());
let mut renderer = SimpleRendererMock::new(config.clone());
let mut editor = FileEditor::new(config.clone());
let mut file = EditorFile::new("test.txt".to_owned(), "hello".to_owned(), config);
file.prepare_ui(&mut renderer);
editor.open_file(file);
editor.prepare_ui(&mut renderer);
editor.move_caret(MoveDirection::Right);
editor.caret_mut().set_text_position(2);
editor.caret_mut().set_line_position(2);
editor.caret_mut().set_line_number(0);
assert_eq!(move_caret_left(&mut editor).is_some(), true);
assert_eq!(
move_caret_left(&mut editor),
editor.file().unwrap().get_character_at(1)
);
}
}
#[cfg(test)]
mod test_move_up {
use super::*;
use crate::tests::support;
use crate::tests::support::SimpleRendererMock;
#[test]
fn assert_move_caret_with_top_of_filled_file() {
let config = support::build_config();
let mut renderer = SimpleRendererMock::new(config.clone());
let mut editor = FileEditor::new(config.clone());
let mut file = EditorFile::new("test.txt".to_owned(), "he\nll\no".to_owned(), config);
file.prepare_ui(&mut renderer);
editor.open_file(file);
editor.prepare_ui(&mut renderer);
assert_eq!(move_caret_up(&mut editor), None);
}
#[test]
fn assert_move_caret_with_filled_file() {
let config = support::build_config();
let mut renderer = SimpleRendererMock::new(config.clone());
let mut editor = FileEditor::new(config.clone());
let mut file = EditorFile::new("test.txt".to_owned(), "he\nll\no".to_owned(), config);
file.prepare_ui(&mut renderer);
editor.open_file(file);
editor.prepare_ui(&mut renderer);
editor.caret_mut().set_line_position(1);
editor.caret_mut().set_line_number(1);
editor.caret_mut().set_text_position(3);
assert_eq!(
move_caret_up(&mut editor),
editor.file().unwrap().get_character_at(1)
);
assert_eq!(editor.caret().position(), &CaretPosition::new(1, 0, 1));
}
}
#[cfg(test)]
mod test_move_down {
use super::*;
use crate::tests::support;
use crate::tests::support::SimpleRendererMock;
#[test]
fn assert_move_caret_with_bottom_of_filled_file() {
let config = support::build_config();
let mut renderer = SimpleRendererMock::new(config.clone());
let mut editor = FileEditor::new(config.clone());
let mut file = EditorFile::new("test.txt".to_owned(), "he\nll\no".to_owned(), config);
file.prepare_ui(&mut renderer);
editor.open_file(file);
editor.prepare_ui(&mut renderer);
editor.caret_mut().set_line_position(1);
editor.caret_mut().set_line_number(2);
editor.caret_mut().set_text_position(6);
assert_eq!(move_caret_down(&mut editor), None);
}
#[test]
fn assert_move_caret_with_filled_file() {
let config = support::build_config();
let mut renderer = SimpleRendererMock::new(config.clone());
let mut editor = FileEditor::new(config.clone());
let mut file = EditorFile::new("test.txt".to_owned(), "he\nll\nod".to_owned(), config);
file.prepare_ui(&mut renderer);
editor.open_file(file);
editor.prepare_ui(&mut renderer);
editor.caret_mut().set_line_position(1);
editor.caret_mut().set_line_number(1);
editor.caret_mut().set_text_position(3);
assert_eq!(
move_caret_down(&mut editor),
editor.file().unwrap().get_character_at(6)
);
assert_eq!(editor.caret().position(), &CaretPosition::new(6, 2, 1));
}
}

View File

@ -2,7 +2,7 @@ use crate::app::UpdateResult as UR;
use crate::ui::*;
use rider_config::ConfigAccess;
use sdl2::rect::{Point, Rect};
use std::ops::Deref;
use std::ops::{Deref, DerefMut};
#[derive(Clone, Debug, PartialEq)]
pub struct Caret {
@ -59,7 +59,13 @@ impl Deref for Caret {
type Target = CaretPosition;
fn deref(&self) -> &<Self as Deref>::Target {
self.position()
&self.position
}
}
impl DerefMut for Caret {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.position
}
}

View File

@ -142,9 +142,6 @@ impl EditorFileToken {
R: Renderer + ConfigHolder,
C: CanvasAccess,
{
if self.token_type.is_new_line() {
return;
}
for text_character in self.characters.iter() {
text_character.render(canvas, renderer, context);
}

View File

@ -111,13 +111,13 @@ impl TextCharacter {
R: Renderer + ConfigHolder,
C: CanvasAccess,
{
if self.is_new_line() {
return;
}
let font_details: FontDetails = renderer.config().read().unwrap().editor_config().into();
let c = self.text_character.clone();
let c = match self.text_character.clone() {
'\n' => '¬',
' ' => '·',
c => c,
};
let mut details = TextDetails {
text: c.to_string(),
color: self.color.clone(),
@ -133,9 +133,6 @@ impl TextCharacter {
.render_image(texture, self.source.clone(), dest)
.unwrap();
}
// let c = Color::RGB(255, 0, 0);
// canvas.set_draw_color(c);
// canvas.draw_rect(dest.clone()).unwrap();
}
pub fn prepare_ui<'l, T>(&mut self, renderer: &mut T)