Lukacs Berki | 14328eb | 2015-10-21 10:47:26 +0000 | [diff] [blame] | 1 | // Copyright 2006 The Bazel Authors. All Rights Reserved. |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | package com.google.devtools.build.lib.syntax; |
| 15 | |
Ulf Adams | 795895a | 2015-03-06 15:58:35 +0000 | [diff] [blame] | 16 | import static com.google.common.truth.Truth.assertThat; |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 17 | import static com.google.devtools.build.lib.util.StringUtilities.joinLines; |
Han-Wen Nienhuys | 6682a31 | 2015-02-26 14:51:57 +0000 | [diff] [blame] | 18 | import static org.junit.Assert.fail; |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 19 | |
Ulf Adams | bdd9c1f | 2015-04-24 14:30:01 +0000 | [diff] [blame] | 20 | import com.google.devtools.build.lib.testutil.Scratch; |
tomlu | 67c84b1 | 2017-11-06 19:49:16 +0100 | [diff] [blame] | 21 | import com.google.devtools.build.lib.vfs.FileSystemUtils; |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 22 | import com.google.devtools.build.lib.vfs.Path; |
Lukacs Berki | 4833822 | 2015-06-12 11:37:46 +0000 | [diff] [blame] | 23 | import com.google.devtools.build.lib.vfs.PathFragment; |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 24 | import java.io.IOException; |
| 25 | import java.nio.charset.StandardCharsets; |
Han-Wen Nienhuys | 6682a31 | 2015-02-26 14:51:57 +0000 | [diff] [blame] | 26 | import org.junit.Test; |
| 27 | import org.junit.runner.RunWith; |
| 28 | import org.junit.runners.JUnit4; |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 29 | |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 30 | /** A test case for {@link ParserInputSource}. */ |
Han-Wen Nienhuys | 6682a31 | 2015-02-26 14:51:57 +0000 | [diff] [blame] | 31 | @RunWith(JUnit4.class) |
| 32 | public class ParserInputSourceTest { |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 33 | |
Ulf Adams | bdd9c1f | 2015-04-24 14:30:01 +0000 | [diff] [blame] | 34 | private Scratch scratch = new Scratch(); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 35 | |
Han-Wen Nienhuys | 6682a31 | 2015-02-26 14:51:57 +0000 | [diff] [blame] | 36 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 37 | public void testCreateFromFile() throws IOException { |
| 38 | String content = joinLines("Line 1", "Line 2", "Line 3", ""); |
Ulf Adams | bdd9c1f | 2015-04-24 14:30:01 +0000 | [diff] [blame] | 39 | Path file = scratch.file("/tmp/my/file.txt", content.getBytes(StandardCharsets.UTF_8)); |
tomlu | 67c84b1 | 2017-11-06 19:49:16 +0100 | [diff] [blame] | 40 | byte[] bytes = FileSystemUtils.readWithKnownFileSize(file, file.getFileSize()); |
| 41 | ParserInputSource input = ParserInputSource.create(bytes, file.asFragment()); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 42 | assertThat(new String(input.getContent())).isEqualTo(content); |
| 43 | assertThat(input.getPath().toString()).isEqualTo("/tmp/my/file.txt"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 44 | } |
| 45 | |
Han-Wen Nienhuys | 6682a31 | 2015-02-26 14:51:57 +0000 | [diff] [blame] | 46 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 47 | public void testCreateFromString() { |
| 48 | String content = "Content provided as a string."; |
| 49 | String pathName = "/the/name/of/the/content.txt"; |
nharmata | b4060b6 | 2017-04-04 17:11:39 +0000 | [diff] [blame] | 50 | ParserInputSource input = ParserInputSource.create(content, PathFragment.create(pathName)); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 51 | assertThat(new String(input.getContent())).isEqualTo(content); |
| 52 | assertThat(input.getPath().toString()).isEqualTo(pathName); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 53 | } |
| 54 | |
Han-Wen Nienhuys | 6682a31 | 2015-02-26 14:51:57 +0000 | [diff] [blame] | 55 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 56 | public void testCreateFromCharArray() { |
| 57 | String content = "Content provided as a string."; |
| 58 | String pathName = "/the/name/of/the/content.txt"; |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 59 | char[] contentChars = content.toCharArray(); |
nharmata | b4060b6 | 2017-04-04 17:11:39 +0000 | [diff] [blame] | 60 | ParserInputSource input = ParserInputSource.create(contentChars, PathFragment.create(pathName)); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 61 | assertThat(new String(input.getContent())).isEqualTo(content); |
| 62 | assertThat(input.getPath().toString()).isEqualTo(pathName); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 63 | } |
| 64 | |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 65 | |
Han-Wen Nienhuys | 6682a31 | 2015-02-26 14:51:57 +0000 | [diff] [blame] | 66 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 67 | public void testIOExceptionIfInputFileDoesNotExistForSingleArgConstructor() { |
| 68 | try { |
Ulf Adams | bdd9c1f | 2015-04-24 14:30:01 +0000 | [diff] [blame] | 69 | Path path = scratch.resolve("/does/not/exist"); |
tomlu | 67c84b1 | 2017-11-06 19:49:16 +0100 | [diff] [blame] | 70 | byte[] bytes = FileSystemUtils.readWithKnownFileSize(path, path.getFileSize()); |
| 71 | ParserInputSource.create(bytes, path.asFragment()); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 72 | fail(); |
| 73 | } catch (IOException e) { |
| 74 | String expected = "/does/not/exist (No such file or directory)"; |
Ulf Adams | 795895a | 2015-03-06 15:58:35 +0000 | [diff] [blame] | 75 | assertThat(e).hasMessage(expected); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 76 | } |
| 77 | } |
| 78 | |
Han-Wen Nienhuys | 6682a31 | 2015-02-26 14:51:57 +0000 | [diff] [blame] | 79 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 80 | public void testWillNotTryToReadInputFileIfContentProvidedAsString() { |
Lukacs Berki | 4833822 | 2015-06-12 11:37:46 +0000 | [diff] [blame] | 81 | ParserInputSource.create( |
nharmata | b4060b6 | 2017-04-04 17:11:39 +0000 | [diff] [blame] | 82 | "Content provided as string.", PathFragment.create("/will/not/try/to/read")); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 83 | } |
| 84 | |
Han-Wen Nienhuys | 6682a31 | 2015-02-26 14:51:57 +0000 | [diff] [blame] | 85 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 86 | public void testWillNotTryToReadInputFileIfContentProvidedAsChars() { |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 87 | char[] content = "Content provided as char array.".toCharArray(); |
nharmata | b4060b6 | 2017-04-04 17:11:39 +0000 | [diff] [blame] | 88 | ParserInputSource.create(content, PathFragment.create("/will/not/try/to/read")); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 89 | } |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 90 | } |