| 1 |
use rstest_test::{ |
| 2 |
assert_in, assert_not_in, sanitize_name, testname, Project, Stringable, TestResults, |
| 3 |
}; |
| 4 |
|
| 5 |
use lazy_static::lazy_static; |
| 6 |
|
| 7 |
use rstest::rstest; |
| 8 |
use std::path::{Path, PathBuf}; |
| 9 |
use temp_testdir::TempDir; |
| 10 |
|
| 11 |
pub fn resources<O: AsRef<Path>>(name: O) -> PathBuf { |
| 12 |
Path::new("tests").join("resources").join(name) |
| 13 |
} |
| 14 |
|
| 15 |
fn create_prj(name: &str) -> Project { |
| 16 |
let prj = ROOT_PROJECT.subproject(name); |
| 17 |
prj.add_local_dependency("rstest_reuse"); |
| 18 |
prj.add_dependency("rstest", r#""*""#); |
| 19 |
prj |
| 20 |
} |
| 21 |
|
| 22 |
fn prj(res: impl AsRef<Path>) -> Project { |
| 23 |
let prj_name = sanitize_name(testname()); |
| 24 |
|
| 25 |
let prj = create_prj(&prj_name); |
| 26 |
prj.set_code_file(resources(res)) |
| 27 |
} |
| 28 |
|
| 29 |
fn run_test(res: impl AsRef<Path>) -> (std::process::Output, String) { |
| 30 |
let prj = prj(res); |
| 31 |
( |
| 32 |
prj.run_tests().unwrap(), |
| 33 |
prj.get_name().to_owned().to_string(), |
| 34 |
) |
| 35 |
} |
| 36 |
|
| 37 |
#[test] |
| 38 |
fn simple_example() { |
| 39 |
let (output, _) = run_test("simple_example.rs"); |
| 40 |
|
| 41 |
TestResults::new() |
| 42 |
.ok("it_works::case_1") |
| 43 |
.ok("it_works::case_2") |
| 44 |
.fail("it_fail::case_1") |
| 45 |
.fail("it_fail::case_2") |
| 46 |
.ok("it_fail_but_ok::case_1") |
| 47 |
.ok("it_fail_but_ok::case_2") |
| 48 |
.assert(output); |
| 49 |
} |
| 50 |
|
| 51 |
#[test] |
| 52 |
fn use_before_define() { |
| 53 |
let (output, _) = run_test("use_before_define.rs"); |
| 54 |
|
| 55 |
TestResults::new() |
| 56 |
.ok("it_works::case_1") |
| 57 |
.ok("it_works::case_2") |
| 58 |
.assert(output); |
| 59 |
} |
| 60 |
|
| 61 |
#[rstest] |
| 62 |
#[case::simple("simple_example.rs")] |
| 63 |
#[case::export_not_used("export_not_used.rs")] |
| 64 |
fn not_show_any_warning(#[case] path: &str) { |
| 65 |
let (output, _) = run_test(path); |
| 66 |
|
| 67 |
assert_not_in!(output.stderr.str(), "warning:"); |
| 68 |
} |
| 69 |
|
| 70 |
#[test] |
| 71 |
fn should_show_warning_if_not_used_template() { |
| 72 |
let (output, _) = run_test("not_used.rs"); |
| 73 |
|
| 74 |
assert_in!(output.stderr.str(), "warning:"); |
| 75 |
} |
| 76 |
|
| 77 |
#[test] |
| 78 |
fn in_mod() { |
| 79 |
let (output, _) = run_test("in_mod.rs"); |
| 80 |
|
| 81 |
TestResults::new() |
| 82 |
.ok("sub::it_works::case_1") |
| 83 |
.ok("sub::it_works::case_2") |
| 84 |
.fail("sub::it_fail::case_1") |
| 85 |
.fail("sub::it_fail::case_2") |
| 86 |
.assert(output); |
| 87 |
} |
| 88 |
|
| 89 |
#[test] |
| 90 |
fn import_from_mod() { |
| 91 |
let (output, _) = run_test("qualify_template_use.rs"); |
| 92 |
|
| 93 |
TestResults::new() |
| 94 |
.ok("user::it_works::case_1") |
| 95 |
.ok("user::it_works::case_2") |
| 96 |
.ok("qualify::it_works::case_1") |
| 97 |
.ok("qualify::it_works::case_2") |
| 98 |
.assert(output); |
| 99 |
} |
| 100 |
|
| 101 |
#[test] |
| 102 |
fn copy_case_attributes_from_template() { |
| 103 |
let (output, _) = run_test("copy_args_attributes_from_template.rs"); |
| 104 |
|
| 105 |
TestResults::new() |
| 106 |
.ok("cases::it_works::case_1") |
| 107 |
.ok("cases::it_works::case_2") |
| 108 |
.ok("cases::should_not_copy_attributes_if_already_present::case_1") |
| 109 |
.ok("cases::should_not_copy_attributes_if_already_present::case_2") |
| 110 |
.ok("cases::add_a_case::case_1") |
| 111 |
.ok("cases::add_a_case::case_2") |
| 112 |
.ok("cases::add_a_case::case_3_more") |
| 113 |
.ok_in("cases::add_values::case_1::_add_some_tests_1") |
| 114 |
.ok_in("cases::add_values::case_1::_add_some_tests_2") |
| 115 |
.ok_in("cases::add_values::case_1::_add_some_tests_3") |
| 116 |
.ok_in("cases::add_values::case_2::_add_some_tests_1") |
| 117 |
.ok_in("cases::add_values::case_2::_add_some_tests_2") |
| 118 |
.ok_in("cases::add_values::case_2::_add_some_tests_3") |
| 119 |
.ok("cases::should_copy_cases_also_from_underscored_attrs::case_1") |
| 120 |
.ok("cases::should_copy_cases_also_from_underscored_attrs::case_2") |
| 121 |
.ok_in("values::it_works::cases_1") |
| 122 |
.ok_in("values::it_works::cases_2") |
| 123 |
.ok_in("values::add_a_case::case_1_more::cases_1") |
| 124 |
.ok_in("values::add_a_case::case_1_more::cases_2") |
| 125 |
.ok_with("values::add_values::a_1", false, 2) |
| 126 |
.ok_with("values::add_values::a_2", false, 2) |
| 127 |
.ok_in("values::should_copy_values_also_from_underscored_attrs::_cases_1") |
| 128 |
.ok_in("values::should_copy_values_also_from_underscored_attrs::_cases_2") |
| 129 |
.assert(output); |
| 130 |
} |
| 131 |
|
| 132 |
#[test] |
| 133 |
fn deny_docs() { |
| 134 |
let (output, _) = run_test("deny_docs.rs"); |
| 135 |
|
| 136 |
TestResults::new() |
| 137 |
.ok("it_works::case_1") |
| 138 |
.ok("it_works::case_2") |
| 139 |
.assert(output); |
| 140 |
} |
| 141 |
|
| 142 |
#[test] |
| 143 |
fn enable_export_macros() { |
| 144 |
let (output, _) = run_test("export_template.rs"); |
| 145 |
|
| 146 |
TestResults::new() |
| 147 |
.ok("foo::bar::test::case_1") |
| 148 |
.ok("test_path::case_1") |
| 149 |
.ok("test_import::case_1") |
| 150 |
.assert(output); |
| 151 |
} |
| 152 |
|
| 153 |
#[test] |
| 154 |
fn use_same_name_for_more_templates() { |
| 155 |
let (output, _) = run_test("templates_with_same_name.rs"); |
| 156 |
|
| 157 |
TestResults::new() |
| 158 |
.ok("inner1::it_works::case_1") |
| 159 |
.ok("inner1::it_works::case_2") |
| 160 |
.ok("inner2::it_works::case_1") |
| 161 |
.ok("inner2::it_works::case_2") |
| 162 |
.assert(output); |
| 163 |
} |
| 164 |
|
| 165 |
#[test] |
| 166 |
fn no_local_macro_should_not_compile() { |
| 167 |
let (output, _) = run_test("no_local_macro_should_not_compile.rs"); |
| 168 |
|
| 169 |
assert!(!output.status.success()); |
| 170 |
} |
| 171 |
|
| 172 |
#[test] |
| 173 |
fn should_export_main_root() { |
| 174 |
// Add project with template |
| 175 |
let _prj_template = |
| 176 |
create_prj("export_template_root").set_code_file(resources("export_template_root.rs")); |
| 177 |
|
| 178 |
// Main test project that use template |
| 179 |
let prj = prj("import_template.rs"); |
| 180 |
prj.add_path_dependency("export_template_root", "../export_template_root"); |
| 181 |
|
| 182 |
let output = prj.run_tests().unwrap(); |
| 183 |
TestResults::new().ok("test::case_1").assert(output); |
| 184 |
} |
| 185 |
|
| 186 |
#[test] |
| 187 |
fn rstest_reuse_not_in_crate_root() { |
| 188 |
let (output, _) = run_test("rstest_reuse_not_in_crate_root.rs"); |
| 189 |
|
| 190 |
TestResults::new() |
| 191 |
.ok("test::case_1") |
| 192 |
.assert(output); |
| 193 |
} |
| 194 |
|
| 195 |
lazy_static! { |
| 196 |
static ref ROOT_DIR: TempDir = TempDir::new(std::env::temp_dir().join("rstest_reuse"), false); |
| 197 |
static ref ROOT_PROJECT: Project = Project::new(ROOT_DIR.as_ref()); |
| 198 |
} |