Skip to content

Commit 2dc9b22

Browse files
committed
feat: split lexer test
1 parent c685622 commit 2dc9b22

14 files changed

+442
-442
lines changed

lexer/Cargo.toml

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
1-
[package]
2-
name = "monkey-lexer"
3-
version = "0.6.0"
4-
description = "a lexer for monkey lang"
5-
homepage = "https://github.com/gengjiawen/monkey-rust"
6-
repository = "https://github.com/gengjiawen/monkey-rust"
7-
authors = ["gengjiawen <[email protected]>"]
8-
edition = "2018"
9-
license = "MIT"
10-
11-
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
12-
13-
[lib]
14-
name = "lexer"
15-
path= "lib.rs"
16-
17-
[[bin]]
18-
name = "monkey-lexer"
19-
path = "main.rs"
20-
21-
[dependencies]
22-
serde = {version = "1.0", features = ["derive"]}
23-
serde_json = "1.0"
24-
25-
[dev-dependencies]
26-
insta = "1.7.1"
1+
[package]
2+
name = "monkey-lexer"
3+
version = "0.6.0"
4+
description = "a lexer for monkey lang"
5+
homepage = "https://github.com/gengjiawen/monkey-rust"
6+
repository = "https://github.com/gengjiawen/monkey-rust"
7+
authors = ["gengjiawen <[email protected]>"]
8+
edition = "2018"
9+
license = "MIT"
10+
11+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
12+
13+
[lib]
14+
name = "lexer"
15+
path= "lib.rs"
16+
17+
[[bin]]
18+
name = "monkey-lexer"
19+
path = "main.rs"
20+
21+
[dependencies]
22+
serde = {version = "1.0", features = ["derive"]}
23+
serde_json = "1.0"
24+
25+
[dev-dependencies]
26+
insta = "1.7.1"

lexer/README.md

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
# monkey-rust
2-
![Rust](https://github.com/gengjiawen/monkey-rust/workflows/Rust/badge.svg)
3-
[![Gitpod ready-to-code](https://img.shields.io/badge/Gitpod-ready--to--code-blue?logo=gitpod)](https://gitpod.io/#https://github.com/gengjiawen/monkey_rust)
4-
5-
This is lexer for the Monkey programming language written in Rust
6-
7-
![The Monkey Programming Language](https://cloud.githubusercontent.com/assets/1013641/22617482/9c60c27c-eb09-11e6-9dfa-b04c7fe498ea.png)
8-
9-
## What’s Monkey?
10-
11-
Monkey has a C-like syntax, supports **variable bindings**, **prefix** and **infix operators**, has **first-class** and **higher-order functions**, can handle **closures** with ease and has **integers**, **booleans**, **arrays** and **hashes** built-in.
12-
13-
Official site is: https://monkeylang.org/. It's has various implementation languages :).
14-
15-
There is a book about learning how to make an interpreter: [Writing An Interpreter In Go](https://interpreterbook.com/#the-monkey-programming-language). This is where the Monkey programming language come from.
1+
# monkey-rust
2+
![Rust](https://github.com/gengjiawen/monkey-rust/workflows/Rust/badge.svg)
3+
[![Gitpod ready-to-code](https://img.shields.io/badge/Gitpod-ready--to--code-blue?logo=gitpod)](https://gitpod.io/#https://github.com/gengjiawen/monkey_rust)
4+
5+
This is lexer for the Monkey programming language written in Rust
6+
7+
![The Monkey Programming Language](https://cloud.githubusercontent.com/assets/1013641/22617482/9c60c27c-eb09-11e6-9dfa-b04c7fe498ea.png)
8+
9+
## What’s Monkey?
10+
11+
Monkey has a C-like syntax, supports **variable bindings**, **prefix** and **infix operators**, has **first-class** and **higher-order functions**, can handle **closures** with ease and has **integers**, **booleans**, **arrays** and **hashes** built-in.
12+
13+
Official site is: https://monkeylang.org/. It's has various implementation languages :).
14+
15+
There is a book about learning how to make an interpreter: [Writing An Interpreter In Go](https://interpreterbook.com/#the-monkey-programming-language). This is where the Monkey programming language come from.

lexer/lexer_test.rs

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#[cfg(test)]
2+
mod tests {
3+
use crate::Lexer;
4+
use crate::token::{TokenKind, Token};
5+
use insta::*;
6+
7+
fn test_token_set(l: &mut Lexer) -> Vec<Token> {
8+
let mut token_vs: Vec<Token> = vec![];
9+
loop {
10+
let t = l.next_token();
11+
if t.kind == TokenKind::EOF {
12+
token_vs.push(t);
13+
break;
14+
} else {
15+
token_vs.push(t);
16+
}
17+
}
18+
token_vs
19+
}
20+
21+
pub fn test_lexer_common(name: &str, input: &str) {
22+
let mut l = Lexer::new(input);
23+
let token_vs = test_token_set(&mut l);
24+
25+
assert_snapshot!(name, serde_json::to_string_pretty(&token_vs).unwrap(), input);
26+
}
27+
28+
#[test]
29+
fn test_lexer_simple() {
30+
test_lexer_common("simple", "=+(){},:;");
31+
}
32+
33+
#[test]
34+
fn test_lexer_let() {
35+
test_lexer_common("let", "let x=5");
36+
}
37+
38+
#[test]
39+
fn test_lexer_let_with_space() {
40+
test_lexer_common("let_with_space", "let x = 5");
41+
}
42+
43+
#[test]
44+
fn test_lexer_string() {
45+
test_lexer_common("string", r#""a""#);
46+
}
47+
48+
#[test]
49+
fn test_lexer_array() {
50+
test_lexer_common("array", "[3]");
51+
}
52+
53+
#[test]
54+
fn test_lexer_hash() {
55+
test_lexer_common("hash", r#"{"one": 1, "two": 2, "three": 3}"#);
56+
}
57+
58+
#[test]
59+
fn test_lexer_bool() {
60+
test_lexer_common("bool", "let y=true");
61+
}
62+
63+
#[test]
64+
fn test_lexer_complex() {
65+
test_lexer_common("complex", "let five = 5;
66+
let ten = 10;
67+
68+
let add = fn(x, y) {
69+
x + y;
70+
};
71+
72+
let result = add(five, ten);
73+
!-/*5;
74+
5 < 10 > 5;
75+
76+
if (5 < 10) {
77+
return true;
78+
} else {
79+
return false;
80+
}
81+
82+
10 == 10;
83+
10 != 9;");
84+
}
85+
}

0 commit comments

Comments
 (0)