| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| WithRegex |
|
| 1.5;1.5 |
| 1 | /******************************************************************************* | |
| 2 | * Copyright (c) 2008 Ketan Padegaonkar and others. | |
| 3 | * All rights reserved. This program and the accompanying materials | |
| 4 | * are made available under the terms of the Eclipse Public License v1.0 | |
| 5 | * which accompanies this distribution, and is available at | |
| 6 | * http://www.eclipse.org/legal/epl-v10.html | |
| 7 | * | |
| 8 | * Contributors: | |
| 9 | * Ketan Padegaonkar - initial API and implementation | |
| 10 | * Ketan Padegaonkar - http://swtbot.org/bugzilla/show_bug.cgi?id=126 | |
| 11 | *******************************************************************************/ | |
| 12 | package org.eclipse.swtbot.swt.finder.matchers; | |
| 13 | ||
| 14 | import java.util.regex.Pattern; | |
| 15 | ||
| 16 | import org.eclipse.swt.widgets.Widget; | |
| 17 | import org.hamcrest.Description; | |
| 18 | import org.hamcrest.Factory; | |
| 19 | import org.hamcrest.Matcher; | |
| 20 | ||
| 21 | /** | |
| 22 | * Matches widgets if the getText() method of the widget matches the specified regex. | |
| 23 | * | |
| 24 | * @author Ketan Padegaonkar <KetanPadegaonkar [at] gmail [dot] com> | |
| 25 | * @version $Id$ | |
| 26 | * @since 2.0 | |
| 27 | */ | |
| 28 | public class WithRegex<T extends Widget> extends AbstractMatcher<T> { | |
| 29 | ||
| 30 | /** The regular expression string. */ | |
| 31 | private Pattern pattern; | |
| 32 | ||
| 33 | /** | |
| 34 | * COnstructs the regular expression matcher with the given regular expression stirng. | |
| 35 | * | |
| 36 | * @param regex the regex to match on the {@link org.eclipse.swt.widgets.Widget} | |
| 37 | */ | |
| 38 | 6 | WithRegex(String regex) { |
| 39 | 6 | pattern = Pattern.compile("([\r\n]|.)*" + regex + "([\r\n]|.)*"); |
| 40 | 6 | } |
| 41 | ||
| 42 | protected boolean doMatch(Object obj) { | |
| 43 | try { | |
| 44 | 55 | return pattern.matcher(WithText.getText(obj)).matches(); |
| 45 | 8 | } catch (Exception e) { |
| 46 | // do nothing | |
| 47 | } | |
| 48 | 8 | return false; |
| 49 | } | |
| 50 | ||
| 51 | public void describeTo(Description description) { | |
| 52 | 1 | description.appendText("with regex '").appendValue(pattern).appendText("'"); //$NON-NLS-1$ //$NON-NLS-2$ |
| 53 | 1 | } |
| 54 | ||
| 55 | /** | |
| 56 | * Matches a widget that has the specified regex. | |
| 57 | * | |
| 58 | * @param regex the label. | |
| 59 | * @return a matcher. | |
| 60 | * @since 2.0 | |
| 61 | */ | |
| 62 | @Factory | |
| 63 | public static <T extends Widget> Matcher<T> withRegex(String regex) { | |
| 64 | 6 | return new WithRegex<T>(regex); |
| 65 | } | |
| 66 | } |