All Downloads are FREE. Search and download functionalities are using the official Maven repository.

fit.FitMatcherTest Maven / Gradle / Ivy

There is a newer version: 20240707
Show newest version
// Modified or written by Object Mentor, Inc. for inclusion with FitNesse.
// Copyright (c) 2002 Cunningham & Cunningham, Inc.
// Released under the terms of the GNU General Public License version 2 or later.
package fit;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import org.junit.Test;

public class FitMatcherTest {

  private void assertMatch(String expression, Number parameter) throws Exception {
    FitMatcher matcher = new FitMatcher(expression, parameter);
    assertTrue(matcher.matches());
  }

  private void assertNoMatch(String expression, Number parameter) throws Exception {
    FitMatcher matcher = new FitMatcher(expression, parameter);
    assertFalse(matcher.matches());
  }

  private void assertException(String expression, Object parameter) {
    FitMatcher matcher = new FitMatcher(expression, parameter);
    try {
      matcher.matches();
    }
    catch (Exception e) {
    }
  }

  @Test
  public void testSimpleMatches() throws Exception {
    assertMatch("_<3", new Integer(2));
    assertNoMatch("_<3", new Integer(3));
    assertMatch("_<4", new Integer(3));
    assertMatch("_ < 9", new Integer(4));
    assertMatch("<3", new Integer(2));
    assertMatch(">4", new Integer(5));
    assertMatch(">-3", new Integer(-2));
    assertMatch("<3.2", new Double(3.1));
    assertNoMatch("<3.2", new Double(3.3));
    assertMatch("<=3", new Double(3));
    assertMatch("<=3", new Double(2));
    assertNoMatch("<=3", new Double(4));
    assertMatch(">=2", new Double(2));
    assertMatch(">=2", new Double(3));
    assertNoMatch(">=2", new Double(1));
  }

  @Test
  public void testExceptions() throws Exception {
    assertException("X", new Integer(1));
    assertException("<32", "xxx");
  }

  @Test
  public void testMessage() throws Exception {
    FitMatcher matcher = new FitMatcher("_>25", new Integer(3));
    assertEquals("3>25", matcher.message());
    matcher = new FitMatcher(" < 32", new Integer(5));
    assertEquals("5 < 32", matcher.message());
  }

  @Test
  public void testTrichotomy() throws Exception {
    assertMatch("5<_<32", new Integer(8));
    assertNoMatch("5<_<32", new Integer(5));
    assertNoMatch("5<_<32", new Integer(32));
    assertMatch("10>_>5", new Integer(6));
    assertNoMatch("10>_>5", new Integer(10));
    assertNoMatch("10>_>5", new Integer(5));
    assertMatch("10>=_>=5", new Integer(10));
    assertMatch("10>=_>=5", new Integer(5));
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy