Skip to content

Commit

Permalink
Enable doctests in CI again with test configuration
Browse files Browse the repository at this point in the history
`cfg(test)` is not set when running doc tests.
This enables `cfg(doctest)` everywhere where `cfg(test)` is set (to use the `ratatui::TestBackend`)
  • Loading branch information
Philipp-M committed Feb 18, 2024
1 parent 94a47d0 commit 06052ea
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 43 deletions.
40 changes: 20 additions & 20 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{
};
use anyhow::Result;

#[cfg(not(test))]
#[cfg(not(any(test, doctest)))]
use crossterm::{
cursor,
event::{DisableFocusChange, DisableMouseCapture, EnableFocusChange, EnableMouseCapture},
Expand All @@ -23,7 +23,7 @@ use crossterm::event::{poll, read, Event as CxEvent, KeyCode, KeyEvent};
use directories::ProjectDirs;
use ratatui::Terminal;

#[cfg(not(test))]
#[cfg(not(any(test, doctest)))]
use std::io::stdout;

use std::{
Expand All @@ -34,12 +34,12 @@ use std::{
use tracing_subscriber::{fmt::writer::MakeWriterExt, layer::SubscriberExt, Registry};
use xilem_core::{AsyncWake, Id, IdPath, MessageResult};

#[cfg(test)]
#[cfg(any(test, doctest))]
use ratatui::backend::TestBackend;

#[cfg(not(test))]
#[cfg(not(any(test, doctest)))]
use ratatui::backend::CrosstermBackend;
#[cfg(not(test))]
#[cfg(not(any(test, doctest)))]
use std::io::{Stdout, Write};

// TODO less hardcoding and cross-platform support
Expand All @@ -64,13 +64,13 @@ pub struct App<T: Send + 'static, V: View<T> + 'static> {
return_chan: tokio::sync::mpsc::Sender<(V, V::State, HashSet<Id>)>,
event_chan: tokio::sync::mpsc::Receiver<Event>,

#[cfg(test)]
#[cfg(any(test, doctest))]
event_tx: tokio::sync::mpsc::Sender<Event>,

#[cfg(test)]
#[cfg(any(test, doctest))]
terminal: Terminal<TestBackend>,

#[cfg(not(test))]
#[cfg(not(any(test, doctest)))]
terminal: Terminal<CrosstermBackend<Stdout>>,
time_since_last_rebuild: Instant,
size: Size,
Expand Down Expand Up @@ -137,10 +137,10 @@ enum UiState {

impl<T: Send + 'static, V: View<T> + 'static> App<T, V> {
pub fn new(data: T, app_logic: impl FnMut(&mut T) -> V + Send + 'static) -> Self {
#[cfg(not(test))]
#[cfg(not(any(test, doctest)))]
let backend = CrosstermBackend::new(stdout()); // TODO handle errors...

#[cfg(test)]
#[cfg(any(test, doctest))]
let backend = TestBackend::new(80, 40);

let terminal = Terminal::new(backend).unwrap();
Expand Down Expand Up @@ -253,7 +253,7 @@ impl<T: Send + 'static, V: View<T> + 'static> App<T, V> {
return_chan: return_tx,
event_chan: event_rx,

#[cfg(test)]
#[cfg(any(test, doctest))]
event_tx: event_tx.clone(),

terminal,
Expand Down Expand Up @@ -344,17 +344,17 @@ impl<T: Send + 'static, V: View<T> + 'static> App<T, V> {

root_pod.paint(&mut paint_cx);

#[cfg(not(test))]
#[cfg(not(any(test, doctest)))]
queue!(stdout(), BeginSynchronizedUpdate)?;

self.terminal.flush()?;

#[cfg(not(test))]
#[cfg(not(any(test, doctest)))]
execute!(stdout(), EndSynchronizedUpdate)?;

self.terminal.swap_buffers();

#[cfg(not(test))]
#[cfg(not(any(test, doctest)))]
self.terminal.backend_mut().flush()?;
}
Ok(())
Expand Down Expand Up @@ -411,7 +411,7 @@ impl<T: Send + 'static, V: View<T> + 'static> App<T, V> {

// TODO(zoechi): setup proper configuration for App
pub fn run_without_logging(mut self) -> Result<()> {
#[cfg(not(test))]
#[cfg(not(any(test, doctest)))]
self.init_terminal()?;

self.terminal.clear()?;
Expand Down Expand Up @@ -459,7 +459,7 @@ impl<T: Send + 'static, V: View<T> + 'static> App<T, V> {
Ok(())
}

#[cfg(not(test))]
#[cfg(not(any(test, doctest)))]
fn init_terminal(&self) -> Result<()> {
enable_raw_mode()?;
execute!(
Expand All @@ -472,7 +472,7 @@ impl<T: Send + 'static, V: View<T> + 'static> App<T, V> {
Ok(())
}

#[cfg(not(test))]
#[cfg(not(any(test, doctest)))]
fn restore_terminal(&self) -> Result<()> {
execute!(
stdout(),
Expand All @@ -485,12 +485,12 @@ impl<T: Send + 'static, V: View<T> + 'static> App<T, V> {
Ok(())
}

#[cfg(test)]
#[cfg(any(test, doctest))]
pub fn event_tx(&self) -> tokio::sync::mpsc::Sender<Event> {
self.event_tx.clone()
}

#[cfg(test)]
#[cfg(any(test, doctest))]
pub fn terminal_mut(&mut self) -> &mut Terminal<TestBackend> {
&mut self.terminal
}
Expand All @@ -499,7 +499,7 @@ impl<T: Send + 'static, V: View<T> + 'static> App<T, V> {
/// Restore the terminal no matter how the app exits
impl<T: Send + 'static, V: View<T> + 'static> Drop for App<T, V> {
fn drop(&mut self) {
#[cfg(not(test))]
#[cfg(not(any(test, doctest)))]
self.restore_terminal()
.unwrap_or_else(|e| eprint!("Restoring the terminal failed: {e}"));
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ pub use ratatui::style::{Color, Modifier, Style};
pub use view::*;
pub use widget::CatchMouseButton;

#[cfg(test)]
#[cfg(any(test, doctest))]
mod test_helper;
31 changes: 16 additions & 15 deletions src/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,15 @@ pub trait ViewExt<T, A>: View<T, A> + Sized {

// TODO these doctests should work without ignore, but need a TestBackend as terminal backend in CI
/// # Examples
/// ```ignore
/// use trui::*;
/// App::new((), move |()| {
/// v_stack((
/// "Text with border".border(BorderKind::Rounded),
/// "Other style".border((Borders::VERTICAL, Style::default().fg(Color::Red))),
/// ))
/// });
/// ```
/// # use trui::*;
/// # App::new((), move |()| {
/// v_stack((
/// "Rounded borders".border(BorderKind::Rounded),
/// "Red borders left and right".border((Borders::VERTICAL, Style::default().fg(Color::Red))),
/// "Top and right but without corners".border(Borders::TOP | Borders::RIGHT),
/// ))
/// # });
/// ```
fn border<S: Into<BorderStyle>>(self, style: S) -> Border<Self, T, A> {
let style = style.into();
Expand All @@ -82,13 +83,13 @@ pub trait ViewExt<T, A>: View<T, A> + Sized {
}

/// # Examples
/// ```ignore
/// use trui::*;
/// App::new((), move |()| {
/// "Fill half of the parent width/height"
/// .border(BorderKind::Rounded)
/// .fill_max_size(0.5)
/// });
/// ```
/// # use trui::*;
/// # App::new((), move |()| {
/// "Fill half of the parent width/height"
/// .border(BorderKind::Rounded)
/// .fill_max_size(0.5)
/// # });
/// ```
fn fill_max_size<P: Animatable<f64>, S: IntoFillMaxSizeStyle<P>>(
self,
Expand Down
4 changes: 2 additions & 2 deletions src/widget.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
mod border;
mod box_constraints;

#[cfg(not(test))]
#[cfg(not(any(test, doctest)))]
mod core;

#[cfg(test)]
#[cfg(any(test, doctest))]
pub(crate) mod core;

mod events;
Expand Down
10 changes: 5 additions & 5 deletions src/widget/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ use ratatui::Terminal;
use std::{any::Any, ops::DerefMut};
use xilem_core::{message, Id};

#[cfg(test)]
#[cfg(any(test, doctest))]
use ratatui::backend::TestBackend;

#[cfg(not(test))]
#[cfg(not(any(test, doctest)))]
use ratatui::backend::CrosstermBackend;

#[cfg(not(test))]
#[cfg(not(any(test, doctest)))]
use std::io::Stdout;

message!(Send);
Expand Down Expand Up @@ -55,10 +55,10 @@ pub struct PaintCx<'a, 'b> {
// TODO mutable? (xilem doesn't do this, but I think there are use cases for this...)
pub(crate) widget_state: &'a mut WidgetState,

#[cfg(test)]
#[cfg(any(test, doctest))]
pub(crate) terminal: &'a mut Terminal<TestBackend>,

#[cfg(not(test))]
#[cfg(not(any(test, doctest)))]
pub(crate) terminal: &'a mut Terminal<CrosstermBackend<Stdout>>,
// TODO this kinda feels hacky, find a better solution for this issue:
// this is currently necessary because the most outer styleable widget should be able to override the style for a styleable widget
Expand Down

0 comments on commit 06052ea

Please sign in to comment.