fix getLanguage when arguments are null

This commit is contained in:
Manuel Carmona 2017-09-07 14:17:56 +02:00
parent 0fe0a97f67
commit 1d7b975743
2 changed files with 29 additions and 4 deletions

View File

@ -15,12 +15,19 @@ class GoUtils {
bytes = str.getBytes("utf-8"); bytes = str.getBytes("utf-8");
} catch (UnsupportedEncodingException e) { } catch (UnsupportedEncodingException e) {
bytes = str.getBytes(); 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(); GoString.ByValue val = new GoString.ByValue();
val.n = bytes.length; val.n = length;
Pointer ptr = new Memory(bytes.length);
ptr.write(0, bytes, 0, bytes.length);
val.p = ptr; val.p = ptr;
return val; return val;
} }
@ -49,7 +56,14 @@ class GoUtils {
} }
static GoSlice.ByValue toGoByteSlice(byte[] bytes) { 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) { static GoSlice.ByValue sliceFromPtr(int len, Pointer ptr) {

View File

@ -18,6 +18,17 @@ public class EnryTest {
assertEquals("PHP", Enry.getLanguage("foobar.php", code.getBytes())); 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 @Test
public void getLanguageByContent() { public void getLanguageByContent() {
String code = "<?php $foo = bar();"; String code = "<?php $foo = bar();";