PageRenderTime 37ms CodeModel.GetById 15ms app.highlight 19ms RepoModel.GetById 1ms app.codeStats 0ms

/cpp/src/test/java/com/google/test/metric/cpp/dom/NodeTest.java

http://testability-explorer.googlecode.com/
Java | 144 lines | 104 code | 15 blank | 25 comment | 0 complexity | 89cbd94797899fdd053c7135baeac6ef MD5 | raw file
  1/*
  2 * Copyright 2008 Google Inc.
  3 *
  4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5 * use this file except in compliance with the License. You may obtain a copy of
  6 * the License at
  7 *
  8 * http://www.apache.org/licenses/LICENSE-2.0
  9 *
 10 * Unless required by applicable law or agreed to in writing, software
 11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 13 * License for the specific language governing permissions and limitations under
 14 * the License.
 15 */
 16package com.google.test.metric.cpp.dom;
 17
 18import com.google.test.metric.cpp.Parser;
 19
 20import junit.framework.TestCase;
 21
 22public class NodeTest extends TestCase {
 23
 24  private TranslationUnit parse(String source) throws Exception {
 25    return new Parser().parse(source);
 26  }
 27
 28  public void testSimpleVariableLookup() throws Exception {
 29    TranslationUnit unit = parse("void foo() { int a = 0; int b = a; };");
 30    FunctionDefinition functionFoo = unit.getChild(0);
 31    VariableDeclaration variableB = functionFoo.getChild(1);
 32    VariableDeclaration variableA = variableB.lookupVariable("a");
 33    assertNotNull(variableA);
 34    assertEquals(functionFoo.getChild(0), variableA);
 35  }
 36
 37  public void testVariableLookup() throws Exception {
 38    TranslationUnit unit = parse("void foo() { int a = 0; if (a == 0) { int b = a; } }");
 39    FunctionDefinition functionFoo = unit.getChild(0);
 40    IfStatement ifStatement = functionFoo.getChild(1);
 41    VariableDeclaration variableB = ifStatement.getChild(0);
 42    // lookup variable a in the context of declaration of variable b
 43    VariableDeclaration variableA = variableB.lookupVariable("a");
 44    assertNotNull(variableA);
 45    assertEquals(functionFoo.getChild(0), variableA);
 46  }
 47
 48  public void testGlobalVariableLookup() throws Exception {
 49    TranslationUnit unit = parse("int a = 0; void foo() { int b = a; }");
 50    FunctionDefinition functionFoo = unit.getChild(1);
 51    VariableDeclaration variableB = functionFoo.getChild(0);
 52    // lookup variable a in the context of declaration of variable b
 53    VariableDeclaration variableA = variableB.lookupVariable("a");
 54    assertNotNull(variableA);
 55    assertEquals(unit.getChild(0), variableA);
 56  }
 57
 58  public void testVariableLookupFailure() throws Exception {
 59    TranslationUnit unit = parse("void foo() { if (true) { int a = 0; } int b = 1; }");
 60    FunctionDefinition functionFoo = unit.getChild(0);
 61    VariableDeclaration variableB = functionFoo.getChild(1);
 62    // lookup variable a in the context of declaration of variable b
 63    VariableDeclaration variableA = variableB.lookupVariable("a");
 64    assertNull(variableA);
 65  }
 66
 67  public void testUnnamedNamespaceVariableLookup() throws Exception {
 68    TranslationUnit unit = parse("namespace { int a = 0; } void foo() { int b = a; }");
 69    FunctionDefinition functionFoo = unit.getChild(1);
 70    VariableDeclaration variableB = functionFoo.getChild(0);
 71    // lookup variable a in the context of declaration of variable b
 72    VariableDeclaration variableA = variableB.lookupVariable("a");
 73    assertNotNull(variableA);
 74    Namespace namespace = unit.getChild(0);
 75    assertEquals(namespace.getChild(0), variableA);
 76  }
 77
 78  public void testNamedNamespaceVariableLookup() throws Exception {
 79    TranslationUnit unit = parse("namespace A { int a = 0; } void foo() { int b = A::a; }");
 80    FunctionDefinition functionFoo = unit.getChild(1);
 81    VariableDeclaration variableB = functionFoo.getChild(0);
 82    // lookup variable a in the context of declaration of variable b
 83    VariableDeclaration variableA = variableB.lookupVariable("A::a");
 84    assertNotNull(variableA);
 85    Namespace namespaceA = unit.getChild(0);
 86    assertEquals(namespaceA.getChild(0), variableA);
 87  }
 88
 89  public void testComplexNamespaceVariableLookup() throws Exception {
 90    TranslationUnit unit = parse("namespace A { namespace B { int a = 0; } } void foo() { int b = A::B::a; }");
 91    FunctionDefinition functionFoo = unit.getChild(1);
 92    VariableDeclaration variableB = functionFoo.getChild(0);
 93    // lookup variable a in the context of declaration of variable b
 94    VariableDeclaration variableA = variableB.lookupVariable("A::B::a");
 95    assertNotNull(variableA);
 96    Namespace namespaceA = unit.getChild(0);
 97    Namespace namespaceB = namespaceA.getChild(0);
 98    assertEquals(namespaceB.getChild(0), variableA);
 99  }
100
101  public void testNamespaceVariableLookup() throws Exception {
102    TranslationUnit unit = parse("namespace A { namespace B { int a = 0; } } namespace C { void foo() { int b = A::B::a; } }");
103    Namespace namespaceC = unit.getChild(1);
104    FunctionDefinition functionFoo = namespaceC.getChild(0);
105    VariableDeclaration variableB = functionFoo.getChild(0);
106    // lookup variable a in the context of declaration of variable b
107    VariableDeclaration variableA = variableB.lookupVariable("A::B::a");
108    assertNotNull(variableA);
109    Namespace namespaceA = unit.getChild(0);
110    Namespace namespaceB = namespaceA.getChild(0);
111    assertEquals(namespaceB.getChild(0), variableA);
112  }
113
114  public void testLateVariableLookup() throws Exception {
115    TranslationUnit unit = parse("void foo() { int b = a; } int a = 0;");
116    FunctionDefinition functionFoo = unit.getChild(0);
117    VariableDeclaration variableB = functionFoo.getChild(0);
118    // lookup variable a in the context of declaration of variable b
119    VariableDeclaration variableA = variableB.lookupVariable("a");
120    assertNull(variableA);
121  }
122
123  public void testFieldLookup() throws Exception {
124    TranslationUnit unit = parse("class A{ int a; void foo() { int b = a; } };");
125    ClassDeclaration classA = unit.getChild(0);
126    FunctionDefinition functionFoo = classA.getChild(1);
127    VariableDeclaration variableB = functionFoo.getChild(0);
128    // lookup variable a in the context of declaration of variable b
129    VariableDeclaration variableA = variableB.lookupVariable("a");
130    assertNotNull(variableA);
131    assertEquals("a", variableA.getName());
132  }
133
134  public void testFieldLookupPostDeclaration() throws Exception {
135    TranslationUnit unit = parse("class A{ void foo() { int b = a; } int a; };");
136    ClassDeclaration classA = unit.getChild(0);
137    FunctionDefinition functionFoo = classA.getChild(0);
138    VariableDeclaration variableB = functionFoo.getChild(0);
139    // lookup variable a in the context of declaration of variable b
140    VariableDeclaration variableA = variableB.lookupVariable("a");
141    assertNotNull(variableA);
142    assertEquals("a", variableA.getName());
143  }
144}