Ejemplo n.º 1
0
  /*
   * Tests the method public int first()
   */
  public void TestFirst() {
    RuleBasedBreakIterator rbbi = new RuleBasedBreakIterator(".;");
    // Tests when "if (fText == null)" is true
    rbbi.setText((CharacterIterator) null);
    assertEquals("RuleBasedBreakIterator.first()", BreakIterator.DONE, rbbi.first());

    rbbi.setText("abc");
    assertEquals("RuleBasedBreakIterator.first()", 0, rbbi.first());
    assertEquals("RuleBasedBreakIterator.next()", 1, rbbi.next());
  }
Ejemplo n.º 2
0
  private boolean verifyPushBack(int current, int next) {
    int shortenedSyllable = next - current - 1;

    verifyText.setText(text.getText(), text.getStart() + current, shortenedSyllable);
    verify.setText(verifyText);
    if (verify.next() != shortenedSyllable || verify.getRuleStatus() == 0) return false;

    verifyText.setText(text.getText(), text.getStart() + next - 1, text.getLength() - next + 1);
    verify.setText(verifyText);

    return (verify.next() != BreakIterator.DONE && verify.getRuleStatus() != 0);
  }
Ejemplo n.º 3
0
 /* Tests the method public int current() */
 public void TestCurrent() {
   RuleBasedBreakIterator rbbi = new RuleBasedBreakIterator(".;");
   // Tests when "(fText != null) ? fText.getIndex() : BreakIterator.DONE" is true and false
   rbbi.setText((CharacterIterator) null);
   if (rbbi.current() != BreakIterator.DONE) {
     errln(
         "RuleBasedBreakIterator.current() was suppose to return "
             + "BreakIterator.DONE when the object has a fText of null.");
   }
   rbbi.setText("dummy");
   if (rbbi.current() != 0) {
     errln(
         "RuleBasedBreakIterator.current() was suppose to return "
             + "0 when the object has a fText of dummy.");
   }
 }
Ejemplo n.º 4
0
 @Override
 public int first() {
   working.setText(this.text.getText(), this.text.getStart(), this.text.getLength());
   rules.setText(working);
   workingOffset = 0;
   int first = rules.first();
   return first == BreakIterator.DONE ? BreakIterator.DONE : workingOffset + first;
 }
Ejemplo n.º 5
0
  /*
   * Tests the method public int preceding(int offset)
   */
  public void TestPreceding() {
    RuleBasedBreakIterator rbbi = new RuleBasedBreakIterator(".;");
    // Tests when "if (fText == null || offset > fText.getEndIndex())" is true
    rbbi.setText((CharacterIterator) null);
    if (rbbi.preceding(-1) != BreakIterator.DONE) {
      errln(
          "RuleBasedBreakIterator.preceding(-1) was suppose to return "
              + "0 when the object has a fText of null.");
    }

    // Tests when "else if (offset < fText.getBeginIndex())" is true
    rbbi.setText("dummy");
    if (rbbi.preceding(-1) != 0) {
      errln(
          "RuleBasedBreakIterator.preceding(-1) was suppose to return "
              + "0 when the object has a fText of dummy.");
    }
  }
Ejemplo n.º 6
0
 @Override
 public void setText(CharacterIterator text) {
   if (!(text instanceof CharArrayIterator))
     throw new UnsupportedOperationException("unsupported CharacterIterator");
   this.text = (CharArrayIterator) text;
   ccReorder(this.text.getText(), this.text.getStart(), this.text.getLength());
   working.setText(this.text.getText(), this.text.getStart(), this.text.getLength());
   rules.setText(working);
   workingOffset = 0;
 }
Ejemplo n.º 7
0
 /*
  * Tests the method public int following(int offset)
  */
 public void TestFollowing() {
   RuleBasedBreakIterator rbbi = new RuleBasedBreakIterator(".;");
   // Tests when "else if (offset < fText.getBeginIndex())" is true
   rbbi.setText("dummy");
   if (rbbi.following(-1) != 0) {
     errln(
         "RuleBasedBreakIterator.following(-1) was suppose to return "
             + "0 when the object has a fText of dummy.");
   }
 }
Ejemplo n.º 8
0
 /*
  * Tests the method public int last()
  */
 public void TestLast() {
   RuleBasedBreakIterator rbbi = new RuleBasedBreakIterator(".;");
   // Tests when "if (fText == null)" is true
   rbbi.setText((CharacterIterator) null);
   if (rbbi.last() != BreakIterator.DONE) {
     errln(
         "RuleBasedBreakIterator.last() was suppose to return "
             + "BreakIterator.DONE when the object has a null fText.");
   }
 }
Ejemplo n.º 9
0
 /* Tests the method public Object clone() */
 public void TestClone() {
   RuleBasedBreakIterator rbbi = new RuleBasedBreakIterator(".;");
   try {
     rbbi.setText((CharacterIterator) null);
     if (((RuleBasedBreakIterator) rbbi.clone()).getText() != null)
       errln(
           "RuleBasedBreakIterator.clone() was suppose to return "
               + "the same object because fText is set to null.");
   } catch (Exception e) {
     errln("RuleBasedBreakIterator.clone() was not suppose to return " + "an exception.");
   }
 }
Ejemplo n.º 10
0
  /*
   * Tests the method public boolean equals(Object that)
   */
  public void TestEquals() {
    RuleBasedBreakIterator rbbi = new RuleBasedBreakIterator(".;");
    RuleBasedBreakIterator rbbi1 = new RuleBasedBreakIterator(".;");

    // TODO: Tests when "if (fRData != other.fRData && (fRData == null || other.fRData == null))" is
    // true

    // Tests when "if (fText == null || other.fText == null)" is true
    rbbi.setText((CharacterIterator) null);
    if (rbbi.equals(rbbi1)) {
      errln(
          "RuleBasedBreakIterator.equals(Object) was not suppose to return "
              + "true when the other object has a null fText.");
    }

    // Tests when "if (fText == null && other.fText == null)" is true
    rbbi1.setText((CharacterIterator) null);
    if (!rbbi.equals(rbbi1)) {
      errln(
          "RuleBasedBreakIterator.equals(Object) was not suppose to return "
              + "false when both objects has a null fText.");
    }

    // Tests when an exception occurs
    if (rbbi.equals(0)) {
      errln(
          "RuleBasedBreakIterator.equals(Object) was suppose to return "
              + "false when comparing to integer 0.");
    }
    if (rbbi.equals(0.0)) {
      errln(
          "RuleBasedBreakIterator.equals(Object) was suppose to return "
              + "false when comparing to float 0.0.");
    }
    if (rbbi.equals("0")) {
      errln(
          "RuleBasedBreakIterator.equals(Object) was suppose to return "
              + "false when comparing to string '0'.");
    }
  }
Ejemplo n.º 11
0
  @Override
  public void lexer_open(String text, LocaleId language, Tokens tokens) {

    if (Util.isEmpty(text)) {
      cancel();
      return;
    }
    this.text = text;

    if (iterators.containsKey(language)) {
      iterator = iterators.get(language);
    } else {
      iterator =
          (RuleBasedBreakIterator)
              BreakIterator.getWordInstance(ULocale.createCanonical(language.toString()));
      String defaultRules = iterator.toString();

      // Collect rules for the language, combine with defaultRules
      String newRules = defaultRules;

      for (LexerRule rule : getRules()) {

        boolean isInternal = Util.isEmpty(rule.getPattern());

        if (checkRule(rule, language) && !isInternal) {

          newRules =
              formatRule(
                  newRules,
                  rule.getName(),
                  rule.getDescription(),
                  rule.getPattern(),
                  rule.getLexemId());
        }
      }

      // Recreate iterator for the language(with new rules), store for future reuse
      iterator = new RuleBasedBreakIterator(newRules);
      iterators.put(language, iterator);
    }

    if (iterator == null) return;
    iterator.setText(text);

    // Sets the current iteration position to the beginning of the text
    start = iterator.first();
    end = start;
  }
Ejemplo n.º 12
0
  private void generalIteratorTest(RuleBasedBreakIterator rbbi, List<String> expectedResult) {
    StringBuffer buffer = new StringBuffer();
    String text;
    for (int i = 0; i < expectedResult.size(); i++) {
      text = expectedResult.get(i);
      buffer.append(text);
    }
    text = buffer.toString();
    if (rbbi == null) {
      errln("null iterator, test skipped.");
      return;
    }

    rbbi.setText(text);

    List<String> nextResults = _testFirstAndNext(rbbi, text);
    List<String> previousResults = _testLastAndPrevious(rbbi, text);

    logln("comparing forward and backward...");
    int errs = getErrorCount();
    compareFragmentLists("forward iteration", "backward iteration", nextResults, previousResults);
    if (getErrorCount() == errs) {
      logln("comparing expected and actual...");
      compareFragmentLists("expected result", "actual result", expectedResult, nextResults);
    }

    int[] boundaries = new int[expectedResult.size() + 3];
    boundaries[0] = RuleBasedBreakIterator.DONE;
    boundaries[1] = 0;
    for (int i = 0; i < expectedResult.size(); i++) {
      boundaries[i + 2] = boundaries[i + 1] + (expectedResult.get(i).length());
    }

    boundaries[boundaries.length - 1] = RuleBasedBreakIterator.DONE;

    _testFollowing(rbbi, text, boundaries);
    _testPreceding(rbbi, text, boundaries);
    _testIsBoundary(rbbi, text, boundaries);

    doMultipleSelectionTest(rbbi, text);
  }