// Path: streamflyer-core/src/main/java/com/github/rwitzel/streamflyer/regex/addons/tokens/MatchResultWithOffset.java
// public class MatchResultWithOffset implements MatchResult {
// 
//     private MatchResult delegate;
// 
//     private int groupOffset;
// 
//     /**
//      * Calculated on demand.
//      */
//     private Integer groupCount;
// 
//     public MatchResultWithOffset(MatchResult delegate, int groupOffset) {
//         super();
// 
//         if (groupOffset > delegate.groupCount() || groupOffset < 0) {
//             throw new IndexOutOfBoundsException("No group " + groupOffset);
//         }
// 
//         this.delegate = delegate;
//         this.groupOffset = groupOffset;
//     }
// 
//     @Override
//     public int start() {
//         return delegate.start(groupOffset);
//     }
// 
//     @Override
//     public int start(int group) {
//         return delegate.start(groupOffset + group);
//     }
// 
//     @Override
//     public int end() {
//         return delegate.end(groupOffset);
//     }
// 
//     @Override
//     public int end(int group) {
//         return delegate.end(groupOffset + group);
//     }
// 
//     @Override
//     public String group() {
//         return delegate.group(groupOffset);
//     }
// 
//     @Override
//     public String group(int group) {
//         return delegate.group(groupOffset + group);
//     }
// 
//     /**
//      * Attention! Without Java reflection we cannot find out whether an unmatched or empty group immediately placed
//      * before or after the end of a group is within or outside the other group. That is the returned group count might
//      * be to high because it includes additional unmatched or empty groups.
//      */
//     @Override
//     public int groupCount() {
//         if (groupCount == null) {
//             int groupIndex = groupOffset + 1;
//             while (groupIndex <= delegate.groupCount() && delegate.end(groupIndex) <= delegate.end(groupOffset)) {
//                 groupIndex++;
//             }
//             groupCount = (groupIndex - 1) - groupOffset;
//         }
//         return groupCount;
//     }
// 
//     /*
//      * (non-Javadoc)
//      * 
//      * @see java.lang.Object#toString()
//      */
//     @Override
//     public String toString() {
//         return "MatchResultWithOffset [delegate=" + delegate + ", groupOffset=" + groupOffset + ", groupCount="
//                 + groupCount + "]";
//     }
// 
// }


import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.util.regex.MatchResult;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.junit.Test;
import com.github.rwitzel.streamflyer.regex.addons.tokens.MatchResultWithOffset;

/**
 * Copyright (C) 2011 rwitzel75@googlemail.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.github.rwitzel.streamflyer.regex.addons.tokens;





/**
 * Tests {@link MatchResultWithOffset}.
 * 
 * @author rwoo
 * 
 */
public class MatchResultWithOffsetTest {

    /**
     * Test for the unexpected behaviour of empty groups
     * 
     * @throws Exception
     */
    @Test
    public void testGroupCountAndMore_UNEXPECTED_emptyGroupsAfterEndOfTargetGroupIncluded() throws Exception {

        String input = "1xxnnnnnnnnnn";
        String regex = "(1xx())()";
        Matcher matcher = Pattern.compile(regex).matcher(input);
        assertTrue(matcher.find());
        assertEquals(3, matcher.groupCount()); // principle of the longest possible match

        {
            // offset = 1 -> "1xx" will be the match result

// Path: streamflyer-core/src/main/java/com/github/rwitzel/streamflyer/regex/addons/tokens/MatchResultWithOffset.java
// public class MatchResultWithOffset implements MatchResult {
// 
//     private MatchResult delegate;
// 
//     private int groupOffset;
// 
//     /**
//      * Calculated on demand.
//      */
//     private Integer groupCount;
// 
//     public MatchResultWithOffset(MatchResult delegate, int groupOffset) {
//         super();
// 
//         if (groupOffset > delegate.groupCount() || groupOffset < 0) {
//             throw new IndexOutOfBoundsException("No group " + groupOffset);
//         }
// 
//         this.delegate = delegate;
//         this.groupOffset = groupOffset;
//     }
// 
//     @Override
//     public int start() {
//         return delegate.start(groupOffset);
//     }
// 
//     @Override
//     public int start(int group) {
//         return delegate.start(groupOffset + group);
//     }
// 
//     @Override
//     public int end() {
//         return delegate.end(groupOffset);
//     }
// 
//     @Override
//     public int end(int group) {
//         return delegate.end(groupOffset + group);
//     }
// 
//     @Override
//     public String group() {
//         return delegate.group(groupOffset);
//     }
// 
//     @Override
//     public String group(int group) {
//         return delegate.group(groupOffset + group);
//     }
// 
//     /**
//      * Attention! Without Java reflection we cannot find out whether an unmatched or empty group immediately placed
//      * before or after the end of a group is within or outside the other group. That is the returned group count might
//      * be to high because it includes additional unmatched or empty groups.
//      */
//     @Override
//     public int groupCount() {
//         if (groupCount == null) {
//             int groupIndex = groupOffset + 1;
//             while (groupIndex <= delegate.groupCount() && delegate.end(groupIndex) <= delegate.end(groupOffset)) {
//                 groupIndex++;
//             }
//             groupCount = (groupIndex - 1) - groupOffset;
//         }
//         return groupCount;
//     }
// 
//     /*
//      * (non-Javadoc)
//      * 
//      * @see java.lang.Object#toString()
//      */
//     @Override
//     public String toString() {
//         return "MatchResultWithOffset [delegate=" + delegate + ", groupOffset=" + groupOffset + ", groupCount="
//                 + groupCount + "]";
//     }
// 
// }


// Path: streamflyer-core/src/test/java/com/github/rwitzel/streamflyer/regex/addons/tokens/MatchResultWithOffsetTest.java
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.util.regex.MatchResult;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.junit.Test;
import com.github.rwitzel.streamflyer.regex.addons.tokens.MatchResultWithOffset;

/**
 * Copyright (C) 2011 rwitzel75@googlemail.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.github.rwitzel.streamflyer.regex.addons.tokens;





/**
 * Tests {@link MatchResultWithOffset}.
 * 
 * @author rwoo
 * 
 */
public class MatchResultWithOffsetTest {

    /**
     * Test for the unexpected behaviour of empty groups
     * 
     * @throws Exception
     */
    @Test
    public void testGroupCountAndMore_UNEXPECTED_emptyGroupsAfterEndOfTargetGroupIncluded() throws Exception {

        String input = "1xxnnnnnnnnnn";
        String regex = "(1xx())()";
        Matcher matcher = Pattern.compile(regex).matcher(input);
        assertTrue(matcher.find());
        assertEquals(3, matcher.groupCount()); // principle of the longest possible match

        {
            // offset = 1 -> "1xx" will be the match result

            MatchResultWithOffset mo = new MatchResultWithOffset(matcher, 1);
