/
AnImport_PDETest.java
213 lines (194 loc) · 10.8 KB
/
AnImport_PDETest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/**
* Copyright (c) 2012 by JP Moresmau
* This code is made available under the terms of the Eclipse Public License,
* version 1.0 (EPL). See http://www.eclipse.org/legal/epl-v10.html
*/
package net.sf.eclipsefp.haskell.ui.internal.editors.haskell.imports;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import java.util.ArrayList;
import java.util.Arrays;
import net.sf.eclipsefp.haskell.buildwrapper.types.ImportDef;
import net.sf.eclipsefp.haskell.buildwrapper.types.ImportExportType;
import net.sf.eclipsefp.haskell.buildwrapper.types.ImportSpecDef;
import net.sf.eclipsefp.haskell.buildwrapper.types.Location;
import net.sf.eclipsefp.haskell.core.cabalmodel.TestDocument;
import net.sf.eclipsefp.haskell.util.PlatformUtil;
import org.eclipse.jface.text.contentassist.ICompletionProposal;
import org.junit.Test;
/**
* @author JP Moresmau
*
*/
public class AnImport_PDETest {
@Test
public void testRemoveSimple(){
// test in middle
TestDocument td=new TestDocument( "module TestSimpleRemove where"+PlatformUtil.NL+"import Data.Sequence (Seq, singleton, empty)"+PlatformUtil.NL );
Location loc=new Location( "TestSimpleRemove.hs",2,0,2,44);
ImportDef def=new ImportDef( "Data.Sequence", loc, false, false, null );
AnImport ai=new AnImport( def, false );
ICompletionProposal cp=ai.removeItem( td, "singleton" , "remove" );
assertNotNull( cp );
cp.apply( td );
// test at end
assertEquals("module TestSimpleRemove where"+PlatformUtil.NL+"import Data.Sequence (Seq, empty)"+PlatformUtil.NL,td.get());
td=new TestDocument( "module TestSimpleRemove where"+PlatformUtil.NL+"import Data.Sequence (Seq, singleton, empty)"+PlatformUtil.NL );
cp=ai.removeItem( td, "empty" , "remove" );
assertNotNull( cp );
cp.apply( td );
// test at start + a name that appears in the module
assertEquals("module TestSimpleRemove where"+PlatformUtil.NL+"import Data.Sequence (Seq, singleton)"+PlatformUtil.NL,td.get());
td=new TestDocument( "module TestSimpleRemove where"+PlatformUtil.NL+"import Data.Sequence (Seq, singleton, empty)"+PlatformUtil.NL );
cp=ai.removeItem( td, "Seq" , "remove" );
assertNotNull( cp );
cp.apply( td );
assertEquals("module TestSimpleRemove where"+PlatformUtil.NL+"import Data.Sequence (singleton, empty)"+PlatformUtil.NL,td.get());
}
@Test
public void testRemoveConstructor(){
TestDocument td=new TestDocument( "module TestSimpleRemove where"+PlatformUtil.NL+"import Data.Sequence (Seq(..), singleton, empty)"+PlatformUtil.NL );
Location loc=new Location( "TestSimpleRemove.hs",2,0,2,48);
ImportDef def=new ImportDef( "Data.Sequence", loc, false, false, null );
AnImport ai=new AnImport( def, false );
ICompletionProposal cp=ai.removeItem( td, "Seq" , "remove" );
assertNotNull( cp );
cp.apply( td );
assertEquals("module TestSimpleRemove where"+PlatformUtil.NL+"import Data.Sequence (singleton, empty)"+PlatformUtil.NL,td.get());
}
@Test
public void testRemoveConstructorAtEnd(){
TestDocument td=new TestDocument( "module TestSimpleRemove where"+PlatformUtil.NL+"import Data.Sequence (singleton, empty, Seq(..))"+PlatformUtil.NL );
Location loc=new Location( "TestSimpleRemove.hs",2,0,2,48);
ImportDef def=new ImportDef( "Data.Sequence", loc, false, false, null );
AnImport ai=new AnImport( def, false );
ICompletionProposal cp=ai.removeItem( td, "Seq" , "remove" );
assertNotNull( cp );
cp.apply( td );
assertEquals("module TestSimpleRemove where"+PlatformUtil.NL+"import Data.Sequence (singleton, empty)"+PlatformUtil.NL,td.get());
}
@Test
public void testRemoveMultipleAtEnd(){
TestDocument td=new TestDocument( "module TestSimpleRemove where"+PlatformUtil.NL+"import Data.Sequence (singleton, empty, Seq(..))"+PlatformUtil.NL );
Location loc=new Location( "TestSimpleRemove.hs",2,0,2,48);
ImportDef def=new ImportDef( "Data.Sequence", loc, false, false, null );
AnImport ai=new AnImport( def, false );
ICompletionProposal cp=ai.removeItem( td, Arrays.asList("Seq","singleton") , "remove" );
assertNotNull( cp );
cp.apply( td );
assertEquals("module TestSimpleRemove where"+PlatformUtil.NL+"import Data.Sequence (empty)"+PlatformUtil.NL,td.get());
}
@Test
public void testRemoveLast(){
TestDocument td=new TestDocument( "module TestSimpleRemove where"+PlatformUtil.NL+"import Data.Sequence (Seq)"+PlatformUtil.NL );
Location loc=new Location( "TestSimpleRemove.hs",2,0,2,26);
ImportDef def=new ImportDef( "Data.Sequence", loc, false, false, null );
AnImport ai=new AnImport( def, false );
ICompletionProposal cp=ai.removeItem( td, "Seq" , "remove" );
assertNotNull( cp );
cp.apply( td );
assertEquals("module TestSimpleRemove where"+PlatformUtil.NL+"import Data.Sequence ()"+PlatformUtil.NL,td.get());
td=new TestDocument( "module TestSimpleRemove where"+PlatformUtil.NL+"import Data.Sequence (Seq )"+PlatformUtil.NL );
loc=new Location( "TestSimpleRemove.hs",2,0,2,27);
def=new ImportDef( "Data.Sequence", loc, false, false, null );
ai=new AnImport( def, false );
cp=ai.removeItem( td, "Seq" , "remove" );
assertNotNull( cp );
cp.apply( td );
assertEquals("module TestSimpleRemove where"+PlatformUtil.NL+"import Data.Sequence ()"+PlatformUtil.NL,td.get());
td=new TestDocument( "module TestSimpleRemove where"+PlatformUtil.NL+"import Data.Sequence ( Seq)"+PlatformUtil.NL );
cp=ai.removeItem( td, "Seq" , "remove" );
assertNotNull( cp );
cp.apply( td );
assertEquals("module TestSimpleRemove where"+PlatformUtil.NL+"import Data.Sequence ()"+PlatformUtil.NL,td.get());
}
@Test
public void testRemoveOperator(){
TestDocument td=new TestDocument( "module TestSimpleRemove where"+PlatformUtil.NL+"import Data.Sequence ((><), Seq)"+PlatformUtil.NL );
Location loc=new Location( "TestSimpleRemove.hs",2,0,2,32);
ImportDef def=new ImportDef( "Data.Sequence", loc, false, false, null );
AnImport ai=new AnImport( def, false );
ICompletionProposal cp=ai.removeItem( td, "><" , "remove" );
assertNotNull( cp );
cp.apply( td );
assertEquals("module TestSimpleRemove where"+PlatformUtil.NL+"import Data.Sequence (Seq)"+PlatformUtil.NL,td.get());
}
@Test
public void testRemoveSuperStringBefore(){
TestDocument td=new TestDocument( "module TestSimpleRemove where"+PlatformUtil.NL+"import Control.Monad.Trans.Reader (runReaderT,runReader)"+PlatformUtil.NL );
Location loc=new Location( "testRemoveSuperString.hs",2,0,2,56);
ImportDef def=new ImportDef( "Control.Monad.Trans.Reader", loc, false, false, null );
AnImport ai=new AnImport( def, false );
ICompletionProposal cp=ai.removeItem( td, "runReader" , "remove" );
assertNotNull( cp );
cp.apply( td );
assertEquals("module TestSimpleRemove where"+PlatformUtil.NL+"import Control.Monad.Trans.Reader (runReaderT)"+PlatformUtil.NL,td.get());
}
@Test
public void testRemoveSuperStringAfter(){
TestDocument td=new TestDocument( "module TestSimpleRemove where"+PlatformUtil.NL+"import Control.Monad.Trans.Reader (runReader,runReaderT)"+PlatformUtil.NL );
Location loc=new Location( "testRemoveSuperString.hs",2,0,2,56);
ImportDef def=new ImportDef( "Control.Monad.Trans.Reader", loc, false, false, null );
AnImport ai=new AnImport( def, false );
ICompletionProposal cp=ai.removeItem( td, "runReader" , "remove" );
assertNotNull( cp );
cp.apply( td );
assertEquals("module TestSimpleRemove where"+PlatformUtil.NL+"import Control.Monad.Trans.Reader (runReaderT)"+PlatformUtil.NL,td.get());
}
@Test
public void testAddSimple(){
TestDocument td=new TestDocument( "module TestSimpleRemove where"+PlatformUtil.NL+"import Data.Sequence ((><), Seq)"+PlatformUtil.NL );
Location loc=new Location( "TestSimpleRemove.hs",2,0,2,32);
ImportDef def=new ImportDef( "Data.Sequence", loc, false, false, null );
ImportSpecDef sp1=new ImportSpecDef( "><", new Location( "TestSimpleRemove.hs",2,22,2,26), ImportExportType.IEVar );
ImportSpecDef sp2=new ImportSpecDef( "Seq", new Location( "TestSimpleRemove.hs",2,27,2,31), ImportExportType.IEVar );
def.setChildren( new ArrayList<ImportSpecDef>() );
def.getChildren().add(sp1);
def.getChildren().add(sp2);
AnImport ai=new AnImport( def, false );
ICompletionProposal cp=ai.addItem(td, "singleton" , "add" );
assertNotNull( cp );
cp.apply( td );
assertEquals("module TestSimpleRemove where"+PlatformUtil.NL+"import Data.Sequence ((><), Seq, singleton)"+PlatformUtil.NL,td.get());
}
/**
* this should never happen, if we import everything we're not missing one item, but ensure it's ok
*/
@Test
public void testAddToNoChildren(){
TestDocument td=new TestDocument( "module TestSimpleRemove where"+PlatformUtil.NL+"import Data.Sequence"+PlatformUtil.NL );
Location loc=new Location( "TestSimpleRemove.hs",2,0,2,20);
ImportDef def=new ImportDef( "Data.Sequence", loc, false, false, null );
AnImport ai=new AnImport( def, false );
ICompletionProposal cp=ai.addItem(td, "singleton" , "add" );
assertNotNull( cp );
cp.apply( td );
assertEquals("module TestSimpleRemove where"+PlatformUtil.NL+"import Data.Sequence (singleton)"+PlatformUtil.NL,td.get());
}
@Test
public void testAddToEmptyChildren(){
TestDocument td=new TestDocument( "module TestSimpleRemove where"+PlatformUtil.NL+"import Data.Sequence ()"+PlatformUtil.NL );
Location loc=new Location( "TestSimpleRemove.hs",2,0,2,23);
ImportDef def=new ImportDef( "Data.Sequence", loc, false, false, null );
def.setChildren( new ArrayList<ImportSpecDef>() );
AnImport ai=new AnImport( def, false );
ICompletionProposal cp=ai.addItem(td, "singleton" , "add" );
assertNotNull( cp );
cp.apply( td );
assertEquals("module TestSimpleRemove where"+PlatformUtil.NL+"import Data.Sequence (singleton)"+PlatformUtil.NL,td.get());
}
@Test
public void testAddOperator(){
TestDocument td=new TestDocument( "module TestSimpleRemove where"+PlatformUtil.NL+"import Data.Sequence (Seq)"+PlatformUtil.NL );
Location loc=new Location( "TestSimpleRemove.hs",2,0,2,26);
ImportDef def=new ImportDef( "Data.Sequence", loc, false, false, null );
ImportSpecDef sp1=new ImportSpecDef( "Seq", new Location( "TestSimpleRemove.hs",2,24,2,25), ImportExportType.IEVar );
def.setChildren( new ArrayList<ImportSpecDef>() );
def.getChildren().add(sp1);
AnImport ai=new AnImport( def, false );
ICompletionProposal cp=ai.addItem(td, "><" , "add" );
assertNotNull( cp );
cp.apply( td );
assertEquals("module TestSimpleRemove where"+PlatformUtil.NL+"import Data.Sequence (Seq, (><))"+PlatformUtil.NL,td.get());
}
}