Skip to content

Commit

Permalink
test: add tests for parsing of constants
Browse files Browse the repository at this point in the history
  • Loading branch information
tohrnii committed Nov 4, 2022
1 parent 03a8dd7 commit 26a024a
Show file tree
Hide file tree
Showing 5 changed files with 172 additions and 1 deletion.
2 changes: 1 addition & 1 deletion parser/src/ast/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub struct Constants {
}

/// Stores a constant and the identifier used to access the constant. There are three types of
/// constants.
/// constants.
/// - Scalar: 1, 2, 3
/// - Vector: \[1, 2, 3\]
/// - Matrix: \[\[1, 2, 3\], \[4, 5, 6\]\]
Expand Down
101 changes: 101 additions & 0 deletions parser/src/lexer/tests/constants.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
use super::{expect_valid_tokenization, Token};

#[test]
fn constants_scalar() {
let source = "
constants:
a: 1
b: 2";

let tokens = vec![
Token::Constants,
Token::Colon,
Token::Ident("a".to_string()),
Token::Colon,
Token::Num("1".to_string()),
Token::Ident("b".to_string()),
Token::Colon,
Token::Num("2".to_string()),
];
expect_valid_tokenization(source, tokens);
}

#[test]
fn constants_vector() {
let source = "
constants:
a: [1, 2, 3, 4]
b: [5, 6, 7, 8]";

let tokens = vec![
Token::Constants,
Token::Colon,
Token::Ident("a".to_string()),
Token::Colon,
Token::Lsqb,
Token::Num("1".to_string()),
Token::Comma,
Token::Num("2".to_string()),
Token::Comma,
Token::Num("3".to_string()),
Token::Comma,
Token::Num("4".to_string()),
Token::Rsqb,
Token::Ident("b".to_string()),
Token::Colon,
Token::Lsqb,
Token::Num("5".to_string()),
Token::Comma,
Token::Num("6".to_string()),
Token::Comma,
Token::Num("7".to_string()),
Token::Comma,
Token::Num("8".to_string()),
Token::Rsqb,
];
expect_valid_tokenization(source, tokens);
}

#[test]
fn constants_matrix() {
let source = "
constants:
a: [[1, 2], [3, 4]]
b: [[5, 6], [7, 8]]";

let tokens = vec![
Token::Constants,
Token::Colon,
Token::Ident("a".to_string()),
Token::Colon,
Token::Lsqb,
Token::Lsqb,
Token::Num("1".to_string()),
Token::Comma,
Token::Num("2".to_string()),
Token::Rsqb,
Token::Comma,
Token::Lsqb,
Token::Num("3".to_string()),
Token::Comma,
Token::Num("4".to_string()),
Token::Rsqb,
Token::Rsqb,
Token::Ident("b".to_string()),
Token::Colon,
Token::Lsqb,
Token::Lsqb,
Token::Num("5".to_string()),
Token::Comma,
Token::Num("6".to_string()),
Token::Rsqb,
Token::Comma,
Token::Lsqb,
Token::Num("7".to_string()),
Token::Comma,
Token::Num("8".to_string()),
Token::Rsqb,
Token::Rsqb,
];
expect_valid_tokenization(source, tokens);
}
1 change: 1 addition & 0 deletions parser/src/lexer/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::{
};

mod boundary_constraints;
mod constants;
mod expressions;
mod identifiers;
mod periodic_columns;
Expand Down
68 changes: 68 additions & 0 deletions parser/src/parser/tests/constants.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
use crate::ast::constants::{Constant, ConstantType, Constants};

use super::{build_parse_test, Identifier, Source, SourceSection};

// CONSTANTS
// ================================================================================================

#[test]
fn constants_scalars() {
let source = "constants:
a: 1
b: 2";
let expected = Source(vec![SourceSection::Constants(Constants {
constants: vec![
Constant::new(Identifier("a".to_string()), ConstantType::Scalar(1)),
Constant::new(Identifier("b".to_string()), ConstantType::Scalar(2)),
],
})]);
build_parse_test!(source).expect_ast(expected);
}

#[test]
fn constants_vectors() {
let source = "constants:
a: [1, 2, 3, 4]
b: [5, 6, 7, 8]";
let expected = Source(vec![SourceSection::Constants(Constants {
constants: vec![
Constant::new(
Identifier("a".to_string()),
ConstantType::Vector(vec![1, 2, 3, 4]),
),
Constant::new(
Identifier("b".to_string()),
ConstantType::Vector(vec![5, 6, 7, 8]),
),
],
})]);
build_parse_test!(source).expect_ast(expected);
}

#[test]
fn constants_matrices() {
let source = "constants:
a: [[1, 2], [3, 4]]
b: [[5, 6], [7, 8]]";
let expected = Source(vec![SourceSection::Constants(Constants {
constants: vec![
Constant::new(
Identifier("a".to_string()),
ConstantType::Matrix(vec![vec![1, 2], vec![3, 4]]),
),
Constant::new(
Identifier("b".to_string()),
ConstantType::Matrix(vec![vec![5, 6], vec![7, 8]]),
),
],
})]);
build_parse_test!(source).expect_ast(expected);
}

#[test]
fn error_empty_constants_section() {
let source = "
constants:
";
assert!(build_parse_test!(source).parse().is_err());
}
1 change: 1 addition & 0 deletions parser/src/parser/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ mod trace_columns;
mod transition_constraints;

mod comments;
mod constants;
mod expressions;
mod identifiers;

Expand Down

0 comments on commit 26a024a

Please sign in to comment.