001: /*******************************************************************************
002: * Copyright (c) 2005, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jface.tests.examples.databinding.mask.internal;
011:
012: import junit.framework.TestCase;
013:
014: import org.eclipse.jface.examples.databinding.mask.internal.EditMaskLexerAndToken;
015:
016: public class EditMaskLexerAndTokenTest extends TestCase {
017:
018: private EditMaskLexerAndToken token;
019:
020: protected void setUp() throws Exception {
021: token = new EditMaskLexerAndToken();
022: }
023:
024: public void testInitWithNumeric() throws Exception {
025: token.initializeEditMask("#", 0);
026: assertTrue("Should accept a digit", token.accept("0"));
027: token.clear();
028: assertTrue("Should accept a digit", token.accept("1"));
029: token.clear();
030: assertTrue("Should accept a digit", token.accept("2"));
031: token.clear();
032: assertTrue("Should accept a digit", token.accept("3"));
033: token.clear();
034: assertTrue("Should accept a digit", token.accept("4"));
035: token.clear();
036: assertTrue("Should accept a digit", token.accept("5"));
037: token.clear();
038: assertTrue("Should accept a digit", token.accept("6"));
039: token.clear();
040: assertTrue("Should accept a digit", token.accept("7"));
041: token.clear();
042: assertTrue("Should accept a digit", token.accept("8"));
043: token.clear();
044: assertTrue("Should accept a digit", token.accept("9"));
045: token.clear();
046: assertFalse("Should not accept an alpha", token.accept("A"));
047: token.clear();
048: assertFalse("Should not accept an alpha", token.accept("z"));
049: assertFalse("Placeholders are not read-only", token
050: .isReadOnly());
051: }
052:
053: public void testInitWithLiteral() throws Exception {
054: token.initializeEditMask("(", 0);
055: assertEquals("Literals automatically set their input", "(",
056: token.getInput());
057: assertFalse("Literals don't accept anything", token.accept("("));
058: assertTrue("literals are read-only", token.isReadOnly());
059: assertTrue("Literals are complete", token.isComplete());
060: assertFalse("Literals cannot accept characters", token
061: .canAcceptMoreCharacters());
062: }
063:
064: public void testInitWithBackslashLiteral() throws Exception {
065: token.initializeEditMask("\\#", 0);
066: assertEquals("Should get backslash literal", "#", token
067: .getInput());
068: }
069:
070: public void testAcceptWithValidInputAndEmpty() throws Exception {
071: token.initializeEditMask("#", 0);
072: assertTrue("Should accept a 0", token.accept("0"));
073: }
074:
075: public void testAcceptWhenParserCannotAcceptMoreCharacters()
076: throws Exception {
077: token.initializeEditMask("#", 0);
078: assertTrue("Should accept a 0", token.accept("0"));
079: assertFalse("Should not accept a 0 -- input full", token
080: .accept("0"));
081: }
082:
083: public void testGetInput() throws Exception {
084: token.initializeEditMask("#", 0);
085: assertTrue("Should accept a #", token.accept("0"));
086: assertEquals(token.getInput(), "0");
087: }
088:
089: public void testClear_withNonLiteral() throws Exception {
090: token.initializeEditMask("#", 0);
091: assertTrue("Should accept a 0", token.accept("0"));
092: assertNotNull("Input should not be null", token.getInput());
093: token.clear();
094: assertNull("Input should be null after clear", token.getInput());
095: }
096:
097: public void testClear_withLiteral() throws Exception {
098: token.initializeEditMask("(", 0);
099: assertNotNull("Input should not be null", token.getInput());
100: token.clear();
101: assertNotNull(
102: "Input should still not be null after clear of read-only literal",
103: token.getInput());
104: }
105:
106: public void testIsComplete_withNonLiteral() throws Exception {
107: token.initializeEditMask("#", 0);
108: assertFalse("should not be complete", token.isComplete());
109: token.accept("1");
110: assertTrue("should be complete", token.isComplete());
111: token.clear();
112: assertFalse("should not be complete", token.isComplete());
113: }
114:
115: }
|