|
| 1 | +package org.jooby.whoops; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertArrayEquals; |
| 4 | +import static org.junit.Assert.assertEquals; |
| 5 | +import static org.junit.Assert.assertFalse; |
| 6 | +import static org.junit.Assert.assertNotNull; |
| 7 | +import static org.junit.Assert.assertTrue; |
| 8 | + |
| 9 | +import java.io.IOException; |
| 10 | + |
| 11 | +import org.jooby.whoops.SourceLocator.Source; |
| 12 | +import org.junit.Test; |
| 13 | + |
| 14 | +public class SourceLocatorTest { |
| 15 | + |
| 16 | + @Test |
| 17 | + public void findJava() throws IOException { |
| 18 | + SourceLocator locator = SourceLocator.local(); |
| 19 | + Source source = locator.source(WhoopsApp.class.getName()); |
| 20 | + assertNotNull(source); |
| 21 | + assertTrue(source.getPath().toFile().exists()); |
| 22 | + assertTrue(source.getLines().size() > 0); |
| 23 | + assertEquals("public class WhoopsApp extends Jooby {", source.source(5, 6)); |
| 24 | + assertEquals(source.getPath().toString(), source.toString()); |
| 25 | + } |
| 26 | + |
| 27 | + @Test |
| 28 | + public void range() throws IOException { |
| 29 | + SourceLocator locator = SourceLocator.local(); |
| 30 | + Source source = locator.source(WhoopsApp.class.getName()); |
| 31 | + assertNotNull(source); |
| 32 | + assertArrayEquals(new int[]{0, 20}, source.range(1, 10)); |
| 33 | + assertArrayEquals(new int[]{5, 25}, source.range(15, 10)); |
| 34 | + assertArrayEquals(new int[]{15, 35}, source.range(33, 10)); |
| 35 | + } |
| 36 | + |
| 37 | + @Test |
| 38 | + public void emptyLinesShouldBeOneSpace() throws IOException { |
| 39 | + SourceLocator locator = SourceLocator.local(); |
| 40 | + Source source = locator.source(WhoopsApp.class.getName()); |
| 41 | + assertNotNull(source); |
| 42 | + assertEquals(" ", source.source(1, 2)); |
| 43 | + assertEquals("", source.source(-1, 2)); |
| 44 | + assertEquals("", source.source(10, Integer.MAX_VALUE)); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + public void findFile() throws IOException { |
| 49 | + SourceLocator locator = SourceLocator.local(); |
| 50 | + SourceLocator.Source source = locator.source("whoops.js"); |
| 51 | + assertNotNull(source); |
| 52 | + assertTrue(source.getPath().toFile().exists()); |
| 53 | + assertTrue(source.getLines().size() > 0); |
| 54 | + assertEquals(" console.log('hey')", source.source(1, 2)); |
| 55 | + |
| 56 | + assertEquals("})(jQuery);", source.source(2, 3)); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void missingFile() throws IOException { |
| 61 | + SourceLocator locator = SourceLocator.local(); |
| 62 | + Source source = locator.source("missing.js"); |
| 63 | + assertNotNull(source); |
| 64 | + assertFalse(source.getPath().toFile().exists()); |
| 65 | + assertTrue(source.getLines().size() == 0); |
| 66 | + assertEquals("", source.source(2, 1)); |
| 67 | + } |
| 68 | + |
| 69 | +} |
0 commit comments