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; |
| 18 | |
Ulf Adams | bdd9c1f | 2015-04-24 14:30:01 +0000 | [diff] [blame] | 19 | import com.google.devtools.build.lib.testutil.Scratch; |
tomlu | 67c84b1 | 2017-11-06 19:49:16 +0100 | [diff] [blame] | 20 | import com.google.devtools.build.lib.vfs.FileSystemUtils; |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 21 | import com.google.devtools.build.lib.vfs.Path; |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 22 | import java.io.IOException; |
| 23 | import java.nio.charset.StandardCharsets; |
Han-Wen Nienhuys | 6682a31 | 2015-02-26 14:51:57 +0000 | [diff] [blame] | 24 | import org.junit.Test; |
| 25 | import org.junit.runner.RunWith; |
| 26 | import org.junit.runners.JUnit4; |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 27 | |
Googler | 2abde27 | 2019-09-17 12:06:08 -0700 | [diff] [blame] | 28 | /** A test case for {@link ParserInput}. */ |
Han-Wen Nienhuys | 6682a31 | 2015-02-26 14:51:57 +0000 | [diff] [blame] | 29 | @RunWith(JUnit4.class) |
Googler | 2abde27 | 2019-09-17 12:06:08 -0700 | [diff] [blame] | 30 | public class ParserInputTest { |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 31 | |
Ulf Adams | bdd9c1f | 2015-04-24 14:30:01 +0000 | [diff] [blame] | 32 | private Scratch scratch = new Scratch(); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 33 | |
Han-Wen Nienhuys | 6682a31 | 2015-02-26 14:51:57 +0000 | [diff] [blame] | 34 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 35 | public void testCreateFromFile() throws IOException { |
| 36 | String content = joinLines("Line 1", "Line 2", "Line 3", ""); |
Ulf Adams | bdd9c1f | 2015-04-24 14:30:01 +0000 | [diff] [blame] | 37 | Path file = scratch.file("/tmp/my/file.txt", content.getBytes(StandardCharsets.UTF_8)); |
tomlu | 67c84b1 | 2017-11-06 19:49:16 +0100 | [diff] [blame] | 38 | byte[] bytes = FileSystemUtils.readWithKnownFileSize(file, file.getFileSize()); |
adonovan | 22096af | 2020-01-08 07:09:52 -0800 | [diff] [blame] | 39 | ParserInput input = ParserInput.create(bytes, file.toString()); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 40 | assertThat(new String(input.getContent())).isEqualTo(content); |
adonovan | 22096af | 2020-01-08 07:09:52 -0800 | [diff] [blame] | 41 | assertThat(input.getFile()).isEqualTo("/tmp/my/file.txt"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 42 | } |
| 43 | |
Han-Wen Nienhuys | 6682a31 | 2015-02-26 14:51:57 +0000 | [diff] [blame] | 44 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 45 | public void testCreateFromString() { |
| 46 | String content = "Content provided as a string."; |
| 47 | String pathName = "/the/name/of/the/content.txt"; |
adonovan | 22096af | 2020-01-08 07:09:52 -0800 | [diff] [blame] | 48 | ParserInput input = ParserInput.create(content, pathName); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 49 | assertThat(new String(input.getContent())).isEqualTo(content); |
adonovan | 22096af | 2020-01-08 07:09:52 -0800 | [diff] [blame] | 50 | assertThat(input.getFile()).isEqualTo(pathName); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 51 | } |
| 52 | |
Han-Wen Nienhuys | 6682a31 | 2015-02-26 14:51:57 +0000 | [diff] [blame] | 53 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 54 | public void testCreateFromCharArray() { |
| 55 | String content = "Content provided as a string."; |
| 56 | String pathName = "/the/name/of/the/content.txt"; |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 57 | char[] contentChars = content.toCharArray(); |
adonovan | 22096af | 2020-01-08 07:09:52 -0800 | [diff] [blame] | 58 | ParserInput input = ParserInput.create(contentChars, pathName); |
lberki | aea56b3 | 2017-05-30 12:35:33 +0200 | [diff] [blame] | 59 | assertThat(new String(input.getContent())).isEqualTo(content); |
adonovan | 22096af | 2020-01-08 07:09:52 -0800 | [diff] [blame] | 60 | assertThat(input.getFile()).isEqualTo(pathName); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 61 | } |
| 62 | |
Han-Wen Nienhuys | 6682a31 | 2015-02-26 14:51:57 +0000 | [diff] [blame] | 63 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 64 | public void testWillNotTryToReadInputFileIfContentProvidedAsString() { |
adonovan | 22096af | 2020-01-08 07:09:52 -0800 | [diff] [blame] | 65 | ParserInput.create("Content provided as string.", "/will/not/try/to/read"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Han-Wen Nienhuys | 6682a31 | 2015-02-26 14:51:57 +0000 | [diff] [blame] | 68 | @Test |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 69 | public void testWillNotTryToReadInputFileIfContentProvidedAsChars() { |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 70 | char[] content = "Content provided as char array.".toCharArray(); |
adonovan | 22096af | 2020-01-08 07:09:52 -0800 | [diff] [blame] | 71 | ParserInput.create(content, "/will/not/try/to/read"); |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 72 | } |
Ulf Adams | 89f012d | 2015-02-26 13:39:28 +0000 | [diff] [blame] | 73 | } |