aboutsummaryrefslogtreecommitdiff
path: root/crates/libsyntax2/tests/lexer.rs
blob: 46ac9fedd49885cac1dac464c3ba355a15db0185 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
extern crate libsyntax2;
extern crate testutils;

use std::fmt::Write;

use libsyntax2::{tokenize, Token};
use testutils::dir_tests;

#[test]
fn lexer_tests() {
    dir_tests(&["lexer"], |text| {
        let tokens = tokenize(text);
        dump_tokens(&tokens, text)
    })
}

fn dump_tokens(tokens: &[Token], text: &str) -> String {
    let mut acc = String::new();
    let mut offset = 0;
    for token in tokens {
        let len: u32 = token.len.into();
        let len = len as usize;
        let token_text = &text[offset..offset + len];
        offset += len;
        write!(acc, "{:?} {} {:?}\n", token.kind, token.len, token_text).unwrap()
    }
    acc
}