Skip to content

Commit

Permalink
Merge pull request #99 from mcarmonaa/fix/enry-java
Browse files Browse the repository at this point in the history
fix getLanguage when arguments are null
  • Loading branch information
abeaumont authored Sep 7, 2017
2 parents e1b2326 + 1d7b975 commit a6faaaf
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
22 changes: 18 additions & 4 deletions java/src/main/java/tech/sourced/enry/GoUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,19 @@ static GoString.ByValue toGoString(String str) {
bytes = str.getBytes("utf-8");
} catch (UnsupportedEncodingException e) {
bytes = str.getBytes();
} catch (NullPointerException e) {
bytes = null;
}

int length = 0;
Pointer ptr = null;
if (bytes != null) {
length = bytes.length;
ptr = ptrFromBytes(bytes);
}

GoString.ByValue val = new GoString.ByValue();
val.n = bytes.length;
Pointer ptr = new Memory(bytes.length);
ptr.write(0, bytes, 0, bytes.length);
val.n = length;
val.p = ptr;
return val;
}
Expand Down Expand Up @@ -49,7 +56,14 @@ static String[] toJavaStringArray(GoSlice slice) {
}

static GoSlice.ByValue toGoByteSlice(byte[] bytes) {
return sliceFromPtr(bytes.length, ptrFromBytes(bytes));
int length = 0;
Pointer ptr = null;
if (bytes != null) {
length = bytes.length;
ptr = ptrFromBytes(bytes);
}

return sliceFromPtr(length, ptr);
}

static GoSlice.ByValue sliceFromPtr(int len, Pointer ptr) {
Expand Down
11 changes: 11 additions & 0 deletions java/src/test/java/tech/sourced/enry/EnryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,17 @@ public void getLanguage() {
assertEquals("PHP", Enry.getLanguage("foobar.php", code.getBytes()));
}

@Test
public void getLanguageWithNullContent() {
assertEquals("Python", Enry.getLanguage("foo.py", null));
}

@Test
public void getLanguageWithNullFilename() {
byte[] content = "#!/usr/bin/env python".getBytes();
assertEquals("Python", Enry.getLanguage(null, content));
}

@Test
public void getLanguageByContent() {
String code = "<?php $foo = bar();";
Expand Down

0 comments on commit a6faaaf

Please sign in to comment.