Skip to content

Commit

Permalink
More Binder Testing
Browse files Browse the repository at this point in the history
These unit tests are cumbersome to write. Time to call it a day and
work on the expression builder API I think.
  • Loading branch information
iwillspeak committed Jul 3, 2019
1 parent cdb503f commit 3e10083
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 7 deletions.
10 changes: 9 additions & 1 deletion spec/fail/badcalls.ulg
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,12 @@ foo(100, 100) # !> 12:9:error: Invalid argument. Expected 'String' but found 'Nu

# !> 16:4:error: Invalid argument. Expected 'Number' but found 'String'
# !> 16:13:error: Invalid argument. Expected 'String' but found 'Bool'
foo('hello', false)
foo('hello', false)

let bar = 100

# !> 21:0:error: Called item is not a function
bar()

# !> 24:0:error: Called item is not a function
false()
57 changes: 51 additions & 6 deletions src/sem/binder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ pub enum Symbol {
/// ```
/// # let interner = Interner::new();
/// let mut scope = Scope::new();
///
///
/// assert!(scope.try_declare(interner.intern("foo"), Symbol::Type(Typ::Unit));
///
/// // we can look the symbols up later
Expand Down Expand Up @@ -852,7 +852,10 @@ mod test {
use super::super::BuiltinType;
use super::*;
use crate::syntax::text::Interner;
use crate::syntax::{IdentifierExpression, Literal, LiteralExpression, Token, TokenKind};
use crate::syntax::{
IdentifierExpression, InfixOperatorExpression, Literal, LiteralExpression,
PrefixExpression, Token, TokenKind,
};

#[test]
fn create_scope() {
Expand Down Expand Up @@ -1004,7 +1007,7 @@ mod test {
}

#[test]
fn bind_lookup() {
fn bind_identifier_lookup() {
let source = SourceText::new("");
let mut scope = Scope::new();
scope.try_declare(
Expand All @@ -1026,7 +1029,7 @@ mod test {
}

#[test]
fn bind_const() {
fn bind_const_value() {
let mut binder = Binder::new(Scope::new());

let bound = binder.bind_literal(&LiteralExpression {
Expand All @@ -1038,6 +1041,48 @@ mod test {
assert_eq!(Some(Typ::Builtin(BuiltinType::Number)), bound.typ);
}

// TODO: Do we want some kind of snapshot testing here to check
// the transformation / bind of known parses?
#[test]
fn bind_prefix_expression() {
let source = SourceText::new("");
let mut binder = Binder::new(Scope::new());

let bound = binder.bind_prefix(
&PrefixExpression {
op_token: Box::new(Token::new(TokenKind::Minus)),
op: PrefixOp::Negate,
inner: Box::new(syntax::Expression::constant_num(
Token::new(TokenKind::Literal(Literal::Number(23))),
23,
)),
},
&source,
);

assert_eq!(
ExpressionKind::Prefix(
PrefixOp::Negate,
Box::new(Expression::new(
ExpressionKind::Literal(Constant::Number(23)),
Some(Typ::Builtin(BuiltinType::Number))
))
),
bound.kind
);
assert_eq!(Some(Typ::Builtin(BuiltinType::Number)), bound.typ);
}

// TODO: need a better way of creating the expression trees to run
// the binder over for these tests. More complex tests may
// also benefit from snapshot testing.

// Infix(ref innie)
// Call(ref call)
// Index(ref index)
// IfThenElse(ref if_else_expr)
// Function(ref func)
// Loop(ref loop_expr)
// Sequence(ref exprs)
// Print(ref print)
// Declaration(ref decl)
// Grouping(ref group)
}

0 comments on commit 3e10083

Please sign in to comment.