Skip to content

Commit

Permalink
[#119] lexer 테스트코드 추가 작성
Browse files Browse the repository at this point in the history
  • Loading branch information
myyrakle committed Jul 14, 2024
1 parent f7c07c1 commit c5c5804
Show file tree
Hide file tree
Showing 2 changed files with 194 additions and 13 deletions.
200 changes: 187 additions & 13 deletions src/lexer/operator_token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,22 +128,196 @@ mod tests {
use super::{BinaryOperator, OperatorToken};
use std::convert::TryInto;

struct TestCase {
name: String,
input: OperatorToken,
want_error: bool,
expected: BinaryOperator,
}

let test_cases = vec![
(OperatorToken::Plus, BinaryOperator::Add),
(OperatorToken::Minus, BinaryOperator::Sub),
(OperatorToken::Asterisk, BinaryOperator::Mul),
(OperatorToken::Slash, BinaryOperator::Div),
(OperatorToken::Lt, BinaryOperator::Lt),
(OperatorToken::Gt, BinaryOperator::Gt),
(OperatorToken::Lte, BinaryOperator::Lte),
(OperatorToken::Gte, BinaryOperator::Gte),
(OperatorToken::Eq, BinaryOperator::Eq),
(OperatorToken::Neq, BinaryOperator::Neq),
TestCase {
name: "연산자: +".to_owned(),
input: OperatorToken::Plus,
want_error: false,
expected: BinaryOperator::Add,
},
TestCase {
name: "연산자: -".to_owned(),
input: OperatorToken::Minus,
want_error: false,
expected: BinaryOperator::Sub,
},
TestCase {
name: "연산자: *".to_owned(),
input: OperatorToken::Asterisk,
want_error: false,
expected: BinaryOperator::Mul,
},
TestCase {
name: "연산자: /".to_owned(),
input: OperatorToken::Slash,
want_error: false,
expected: BinaryOperator::Div,
},
TestCase {
name: "연산자: <".to_owned(),
input: OperatorToken::Lt,
want_error: false,
expected: BinaryOperator::Lt,
},
TestCase {
name: "연산자: >".to_owned(),
input: OperatorToken::Gt,
want_error: false,
expected: BinaryOperator::Gt,
},
TestCase {
name: "연산자: <=".to_owned(),
input: OperatorToken::Lte,
want_error: false,
expected: BinaryOperator::Lte,
},
TestCase {
name: "연산자: >=".to_owned(),
input: OperatorToken::Gte,
want_error: false,
expected: BinaryOperator::Gte,
},
TestCase {
name: "연산자: =".to_owned(),
input: OperatorToken::Eq,
want_error: false,
expected: BinaryOperator::Eq,
},
TestCase {
name: "연산자: !=".to_owned(),
input: OperatorToken::Neq,
want_error: false,
expected: BinaryOperator::Neq,
},
TestCase {
name: "연산자: !".to_owned(),
input: OperatorToken::Not,
want_error: true,
expected: BinaryOperator::Neq,
},
];

for (input, expected) in test_cases {
let got: BinaryOperator = input.try_into().unwrap();
assert_eq!(got, expected);
for t in test_cases {
let got = TryInto::<BinaryOperator>::try_into(t.input);

assert_eq!(
got.is_err(),
t.want_error,
"{}: want_error: {}, error: {:?}",
t.name,
t.want_error,
got.err()
);

if let Ok(tokens) = got {
assert_eq!(tokens, t.expected, "TC: {}", t.name);
}
}
}

#[test]
fn test_operator_token_try_into_unary_operator() {
use super::{OperatorToken, UnaryOperator};
use std::convert::TryInto;

struct TestCase {
name: String,
input: OperatorToken,
want_error: bool,
expected: UnaryOperator,
}

let test_cases = vec![
TestCase {
name: "연산자: +".to_owned(),
input: OperatorToken::Plus,
want_error: false,
expected: UnaryOperator::Pos,
},
TestCase {
name: "연산자: -".to_owned(),
input: OperatorToken::Minus,
want_error: false,
expected: UnaryOperator::Neg,
},
TestCase {
name: "연산자: *".to_owned(),
input: OperatorToken::Asterisk,
want_error: true,
expected: UnaryOperator::Neg,
},
TestCase {
name: "연산자: /".to_owned(),
input: OperatorToken::Slash,
want_error: true,
expected: UnaryOperator::Neg,
},
TestCase {
name: "연산자: <".to_owned(),
input: OperatorToken::Lt,
want_error: true,
expected: UnaryOperator::Neg,
},
TestCase {
name: "연산자: >".to_owned(),
input: OperatorToken::Gt,
want_error: true,
expected: UnaryOperator::Neg,
},
TestCase {
name: "연산자: <=".to_owned(),
input: OperatorToken::Lte,
want_error: true,
expected: UnaryOperator::Neg,
},
TestCase {
name: "연산자: >=".to_owned(),
input: OperatorToken::Gte,
want_error: true,
expected: UnaryOperator::Neg,
},
TestCase {
name: "연산자: =".to_owned(),
input: OperatorToken::Eq,
want_error: true,
expected: UnaryOperator::Neg,
},
TestCase {
name: "연산자: !=".to_owned(),
input: OperatorToken::Neq,
want_error: true,
expected: UnaryOperator::Neg,
},
TestCase {
name: "연산자: !".to_owned(),
input: OperatorToken::Not,
want_error: false,
expected: UnaryOperator::Not,
},
];

for t in test_cases {
let got = TryInto::<UnaryOperator>::try_into(t.input);

assert_eq!(
got.is_err(),
t.want_error,
"{}: want_error: {}, error: {:?}",
t.name,
t.want_error,
got.err()
);

if let Ok(tokens) = got {
assert_eq!(tokens, t.expected, "TC: {}", t.name);
}
}
}
}
7 changes: 7 additions & 0 deletions src/lexer/tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,13 @@ mod tests {
expected: BinaryOperator::Is,
want_error: false,
},
TestCase {
name: "Error".into(),
first: Token::Select,
second: Token::Like,
expected: BinaryOperator::Is,
want_error: true,
},
];

for t in test_cases {
Expand Down

0 comments on commit c5c5804

Please sign in to comment.