Testing yassgy

For this post, I would like to talk about testing yassgy (and how difficult it is…).

I have implement very small test cases on a very small part of yassgy. Since yassgy currently is untestable, and I noticed that refactoring it would a major task, I…


This content originally appeared on DEV Community and was authored by Gerardo Enrique Arriaga Rendon

For this post, I would like to talk about testing yassgy (and how difficult it is...).

I have implement very small test cases on a very small part of yassgy. Since yassgy currently is untestable, and I noticed that refactoring it would a major task, I decided to focus on a really small function of yassgy, testing whether a file path refers to a text file.

I used the built-in testing features included in cargo, by using the cfg attribute and prefixing my test functions with the test attribute.

For example, let us say we have a file called complex.rs, that defines a structure for a complex number:

#[derive(Debug, Eq)]
struct Complex {
  real: f64,
  imaginary: f64
}

impl Complex {
  fn add(self, rhs: Complex) -> Complex {
    Complex {
      real: self.real + rhs.real,
      imaginary: self.imaginary + rhs.imaginary,
    }
  }
}

impl Add for Complex {
  type Output = Self;

  fn add(self, other: Self) -> Self {
    self.add(other)
  }
}

If I wanted to test this structure, I can simply write the following in the same file:

struct Complex {
} 

// snip

#[cfg(test)]
mod tests {
  use super::*;

  #[test]
  fn add_two_complex_numbers() {
    let a = Complex { real: 1.0, imaginary: 1.0 };
    let b = Complex { real: 1.5, imaginary: 2.3 };
    let expected = Complex { real: 2.5, imaginary: 3.3 };
    assert_eq!(a + b, expected);
  }
}

And thus, we have implemented the first test case!
This test case can be easily run in the command line with cargo test.

We can also implement a second test case, where we test the subtraction of two complex numbers, and, because we are not supporting a Sub operation, the test case would fail!

Test cases for yassgy

The test cases for yassgy were pretty simple, focused on the function that tests whether a file path refers to a text file.

Writing a few test cases helped me to realize test cases define a specification of your program, and they count in some way as documentation of your program. Thus, testing provide several benefits that outweigh the maintenance costs of the code.


This content originally appeared on DEV Community and was authored by Gerardo Enrique Arriaga Rendon


Print Share Comment Cite Upload Translate Updates
APA

Gerardo Enrique Arriaga Rendon | Sciencx (2021-11-14T17:01:51+00:00) Testing yassgy. Retrieved from https://www.scien.cx/2021/11/14/testing-yassgy/

MLA
" » Testing yassgy." Gerardo Enrique Arriaga Rendon | Sciencx - Sunday November 14, 2021, https://www.scien.cx/2021/11/14/testing-yassgy/
HARVARD
Gerardo Enrique Arriaga Rendon | Sciencx Sunday November 14, 2021 » Testing yassgy., viewed ,<https://www.scien.cx/2021/11/14/testing-yassgy/>
VANCOUVER
Gerardo Enrique Arriaga Rendon | Sciencx - » Testing yassgy. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/11/14/testing-yassgy/
CHICAGO
" » Testing yassgy." Gerardo Enrique Arriaga Rendon | Sciencx - Accessed . https://www.scien.cx/2021/11/14/testing-yassgy/
IEEE
" » Testing yassgy." Gerardo Enrique Arriaga Rendon | Sciencx [Online]. Available: https://www.scien.cx/2021/11/14/testing-yassgy/. [Accessed: ]
rf:citation
» Testing yassgy | Gerardo Enrique Arriaga Rendon | Sciencx | https://www.scien.cx/2021/11/14/testing-yassgy/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.