Skip to main content
summaryrefslogtreecommitdiffstats
blob: b06c69ea2ac5677475ceb7515af7b71d8dfa1411 (plain) (blame)
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
/*******************************************************************************
 * Copyright (c) 2004, 2008 Eugene Kuleshov and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Eugene Kuleshov - initial API and implementation
 *******************************************************************************/

package org.eclipse.mylyn.tasks.tests.web;

import java.util.regex.Matcher;

import junit.framework.TestCase;

import org.eclipse.mylyn.internal.web.tasks.NamedPattern;

/**
 * @author Eugene Kuleshov
 */
public class NamedPatternTest extends TestCase {

	public void testNamedGroups() {
		NamedPattern p = new NamedPattern("({Hour}\\d\\d):({Minute}\\d\\d):({Second}\\d\\d)", 0);
		assertEquals("(\\d\\d):(\\d\\d):(\\d\\d)", p.getPattern().pattern());
		assertEquals(3, p.getGroups().size());
		assertEquals("Hour", p.groupName(0));
		assertEquals("Minute", p.groupName(1));
		assertEquals("Second", p.groupName(2));

		Matcher m = p.matcher("01:02:03");
		assertTrue(m.find());
		assertEquals("01", p.group("Hour", m));
		assertEquals("02", p.group("Minute", m));
		assertEquals("03", p.group("Second", m));
	}

	public void testUnnamedGroups() {
		NamedPattern p = new NamedPattern("(\\d\\d):(\\d\\d):(\\d\\d)", 0);
		assertEquals("(\\d\\d):(\\d\\d):(\\d\\d)", p.getPattern().pattern());
		assertEquals(0, p.getGroups().size());

		Matcher m = p.matcher("01:02:03");
		assertTrue(m.find());
		assertEquals("01", m.group(1));
		assertEquals("02", m.group(2));
		assertEquals("03", m.group(3));
	}

	public void testNestedGroups() {
		NamedPattern p = new NamedPattern(":({a}:({b}:({c}foo)boo)doo)", 0);
		assertEquals(":(:(:(foo)boo)doo)", p.getPattern().pattern());
		assertEquals(3, p.getGroups().size());

		Matcher m = p.matcher(":::fooboodoo");
		assertTrue(m.find());
		assertEquals("::fooboodoo", p.group("a", m));
		assertEquals(":fooboo", p.group("b", m));
		assertEquals("foo", p.group("c", m));
	}

}

Back to the top