-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add TDocument mock class for expected-result testing
- Loading branch information
Showing
4 changed files
with
115 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters