Browse Source

Mut test dev 16 (#1428)

* chore: mutation tests

* chore: ignore simple fns mutation tests
tsk 4 weeks ago
parent
commit
e7dc4a384e
2 changed files with 93 additions and 0 deletions
  1. 10 0
      .cargo/mutants.toml
  2. 83 0
      crates/cashu/src/amount.rs

+ 10 - 0
.cargo/mutants.toml

@@ -40,4 +40,14 @@ exclude_re = [
     # Trivial getters - not worth testing
     "FeeAndAmounts::fee",
     "FeeAndAmounts::amounts",
+
+    # Trivial trait implementations - unrealistic mutations
+    "crates/cashu/src/amount.rs.*impl From<&u64> for Amount.*from",
+    "crates/cashu/src/amount.rs.*impl AsRef<u64> for Amount.*as_ref",
+    "crates/cashu/src/amount.rs.*impl std::ops::Mul for Amount.*mul",
+    "crates/cashu/src/amount.rs.*impl std::ops::Div for Amount.*div",
+    "crates/cashu/src/amount.rs.*impl From<Amount> for u64.*from",
+    "crates/cashu/src/amount.rs.*impl Default for Amount",
+    "crates/cashu/src/amount.rs.*impl Default for &Amount",
+    "crates/cashu/src/amount.rs.*impl.*Display for Amount.*fmt",
 ]

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

@@ -1182,4 +1182,87 @@ mod tests {
         assert!(below_max.to_i64().is_some());
         assert_eq!(below_max.to_i64().unwrap(), i64::MAX - 1);
     }
+
+    /// Tests Amount::from_i64 returns the correct value.
+    ///
+    /// Mutant testing: Catches mutations that:
+    /// - Replace return with None
+    /// - Replace return with Some(Default::default())
+    /// - Replace >= with < in the condition
+    #[test]
+    fn test_amount_from_i64() {
+        // Positive value - should return Some with correct value
+        let result = Amount::from_i64(100);
+        assert!(result.is_some());
+        assert_eq!(result.unwrap(), Amount::from(100));
+        assert_ne!(result, None);
+        assert_ne!(result, Some(Amount::ZERO));
+
+        // Zero - boundary case for >= vs <
+        // If >= becomes <, this would return None instead of Some
+        let result = Amount::from_i64(0);
+        assert!(result.is_some());
+        assert_eq!(result.unwrap(), Amount::ZERO);
+
+        // Negative value - should return None
+        let result = Amount::from_i64(-1);
+        assert!(result.is_none());
+
+        let result = Amount::from_i64(-100);
+        assert!(result.is_none());
+
+        // Large positive value
+        let result = Amount::from_i64(i64::MAX);
+        assert!(result.is_some());
+        assert_eq!(result.unwrap(), Amount::from(i64::MAX as u64));
+        assert_ne!(result, Some(Amount::ZERO));
+
+        // Value 1 - catches Some(Default::default()) mutation
+        let result = Amount::from_i64(1);
+        assert!(result.is_some());
+        assert_eq!(result.unwrap(), Amount::ONE);
+        assert_ne!(result, Some(Amount::ZERO));
+    }
+
+    /// Tests AddAssign actually modifies the value.
+    ///
+    /// Mutant testing: Catches mutation that replaces add_assign with ().
+    #[test]
+    fn test_add_assign() {
+        let mut amount = Amount::from(100);
+        amount += Amount::from(50);
+        assert_eq!(amount, Amount::from(150));
+        assert_ne!(amount, Amount::from(100)); // Should have changed
+
+        let mut amount = Amount::from(1);
+        amount += Amount::from(1);
+        assert_eq!(amount, Amount::from(2));
+        assert_ne!(amount, Amount::ONE); // Should have changed
+
+        let mut amount = Amount::ZERO;
+        amount += Amount::from(42);
+        assert_eq!(amount, Amount::from(42));
+        assert_ne!(amount, Amount::ZERO); // Should have changed
+    }
+
+    /// Tests SubAssign actually modifies the value.
+    ///
+    /// Mutant testing: Catches mutation that replaces sub_assign with ().
+    #[test]
+    fn test_sub_assign() {
+        let mut amount = Amount::from(100);
+        amount -= Amount::from(30);
+        assert_eq!(amount, Amount::from(70));
+        assert_ne!(amount, Amount::from(100)); // Should have changed
+
+        let mut amount = Amount::from(50);
+        amount -= Amount::from(1);
+        assert_eq!(amount, Amount::from(49));
+        assert_ne!(amount, Amount::from(50)); // Should have changed
+
+        let mut amount = Amount::from(10);
+        amount -= Amount::from(10);
+        assert_eq!(amount, Amount::ZERO);
+        assert_ne!(amount, Amount::from(10)); // Should have changed
+    }
 }