Skip to content

Commit

Permalink
Add TDocument mock class for expected-result testing
Browse files Browse the repository at this point in the history
  • Loading branch information
scouten committed Dec 29, 2023
1 parent 538a088 commit 9d01a6c
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 80 deletions.
1 change: 1 addition & 0 deletions src/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use crate::{blocks::Block, primitives::consume_empty_lines, Error, Span};
/// block itself, but contributes metadata to the document, such as the document
/// title and document attributes.
#[allow(dead_code)] // TEMPORARY while building
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Document<'a> {
blocks: Vec<Block<'a>>,
source: Span<'a>,
Expand Down
138 changes: 58 additions & 80 deletions src/tests/document.rs
Original file line number Diff line number Diff line change
@@ -1,117 +1,95 @@
use crate::{
tests::fixtures::{
blocks::{TBlock, TSimpleBlock},
TSpan,
TDocument, TSpan,
},
Document,
};

#[test]
fn empty_source() {
let doc = Document::parse("").unwrap();

assert_eq!(
doc.span(),
TSpan {
data: "",
line: 1,
col: 1,
offset: 0
Document::parse("").unwrap(),
TDocument {
source: TSpan {
data: "",
line: 1,
col: 1,
offset: 0
},
blocks: vec![],
}
);

let mut blocks = doc.blocks();
assert!(blocks.next().is_none());
}

#[test]
fn only_spaces() {
let doc = Document::parse(" ").unwrap();

assert_eq!(
doc.span(),
TSpan {
data: " ",
line: 1,
col: 1,
offset: 0
Document::parse(" ").unwrap(),
TDocument {
source: TSpan {
data: " ",
line: 1,
col: 1,
offset: 0
},
blocks: vec![],
}
);

let mut blocks = doc.blocks();
assert!(blocks.next().is_none());
}

#[test]
fn one_simple_block() {
let doc = Document::parse("abc").unwrap();

assert_eq!(
doc.span(),
TSpan {
data: "abc",
line: 1,
col: 1,
offset: 0
}
);

let mut blocks = doc.blocks();

assert_eq!(
blocks.next().unwrap(),
&TBlock::Simple(TSimpleBlock {
inlines: vec![TSpan {
Document::parse("abc").unwrap(),
TDocument {
source: TSpan {
data: "abc",
line: 1,
col: 1,
offset: 0,
},],
})
offset: 0
},
blocks: vec![TBlock::Simple(TSimpleBlock {
inlines: vec![TSpan {
data: "abc",
line: 1,
col: 1,
offset: 0,
},],
})],
}
);

assert!(blocks.next().is_none());
}

#[test]
fn two_simple_blocks() {
let doc = Document::parse("abc\n\ndef").unwrap();

assert_eq!(
doc.span(),
TSpan {
data: "abc\n\ndef",
line: 1,
col: 1,
offset: 0
}
);

let mut blocks = doc.blocks();

assert_eq!(
blocks.next().unwrap(),
&TBlock::Simple(TSimpleBlock {
inlines: vec![TSpan {
data: "abc",
Document::parse("abc\n\ndef").unwrap(),
TDocument {
source: TSpan {
data: "abc\n\ndef",
line: 1,
col: 1,
offset: 0,
},],
})
);

assert_eq!(
blocks.next().unwrap(),
&TBlock::Simple(TSimpleBlock {
inlines: vec![TSpan {
data: "def",
line: 3,
col: 1,
offset: 5,
},],
})
offset: 0
},
blocks: vec![
TBlock::Simple(TSimpleBlock {
inlines: vec![TSpan {
data: "abc",
line: 1,
col: 1,
offset: 0,
},],
}),
TBlock::Simple(TSimpleBlock {
inlines: vec![TSpan {
data: "def",
line: 3,
col: 1,
offset: 5,
},],
}),
],
}
);

assert!(blocks.next().is_none());
}
53 changes: 53 additions & 0 deletions src/tests/fixtures/document.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
use std::cmp::PartialEq;

use crate::{
tests::fixtures::{blocks::TBlock, TSpan},
Document,
};

// Approximate mock of Document type that we can use
// to declare expected values for easier test writing.
//
// Primary difference is that the data members are public
// so we can declare them inline.
#[derive(Debug, Eq, PartialEq)]
pub(crate) struct TDocument {
pub blocks: Vec<TBlock>,
pub source: TSpan,
}

impl<'a> PartialEq<Document<'a>> for TDocument {
fn eq(&self, other: &Document<'a>) -> bool {
tdocument_eq(self, other)
}
}

impl<'a> PartialEq<TDocument> for Document<'a> {
fn eq(&self, other: &TDocument) -> bool {
tdocument_eq(other, self)
}
}

impl<'a> PartialEq<TDocument> for &Document<'a> {
fn eq(&self, other: &TDocument) -> bool {
tdocument_eq(other, self)
}
}

fn tdocument_eq(tdocument: &TDocument, document: &Document) -> bool {
if &tdocument.source != document.span() {
return false;
}

if tdocument.blocks.len() != document.blocks().len() {
return false;
}

for (td_block, block) in tdocument.blocks.iter().zip(document.blocks()) {
if td_block != block {
return false;
}
}

true
}
3 changes: 3 additions & 0 deletions src/tests/fixtures/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ use std::{env, path::PathBuf};

pub(crate) mod blocks;

mod document;
pub(crate) use document::TDocument;

mod span;
pub(crate) use span::TSpan;

Expand Down

0 comments on commit 9d01a6c

Please sign in to comment.