From 694f7a7e9f90cc435afcaade23b5908728d17ed2 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Wed, 24 Feb 2021 11:42:32 +0100 Subject: Add tests for apply_demorgan --- crates/ide_assists/src/handlers/apply_demorgan.rs | 67 +++++++++++++++++++++++ 1 file changed, 67 insertions(+) (limited to 'crates/ide_assists/src/handlers') diff --git a/crates/ide_assists/src/handlers/apply_demorgan.rs b/crates/ide_assists/src/handlers/apply_demorgan.rs index 3cd6699c3..6997ea048 100644 --- a/crates/ide_assists/src/handlers/apply_demorgan.rs +++ b/crates/ide_assists/src/handlers/apply_demorgan.rs @@ -61,10 +61,77 @@ fn opposite_logic_op(kind: ast::BinOp) -> Option<&'static str> { #[cfg(test)] mod tests { + use ide_db::helpers::FamousDefs; + use super::*; use crate::tests::{check_assist, check_assist_not_applicable}; + const ORDABLE_FIXTURE: &'static str = r" +//- /lib.rs deps:core crate:ordable +struct NonOrderable; +struct Orderable; +impl core::cmp::Ord for Orderable {} +"; + + fn check(ra_fixture_before: &str, ra_fixture_after: &str) { + let before = &format!( + "//- /main.rs crate:main deps:core,ordable\n{}\n{}{}", + ra_fixture_before, + FamousDefs::FIXTURE, + ORDABLE_FIXTURE + ); + check_assist(apply_demorgan, before, &format!("{}\n", ra_fixture_after)); + } + + #[test] + fn demorgan_handles_leq() { + check( + r"use ordable::Orderable; +fn f() { + Orderable < Orderable &&$0 Orderable <= Orderable +}", + r"use ordable::Orderable; +fn f() { + !(Orderable >= Orderable || Orderable > Orderable) +}", + ); + check( + r"use ordable::NonOrderable; +fn f() { + NonOrderable < NonOrderable &&$0 NonOrderable <= NonOrderable +}", + r"use ordable::NonOrderable; +fn f() { + !(!(NonOrderable < NonOrderable) || !(NonOrderable <= NonOrderable)) +}", + ); + } + + #[test] + fn demorgan_handles_geq() { + check( + r"use ordable::Orderable; +fn f() { + Orderable > Orderable &&$0 Orderable >= Orderable +}", + r"use ordable::Orderable; +fn f() { + !(Orderable <= Orderable || Orderable < Orderable) +}", + ); + check( + r"use ordable::NonOrderable; +fn f() { + Orderable > Orderable &&$0 Orderable >= Orderable +}", + r"use ordable::NonOrderable; +fn f() { + !(!(Orderable > Orderable) || !(Orderable >= Orderable)) +}", + ); + } + #[test] fn demorgan_turns_and_into_or() { check_assist(apply_demorgan, "fn f() { !x &&$0 !x }", "fn f() { !(x || x) }") -- cgit v1.2.3