KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jline > TestCompletion


1 /**
2  * jline - Java console input library
3  * Copyright (c) 2002-2006, Marc Prud'hommeaux mwp1@cornell.edu
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or
7  * without modification, are permitted provided that the following
8  * conditions are met:
9  *
10  * Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer
15  * in the documentation and/or other materials provided with
16  * the distribution.
17  *
18  * Neither the name of JLine nor the names of its contributors
19  * may be used to endorse or promote products derived from this
20  * software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
24  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
25  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
26  * EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
28  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
31  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
33  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
34  * OF THE POSSIBILITY OF SUCH DAMAGE.
35  */

36 package jline;
37
38 import java.util.*;
39
40 /**
41  * Tests command history.
42  *
43  * @author <a HREF="mailto:mwp1@cornell.edu">Marc Prud'hommeaux</a>
44  */

45 public class TestCompletion
46     extends JLineTestCase
47 {
48     public TestCompletion (String JavaDoc test)
49     {
50         super (test);
51     }
52
53
54     public void testSimpleCompletor ()
55         throws Exception JavaDoc
56     {
57         // clear any current completors
58
for (Iterator i = console.getCompletors ().iterator ();
59             i.hasNext (); console.removeCompletor ((Completor)i.next ()));
60
61         console.addCompletor (new SimpleCompletor (
62             new String JavaDoc [] { "foo", "bar", "baz"}));
63
64         assertBuffer ("foo ", new Buffer ("f").op (ConsoleReader.COMPLETE));
65         // single tab completes to unabbiguous "ba"
66
assertBuffer ("ba", new Buffer ("b").op (ConsoleReader.COMPLETE));
67         assertBuffer ("ba", new Buffer ("ba").op (ConsoleReader.COMPLETE));
68         assertBuffer ("baz ", new Buffer ("baz").op (ConsoleReader.COMPLETE));
69     }
70
71
72     public void testArgumentCompletor ()
73         throws Exception JavaDoc
74     {
75         // clear any current completors
76
for (Iterator i = console.getCompletors ().iterator ();
77             i.hasNext (); console.removeCompletor ((Completor)i.next ()));
78
79         console.addCompletor (new ArgumentCompletor (
80             new SimpleCompletor (new String JavaDoc [] { "foo", "bar", "baz"})));
81
82         assertBuffer ("foo foo ", new Buffer ("foo f")
83             .op (ConsoleReader.COMPLETE));
84         assertBuffer ("foo ba", new Buffer ("foo b")
85             .op (ConsoleReader.COMPLETE));
86         assertBuffer ("foo ba", new Buffer ("foo ba")
87             .op (ConsoleReader.COMPLETE));
88         assertBuffer ("foo baz ", new Buffer ("foo baz")
89             .op (ConsoleReader.COMPLETE));
90
91         // test completion in the mid range
92
assertBuffer ("foo baz", new Buffer ("f baz")
93             .left ().left ().left ().left ()
94             .op (ConsoleReader.COMPLETE));
95         assertBuffer ("ba foo", new Buffer ("b foo")
96             .left ().left ().left ().left ()
97             .op (ConsoleReader.COMPLETE));
98         assertBuffer ("foo ba baz", new Buffer ("foo b baz")
99             .left ().left ().left ().left ()
100             .op (ConsoleReader.COMPLETE));
101         assertBuffer ("foo foo baz", new Buffer ("foo f baz")
102             .left ().left ().left ().left ()
103             .op (ConsoleReader.COMPLETE));
104     }
105 }
106
107
108
Popular Tags