From 9b00529bbdb8e70ab9c0264e870bbeab3be8a925 Mon Sep 17 00:00:00 2001 From: xqtc161 Date: Wed, 10 Jul 2024 21:37:34 +0200 Subject: [PATCH] Add logging for tests --- Cargo.lock | 223 +++++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 2 + src/lib.rs | 41 +++++++--- 3 files changed, 254 insertions(+), 12 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 89fd9fd..1e8cd8a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,229 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "aho-corasick" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916" +dependencies = [ + "memchr", +] + +[[package]] +name = "anstream" +version = "0.6.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "418c75fa768af9c03be99d17643f93f79bbba589895012a80e3452a19ddda15b" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "is_terminal_polyfill", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "038dfcf04a5feb68e9c60b21c9625a54c2c0616e79b72b0fd87075a056ae1d1b" + +[[package]] +name = "anstyle-parse" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c03a11a9034d92058ceb6ee011ce58af4a9bf61491aa7e1e59ecd24bd40d22d4" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ad186efb764318d35165f1758e7dcef3b10628e26d41a44bc5550652e6804391" +dependencies = [ + "windows-sys", +] + +[[package]] +name = "anstyle-wincon" +version = "3.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "61a38449feb7068f52bb06c12759005cf459ee52bb4adc1d5a7c4322d716fb19" +dependencies = [ + "anstyle", + "windows-sys", +] + +[[package]] +name = "colorchoice" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b6a852b24ab71dffc585bcb46eaf7959d175cb865a7152e35b348d1b2960422" + +[[package]] +name = "env_filter" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a009aa4810eb158359dda09d0c87378e4bbb89b5a801f016885a4707ba24f7ea" +dependencies = [ + "log", + "regex", +] + +[[package]] +name = "env_logger" +version = "0.11.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "38b35839ba51819680ba087cd351788c9a3c476841207e0b8cee0b04722343b9" +dependencies = [ + "anstream", + "anstyle", + "env_filter", + "humantime", + "log", +] + [[package]] name = "glsl-lexer" version = "0.1.0" +dependencies = [ + "env_logger", + "log", +] + +[[package]] +name = "humantime" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" + +[[package]] +name = "is_terminal_polyfill" +version = "1.70.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8478577c03552c21db0e2724ffb8986a5ce7af88107e6be5d2ee6e158c12800" + +[[package]] +name = "log" +version = "0.4.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24" + +[[package]] +name = "memchr" +version = "2.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" + +[[package]] +name = "regex" +version = "1.10.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b91213439dad192326a0d7c6ee3955910425f441d7038e0d6933b0aec5c4517f" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.4.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "38caf58cc5ef2fed281f89292ef23f6365465ed9a41b7a7754eb4e26496c92df" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a66a03ae7c801facd77a29370b4faec201768915ac14a721ba36f20bc9c209b" + +[[package]] +name = "utf8parse" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" + +[[package]] +name = "windows-sys" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" +dependencies = [ + "windows-targets", +] + +[[package]] +name = "windows-targets" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_gnullvm", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" + +[[package]] +name = "windows_i686_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" + +[[package]] +name = "windows_i686_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" diff --git a/Cargo.toml b/Cargo.toml index a68cfaf..3827790 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,3 +4,5 @@ version = "0.1.0" edition = "2021" [dependencies] +env_logger = "0.11.3" +log = "0.4.22" diff --git a/src/lib.rs b/src/lib.rs index 244f8bb..fcb6df4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -11,7 +11,7 @@ pub enum Token { Whitespace, Comment(String), Unknown(char), - EndOfFile, + EOF, } pub struct Lexer { @@ -61,7 +61,7 @@ impl Lexer { tokens.push(self.consume_symbol()); } } - tokens.push(Token::EndOfFile); + tokens.push(Token::EOF); tokens } @@ -167,67 +167,82 @@ fn is_keyword(word: &str) -> bool { #[cfg(test)] mod tests { use super::*; + use log::info; + + fn init() { + std::env::set_var("RUST_LOG", "INFO"); + let _ = env_logger::builder().is_test(true).try_init(); + } #[test] fn whitespace() { + init(); let source_code = " \t\n"; let mut lexer = Lexer::new(source_code); let tokens = lexer.get_tokens(); - assert_eq!(tokens, vec![Token::Whitespace, Token::EndOfFile]); + info!("[Whitespace] Tokens: {:#?}", tokens); + assert_eq!(tokens, vec![Token::Whitespace, Token::EOF]); } #[test] fn identifier() { + init(); let source_code = "variableName"; let mut lexer = Lexer::new(source_code); let tokens = lexer.get_tokens(); + info!("[Identifier] Tokens: {:#?}", tokens); assert_eq!( tokens, - vec![ - Token::Identifier("variableName".to_string()), - Token::EndOfFile - ] + vec![Token::Identifier("variableName".to_string()), Token::EOF] ); } #[test] fn keyword() { + init(); let source_code = "uniform"; let mut lexer = Lexer::new(source_code); let tokens = lexer.get_tokens(); + info!("[Keyword] Tokens: {:#?}", tokens); assert_eq!( tokens, - vec![Token::Keyword("uniform".to_string()), Token::EndOfFile] + vec![Token::Keyword("uniform".to_string()), Token::EOF] ); } #[test] fn integer_literal() { + init(); let source_code = "12345"; let mut lexer = Lexer::new(source_code); let tokens = lexer.get_tokens(); - assert_eq!(tokens, vec![Token::IntegerLiteral(12345), Token::EndOfFile]); + info!("[IntegerLiteral] Tokens: {:#?}", tokens); + assert_eq!(tokens, vec![Token::IntegerLiteral(12345), Token::EOF]); } #[test] fn float_literal() { + init(); let source_code = "123.45"; let mut lexer = Lexer::new(source_code); let tokens = lexer.get_tokens(); - assert_eq!(tokens, vec![Token::FloatLiteral(123.45), Token::EndOfFile]); + info!("[FloatLiteral] Tokens: {:#?}", tokens); + assert_eq!(tokens, vec![Token::FloatLiteral(123.45), Token::EOF]); } #[test] fn test_single_line_comment() { + init(); let source = "// This is a comment\n"; let mut lexer = Lexer::new(source); let tokens = lexer.get_tokens(); + info!("[Comment] Tokens: {:#?}", tokens); assert_eq!( tokens, vec![ Token::Comment("// This is a comment".to_string()), Token::Whitespace, - Token::EndOfFile, + Token::EOF, ] ); } @@ -235,6 +250,7 @@ mod tests { // I hope that does it. Writing this test was pain. #[test] fn complex_source() { + init(); let source = r#" uniform float time; void main() { @@ -243,6 +259,7 @@ mod tests { "#; let mut lexer = Lexer::new(source); let tokens = lexer.get_tokens(); + info!("[Complex Source] Tokens: {:#?}", tokens); assert_eq!( tokens, vec![ @@ -283,7 +300,7 @@ mod tests { Token::Whitespace, Token::Symbol('}'), Token::Whitespace, - Token::EndOfFile, + Token::EOF, ] ); }