Skip to content

Commit ef0b887

Browse files
committed
Add tests of CommitUtils helpers
1 parent 25cb993 commit ef0b887

File tree

2 files changed

+159
-5
lines changed

2 files changed

+159
-5
lines changed

app/src/main/java/com/github/mobile/core/commit/CommitUtils.java

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,14 @@
3636
*/
3737
public class CommitUtils {
3838

39+
/**
40+
* Length of used for abbreviations
41+
*/
42+
public static final int LENGTH = 10;
43+
3944
private static final NumberFormat FORMAT = NumberFormat
4045
.getIntegerInstance();
4146

42-
private static final int LENGTH = 10;
43-
4447
/**
4548
* Abbreviate commit sha to default length if longer
4649
*
@@ -258,10 +261,19 @@ public static StyledText formatStats(final Collection<CommitFile> files) {
258261
* @param file
259262
* @return last segment of commit file path
260263
*/
261-
public static String getName(CommitFile file) {
262-
String path = file.getFilename();
264+
public static String getName(final CommitFile file) {
265+
return file != null ? getName(file.getFilename()) : null;
266+
}
267+
268+
/**
269+
* Get file name for path
270+
*
271+
* @param path
272+
* @return last segment of path
273+
*/
274+
public static String getName(final String path) {
263275
if (TextUtils.isEmpty(path))
264-
return null;
276+
return path;
265277

266278
int lastSlash = path.lastIndexOf('/');
267279
if (lastSlash != -1 && lastSlash + 1 < path.length())
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
/*
2+
* Copyright 2012 GitHub Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.github.mobile.tests.commit;
17+
18+
import android.test.AndroidTestCase;
19+
20+
import com.github.mobile.core.commit.CommitUtils;
21+
22+
import java.util.Date;
23+
24+
import org.eclipse.egit.github.core.Commit;
25+
import org.eclipse.egit.github.core.CommitFile;
26+
import org.eclipse.egit.github.core.CommitUser;
27+
import org.eclipse.egit.github.core.RepositoryCommit;
28+
import org.eclipse.egit.github.core.User;
29+
30+
/**
31+
* Test of {@link CommitUtils}
32+
*/
33+
public class CommitUtilsTest extends AndroidTestCase {
34+
35+
/**
36+
* Test commit SHA-1 abbreviation
37+
*/
38+
public void testAbbreviate() {
39+
assertNull(CommitUtils.abbreviate((Commit) null));
40+
assertNull(CommitUtils.abbreviate((RepositoryCommit) null));
41+
assertNull(CommitUtils.abbreviate((String) null));
42+
assertEquals("", CommitUtils.abbreviate(""));
43+
assertEquals("a", CommitUtils.abbreviate("a"));
44+
assertEquals("abcdefghij", CommitUtils.abbreviate("abcdefghijk"));
45+
assertEquals("abc", CommitUtils.abbreviate(new Commit().setSha("abc")));
46+
assertEquals("abcd",
47+
CommitUtils.abbreviate(new RepositoryCommit().setSha("abcd")));
48+
}
49+
50+
/**
51+
* Test commit name parsing from path
52+
*/
53+
public void testGetName() {
54+
assertNull(CommitUtils.getName((String) null));
55+
assertNull(CommitUtils.getName((CommitFile) null));
56+
assertEquals("", CommitUtils.getName(""));
57+
assertEquals("/", CommitUtils.getName("/"));
58+
assertEquals("b", CommitUtils.getName("a/b"));
59+
assertEquals("c",
60+
CommitUtils.getName(new CommitFile().setFilename("a/b/c")));
61+
}
62+
63+
/**
64+
* Test commit SHA-1 evaluation
65+
*/
66+
public void testIsValidCommit() {
67+
assertFalse(CommitUtils.isValidCommit(null));
68+
assertFalse(CommitUtils.isValidCommit(""));
69+
assertTrue(CommitUtils.isValidCommit("a"));
70+
assertTrue(CommitUtils.isValidCommit("bbbbb"));
71+
assertFalse(CommitUtils.isValidCommit("am"));
72+
assertFalse(CommitUtils.isValidCommit("xyz"));
73+
}
74+
75+
/**
76+
* Test parsing author from commit
77+
*/
78+
public void testGetAuthor() {
79+
RepositoryCommit commit = new RepositoryCommit();
80+
assertNull(CommitUtils.getAuthor(commit));
81+
Commit rawCommit = new Commit();
82+
commit.setCommit(rawCommit);
83+
assertNull(CommitUtils.getAuthor(commit));
84+
CommitUser user = new CommitUser();
85+
rawCommit.setAuthor(user);
86+
assertNull(CommitUtils.getAuthor(commit));
87+
user.setName("u1");
88+
assertEquals("u1", CommitUtils.getAuthor(commit));
89+
commit.setAuthor(new User().setLogin("u2"));
90+
assertEquals("u2", CommitUtils.getAuthor(commit));
91+
}
92+
93+
/**
94+
* Test parsing committer from commit
95+
*/
96+
public void testGetCommitter() {
97+
RepositoryCommit commit = new RepositoryCommit();
98+
assertNull(CommitUtils.getCommitter(commit));
99+
Commit rawCommit = new Commit();
100+
commit.setCommit(rawCommit);
101+
assertNull(CommitUtils.getCommitter(commit));
102+
CommitUser user = new CommitUser();
103+
rawCommit.setCommitter(user);
104+
assertNull(CommitUtils.getCommitter(commit));
105+
user.setName("u1");
106+
assertEquals("u1", CommitUtils.getCommitter(commit));
107+
commit.setCommitter(new User().setLogin("u2"));
108+
assertEquals("u2", CommitUtils.getCommitter(commit));
109+
}
110+
111+
/**
112+
* Test parsing author date from commit
113+
*/
114+
public void testGetAuthorDate() {
115+
RepositoryCommit commit = new RepositoryCommit();
116+
assertNull(CommitUtils.getAuthorDate(commit));
117+
Commit rawCommit = new Commit();
118+
commit.setCommit(rawCommit);
119+
assertNull(CommitUtils.getAuthorDate(commit));
120+
CommitUser user = new CommitUser();
121+
rawCommit.setAuthor(user);
122+
assertNull(CommitUtils.getAuthorDate(commit));
123+
user.setDate(new Date(12345));
124+
assertEquals(new Date(12345), CommitUtils.getAuthorDate(commit));
125+
}
126+
127+
/**
128+
* Test parsing committer date from commit
129+
*/
130+
public void testGetCommitterDate() {
131+
RepositoryCommit commit = new RepositoryCommit();
132+
assertNull(CommitUtils.getCommiterDate(commit));
133+
Commit rawCommit = new Commit();
134+
commit.setCommit(rawCommit);
135+
assertNull(CommitUtils.getCommiterDate(commit));
136+
CommitUser user = new CommitUser();
137+
rawCommit.setCommitter(user);
138+
assertNull(CommitUtils.getCommiterDate(commit));
139+
user.setDate(new Date(12345));
140+
assertEquals(new Date(12345), CommitUtils.getCommiterDate(commit));
141+
}
142+
}

0 commit comments

Comments
 (0)