PageRenderTime 26ms CodeModel.GetById 18ms RepoModel.GetById 0ms 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
Possible License(s): Apache-2.0
  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. */
  16. package com.google.test.metric.cpp.dom;
  17. import com.google.test.metric.cpp.Parser;
  18. import junit.framework.TestCase;
  19. public class NodeTest extends TestCase {
  20. private TranslationUnit parse(String source) throws Exception {
  21. return new Parser().parse(source);
  22. }
  23. public void testSimpleVariableLookup() throws Exception {
  24. TranslationUnit unit = parse("void foo() { int a = 0; int b = a; };");
  25. FunctionDefinition functionFoo = unit.getChild(0);
  26. VariableDeclaration variableB = functionFoo.getChild(1);
  27. VariableDeclaration variableA = variableB.lookupVariable("a");
  28. assertNotNull(variableA);
  29. assertEquals(functionFoo.getChild(0), variableA);
  30. }
  31. public void testVariableLookup() throws Exception {
  32. TranslationUnit unit = parse("void foo() { int a = 0; if (a == 0) { int b = a; } }");
  33. FunctionDefinition functionFoo = unit.getChild(0);
  34. IfStatement ifStatement = functionFoo.getChild(1);
  35. VariableDeclaration variableB = ifStatement.getChild(0);
  36. // lookup variable a in the context of declaration of variable b
  37. VariableDeclaration variableA = variableB.lookupVariable("a");
  38. assertNotNull(variableA);
  39. assertEquals(functionFoo.getChild(0), variableA);
  40. }
  41. public void testGlobalVariableLookup() throws Exception {
  42. TranslationUnit unit = parse("int a = 0; void foo() { int b = a; }");
  43. FunctionDefinition functionFoo = unit.getChild(1);
  44. VariableDeclaration variableB = functionFoo.getChild(0);
  45. // lookup variable a in the context of declaration of variable b
  46. VariableDeclaration variableA = variableB.lookupVariable("a");
  47. assertNotNull(variableA);
  48. assertEquals(unit.getChild(0), variableA);
  49. }
  50. public void testVariableLookupFailure() throws Exception {
  51. TranslationUnit unit = parse("void foo() { if (true) { int a = 0; } int b = 1; }");
  52. FunctionDefinition functionFoo = unit.getChild(0);
  53. VariableDeclaration variableB = functionFoo.getChild(1);
  54. // lookup variable a in the context of declaration of variable b
  55. VariableDeclaration variableA = variableB.lookupVariable("a");
  56. assertNull(variableA);
  57. }
  58. public void testUnnamedNamespaceVariableLookup() throws Exception {
  59. TranslationUnit unit = parse("namespace { int a = 0; } void foo() { int b = a; }");
  60. FunctionDefinition functionFoo = unit.getChild(1);
  61. VariableDeclaration variableB = functionFoo.getChild(0);
  62. // lookup variable a in the context of declaration of variable b
  63. VariableDeclaration variableA = variableB.lookupVariable("a");
  64. assertNotNull(variableA);
  65. Namespace namespace = unit.getChild(0);
  66. assertEquals(namespace.getChild(0), variableA);
  67. }
  68. public void testNamedNamespaceVariableLookup() throws Exception {
  69. TranslationUnit unit = parse("namespace A { int a = 0; } void foo() { int b = A::a; }");
  70. FunctionDefinition functionFoo = unit.getChild(1);
  71. VariableDeclaration variableB = functionFoo.getChild(0);
  72. // lookup variable a in the context of declaration of variable b
  73. VariableDeclaration variableA = variableB.lookupVariable("A::a");
  74. assertNotNull(variableA);
  75. Namespace namespaceA = unit.getChild(0);
  76. assertEquals(namespaceA.getChild(0), variableA);
  77. }
  78. public void testComplexNamespaceVariableLookup() throws Exception {
  79. TranslationUnit unit = parse("namespace A { namespace B { int a = 0; } } void foo() { int b = A::B::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::B::a");
  84. assertNotNull(variableA);
  85. Namespace namespaceA = unit.getChild(0);
  86. Namespace namespaceB = namespaceA.getChild(0);
  87. assertEquals(namespaceB.getChild(0), variableA);
  88. }
  89. public void testNamespaceVariableLookup() throws Exception {
  90. TranslationUnit unit = parse("namespace A { namespace B { int a = 0; } } namespace C { void foo() { int b = A::B::a; } }");
  91. Namespace namespaceC = unit.getChild(1);
  92. FunctionDefinition functionFoo = namespaceC.getChild(0);
  93. VariableDeclaration variableB = functionFoo.getChild(0);
  94. // lookup variable a in the context of declaration of variable b
  95. VariableDeclaration variableA = variableB.lookupVariable("A::B::a");
  96. assertNotNull(variableA);
  97. Namespace namespaceA = unit.getChild(0);
  98. Namespace namespaceB = namespaceA.getChild(0);
  99. assertEquals(namespaceB.getChild(0), variableA);
  100. }
  101. public void testLateVariableLookup() throws Exception {
  102. TranslationUnit unit = parse("void foo() { int b = a; } int a = 0;");
  103. FunctionDefinition functionFoo = unit.getChild(0);
  104. VariableDeclaration variableB = functionFoo.getChild(0);
  105. // lookup variable a in the context of declaration of variable b
  106. VariableDeclaration variableA = variableB.lookupVariable("a");
  107. assertNull(variableA);
  108. }
  109. public void testFieldLookup() throws Exception {
  110. TranslationUnit unit = parse("class A{ int a; void foo() { int b = a; } };");
  111. ClassDeclaration classA = unit.getChild(0);
  112. FunctionDefinition functionFoo = classA.getChild(1);
  113. VariableDeclaration variableB = functionFoo.getChild(0);
  114. // lookup variable a in the context of declaration of variable b
  115. VariableDeclaration variableA = variableB.lookupVariable("a");
  116. assertNotNull(variableA);
  117. assertEquals("a", variableA.getName());
  118. }
  119. public void testFieldLookupPostDeclaration() throws Exception {
  120. TranslationUnit unit = parse("class A{ void foo() { int b = a; } int a; };");
  121. ClassDeclaration classA = unit.getChild(0);
  122. FunctionDefinition functionFoo = classA.getChild(0);
  123. VariableDeclaration variableB = functionFoo.getChild(0);
  124. // lookup variable a in the context of declaration of variable b
  125. VariableDeclaration variableA = variableB.lookupVariable("a");
  126. assertNotNull(variableA);
  127. assertEquals("a", variableA.getName());
  128. }
  129. }