Explorar o código

feat: add tests for sat sub (#1632)

tsk hai 4 días
pai
achega
9071389324
Modificáronse 2 ficheiros con 34 adicións e 0 borrados
  1. 7 0
      .cargo/mutants.toml
  2. 27 0
      crates/cashu/src/amount.rs

+ 7 - 0
.cargo/mutants.toml

@@ -76,4 +76,11 @@ exclude_re = [
     "crates/cashu/src/nuts/nut00/mod.rs:.*PaymentMethod::is_custom",
     "crates/cashu/src/nuts/nut00/mod.rs:.*PaymentMethod::is_bolt11",
     "crates/cashu/src/nuts/nut00/mod.rs:.*impl fmt::Display for KnownMethod.*fmt",
+
+    # Trivial PartialEq implementations - simple delegation for ergonomic comparisons
+    "crates/cashu/src/nuts/nut00/mod.rs:.*impl PartialEq<&str> for PaymentMethod.*eq",
+    "crates/cashu/src/nuts/nut00/mod.rs:.*impl PartialEq<str> for PaymentMethod.*eq",
+    "crates/cashu/src/nuts/nut00/mod.rs:.*impl PartialEq<PaymentMethod> for &str.*eq",
+    "crates/cashu/src/nuts/nut00/mod.rs:.*impl PartialEq<KnownMethod> for PaymentMethod.*eq",
+    "crates/cashu/src/nuts/nut00/mod.rs:.*impl PartialEq<PaymentMethod> for KnownMethod.*eq",
 ]

+ 27 - 0
crates/cashu/src/amount.rs

@@ -2124,4 +2124,31 @@ mod tests {
         let result = Amount::ZERO.saturating_sub(Amount::ZERO);
         assert_eq!(result, Amount::ZERO);
     }
+
+    /// Tests that saturating_sub handles the edge case where other == self + 1.
+    ///
+    /// This catches the mutation where `>` is replaced with `>=`.
+    /// If `other == self + 1`, then `other > self` is true, so we should return ZERO.
+    /// But if changed to `other >= self`, it would incorrectly return 1.
+    #[test]
+    fn test_saturating_sub_edge_case_other_greater_by_one() {
+        // When other is exactly one greater than self
+        let amount1 = Amount::from(10);
+        let amount2 = Amount::from(11); // amount2 = amount1 + 1
+        let result = amount1.saturating_sub(amount2);
+        // Should saturate to ZERO since other > self
+        assert_eq!(result, Amount::ZERO);
+
+        // Another case: subtracting 2 from 1
+        let amount1 = Amount::from(1);
+        let amount2 = Amount::from(2);
+        let result = amount1.saturating_sub(amount2);
+        assert_eq!(result, Amount::ZERO);
+
+        // Edge case with zero: subtracting 1 from 0
+        let amount1 = Amount::ZERO;
+        let amount2 = Amount::from(1);
+        let result = amount1.saturating_sub(amount2);
+        assert_eq!(result, Amount::ZERO);
+    }
 }