-
-
Notifications
You must be signed in to change notification settings - Fork 541
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
416 additions
and
108 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,88 @@ | ||
#![cfg(test)] | ||
#![allow(unused_mut, unused_variables, unused_assignments)] | ||
|
||
use super::{HtmlLexer, TextSize}; | ||
use biome_html_syntax::HtmlSyntaxKind::{self, *}; | ||
use biome_parser::lexer::Lexer; | ||
use biome_rowan::TextRange; | ||
|
||
pub struct Token { | ||
kind: HtmlSyntaxKind, | ||
range: TextRange, | ||
} | ||
|
||
impl Iterator for HtmlLexer<'_> { | ||
type Item = Token; | ||
|
||
fn next(&mut self) -> Option<Self::Item> { | ||
let kind = self.next_token(()); | ||
if kind == EOF { | ||
None | ||
} else { | ||
Some(Token { | ||
kind, | ||
range: self.current_range(), | ||
}) | ||
} | ||
} | ||
} | ||
|
||
// Assert the result of lexing a piece of source code, | ||
// and make sure the tokens yielded are fully lossless and the source can be reconstructed from only the tokens | ||
macro_rules! assert_lex { | ||
($src:expr, $($kind:ident:$len:expr $(,)?)*) => {{ | ||
let mut lexer = HtmlLexer::from_str($src); | ||
let mut idx = 0; | ||
let mut tok_idx = TextSize::default(); | ||
|
||
let mut new_str = String::with_capacity($src.len()); | ||
let tokens: Vec<_> = lexer.collect(); | ||
|
||
$( | ||
assert_eq!( | ||
tokens[idx].kind, | ||
HtmlSyntaxKind::$kind, | ||
"expected token kind {}, but found {:?}", | ||
stringify!($kind), | ||
tokens[idx].kind, | ||
); | ||
|
||
assert_eq!( | ||
tokens[idx].range.len(), | ||
TextSize::from($len), | ||
"expected token length of {}, but found {:?} for token {:?}", | ||
$len, | ||
tokens[idx].range.len(), | ||
tokens[idx].kind, | ||
); | ||
|
||
new_str.push_str(&$src[tokens[idx].range]); | ||
tok_idx += tokens[idx].range.len(); | ||
|
||
idx += 1; | ||
)* | ||
|
||
if idx < tokens.len() { | ||
panic!( | ||
"expected {} tokens but lexer returned {}, first unexpected token is '{:?}'", | ||
idx, | ||
tokens.len(), | ||
tokens[idx].kind | ||
); | ||
} else { | ||
assert_eq!(idx, tokens.len()); | ||
} | ||
|
||
assert_eq!($src, new_str, "Failed to reconstruct input"); | ||
}}; | ||
} | ||
|
||
// TODO: to fix | ||
#[test] | ||
#[ignore = "currently not handled"] | ||
fn doctype() { | ||
assert_lex! { | ||
"doctype", | ||
ERROR_TOKEN:1, | ||
} | ||
} |
Oops, something went wrong.