This repository was archived by the owner on Apr 3, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 9
This repository was archived by the owner on Apr 3, 2024. It is now read-only.
[Support] Unable to init GTK with unit tests #52
Copy link
Copy link
Open
Description
Description
I am trying to add unit tests to my project and my tests keep failing.
Code
#[cfg(test)]
mod tests {
use gtk::Builder;
use libmystudio::notebook::cache::NotebookTabCache;
use tempfile::tempdir;
use crate::ui::notebook::{
editor::{get_editor_by_path, get_editor_instance},
nbmain::{create_notebook_tab, get_notebook},
};
use super::set_text_on_editor;
fn before_each() -> (Builder, tempfile::TempDir, std::path::PathBuf) {
gtk_test::gtk::init().unwrap();
// Load UI from glade file
let glade_src = include_str!("../../../res/ui/main_window.glade");
let builder: Builder = Builder::from_string(glade_src);
// Init Notebook UI for testing
crate::ui::notebook::init(&builder);
// Create mock file
let root_dir = tempdir();
assert!(root_dir.is_ok());
let root_dir = root_dir.unwrap();
let temp_file = root_dir.path().join("index.js");
(builder, root_dir, temp_file)
}
#[test]
fn get_editor_by_path_test() {
// init Gtk
let (builder, root_dir, temp_file) = before_each();
// mock Notebook page
let notebook = get_notebook().unwrap();
let mock_editor = get_editor_instance();
let tab_position = create_notebook_tab(notebook, mock_editor, "title", "icon_name");
// mock Notebook cache entry
let mock_cache = NotebookTabCache {
file_path: temp_file.to_str().unwrap().into(),
icon_name: "file".into(),
position: tab_position,
};
NotebookTabCache::insert(mock_cache.clone());
// Verify if editor is available
let editor = get_editor_by_path(mock_cache.file_path);
assert!(editor.is_some());
}
#[test]
fn set_text_on_editor_test() {
// Init
let (_builder, _root_dir, temp_file_pathbuf) = before_each();
let temp_file_str = temp_file_pathbuf.to_str().map(|f| f.to_string());
let mock_editor = get_editor_instance();
// all params are provided
set_text_on_editor(Some(mock_editor), temp_file_str.clone(), Some("console.log(1);".into()));
// editor is None
// set_text_on_editor(None, temp_file_str, Some("console.log(1);".into()));
}
}
Actual Output
➜ mystudio-ide git:(unit-tests) ✗ cargo test -p mystudio-ide --bin mystudio-ide
Compiling mystudio-ide v0.1.3 (/home/suryateja/Projects/mystudio-ide/mystudio-ide)
warning: unused variable: `builder`
--> mystudio-ide/src/ui/notebook/editor.rs:151:14
|
151 | let (builder, root_dir, temp_file) = before_each();
| ^^^^^^^ help: if this is intentional, prefix it with an underscore: `_builder`
|
= note: `#[warn(unused_variables)]` on by default
warning: unused variable: `root_dir`
--> mystudio-ide/src/ui/notebook/editor.rs:151:23
|
151 | let (builder, root_dir, temp_file) = before_each();
| ^^^^^^^^ help: if this is intentional, prefix it with an underscore: `_root_dir`
warning: `mystudio-ide` (bin "mystudio-ide" test) generated 2 warnings
Finished test [unoptimized + debuginfo] target(s) in 8.29s
Running unittests src/main.rs (target/debug/deps/mystudio_ide-b03e5f831783b6a6)
running 3 tests
test ui::action_row::handler::tests::save_file_changes_test ... ok
test ui::notebook::editor::tests::get_editor_by_path_test ... FAILED
test ui::notebook::editor::tests::set_text_on_editor_test ... FAILED
failures:
---- ui::notebook::editor::tests::get_editor_by_path_test stdout ----
thread 'ui::notebook::editor::tests::get_editor_by_path_test' panicked at 'called `Result::unwrap()` on an `Err` value: BoolError { message: "Failed to acquire default main context", filename: "/home/suryateja/.cargo/registry/src/github.com-1ecc6299db9ec823/gtk-0.15.4/src/rt.rs", function: "gtk::rt", line: 129 }', mystudio-ide/src/ui/notebook/editor.rs:128:31
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
---- ui::notebook::editor::tests::set_text_on_editor_test stdout ----
thread 'ui::notebook::editor::tests::set_text_on_editor_test' panicked at 'called `Option::unwrap()` on a `None` value', mystudio-ide/src/ui/statusbar/line_indicator.rs:162:36
failures:
ui::notebook::editor::tests::get_editor_by_path_test
ui::notebook::editor::tests::set_text_on_editor_test
test result: FAILED. 1 passed; 2 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.15s
error: test failed, to rerun pass '-p mystudio-ide --bin mystudio-ide'
Please advise.
Metadata
Metadata
Assignees
Labels
No labels