1 /* 2 * Copyright (c) 2002-2007, Marc Prud'hommeaux. All rights reserved. 3 * 4 * This software is distributable under the BSD license. See the terms of the 5 * BSD license in the documentation provided with this software. 6 */ 7 package jline; 8 9 import java.util.*; 10 11 /** 12 * Tests command history. 13 * 14 * @author <a href="mailto:mwp1 (at) cornell.edu">Marc Prud'hommeaux</a> 15 */ 16 public class TestCompletion extends JLineTestCase { 17 public TestCompletion(String test) { 18 super(test); 19 } 20 21 public void testSimpleCompletor() throws Exception { 22 // clear any current completors 23 for (Iterator i = console.getCompletors().iterator(); i.hasNext(); 24 console.removeCompletor((Completor) i.next())) { 25 ; 26 } 27 28 console.addCompletor 29 (new SimpleCompletor(new String[] { "foo", "bar", "baz" })); 30 31 assertBuffer("foo ", new Buffer("f").op(ConsoleReader.COMPLETE)); 32 // single tab completes to unabbiguous "ba" 33 assertBuffer("ba", new Buffer("b").op(ConsoleReader.COMPLETE)); 34 assertBuffer("ba", new Buffer("ba").op(ConsoleReader.COMPLETE)); 35 assertBuffer("baz ", new Buffer("baz").op(ConsoleReader.COMPLETE)); 36 } 37 38 public void testArgumentCompletor() throws Exception { 39 // clear any current completors 40 for (Iterator i = console.getCompletors().iterator(); i.hasNext(); 41 console.removeCompletor((Completor) i.next())) { 42 ; 43 } 44 45 console.addCompletor(new ArgumentCompletor 46 (new SimpleCompletor(new String[] { "foo", "bar", "baz" }))); 47 48 assertBuffer("foo foo ", new Buffer("foo f"). 49 op(ConsoleReader.COMPLETE)); 50 assertBuffer("foo ba", new Buffer("foo b"). 51 op(ConsoleReader.COMPLETE)); 52 assertBuffer("foo ba", new Buffer("foo ba"). 53 op(ConsoleReader.COMPLETE)); 54 assertBuffer("foo baz ", new Buffer("foo baz"). 55 op(ConsoleReader.COMPLETE)); 56 57 // test completion in the mid range 58 assertBuffer("foo baz", 59 new Buffer("f baz").left().left().left().left(). 60 op(ConsoleReader.COMPLETE)); 61 assertBuffer("ba foo", 62 new Buffer("b foo").left().left().left().left(). 63 op(ConsoleReader.COMPLETE)); 64 assertBuffer("foo ba baz", 65 new Buffer("foo b baz").left().left().left().left(). 66 op(ConsoleReader.COMPLETE)); 67 assertBuffer("foo foo baz", 68 new Buffer("foo f baz").left().left().left().left(). 69 op(ConsoleReader.COMPLETE)); 70 } 71 } 72