summaryrefslogtreecommitdiffstats
path: root/third_party/rust/glsl/tests/left_associativity.rs
blob: 7e2a19cfb4a74145d179f34d553cc9d22d34591d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
use glsl::parser::Parse;
use glsl::syntax;

#[test]
fn left_associativity() {
  for (opstr, opname) in [
    ("+", syntax::BinaryOp::Add),
    ("&&", syntax::BinaryOp::And),
    ("||", syntax::BinaryOp::Or),
  ]
  .iter()
  {
    let r = syntax::TranslationUnit::parse(format!(
      "
      void main() {{
        x = a {op} b {op} c;
      }}
    ",
      op = opstr
    ));

    let expected = syntax::TranslationUnit::from_non_empty_iter(vec![
      syntax::ExternalDeclaration::FunctionDefinition(syntax::FunctionDefinition {
        prototype: syntax::FunctionPrototype {
          ty: syntax::FullySpecifiedType {
            qualifier: None,
            ty: syntax::TypeSpecifier {
              ty: syntax::TypeSpecifierNonArray::Void,
              array_specifier: None,
            },
          },
          name: "main".into(),
          parameters: Vec::new(),
        },
        statement: syntax::CompoundStatement {
          statement_list: vec![syntax::Statement::Simple(Box::new(
            syntax::SimpleStatement::Expression(Some(syntax::Expr::Assignment(
              Box::new(syntax::Expr::Variable("x".into())),
              syntax::AssignmentOp::Equal,
              Box::new(syntax::Expr::Binary(
                opname.clone(),
                Box::new(syntax::Expr::Binary(
                  opname.clone(),
                  Box::new(syntax::Expr::Variable("a".into())),
                  Box::new(syntax::Expr::Variable("b".into())),
                )),
                Box::new(syntax::Expr::Variable("c".into())),
              )),
            ))),
          ))],
        },
      }),
    ])
    .unwrap();

    assert_eq!(r, Ok(expected));
  }
}