~ubuntu-branches/debian/sid/eclipse-cdt/sid

« back to all changes in this revision

Viewing changes to core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/refactoring/includes/IncludeOrganizerTest.java

  • Committer: Package Import Robot
  • Author(s): Jakub Adam
  • Date: 2013-10-03 20:30:16 UTC
  • mfrom: (1.1.11)
  • Revision ID: package-import@ubuntu.com-20131003203016-d4ug6l0xgosasumq
Tags: 8.2.1-1
* New upstream release.
* Updated autotools documentation sources.

Show diffs side-by-side

added added

removed removed

Lines of Context:
10
10
 *******************************************************************************/
11
11
package org.eclipse.cdt.ui.tests.refactoring.includes;
12
12
 
13
 
import java.util.List;
 
13
import java.util.Collections;
14
14
 
15
15
import junit.framework.Test;
16
16
 
 
17
import org.eclipse.jface.preference.IPreferenceStore;
17
18
import org.eclipse.jface.text.Document;
18
19
import org.eclipse.jface.text.IDocument;
19
20
import org.eclipse.text.edits.MultiTextEdit;
20
 
import org.eclipse.text.edits.TextEdit;
21
21
 
22
22
import org.eclipse.cdt.core.model.ITranslationUnit;
23
23
import org.eclipse.cdt.ui.PreferenceConstants;
24
24
 
 
25
import org.eclipse.cdt.internal.ui.refactoring.includes.HeaderSubstitutionMap;
25
26
import org.eclipse.cdt.internal.ui.refactoring.includes.IHeaderChooser;
 
27
import org.eclipse.cdt.internal.ui.refactoring.includes.IncludeMap;
26
28
import org.eclipse.cdt.internal.ui.refactoring.includes.IncludeOrganizer;
 
29
import org.eclipse.cdt.internal.ui.refactoring.includes.IncludePreferences;
 
30
import org.eclipse.cdt.internal.ui.refactoring.includes.IncludePreferences.UnusedStatementDisposition;
 
31
import org.eclipse.cdt.internal.ui.refactoring.includes.SymbolExportMap;
27
32
 
28
33
/**
29
34
 * Tests for {@link IncludeOrganizer}.
45
50
        @Override
46
51
        protected void resetPreferences() {
47
52
                super.resetPreferences();
48
 
                getPreferenceStore().setToDefault(PreferenceConstants.INCLUDES_UNUSED_STATEMENTS_DISPOSITION);
49
 
                getPreferenceStore().setToDefault(PreferenceConstants.FORWARD_DECLARE_COMPOSITE_TYPES);
50
 
                getPreferenceStore().setToDefault(PreferenceConstants.FORWARD_DECLARE_ENUMS);
51
 
                getPreferenceStore().setToDefault(PreferenceConstants.FORWARD_DECLARE_FUNCTIONS);
52
 
                getPreferenceStore().setToDefault(PreferenceConstants.FORWARD_DECLARE_TEMPLATES);
53
 
                getPreferenceStore().setToDefault(PreferenceConstants.FORWARD_DECLARE_NAMESPACE_ELEMENTS);
54
 
                getPreferenceStore().setToDefault(PreferenceConstants.INCLUDES_ALLOW_REORDERING);
 
53
                IPreferenceStore preferenceStore = getPreferenceStore();
 
54
                preferenceStore.setToDefault(PreferenceConstants.INCLUDES_UNUSED_STATEMENTS_DISPOSITION);
 
55
                preferenceStore.setToDefault(PreferenceConstants.FORWARD_DECLARE_COMPOSITE_TYPES);
 
56
                preferenceStore.setToDefault(PreferenceConstants.FORWARD_DECLARE_ENUMS);
 
57
                preferenceStore.setToDefault(PreferenceConstants.FORWARD_DECLARE_FUNCTIONS);
 
58
                preferenceStore.setToDefault(PreferenceConstants.FORWARD_DECLARE_TEMPLATES);
 
59
                preferenceStore.setToDefault(PreferenceConstants.FORWARD_DECLARE_NAMESPACE_ELEMENTS);
 
60
                preferenceStore.setToDefault(PreferenceConstants.INCLUDES_ALLOW_REORDERING);
 
61
                preferenceStore.setToDefault(IncludePreferences.INCLUDES_HEADER_SUBSTITUTION);
 
62
                preferenceStore.setToDefault(IncludePreferences.INCLUDES_SYMBOL_EXPORTING_HEADERS);
55
63
        }
56
64
 
57
65
        private void assertExpectedResults() throws Exception {
65
73
        private String organizeIncludes(ITranslationUnit tu) throws Exception {
66
74
                IHeaderChooser headerChooser = new FirstHeaderChooser();
67
75
                IncludeOrganizer organizer = new IncludeOrganizer(tu, index, LINE_DELIMITER, headerChooser);
68
 
                List<TextEdit> edits = organizer.organizeIncludes(ast);
 
76
                MultiTextEdit edit = organizer.organizeIncludes(ast);
69
77
                IDocument document = new Document(new String(tu.getContents()));
70
 
                if (!edits.isEmpty()) {
71
 
                        // Apply text edits.
72
 
                        MultiTextEdit edit = new MultiTextEdit();
73
 
                        edit.addChildren(edits.toArray(new TextEdit[edits.size()]));
74
 
                        edit.apply(document);
75
 
                }
 
78
                edit.apply(document);
76
79
                return document.get();
77
80
        }
78
81
 
296
299
                assertExpectedResults();
297
300
        }
298
301
 
 
302
        //f.h
 
303
        //void f(int p);
 
304
 
 
305
        //f.cpp
 
306
        //#include "f.h"
 
307
        //void f(int p) {
 
308
        //}
 
309
        //====================
 
310
        //#include "f.h"
 
311
        //
 
312
        //void f(int p) {
 
313
        //}
 
314
        public void testExistingPartnerIncludeIsNotRemoved() throws Exception {
 
315
                IPreferenceStore preferenceStore = getPreferenceStore();
 
316
                preferenceStore.setValue(PreferenceConstants.INCLUDES_UNUSED_STATEMENTS_DISPOSITION,
 
317
                                UnusedStatementDisposition.REMOVE.toString());
 
318
                preferenceStore.setValue(PreferenceConstants.FORWARD_DECLARE_FUNCTIONS, true);
 
319
                assertExpectedResults();
 
320
        }
 
321
 
299
322
        //h1.h
300
323
        //class A {};
301
324
 
329
352
        public void testHeaderExport() throws Exception {
330
353
                assertExpectedResults();
331
354
        }
332
 
}
 
 
b'\\ No newline at end of file'
 
355
 
 
356
        //h1.h
 
357
        //class A {};
 
358
 
 
359
        //h2.h
 
360
        //#include "h1.h"       // IWYU pragma: export
 
361
        //class B {};
 
362
 
 
363
        //h3.h
 
364
        //#include "h2.h"
 
365
 
 
366
        //source.cpp
 
367
        //A a;
 
368
        //B b;
 
369
        //====================
 
370
        //#include "h3.h"
 
371
        //
 
372
        //A a;
 
373
        //B b;
 
374
        public void testIndirectHeaderExport() throws Exception {
 
375
                HeaderSubstitutionMap headerMap = new HeaderSubstitutionMap("Test", false,
 
376
                                new IncludeMap(true, new String[] { "h2.h", "h3.h"}),
 
377
                                new IncludeMap(false));
 
378
                getPreferenceStore().setValue(IncludePreferences.INCLUDES_HEADER_SUBSTITUTION,
 
379
                                HeaderSubstitutionMap.serializeMaps(Collections.singletonList(headerMap)));
 
380
                assertExpectedResults();
 
381
        }
 
382
 
 
383
        //h1.h
 
384
        //#define M2(t, p) t p
 
385
 
 
386
        //h2.h
 
387
        //#include "h1.h"
 
388
        //#define M1(x, y) M2(int, x) = y
 
389
 
 
390
        //h3.h
 
391
        //#include "h2.h"
 
392
 
 
393
        //source.cpp
 
394
        //#include "h3.h"
 
395
        //M1(a, 1);
 
396
        //====================
 
397
        //#include "h2.h"
 
398
        //
 
399
        //M1(a, 1);
 
400
        public void testMacro() throws Exception {
 
401
                IPreferenceStore preferenceStore = getPreferenceStore();
 
402
                preferenceStore.setValue(PreferenceConstants.INCLUDES_UNUSED_STATEMENTS_DISPOSITION,
 
403
                                UnusedStatementDisposition.REMOVE.toString());
 
404
                assertExpectedResults();
 
405
        }
 
406
 
 
407
        //string.h
 
408
        //#include "stddef.h"
 
409
        //extern char* strchr(char* s, int c);
 
410
 
 
411
        //stddef.h
 
412
        //#define NULL 0
 
413
 
 
414
        //source.cpp
 
415
        //#include "stddef.h"
 
416
        //char* test() {
 
417
        //  int* p = NULL;
 
418
        //  return strchr("aaa", '*');
 
419
        //}
 
420
        //====================
 
421
        //#include "string.h"
 
422
        //
 
423
        //char* test() {
 
424
        //  int* p = NULL;
 
425
        //  return strchr("aaa", '*');
 
426
        //}
 
427
        public void testExportedSymbol() throws Exception {
 
428
                IPreferenceStore preferenceStore = getPreferenceStore();
 
429
                preferenceStore.setValue(PreferenceConstants.INCLUDES_UNUSED_STATEMENTS_DISPOSITION,
 
430
                                UnusedStatementDisposition.REMOVE.toString());
 
431
                SymbolExportMap symbolExportMap = new SymbolExportMap(new String[] { "NULL", "string.h" });
 
432
                preferenceStore.setValue(IncludePreferences.INCLUDES_SYMBOL_EXPORTING_HEADERS,
 
433
                                SymbolExportMap.serializeMaps(Collections.singletonList(symbolExportMap)));
 
434
                assertExpectedResults();
 
435
        }
 
436
 
 
437
        //h1.h
 
438
        //class A {};
 
439
        //class B;
 
440
 
 
441
        //h2.h
 
442
        //class C {};
 
443
 
 
444
        //source.cpp
 
445
        //A a;
 
446
        //B* b;
 
447
        //C* c;
 
448
        //====================
 
449
        //#include "h1.h"
 
450
        //
 
451
        //class C;
 
452
        //
 
453
        //A a;
 
454
        //B* b;
 
455
        //C* c;
 
456
        public void testSymbolToDeclareIsDefinedInIncludedHeader() throws Exception {
 
457
                assertExpectedResults();
 
458
        }
 
459
 
 
460
        //h1.h
 
461
        //typedef int int32;
 
462
 
 
463
        //h2.h
 
464
        //#include "h1.h"
 
465
        //extern int32 var;
 
466
 
 
467
        //source.cpp
 
468
        //int a = var;
 
469
        //====================
 
470
        //#include "h2.h"
 
471
        //
 
472
        //int a = var;
 
473
        public void testVariableReference() throws Exception {
 
474
                assertExpectedResults();
 
475
        }
 
476
 
 
477
        //h1.h
 
478
        //namespace ns3 {
 
479
        //class C {};
 
480
        //namespace ns2 {
 
481
        //class A {};
 
482
        //class B {};
 
483
        //namespace ns1 {
 
484
        //C* f(const A& a, B* b) { return nullptr; }
 
485
        //} // ns1
 
486
        //} // ns2
 
487
        //} // ns3
 
488
 
 
489
        //source.cpp
 
490
        //#include "h1.h"
 
491
        //void test(ns3::ns2::A& a) {
 
492
        //  ns3::C* c = ns3::ns2::ns1::f(a, nullptr);
 
493
        //}
 
494
        //====================
 
495
        //namespace ns3 {
 
496
        //class C;
 
497
        //namespace ns2 {
 
498
        //class A;
 
499
        //class B;
 
500
        //} /* namespace ns2 */
 
501
        //} /* namespace ns3 */
 
502
        //namespace ns3 {
 
503
        //namespace ns2 {
 
504
        //namespace ns1 {
 
505
        //C * f(const A &a, B *b);
 
506
        //} /* namespace ns1 */
 
507
        //} /* namespace ns2 */
 
508
        //} /* namespace ns3 */
 
509
        //
 
510
        //void test(ns3::ns2::A& a) {
 
511
        //  ns3::C* c = ns3::ns2::ns1::f(a, nullptr);
 
512
        //}
 
513
        public void testForwardDeclarations() throws Exception {
 
514
                // TODO(sprigogin): Move ns1 outside of other namespaces after IncludeOrganizer starts using ASTWriter.
 
515
                IPreferenceStore preferenceStore = getPreferenceStore();
 
516
                preferenceStore.setValue(PreferenceConstants.INCLUDES_UNUSED_STATEMENTS_DISPOSITION,
 
517
                                UnusedStatementDisposition.REMOVE.toString());
 
518
                preferenceStore.setValue(PreferenceConstants.FORWARD_DECLARE_FUNCTIONS, true);
 
519
                assertExpectedResults();
 
520
        }
 
521
}