PageRenderTime 38ms CodeModel.GetById 2ms app.highlight 32ms RepoModel.GetById 1ms app.codeStats 0ms

/cpp/src/test/java/com/google/test/metric/cpp/CppClassRepositoryTest.java

http://testability-explorer.googlecode.com/
Java | 146 lines | 119 code | 12 blank | 15 comment | 0 complexity | 7f627b8b6937beb64785f865f77659ba 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;
 17
 18import com.google.test.metric.ClassInfo;
 19import com.google.test.metric.LocalVariableInfo;
 20import com.google.test.metric.MethodInfo;
 21import com.google.test.metric.Visibility;
 22import com.google.test.metric.method.op.turing.FieldAssignment;
 23import com.google.test.metric.method.op.turing.LocalAssignment;
 24import com.google.test.metric.method.op.turing.Operation;
 25import com.google.test.metric.method.op.turing.ReturnOperation;
 26
 27import junit.framework.TestCase;
 28
 29import java.util.ArrayList;
 30import java.util.Iterator;
 31import java.util.List;
 32
 33public class CppClassRepositoryTest extends TestCase {
 34
 35  public void testSimpleClass() throws Exception {
 36    CppClassRepository repository = new CppClassRepository();
 37    repository.parse("class A{};");
 38    ClassInfo classInfo = repository.getClass("A");
 39    assertNotNull(classInfo);
 40    assertFalse(classInfo.isInterface());
 41    assertTrue(classInfo.getMethods().isEmpty());
 42  }
 43
 44  public void testRepositoryAddClass() throws Exception {
 45    CppClassRepository repository = new CppClassRepository();
 46    ClassInfo classInfo = new ClassInfo("A", false, null,
 47        new ArrayList<ClassInfo>(), null);
 48    repository.addClass(classInfo);
 49    assertNotNull(repository.getClass("A"));
 50  }
 51
 52  public void testClassWithMethod() throws Exception {
 53    CppClassRepository repository = new CppClassRepository();
 54    repository.parse("class A{ void foo() {} };");
 55    ClassInfo classInfo = repository.getClass("A");
 56    assertNotNull(classInfo);
 57    assertFalse(classInfo.isInterface());
 58    assertEquals(1, classInfo.getMethods().size());
 59    Iterator<MethodInfo> it = classInfo.getMethods().iterator();
 60    MethodInfo methodInfo = it.next();
 61    assertEquals("foo", methodInfo.getName());
 62    assertEquals(Visibility.PRIVATE, methodInfo.getVisibility());
 63  }
 64
 65  public void testClassWithMethodWithParameters() throws Exception {
 66    CppClassRepository repository = new CppClassRepository();
 67    repository.parse("class A{ public: void foo(int a, int b) {} };");
 68    ClassInfo classInfo = repository.getClass("A");
 69    assertNotNull(classInfo);
 70    assertFalse(classInfo.isInterface());
 71    assertEquals(1, classInfo.getMethods().size());
 72    Iterator<MethodInfo> it = classInfo.getMethods().iterator();
 73    MethodInfo methodInfo = it.next();
 74    assertEquals("foo", methodInfo.getName());
 75    assertEquals(2, methodInfo.getParameters().size());
 76    assertEquals(Visibility.PUBLIC, methodInfo.getVisibility());
 77  }
 78
 79  public void testMethodWithLocalVariables() throws Exception {
 80    CppClassRepository repository = new CppClassRepository();
 81    repository.parse("class A{ protected: void foo() { int a; int b; } };");
 82    ClassInfo classInfo = repository.getClass("A");
 83    assertNotNull(classInfo);
 84    assertFalse(classInfo.isInterface());
 85    assertEquals(1, classInfo.getMethods().size());
 86    Iterator<MethodInfo> it = classInfo.getMethods().iterator();
 87    MethodInfo methodInfo = it.next();
 88    assertEquals("foo", methodInfo.getName());
 89    List<LocalVariableInfo> localVariables = methodInfo.getLocalVariables();
 90    assertEquals(2, localVariables.size());
 91    LocalVariableInfo variableA = localVariables.get(0);
 92    assertEquals("a", variableA.getName());
 93    LocalVariableInfo variableB = localVariables.get(1);
 94    assertEquals("b", variableB.getName());
 95    assertEquals(Visibility.PROTECTED, methodInfo.getVisibility());
 96  }
 97
 98  public void testMethodReturnOperation() throws Exception {
 99    CppClassRepository repository = new CppClassRepository();
100    repository.parse("class A{ protected: void foo() { return; } };");
101    ClassInfo classInfo = repository.getClass("A");
102    assertNotNull(classInfo);
103    Iterator<MethodInfo> it = classInfo.getMethods().iterator();
104    MethodInfo methodInfo = it.next();
105    List<Operation> operations = methodInfo.getOperations();
106    assertEquals(1, operations.size());
107    Operation operation = operations.get(0);
108    assertTrue(operation instanceof ReturnOperation);
109    ReturnOperation returnOperation = (ReturnOperation) operation;
110    assertEquals(1, returnOperation.getLineNumber());
111  }
112
113  public void testLocalAssignmentOperation() throws Exception {
114    CppClassRepository repository = new CppClassRepository();
115    repository.parse("class A{ protected: void foo() { int a = 0; int b = 1; b = a; } };");
116    ClassInfo classInfo = repository.getClass("A");
117    assertNotNull(classInfo);
118    Iterator<MethodInfo> it = classInfo.getMethods().iterator();
119    MethodInfo methodInfo = it.next();
120    List<Operation> operations = methodInfo.getOperations();
121    assertEquals(1, operations.size());
122    Operation operation = operations.get(0);
123    assertTrue(operation instanceof LocalAssignment);
124    LocalAssignment localAssignment = (LocalAssignment) operation;
125    assertEquals(1, localAssignment.getLineNumber());
126    assertEquals("b", localAssignment.getVariable().getName());
127    assertEquals("a", localAssignment.getValue().getName());
128  }
129
130  public void testFieldAssignmentOperation() throws Exception {
131    CppClassRepository repository = new CppClassRepository();
132    repository.parse("class A{ void foo() { int b = 1; a = b; } int a; };");
133    ClassInfo classInfo = repository.getClass("A");
134    assertNotNull(classInfo);
135    Iterator<MethodInfo> it = classInfo.getMethods().iterator();
136    MethodInfo methodInfo = it.next();
137    List<Operation> operations = methodInfo.getOperations();
138    assertEquals(1, operations.size());
139    Operation operation = operations.get(0);
140    assertTrue(operation instanceof FieldAssignment);
141    FieldAssignment fieldAssignment = (FieldAssignment) operation;
142    assertEquals(1, fieldAssignment.getLineNumber());
143    assertEquals("a", fieldAssignment.getFieldInstance().getName());
144    assertEquals("b", fieldAssignment.getValue().getName());
145  }
146}