Skip to content

Commit

Permalink
[#119] Token 테스트코드 작성
Browse files Browse the repository at this point in the history
  • Loading branch information
myyrakle committed Jul 14, 2024
1 parent d0ec524 commit f7c07c1
Show file tree
Hide file tree
Showing 8 changed files with 321 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/errors/execute_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ pub struct ExecuteError {
pub backtrace: std::backtrace::Backtrace,
}

impl PartialEq for ExecuteError {
fn eq(&self, other: &Self) -> bool {
self.message == other.message
}
}

impl ExecuteError {
pub fn wrap<T: ToString>(message: T) -> RRDBError {
RRDBError::ExecuteError(Self {
Expand Down
6 changes: 6 additions & 0 deletions src/errors/into_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ pub struct IntoError {
pub backtrace: std::backtrace::Backtrace,
}

impl PartialEq for IntoError {
fn eq(&self, other: &Self) -> bool {
self.message == other.message
}
}

impl IntoError {
pub fn wrap<T: ToString>(message: T) -> RRDBError {
RRDBError::IntoError(Self {
Expand Down
6 changes: 6 additions & 0 deletions src/errors/lexing_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ pub struct LexingError {
pub backtrace: std::backtrace::Backtrace,
}

impl PartialEq for LexingError {
fn eq(&self, other: &Self) -> bool {
self.message == other.message
}
}

impl LexingError {
pub fn wrap<T: ToString>(message: T) -> RRDBError {
RRDBError::LexingError(Self {
Expand Down
2 changes: 1 addition & 1 deletion src/errors/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub mod predule;
pub mod server_error;
pub mod type_error;

#[derive(Debug)]
#[derive(Debug, PartialEq)]
pub enum RRDBError {
ExecuteError(execute_error::ExecuteError),
IntoError(into_error::IntoError),
Expand Down
6 changes: 6 additions & 0 deletions src/errors/parsing_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ pub struct ParsingError {
pub backtrace: std::backtrace::Backtrace,
}

impl PartialEq for ParsingError {
fn eq(&self, other: &Self) -> bool {
self.message == other.message
}
}

impl ParsingError {
pub fn wrap<T: ToString>(message: T) -> RRDBError {
RRDBError::ParsingError(Self {
Expand Down
6 changes: 6 additions & 0 deletions src/errors/server_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ pub struct ServerError {
pub backtrace: std::backtrace::Backtrace,
}

impl PartialEq for ServerError {
fn eq(&self, other: &Self) -> bool {
self.message == other.message
}
}

impl ServerError {
pub fn new<T: ToString>(message: T) -> Self {
Self {
Expand Down
6 changes: 6 additions & 0 deletions src/errors/type_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ pub struct TypeError {
pub backtrace: std::backtrace::Backtrace,
}

impl PartialEq for TypeError {
fn eq(&self, other: &Self) -> bool {
self.message == other.message
}
}

impl TypeError {
pub fn wrap<T: ToString>(message: T) -> RRDBError {
RRDBError::TypeError(Self {
Expand Down
284 changes: 284 additions & 0 deletions src/lexer/tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,287 @@ impl TryInto<BinaryOperator> for Token {
}
}
}

#[cfg(test)]
mod tests {
use crate::{
ast::dml::expressions::operators::BinaryOperator,
lexer::{predule::OperatorToken, tokens::Token},
};

#[test]
fn test_token_is_unary_operator() {
let test_cases = vec![
(Token::Operator(OperatorToken::Plus), true),
(Token::Operator(OperatorToken::Minus), true),
(Token::Operator(OperatorToken::Not), true),
(Token::And, false),
(Token::Or, false),
(Token::Like, false),
(Token::In, false),
(Token::Is, false),
(Token::Identifier("test".to_owned()), false),
(Token::Integer(1), false),
(Token::Float(1.0), false),
(Token::Boolean(true), false),
(Token::String("test".to_owned()), false),
(Token::Null, false),
(Token::LeftParentheses, false),
];

for (token, want) in test_cases {
assert_eq!(token.is_unary_operator(), want);
}
}

#[test]
fn test_token_try_into_multi_token_operator() {
struct TestCase {
name: String,
first: Token,
second: Token,
expected: BinaryOperator,
want_error: bool,
}

let test_cases = vec![
TestCase {
name: "NOT LIKE".into(),
first: Token::Not,
second: Token::Like,
expected: BinaryOperator::NotLike,
want_error: false,
},
TestCase {
name: "NOT IN".into(),
first: Token::Not,
second: Token::In,
expected: BinaryOperator::NotIn,
want_error: false,
},
TestCase {
name: "IS NOT".into(),
first: Token::Is,
second: Token::Not,
expected: BinaryOperator::IsNot,
want_error: false,
},
TestCase {
name: "IS".into(),
first: Token::Is,
second: Token::Is,
expected: BinaryOperator::Is,
want_error: false,
},
TestCase {
name: "NOT AND".into(),
first: Token::Not,
second: Token::And,
expected: BinaryOperator::Is,
want_error: true,
},
TestCase {
name: "IS LIKE".into(),
first: Token::Is,
second: Token::Like,
expected: BinaryOperator::Is,
want_error: false,
},
];

for t in test_cases {
let got = t.first.try_into_multi_token_operator(t.second);

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_token_is_expression() {
struct TestCase {
name: String,
input: Token,
expected: bool,
}

let test_cases = vec![
TestCase {
name: "Identifier".into(),
input: Token::Identifier("test".into()),
expected: true,
},
TestCase {
name: "Integer".into(),
input: Token::Integer(1),
expected: true,
},
TestCase {
name: "Float".into(),
input: Token::Float(1.0),
expected: true,
},
TestCase {
name: "Boolean".into(),
input: Token::Boolean(true),
expected: true,
},
TestCase {
name: "String".into(),
input: Token::String("test".into()),
expected: true,
},
TestCase {
name: "Null".into(),
input: Token::Null,
expected: true,
},
TestCase {
name: "LeftParentheses".into(),
input: Token::LeftParentheses,
expected: true,
},
TestCase {
name: "Not".into(),
input: Token::Not,
expected: true,
},
TestCase {
name: "Operator".into(),
input: Token::Operator(OperatorToken::Plus),
expected: true,
},
TestCase {
name: "And".into(),
input: Token::And,
expected: false,
},
TestCase {
name: "Or".into(),
input: Token::Or,
expected: false,
},
TestCase {
name: "Like".into(),
input: Token::Like,
expected: false,
},
TestCase {
name: "In".into(),
input: Token::In,
expected: false,
},
TestCase {
name: "Is".into(),
input: Token::Is,
expected: false,
},
TestCase {
name: "Comma".into(),
input: Token::Comma,
expected: false,
},
TestCase {
name: "Period".into(),
input: Token::Period,
expected: false,
},
TestCase {
name: "SemiColon".into(),
input: Token::SemiColon,
expected: false,
},
TestCase {
name: "RightParentheses".into(),
input: Token::RightParentheses,
expected: false,
},
TestCase {
name: "EOF".into(),
input: Token::EOF,
expected: false,
},
];

for t in test_cases {
let got = t.input.is_expression();
assert_eq!(got, t.expected, "TC: {}", t.name);
}
}

#[test]
fn test_token_try_into_binary_operator() {
struct TestCase {
name: String,
input: Token,
expected: BinaryOperator,
want_error: bool,
}

let test_cases = vec![
TestCase {
name: "AND".into(),
input: Token::And,
expected: BinaryOperator::And,
want_error: false,
},
TestCase {
name: "OR".into(),
input: Token::Or,
expected: BinaryOperator::Or,
want_error: false,
},
TestCase {
name: "LIKE".into(),
input: Token::Like,
expected: BinaryOperator::Like,
want_error: false,
},
TestCase {
name: "IN".into(),
input: Token::In,
expected: BinaryOperator::In,
want_error: false,
},
TestCase {
name: "IS".into(),
input: Token::Is,
expected: BinaryOperator::Is,
want_error: false,
},
TestCase {
name: "Identifier".into(),
input: Token::Identifier("test".into()),
expected: BinaryOperator::Is,
want_error: true,
},
];

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);
}
}
}
}

0 comments on commit f7c07c1

Please sign in to comment.