project/com.nokia.carbide.cpp.epoc.engine.tests/src/com/nokia/carbide/cpp/epoc/engine/tests/model/TestMakefileView.java
changeset 0 fb279309251b
child 610 bfb3ab3f70f2
equal deleted inserted replaced
-1:000000000000 0:fb279309251b
       
     1 /*
       
     2 * Copyright (c) 2006 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of the License "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 
       
    18 package com.nokia.carbide.cpp.epoc.engine.tests.model;
       
    19 
       
    20 import com.nokia.carbide.cpp.epoc.engine.DocumentFactory;
       
    21 import com.nokia.carbide.cpp.epoc.engine.model.IViewParserConfiguration;
       
    22 import com.nokia.carbide.cpp.epoc.engine.model.MakefileModelFactory;
       
    23 import com.nokia.carbide.cpp.epoc.engine.model.makefile.IMakefileOwnedModel;
       
    24 import com.nokia.carbide.cpp.epoc.engine.model.makefile.IMakefileView;
       
    25 import com.nokia.carbide.cpp.epoc.engine.model.makefile.IMakefileViewConfiguration;
       
    26 import com.nokia.carbide.cpp.epoc.engine.preprocessor.AcceptedNodesViewFilter;
       
    27 import com.nokia.carbide.cpp.epoc.engine.preprocessor.IDefine;
       
    28 import com.nokia.carbide.cpp.epoc.engine.preprocessor.IViewFilter;
       
    29 import com.nokia.carbide.cpp.epoc.engine.tests.BaseTest;
       
    30 import com.nokia.cpp.internal.api.utils.core.IMessage;
       
    31 
       
    32 import org.eclipse.cdt.make.core.makefile.ICommand;
       
    33 import org.eclipse.cdt.make.core.makefile.IMacroDefinition;
       
    34 import org.eclipse.cdt.make.core.makefile.IMakefile;
       
    35 import org.eclipse.cdt.make.core.makefile.ITargetRule;
       
    36 import org.eclipse.cdt.make.core.makefile.gnu.IConditional;
       
    37 import org.eclipse.core.runtime.IPath;
       
    38 import org.eclipse.jface.text.IDocument;
       
    39 
       
    40 import java.util.ArrayList;
       
    41 import java.util.Collection;
       
    42 
       
    43 
       
    44 public class TestMakefileView extends BaseTest {
       
    45 
       
    46 	private AcceptedNodesViewFilter viewFilter;
       
    47 	private ArrayList<IDefine> macros;
       
    48 	private IMakefileViewConfiguration viewConfig;
       
    49 	private IPath path;
       
    50 	private IMakefileOwnedModel model;
       
    51 		
       
    52 	/* (non-Javadoc)
       
    53 	 * @see com.nokia.carbide.cpp.epoc.engine.tests.BaseTest#setUp()
       
    54 	 */
       
    55 	@Override
       
    56 	protected void setUp() throws Exception {
       
    57 		super.setUp();
       
    58 		
       
    59 		this.path = projectPath.append("group/Icons_scalable_dc.mk");
       
    60 
       
    61 		
       
    62 		viewFilter = new AcceptedNodesViewFilter();
       
    63 		macros = new ArrayList<IDefine>();
       
    64 		viewConfig = new IMakefileViewConfiguration() {
       
    65 
       
    66 			public IViewFilter getViewFilter() {
       
    67 				return viewFilter;
       
    68 			}
       
    69 
       
    70 			public Collection<IDefine> getMacros() {
       
    71 				return macros;
       
    72 			}
       
    73 			
       
    74 			public IViewParserConfiguration getViewParserConfiguration() {
       
    75 				return parserConfig;
       
    76 			}
       
    77 
       
    78 			/* (non-Javadoc)
       
    79 			 * @see com.nokia.carbide.cpp.epoc.engine.model.makefile.IMakefileViewConfiguration#getMakefileStyle()
       
    80 			 */
       
    81 			public String getMakefileStyle() {
       
    82 				return "GNU";
       
    83 			}
       
    84 		};
       
    85 
       
    86 	}
       
    87 	
       
    88 	protected void makeModel(String text) {
       
    89 		IDocument document = DocumentFactory.createDocument(text);
       
    90 		model = new MakefileModelFactory().createModel(path, document);
       
    91 
       
    92 		model.parse();
       
    93 	}
       
    94 	
       
    95 	protected IMakefileView getView(IMakefileViewConfiguration config) {
       
    96 		IMakefileView view = (IMakefileView) model.createView(config);
       
    97 		assertNotNull(view);
       
    98 		return view;
       
    99 	}
       
   100 	
       
   101 	protected void commitTest(IMakefileView view, String refText) {
       
   102 		commitTest(model, view, refText);
       
   103 	}
       
   104 	
       
   105 	final String TEST_1 =
       
   106 		"# Very special makefile\n"+
       
   107 		"SRCS=foo.cpp bar.cpp\n"+
       
   108 		"OBJS=$(SRCS:.cpp=.obj)\n"+
       
   109 		"\n"+
       
   110 		"\n"+
       
   111 		"all: test myfile.exe\n"+
       
   112 		"\n"+
       
   113 		"\n"+
       
   114 		"test:\n"+
       
   115 		"\techo \"Test happens\"\n"+
       
   116 		".PHONY:\n"+
       
   117 		"myfile.exe: $(OBJS)\n"+
       
   118 		"\tld -o $< $^ -lc\n"+
       
   119 		"\n";
       
   120 
       
   121 	public void testParseBasic() {
       
   122 		// ensure everything's wired up properly to the CDT make core plugin
       
   123 		makeModel(TEST_1);
       
   124 		
       
   125 		IMakefileView view = getView(viewConfig);
       
   126 		assertNotNull(view);
       
   127 		
       
   128 		assertNotNull(view.getMakefile());
       
   129 
       
   130 		// basic test 
       
   131 		IMakefile makefile = view.getMakefile();
       
   132 		assertEquals(2, makefile.getMacroDefinitions().length);
       
   133 		assertEquals(9, makefile.getDirectives().length);
       
   134 		assertEquals(4, makefile.getRules().length);
       
   135 		
       
   136 		view.dispose();
       
   137 		model.dispose();
       
   138 	}
       
   139 	
       
   140 	public void testEOL() {
       
   141 		makeModel("FOO=6\r\n"+
       
   142 				"BAR=33\r\n");
       
   143 		
       
   144 		IMakefileView view = getView(viewConfig);
       
   145 		assertEquals("\r\n", view.getEOL());
       
   146 	}
       
   147 
       
   148 	public void testRewriteBasic() {
       
   149 		// test that we can rewrite the document without any changes
       
   150 		makeModel(TEST_1);
       
   151 		
       
   152 		IMakefileView view = getView(viewConfig);
       
   153 		assertNotNull(view);
       
   154 
       
   155 		assertFalse(view.hasChanges());
       
   156 		
       
   157 		commitTest(view, TEST_1);
       
   158 		view.dispose();
       
   159 		model.dispose();
       
   160 	}
       
   161 	
       
   162 	public void testMacroExpansion() {
       
   163 		makeModel(TEST_1);
       
   164 		
       
   165 		IMakefileView view = getView(viewConfig);
       
   166 		assertEquals("SRCS", view.getMakefile().expandString("SRCS"));
       
   167 		assertEquals("these are foo.cpp bar.cpp, man", view.getMakefile().expandString("these are $(SRCS), man"));
       
   168 		
       
   169 		assertFalse(view.hasChanges());
       
   170 		commitTest(view, TEST_1);
       
   171 		
       
   172 		view.dispose();
       
   173 		model.dispose();
       
   174 	}
       
   175 
       
   176 	public void testCommandsInvoking() {
       
   177 		makeModel(TEST_1);
       
   178 
       
   179 		IMakefileView view = getView(viewConfig);
       
   180 
       
   181 		ICommand[] commands = view.findCommandsInvoking("ld");
       
   182 		assertEquals(1, commands.length);
       
   183 		assertNotNull(commands[0].getParent());
       
   184 	
       
   185 		makeModel("TOOLSDIR=c:\\my\\tools\n"+
       
   186 				"MIFCONV=$(TOOLDIR)\\mifconv.exe\n"+
       
   187 				"\n"+
       
   188 				"all: mytool\n"+
       
   189 				"\n"+
       
   190 				"foo.mif: foo.svg\n"+
       
   191 				"\t$(MIFCONV) -o $@ $^\n"+
       
   192 				"\t$(MIFCONV) -o $@.bak $^\n");
       
   193 		
       
   194 		view = getView(viewConfig);
       
   195 
       
   196 		commands = view.findCommandsInvoking("mifconv");
       
   197 		assertEquals(2, commands.length);
       
   198 		assertNotNull(commands[0].getParent());
       
   199 		ITargetRule oldRule = (ITargetRule) commands[1].getParent();
       
   200 		assertNotNull(oldRule);
       
   201 		
       
   202 		// some more tests...
       
   203 		String newRule = oldRule.getTarget() + ": " + oldRule.getPrerequisites()[0] + " foo.h" + view.getEOL()
       
   204 		+ oldRule.getCommands()[0];
       
   205 		
       
   206 		view.replaceDirective(oldRule, newRule);
       
   207 
       
   208 		commitTest(view, "TOOLSDIR=c:\\my\\tools\n"+
       
   209 				"MIFCONV=$(TOOLDIR)\\mifconv.exe\n"+
       
   210 				"\n"+
       
   211 				"all: mytool\n"+
       
   212 				"\n"+
       
   213 				"foo.mif: foo.svg foo.h\n"+
       
   214 				"\t$(MIFCONV) -o $@ $^\n");
       
   215 
       
   216 		
       
   217 	}
       
   218 	
       
   219 	final String TEST_2 =
       
   220 		"ifeq ($(PLATFORM), WINS)\n"+
       
   221 		"ZDIR=$(EPOCROOT)\\release\\wins\\z\n"+
       
   222 		"else\n"+
       
   223 		"ZDIR=$(EPOCROOT)\\release\\$(PLATFORM)\\$(RELEASE)\\$(TARGET)\\z\n"+
       
   224 		"endif\n"+
       
   225 		"RESOURCE: $(ZDIR)\\sys\\gfx\\foo.mif\n";
       
   226 	final String TEST_2_a =
       
   227 		"ifeq ($(PLATFORM), WINS)\n"+
       
   228 		"ZDIR=$(EPOCROOT)\\release\\wins\\z\n"+
       
   229 		"else\n"+
       
   230 		"ZDIR=FOO\n"+
       
   231 		"endif\n"+
       
   232 		"RESOURCE: $(ZDIR)\\sys\\gfx\\foo.mif\n";
       
   233 
       
   234 	public void testQueryMacroDefs() {
       
   235 		makeModel(TEST_1);
       
   236 
       
   237 		IMakefileView view = getView(viewConfig);
       
   238 		IMacroDefinition definition = view.getMakefile().getMacroDefinitions("OBJS")[0];
       
   239 		assertNotNull(definition);
       
   240 		
       
   241 		definition = view.getMakefile().getMacroDefinitions("SRCS")[0];
       
   242 		assertNotNull(definition);
       
   243 		
       
   244 		////////
       
   245 		
       
   246 		makeModel(TEST_2);
       
   247 		view = getView(viewConfig);
       
   248 		IMacroDefinition[] defs = view.getAllMacroDefinitions("ZDIR");
       
   249 		assertNotNull(defs);
       
   250 		
       
   251 		assertEquals(2, defs.length);
       
   252 		assertTrue(defs[0].getParent() instanceof IConditional);
       
   253 		assertTrue(defs[1].getParent() instanceof IConditional);
       
   254 		assertNotSame(defs[0].getParent(), defs[1].getParent());
       
   255 		
       
   256 	}
       
   257 	
       
   258 	public void testQueryTargets() {
       
   259 		makeModel(TEST_1);
       
   260 
       
   261 		IMakefileView view = getView(viewConfig);
       
   262 
       
   263 		ITargetRule rule = view.findRuleForTarget("myfile.exe", false);
       
   264 		assertNotNull(rule);
       
   265 		
       
   266 		makeModel("BLD : do_nothing\r\n" + 
       
   267 				"\r\n" + 
       
   268 				"CLEAN : do_nothing\r\n" + 
       
   269 				"\r\n" + 
       
   270 				"LIB : do_nothing\r\n" + 
       
   271 				"\r\n" + 
       
   272 				"CLEANLIB : do_nothing\r\n" + 
       
   273 				"\r\n" + 
       
   274 				"RESOURCE :	\r\n" + 
       
   275 				"	mifconv $(ICONTARGETFILENAME) \\\r\n" + 
       
   276 				"		/c32 $(ICONDIR)\\qgn_menu_Birthdays.svg\r\n" + 
       
   277 				"		\r\n" + 
       
   278 				"FREEZE : do_nothing\r\n" + 
       
   279 				"\r\n" + 
       
   280 				"SAVESPACE : do_nothing\r\n" + 
       
   281 				"\r\n" + 
       
   282 				"RELEASABLES :\r\n" + 
       
   283 				"	@echo $(ICONTARGETFILENAME)\r\n" + 
       
   284 				"\r\n" + 
       
   285 				"FINAL : do_nothing\r\n" + 
       
   286 				"\r\n"); 
       
   287 		view = getView(viewConfig);
       
   288 		
       
   289 		rule = view.findRuleForTarget("RESOURCE", true);
       
   290 		assertNotNull(rule);
       
   291 		
       
   292 		ICommand[] commands = view.findCommandsInvoking("mifconv");
       
   293 		assertEquals(1, commands.length);
       
   294 		assertEquals(rule, commands[0].getParent());
       
   295 		
       
   296 		view.dispose();
       
   297 		model.dispose();
       
   298 	}
       
   299 	
       
   300 	final String TEST_1_a =	
       
   301 		TEST_1 + 	// we lose the final newlines
       
   302 		"FOO=123  # yup\n";
       
   303 
       
   304 	final String TEST_1_b =
       
   305 		"virus.exe:\n"+
       
   306 		"\tgcc -o $@ doom.c\n"+
       
   307 		TEST_1_a; 
       
   308 
       
   309 
       
   310 	public void testAddedDirectives() {
       
   311 		makeModel(TEST_1);
       
   312 		IMakefileView view = getView(viewConfig);
       
   313 		
       
   314 		assertEquals(2, view.getMakefile().getMacroDefinitions().length);
       
   315 		view.insertTextBefore("FOO=123  # yup\n", null);
       
   316 		assertTrue(view.hasChanges());
       
   317 		
       
   318 		assertEquals(3, view.getMakefile().getMacroDefinitions().length);
       
   319 		assertEquals("123=FOO", view.getMakefile().expandString("$(FOO)=FOO"));
       
   320 		
       
   321 		commitTest(view,
       
   322 				TEST_1_a);
       
   323 		
       
   324 		String target = "virus.exe:\n" + "\tgcc -o $@ doom.c\n";
       
   325 		view.insertText(null, target);
       
   326 		
       
   327 		commitTest(view,
       
   328 				TEST_1_b
       
   329 				);
       
   330 				
       
   331 		
       
   332 		view.dispose();
       
   333 		model.dispose();
       
   334 	}
       
   335 
       
   336 	public void testOverrideDirectives() {
       
   337 		makeModel(TEST_1);
       
   338 		
       
   339 		IMakefileView view = getView(viewConfig);
       
   340 		IMacroDefinition oldDef = view.getMakefile().getMacroDefinitions("SRCS")[0];
       
   341 		String newVal = oldDef.getValue() + " more.cpp";
       
   342 		String newText = oldDef.getName() + " = " + newVal + "\n";
       
   343 		view.replaceDirective(oldDef, newText);
       
   344 		
       
   345 		IMacroDefinition checkDef = view.getMakefile().getMacroDefinitions("SRCS")[0];
       
   346 		assertEquals(newText, checkDef.toString());
       
   347 	
       
   348 		view.dispose();
       
   349 		model.dispose();
       
   350 		
       
   351 		///////
       
   352 		
       
   353 		makeModel(TEST_2);
       
   354 		view = getView(viewConfig);
       
   355 		IMacroDefinition[] defs = view.getAllMacroDefinitions("ZDIR");
       
   356 		assertNotNull(defs);
       
   357 		
       
   358 		assertEquals(2, defs.length);
       
   359 
       
   360 		oldDef = defs[1];
       
   361 		
       
   362 		view.replaceDirective(oldDef, "ZDIR=FOO" + view.getEOL());
       
   363 
       
   364 		commitTest(view, TEST_2_a);
       
   365 
       
   366 	}
       
   367 	
       
   368 	public void testUnexpand() {
       
   369 		makeModel("ZDIR=$(EPOCROOT)\\release\\winscw\n"+
       
   370 				"TARGETDIR=$(ZDIR)\\data\n"+
       
   371 				"ICONTARGETFILENAME=$(TARGETDIR)\\foo.mif\n");
       
   372 		IMakefileView view = getView(viewConfig);
       
   373 
       
   374 		String repl = view.unexpandMacros("$(EPOCROOT)\\release\\winscw\\data\\more.mif", 
       
   375 				new String[] {  "TARGETDIR", "ZDIR" });
       
   376 		assertEquals("$(TARGETDIR)\\more.mif", repl);
       
   377 
       
   378 		String repl1 = view.unexpandMacros("1 2 $(EPOCROOT)\\release\\winscw\\data\\more.mif 3 4", 
       
   379 				new String[] {  "TARGETDIR", "ZDIR" });
       
   380 		assertEquals("1 2 $(TARGETDIR)\\more.mif 3 4", repl1);
       
   381 
       
   382 		String repl2 = view.unexpandMacros("$(EPOCROOT)\\release\\winscw\\more.mif", 
       
   383 				new String[] {  "TARGETDIR", "ZDIR" });
       
   384 		assertEquals("$(ZDIR)\\more.mif", repl2);
       
   385 
       
   386 		String repl3 = view.unexpandMacros("$(EPOCROOT)\\release\\foo\\data\\more.mif", 
       
   387 				new String[] {  "TARGETDIR", "ZDIR" });
       
   388 		assertEquals("$(EPOCROOT)\\release\\foo\\data\\more.mif", repl3);
       
   389 
       
   390 		String repl4 = view.unexpandMacros("\\release\\winscw\\data\\more.mif", 
       
   391 				new String[] {  "TARGETDIR", "ZDIR" });
       
   392 		assertEquals("\\release\\winscw\\data\\more.mif", repl4);
       
   393 
       
   394 	}
       
   395 	
       
   396 	public void testProblems() {
       
   397 		makeModel("\n\n)ZDIR@foo{\n"+
       
   398 				"^^ placate ^^\n");
       
   399 		IMakefileView view = getView(viewConfig);
       
   400 		IMessage[] messages = view.getMessages();
       
   401 		for (IMessage msg : messages)
       
   402 			System.out.println(msg);
       
   403 		assertEquals(2, messages.length);
       
   404 		assertEquals(IMessage.ERROR, messages[0].getSeverity());
       
   405 		assertEquals(IMessage.ERROR, messages[1].getSeverity());
       
   406 	}
       
   407 }