blob: 756bad84768f1fa61ffcd9c655f19da0cdc1ee2a [file] [log] [blame]
/*
* Copyright (c) 2012 Google Inc.
*
* 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
*/
package com.google.eclipse.protobuf.cdt.mapping;
import static com.google.eclipse.protobuf.junit.core.UnitTestModule.unitTestModule;
import static com.google.eclipse.protobuf.junit.core.XtextRule.overrideRuntimeModuleWith;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import org.eclipse.cdt.core.dom.ast.IBinding;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPBase;
import org.eclipse.cdt.internal.core.dom.parser.c.CASTName;
import org.eclipse.cdt.internal.core.dom.parser.cpp.*;
import org.eclipse.xtext.naming.QualifiedName;
import org.junit.*;
import com.google.eclipse.protobuf.junit.core.*;
import com.google.eclipse.protobuf.protobuf.Message;
import com.google.inject.Inject;
/**
* Tests for <code>{@link ClassMappingStrategy#createMappingFrom(IBinding)}</code>
*
* @author alruiz@google.com (Alex Ruiz)
*/
@SuppressWarnings("restriction")
public class ClassMappingStrategy_createMappingFrom_Test {
@Rule public XtextRule xtext = overrideRuntimeModuleWith(unitTestModule(), new TestModule());
@Inject private CPPClassType classType;
@Inject private ClassMappingStrategy mappingStrategy;
@Test public void should_return_qualified_name_for_class_type_if_it_extends_proto_file() {
expectClassTypeToExtendProtoMessage();
String[] segments = { "com", "google", "proto", "Test" };
when(classType.getQualifiedName()).thenReturn(segments);
CppToProtobufMapping mapping = mappingStrategy.createMappingFrom(classType);
assertThat(mapping.qualifiedName(), equalTo(QualifiedName.create(segments)));
assertEquals(Message.class, mapping.type());
}
private void expectClassTypeToExtendProtoMessage() {
ICPPBase base = mock(ICPPBase.class);
when(classType.getBases()).thenReturn(new ICPPBase[] { base });
when(base.getBaseClassSpecifierName()).thenReturn(createQualifiedName("google", "protobuf", "Message"));
}
private CPPASTQualifiedName createQualifiedName(String...segments) {
CPPASTQualifiedName qualifiedName = new CPPASTQualifiedName();
for (String segment : segments) {
qualifiedName.addName(new CASTName(segment.toCharArray()));
}
qualifiedName.setFullyQualified(true);
return qualifiedName;
}
@Test public void should_return_null_if_class_type_does_not_extend_proto_message() {
when(classType.getBases()).thenReturn(new ICPPBase[0]);
CppToProtobufMapping mapping = mappingStrategy.createMappingFrom(classType);
assertNull(mapping);
}
private static class TestModule extends AbstractTestModule {
@Override protected void configure() {
mockAndBind(CPPClassType.class);
}
}
}