blob: 7871fb2deaabe0aee738d842af2c9ee1cdbff844 [file] [log] [blame]
Lukacs Berki14328eb2015-10-21 10:47:26 +00001// Copyright 2006 The Bazel Authors. All Rights Reserved.
Ulf Adams89f012d2015-02-26 13:39:28 +00002//
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.
14package com.google.devtools.build.lib.syntax;
15
Ulf Adams795895a2015-03-06 15:58:35 +000016import static com.google.common.truth.Truth.assertThat;
Ulf Adams89f012d2015-02-26 13:39:28 +000017import static com.google.devtools.build.lib.util.StringUtilities.joinLines;
18
Ulf Adamsbdd9c1f2015-04-24 14:30:01 +000019import com.google.devtools.build.lib.testutil.Scratch;
tomlu67c84b12017-11-06 19:49:16 +010020import com.google.devtools.build.lib.vfs.FileSystemUtils;
Ulf Adams89f012d2015-02-26 13:39:28 +000021import com.google.devtools.build.lib.vfs.Path;
Lukacs Berki48338222015-06-12 11:37:46 +000022import com.google.devtools.build.lib.vfs.PathFragment;
lberkiaea56b32017-05-30 12:35:33 +020023import java.io.IOException;
24import java.nio.charset.StandardCharsets;
Han-Wen Nienhuys6682a312015-02-26 14:51:57 +000025import org.junit.Test;
26import org.junit.runner.RunWith;
27import org.junit.runners.JUnit4;
Ulf Adams89f012d2015-02-26 13:39:28 +000028
Googler2abde272019-09-17 12:06:08 -070029/** A test case for {@link ParserInput}. */
Han-Wen Nienhuys6682a312015-02-26 14:51:57 +000030@RunWith(JUnit4.class)
Googler2abde272019-09-17 12:06:08 -070031public class ParserInputTest {
Ulf Adams89f012d2015-02-26 13:39:28 +000032
Ulf Adamsbdd9c1f2015-04-24 14:30:01 +000033 private Scratch scratch = new Scratch();
Ulf Adams89f012d2015-02-26 13:39:28 +000034
Han-Wen Nienhuys6682a312015-02-26 14:51:57 +000035 @Test
Ulf Adams89f012d2015-02-26 13:39:28 +000036 public void testCreateFromFile() throws IOException {
37 String content = joinLines("Line 1", "Line 2", "Line 3", "");
Ulf Adamsbdd9c1f2015-04-24 14:30:01 +000038 Path file = scratch.file("/tmp/my/file.txt", content.getBytes(StandardCharsets.UTF_8));
tomlu67c84b12017-11-06 19:49:16 +010039 byte[] bytes = FileSystemUtils.readWithKnownFileSize(file, file.getFileSize());
Googler2abde272019-09-17 12:06:08 -070040 ParserInput input = ParserInput.create(bytes, file.asFragment());
lberkiaea56b32017-05-30 12:35:33 +020041 assertThat(new String(input.getContent())).isEqualTo(content);
42 assertThat(input.getPath().toString()).isEqualTo("/tmp/my/file.txt");
Ulf Adams89f012d2015-02-26 13:39:28 +000043 }
44
Han-Wen Nienhuys6682a312015-02-26 14:51:57 +000045 @Test
Ulf Adams89f012d2015-02-26 13:39:28 +000046 public void testCreateFromString() {
47 String content = "Content provided as a string.";
48 String pathName = "/the/name/of/the/content.txt";
Googler2abde272019-09-17 12:06:08 -070049 ParserInput input = ParserInput.create(content, PathFragment.create(pathName));
lberkiaea56b32017-05-30 12:35:33 +020050 assertThat(new String(input.getContent())).isEqualTo(content);
51 assertThat(input.getPath().toString()).isEqualTo(pathName);
Ulf Adams89f012d2015-02-26 13:39:28 +000052 }
53
Han-Wen Nienhuys6682a312015-02-26 14:51:57 +000054 @Test
Ulf Adams89f012d2015-02-26 13:39:28 +000055 public void testCreateFromCharArray() {
56 String content = "Content provided as a string.";
57 String pathName = "/the/name/of/the/content.txt";
Ulf Adams89f012d2015-02-26 13:39:28 +000058 char[] contentChars = content.toCharArray();
Googler2abde272019-09-17 12:06:08 -070059 ParserInput input = ParserInput.create(contentChars, PathFragment.create(pathName));
lberkiaea56b32017-05-30 12:35:33 +020060 assertThat(new String(input.getContent())).isEqualTo(content);
61 assertThat(input.getPath().toString()).isEqualTo(pathName);
Ulf Adams89f012d2015-02-26 13:39:28 +000062 }
63
Han-Wen Nienhuys6682a312015-02-26 14:51:57 +000064 @Test
Ulf Adams89f012d2015-02-26 13:39:28 +000065 public void testWillNotTryToReadInputFileIfContentProvidedAsString() {
Googler2abde272019-09-17 12:06:08 -070066 ParserInput.create("Content provided as string.", PathFragment.create("/will/not/try/to/read"));
Ulf Adams89f012d2015-02-26 13:39:28 +000067 }
68
Han-Wen Nienhuys6682a312015-02-26 14:51:57 +000069 @Test
Ulf Adams89f012d2015-02-26 13:39:28 +000070 public void testWillNotTryToReadInputFileIfContentProvidedAsChars() {
Ulf Adams89f012d2015-02-26 13:39:28 +000071 char[] content = "Content provided as char array.".toCharArray();
Googler2abde272019-09-17 12:06:08 -070072 ParserInput.create(content, PathFragment.create("/will/not/try/to/read"));
Ulf Adams89f012d2015-02-26 13:39:28 +000073 }
Ulf Adams89f012d2015-02-26 13:39:28 +000074}