Added gwt-test-utils to test EditIterator.

Modfied lib/maven.defs so that maven_jar() is able to handle snapshot
builds.

Simplified the implementation of the advance() method in
EditIterator. Previously, the method was calling substring()
on the lines, which was unnecessary.

Change-Id: I3b00caa140539ae77605721d741afae75ae1448e
This commit is contained in:
Michael Zhou 2013-06-11 20:35:12 -07:00
parent 665c2a88d3
commit 7b8161da76
7 changed files with 145 additions and 21 deletions

View File

@ -76,6 +76,7 @@ gwt_module(
'//lib:jsr305',
'//lib/codemirror:codemirror',
'//lib/gwt:user',
'//lib/gwt:gwt-test-utils',
'//lib/jgit:jgit',
],
visibility = [
@ -87,11 +88,17 @@ gwt_module(
java_test(
name = 'ui_tests',
srcs = glob(['src/test/java/**/*.java']),
resources = glob(['src/test/resources/**/*']) + [
'src/main/java/com/google/gerrit/GerritGwtUI.gwt.xml',
],
deps = [
':ui_module',
'//lib:junit',
'//lib/gwt:dev',
'//lib/gwt:user',
'//lib/gwt:gwt-test-utils',
'//lib/jgit:jgit',
],
source_under_test = [':ui_module'],
)

View File

@ -259,34 +259,28 @@ public class CodeMirrorDemo extends Screen {
: null;
}
private static class EditIterator {
static class EditIterator {
private final JsArrayString lines;
private final int startLine;
private int currLineIndex;
private int currLineOffset;
private EditIterator(JsArrayString lineArray, int start) {
EditIterator(JsArrayString lineArray, int start) {
lines = lineArray;
startLine = start;
}
private LineCharacter advance(int numOfChar) {
LineCharacter advance(int numOfChar) {
while (currLineIndex < lines.length()) {
String line = lines.get(currLineIndex).substring(currLineOffset);
int lengthWithNewline = line.length() + 1;
int lengthWithNewline =
lines.get(currLineIndex).length() - currLineOffset + 1;
if (numOfChar < lengthWithNewline) {
LineCharacter at = LineCharacter.create(
startLine + currLineIndex,
numOfChar + currLineOffset);
currLineOffset += numOfChar;
if (currLineOffset == line.length()) {
advanceLine();
}
return at;
}
if (numOfChar == lengthWithNewline) {
return LineCharacter.create(startLine + currLineIndex + 1, 0);
}
numOfChar -= lengthWithNewline;
advanceLine();
}

View File

@ -20,16 +20,13 @@ import com.google.gwt.core.client.JavaScriptObject;
public class LineCharacter extends JavaScriptObject {
public static LineCharacter create(int line, int ch) {
LineCharacter lineCh = createObject().cast();
return lineCh.setLine(line).setCh(ch);
lineCh.setLine(line);
lineCh.setCh(ch);
return lineCh;
}
private final native LineCharacter setLine(int line) /*-{
this.line = line; return this;
}-*/;
private final native LineCharacter setCh(int ch) /*-{
this.ch = ch; return this;
}-*/;
public final native void setLine(int line) /*-{ this.line = line; }-*/;
public final native void setCh(int ch) /*-{ this.ch = ch; }-*/;
public final native int getLine() /*-{ return this.line; }-*/;
public final native int getCh() /*-{ return this.ch; }-*/;

View File

@ -0,0 +1,97 @@
// Copyright (C) 2013 The Android Open Source Project
//
// 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.google.gerrit.client.diff;
import static org.junit.Assert.assertEquals;
import com.google.gerrit.client.diff.CodeMirrorDemo.EditIterator;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.core.client.JsArrayString;
import com.googlecode.gwt.test.GwtModule;
import com.googlecode.gwt.test.GwtTest;
import net.codemirror.lib.LineCharacter;
import org.junit.Before;
import org.junit.Test;
/** Unit tests for EditIterator */
@GwtModule("com.google.gerrit.GerritGwtUI")
public class EditIteratorTest extends GwtTest {
private JsArrayString lines;
private void assertLineChsEqual(LineCharacter a, LineCharacter b) {
assertEquals(a.getLine() + "," + a.getCh(), b.getLine() + "," + b.getCh());
}
@Before
public void initialize() {
lines = (JsArrayString) JavaScriptObject.createArray();
lines.push("1st");
lines.push("2nd");
lines.push("3rd");
}
@Test
public void testNoAdvance() {
EditIterator iter = new EditIterator(lines, 0);
assertLineChsEqual(LineCharacter.create(0, 0), iter.advance(0));
}
@Test
public void testSimpleAdvance() {
EditIterator iter = new EditIterator(lines, 0);
assertLineChsEqual(LineCharacter.create(0, 1), iter.advance(1));
}
@Test
public void testEndsBeforeNewline() {
EditIterator iter = new EditIterator(lines, 0);
assertLineChsEqual(LineCharacter.create(0, 3), iter.advance(3));
}
@Test
public void testEndsOnNewline() {
EditIterator iter = new EditIterator(lines, 0);
assertLineChsEqual(LineCharacter.create(1, 0), iter.advance(4));
}
@Test
public void testAcrossNewline() {
EditIterator iter = new EditIterator(lines, 0);
assertLineChsEqual(LineCharacter.create(1, 1), iter.advance(5));
}
@Test
public void testContinueFromBeforeNewline() {
EditIterator iter = new EditIterator(lines, 0);
iter.advance(3);
assertLineChsEqual(LineCharacter.create(2, 2), iter.advance(7));
}
@Test
public void testContinueFromAfterNewline() {
EditIterator iter = new EditIterator(lines, 0);
iter.advance(4);
assertLineChsEqual(LineCharacter.create(2, 2), iter.advance(6));
}
@Test
public void testAcrossMultipleLines() {
EditIterator iter = new EditIterator(lines, 0);
assertLineChsEqual(LineCharacter.create(2, 2), iter.advance(10));
}
}

View File

@ -0,0 +1 @@
com.google.gerrit.GerritGwtUI = gwt-module

View File

@ -36,3 +36,25 @@ python_binary(
main = 'compiler.py',
visibility = ['PUBLIC'],
)
maven_jar(
name = 'gwt-test-utils',
id = 'com.googlecode.gwt-test-utils:gwt-test-utils:0.45-SNAPSHOT-20130612.054327-7',
sha1 = 'ad53b8a05df35fbe394db80d1d7d2913a646bfb3',
license = 'Apache2.0',
repository = 'http://oss.sonatype.org/content/repositories/snapshots',
deps = [
':javassist',
'//lib/log:api',
],
visibility = ['PUBLIC'],
)
maven_jar(
name = 'javassist',
id = 'org.javassist:javassist:3.16.1-GA',
sha1 = '315891b371395271977af518d4db5cee1a0bc9bf',
license = 'Apache2.0',
visibility = [],
)

View File

@ -44,11 +44,17 @@ def maven_jar(
raise NameError('expected id="groupId:artifactId:version"')
group, artifact, version = parts
jar = path.join(name, artifact.lower() + '-' + version)
if 'SNAPSHOT' in version:
file_version = version.replace('-SNAPSHOT', '')
version = version.split('-')[0] + '-SNAPSHOT'
else:
file_version = version
jar = path.join(name, artifact.lower() + '-' + file_version)
url = '/'.join([
repository,
group.replace('.', '/'), artifact, version,
artifact + '-' + version])
artifact + '-' + file_version])
binjar = jar + '.jar'
binurl = url + '.jar'