Skip to main content
summaryrefslogtreecommitdiffstats
blob: 1b3390dc6233c2817d7210cd92b1cc0167e82cca (plain) (blame)
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
Copyright (c) 2008 Arno Haase.
All rights reserved. This program and the accompanying materials
are made available under the terms of the Eclipse Public License v1.0
which accompanies this distribution, and is available at
http://www.eclipse.org/legal/epl-v10.html

Contributors:
    Arno Haase - initial API and implementation
 */
package org.eclipse.xtend.backend.common;

import static org.eclipse.xtend.backend.testhelpers.BackendTestHelper.*;
import static org.junit.Assert.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.junit.Before;
import org.junit.Test;


/**
 * 
 * @author Arno Haase (http://www.haase-consulting.com)
 */
public class ExpressionBaseTest {
    private final List<String> _log = new ArrayList<String> ();
    
    @Before
    public void init () {
        _log.clear ();
    }
    
    private class TestingExecutionListener implements ExecutionListener {
        private final String _id;
        private final Object _vetoValue;
        
        public TestingExecutionListener (String id, Object vetoValue) {
            _id = id;
            _vetoValue = vetoValue;
        }

        public void preExecute (ExecutionContext ctx, ExpressionBase originator) throws EvaluationVetoException {
            _log.add("pre" + _id);
            if (_vetoValue != null)
                throw new EvaluationVetoException (_vetoValue);
        }
        
        public void postExecute (Object result, ExecutionContext ctx, ExpressionBase originator) {
            _log.add ("post" + _id);
        }
    }
    
    
    
    @Test
    public void testVeto () {
        final ExpressionBase expr = new ExpressionBase (createSourcePos()) {

            @Override
            protected Object evaluateInternal (ExecutionContext ctx) {
                ExpressionBaseTest.this._log.add ("evaluated");
                return null;
            }
        };
        
        expr.registerExecutionListener (new TestingExecutionListener ("First", "veto1"));
        expr.registerExecutionListener (new TestingExecutionListener ("Second", "veto2"));
        
        assertEquals ("veto1", expr.evaluate (createEmptyExecutionContext()));
        assertEquals (Arrays.asList ("preFirst"), _log);
        
    }
    
    
    @Test 
    public void testExecutionListener () {
        final ExpressionBase expr = createLiteral ("a");

        expr.registerExecutionListener (new TestingExecutionListener ("First", null));
        expr.registerExecutionListener (new TestingExecutionListener ("Second", null));
        
        expr.evaluate (createEmptyExecutionContext());
        
        assertEquals (Arrays.asList ("preFirst", "preSecond", "postSecond", "postFirst"), _log);
    }
}

Back to the top