001    /*
002     * Copyright (c) 2002-2007, Marc Prud'hommeaux. All rights reserved.
003     *
004     * This software is distributable under the BSD license. See the terms of the
005     * BSD license in the documentation provided with this software.
006     */
007    package jline;
008    
009    import java.util.*;
010    
011    /**
012     *  Tests command history.
013     *
014     *  @author  <a href="mailto:mwp1@cornell.edu">Marc Prud'hommeaux</a>
015     */
016    public class TestCompletion extends JLineTestCase {
017        public TestCompletion(String test) {
018            super(test);
019        }
020    
021        public void testSimpleCompletor() throws Exception {
022            // clear any current completors
023            for (Iterator i = console.getCompletors().iterator(); i.hasNext();
024                     console.removeCompletor((Completor) i.next())) {
025                ;
026            }
027    
028            console.addCompletor
029                (new SimpleCompletor(new String[] { "foo", "bar", "baz" }));
030    
031            assertBuffer("foo ", new Buffer("f").op(ConsoleReader.COMPLETE));
032            // single tab completes to unabbiguous "ba"
033            assertBuffer("ba", new Buffer("b").op(ConsoleReader.COMPLETE));
034            assertBuffer("ba", new Buffer("ba").op(ConsoleReader.COMPLETE));
035            assertBuffer("baz ", new Buffer("baz").op(ConsoleReader.COMPLETE));
036        }
037    
038        public void testArgumentCompletor() throws Exception {
039            // clear any current completors
040            for (Iterator i = console.getCompletors().iterator(); i.hasNext();
041                     console.removeCompletor((Completor) i.next())) {
042                ;
043            }
044    
045            console.addCompletor(new ArgumentCompletor
046                (new SimpleCompletor(new String[] { "foo", "bar", "baz" })));
047    
048            assertBuffer("foo foo ", new Buffer("foo f").
049                op(ConsoleReader.COMPLETE));
050            assertBuffer("foo ba", new Buffer("foo b").
051                op(ConsoleReader.COMPLETE));
052            assertBuffer("foo ba", new Buffer("foo ba").
053                op(ConsoleReader.COMPLETE));
054            assertBuffer("foo baz ", new Buffer("foo baz").
055                op(ConsoleReader.COMPLETE));
056    
057            // test completion in the mid range
058            assertBuffer("foo baz",
059                new Buffer("f baz").left().left().left().left().
060                    op(ConsoleReader.COMPLETE));
061            assertBuffer("ba foo",
062                new Buffer("b foo").left().left().left().left().
063                    op(ConsoleReader.COMPLETE));
064            assertBuffer("foo ba baz",
065                new Buffer("foo b baz").left().left().left().left().
066                    op(ConsoleReader.COMPLETE));
067            assertBuffer("foo foo baz",
068                new Buffer("foo f baz").left().left().left().left().
069                    op(ConsoleReader.COMPLETE));
070        }
071    }