001/* 002 * ModeShape (http://www.modeshape.org) 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.modeshape.common.text; 017 018import static org.hamcrest.core.Is.is; 019import static org.junit.Assert.assertThat; 020import org.junit.Test; 021 022public class PositionTest { 023 024 private int combinedIndex( int firstIndex, 025 int secondIndex ) { 026 Position first = new Position(firstIndex, 1, 0); 027 Position second = new Position(secondIndex, 1, 0); 028 029 int firstPlusSecond = first.add(second).getIndexInContent(); 030 int secondPlusFirst = second.add(first).getIndexInContent(); 031 032 assertThat(firstPlusSecond, is(secondPlusFirst)); 033 034 return firstPlusSecond; 035 } 036 037 @Test 038 public void shouldAddNoContentPositionToValidPosition() { 039 // -1 to >=0 040 assertThat(combinedIndex(-1, 0), is(0)); 041 assertThat(combinedIndex(-1, 1), is(1)); 042 assertThat(combinedIndex(-1, 10), is(10)); 043 } 044 045 @Test 046 public void shouldAddValidPositionToNoContentPosition() { 047 // >= 0 to -1 048 assertThat(combinedIndex(0, -1), is(0)); 049 assertThat(combinedIndex(1, -1), is(1)); 050 assertThat(combinedIndex(10, -1), is(10)); 051 } 052 053 @Test 054 public void shouldAddValidPositionToValidPosition() { 055 // positive to positive 056 assertThat(combinedIndex(1, 1), is(2)); 057 assertThat(combinedIndex(10, 1), is(11)); 058 assertThat(combinedIndex(1, 10), is(11)); 059 assertThat(combinedIndex(10, 10), is(20)); 060 } 061 062 @Test 063 public void shouldAddStartingPositionToStartingPosition() { 064 // 0 to 0 065 assertThat(combinedIndex(0, 0), is(0)); 066 } 067 068 @Test 069 public void shouldAddNoContentPositionToNoContentPosition() { 070 // -1 to -1 071 assertThat(combinedIndex(-1, -1), is(-1)); 072 assertThat(combinedIndex(-10, -1), is(-1)); 073 assertThat(combinedIndex(-1, -10), is(-1)); 074 } 075}