Skip to content

Commit

Permalink
vcf/io/writer/record/samples/sample/value/genotype: Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zaeleus committed Mar 28, 2024
1 parent 267f521 commit a3af2ac
Showing 1 changed file with 85 additions and 2 deletions.
87 changes: 85 additions & 2 deletions noodles-vcf/src/io/writer/record/samples/sample/value/genotype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ fn write_phasing<W>(writer: &mut W, phasing: Phasing) -> io::Result<()>
where
W: Write,
{
const PHASED: u8 = b'/';
const UNPHASED: u8 = b'|';
const PHASED: u8 = b'|';
const UNPHASED: u8 = b'/';

match phasing {
Phasing::Phased => writer.write_all(&[PHASED]),
Expand All @@ -44,3 +44,86 @@ where
writer.write_all(&[MISSING])
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::variant::record_buf::samples::sample::value::{
genotype::Allele, Genotype as GenotypeBuf,
};

#[test]
fn test_write_genotype() -> Result<(), Box<dyn std::error::Error>> {
let mut buf = Vec::new();

buf.clear();
let genotype = &GenotypeBuf::try_from(vec![Allele::new(Some(0), Phasing::Phased)])?;
write_genotype(&mut buf, &genotype)?;
assert_eq!(buf, b"0");

buf.clear();
let genotype = &GenotypeBuf::try_from(vec![
Allele::new(Some(0), Phasing::Unphased),
Allele::new(Some(1), Phasing::Unphased),
])?;
write_genotype(&mut buf, &genotype)?;
assert_eq!(buf, b"0/1");

buf.clear();
let genotype = &GenotypeBuf::try_from(vec![
Allele::new(Some(0), Phasing::Phased),
Allele::new(Some(1), Phasing::Phased),
])?;
write_genotype(&mut buf, &genotype)?;
assert_eq!(buf, b"0|1");

buf.clear();
let genotype = &GenotypeBuf::try_from(vec![
Allele::new(Some(0), Phasing::Unphased),
Allele::new(Some(1), Phasing::Unphased),
Allele::new(Some(2), Phasing::Phased),
])?;
write_genotype(&mut buf, &genotype)?;
assert_eq!(buf, b"0/1|2");

buf.clear();
let genotype = &GenotypeBuf::try_from(vec![
Allele::new(None, Phasing::Unphased),
Allele::new(None, Phasing::Unphased),
])?;
write_genotype(&mut buf, &genotype)?;
assert_eq!(buf, b"./.");

Ok(())
}

#[test]
fn test_write_phasing() -> io::Result<()> {
let mut buf = Vec::new();

buf.clear();
write_phasing(&mut buf, Phasing::Phased)?;
assert_eq!(buf, b"|");

buf.clear();
write_phasing(&mut buf, Phasing::Unphased)?;
assert_eq!(buf, b"/");

Ok(())
}

#[test]
fn test_write_position() -> io::Result<()> {
let mut buf = Vec::new();

buf.clear();
write_position(&mut buf, None)?;
assert_eq!(buf, b".");

buf.clear();
write_position(&mut buf, Some(0))?;
assert_eq!(buf, b"0");

Ok(())
}
}

0 comments on commit a3af2ac

Please sign in to comment.