1/*
2 * Copyright (C) 2007-2010 Júlio Vilmar Gesser.
3 * Copyright (C) 2011, 2013-2016 The JavaParser Team.
4 *
5 * This file is part of JavaParser.
6 *
7 * JavaParser can be used either under the terms of
8 * a) the GNU Lesser General Public License as published by
9 *     the Free Software Foundation, either version 3 of the License, or
10 *     (at your option) any later version.
11 * b) the terms of the Apache License
12 *
13 * You should have received a copy of both licenses in LICENCE.LGPL and
14 * LICENCE.APACHE. Please refer to those files for details.
15 *
16 * JavaParser is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 * GNU Lesser General Public License for more details.
20 */
21
22package com.github.javaparser.ast;
23
24import com.github.javaparser.*;
25import org.junit.Test;
26
27import java.io.IOException;
28import java.util.LinkedList;
29import java.util.List;
30
31import static org.junit.Assert.assertNotNull;
32import static org.junit.Assert.assertTrue;
33
34public class NodePositionTest {
35
36    private List<Node> getAllNodes(Node node) {
37        List<Node> nodes = new LinkedList<>();
38        nodes.add(node);
39        node.getChildNodes().forEach(c -> nodes.addAll(getAllNodes(c)));
40        return nodes;
41    }
42
43    @Test
44    public void packageProtectedClassShouldHavePositionSet() throws IOException {
45        ensureAllNodesHaveValidBeginPosition("class A { }");
46    }
47
48    @Test
49    public void packageProtectedInterfaceShouldHavePositionSet() throws IOException {
50        ensureAllNodesHaveValidBeginPosition("interface A { }");
51    }
52
53    @Test
54    public void packageProtectedEnumShouldHavePositionSet() throws IOException {
55        ensureAllNodesHaveValidBeginPosition("enum A { }");
56    }
57
58    @Test
59    public void packageProtectedAnnotationShouldHavePositionSet() throws IOException {
60        ensureAllNodesHaveValidBeginPosition("@interface A { }");
61    }
62
63    @Test
64    public void packageProtectedFieldShouldHavePositionSet() throws IOException {
65        ensureAllNodesHaveValidBeginPosition("public class A { int i; }");
66    }
67
68    @Test
69    public void packageProtectedMethodShouldHavePositionSet() throws IOException {
70      ensureAllNodesHaveValidBeginPosition("public class A { void foo() {} }");
71    }
72
73    @Test
74    public void packageProtectedConstructorShouldHavePositionSet() throws IOException {
75      ensureAllNodesHaveValidBeginPosition("public class A { A() {} }");
76    }
77
78    private void ensureAllNodesHaveValidBeginPosition(final String code) throws IOException {
79        ParseResult<CompilationUnit> res = new JavaParser().parse(ParseStart.COMPILATION_UNIT, Providers.provider(code));
80        assertTrue(res.getProblems().isEmpty());
81
82        CompilationUnit cu = res.getResult().get();
83        getAllNodes(cu).forEach(n -> {
84            assertNotNull(String.format("There should be no node without a range: %s (class: %s)",
85                    n, n.getClass().getCanonicalName()), n.getRange());
86            if (n.getBegin().get().line == 0 && !n.toString().isEmpty()) {
87                throw new IllegalArgumentException("There should be no node at line 0: " + n + " (class: "
88                        + n.getClass().getCanonicalName() + ")");
89            }
90        });
91    }
92}
93