blob: 1ab4c6168d3e7131a16ac8bbacc4ba353f77b61f [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;
lberkiaea56b32017-05-30 12:35:33 +020022import java.io.IOException;
23import java.nio.charset.StandardCharsets;
Han-Wen Nienhuys6682a312015-02-26 14:51:57 +000024import org.junit.Test;
25import org.junit.runner.RunWith;
26import org.junit.runners.JUnit4;
Ulf Adams89f012d2015-02-26 13:39:28 +000027
Googler2abde272019-09-17 12:06:08 -070028/** A test case for {@link ParserInput}. */
Han-Wen Nienhuys6682a312015-02-26 14:51:57 +000029@RunWith(JUnit4.class)
Googler2abde272019-09-17 12:06:08 -070030public class ParserInputTest {
Ulf Adams89f012d2015-02-26 13:39:28 +000031
Ulf Adamsbdd9c1f2015-04-24 14:30:01 +000032 private Scratch scratch = new Scratch();
Ulf Adams89f012d2015-02-26 13:39:28 +000033
Han-Wen Nienhuys6682a312015-02-26 14:51:57 +000034 @Test
Ulf Adams89f012d2015-02-26 13:39:28 +000035 public void testCreateFromFile() throws IOException {
36 String content = joinLines("Line 1", "Line 2", "Line 3", "");
Ulf Adamsbdd9c1f2015-04-24 14:30:01 +000037 Path file = scratch.file("/tmp/my/file.txt", content.getBytes(StandardCharsets.UTF_8));
tomlu67c84b12017-11-06 19:49:16 +010038 byte[] bytes = FileSystemUtils.readWithKnownFileSize(file, file.getFileSize());
adonovan22096af2020-01-08 07:09:52 -080039 ParserInput input = ParserInput.create(bytes, file.toString());
lberkiaea56b32017-05-30 12:35:33 +020040 assertThat(new String(input.getContent())).isEqualTo(content);
adonovan22096af2020-01-08 07:09:52 -080041 assertThat(input.getFile()).isEqualTo("/tmp/my/file.txt");
Ulf Adams89f012d2015-02-26 13:39:28 +000042 }
43
Han-Wen Nienhuys6682a312015-02-26 14:51:57 +000044 @Test
Ulf Adams89f012d2015-02-26 13:39:28 +000045 public void testCreateFromString() {
46 String content = "Content provided as a string.";
47 String pathName = "/the/name/of/the/content.txt";
adonovan22096af2020-01-08 07:09:52 -080048 ParserInput input = ParserInput.create(content, pathName);
lberkiaea56b32017-05-30 12:35:33 +020049 assertThat(new String(input.getContent())).isEqualTo(content);
adonovan22096af2020-01-08 07:09:52 -080050 assertThat(input.getFile()).isEqualTo(pathName);
Ulf Adams89f012d2015-02-26 13:39:28 +000051 }
52
Han-Wen Nienhuys6682a312015-02-26 14:51:57 +000053 @Test
Ulf Adams89f012d2015-02-26 13:39:28 +000054 public void testCreateFromCharArray() {
55 String content = "Content provided as a string.";
56 String pathName = "/the/name/of/the/content.txt";
Ulf Adams89f012d2015-02-26 13:39:28 +000057 char[] contentChars = content.toCharArray();
adonovan22096af2020-01-08 07:09:52 -080058 ParserInput input = ParserInput.create(contentChars, pathName);
lberkiaea56b32017-05-30 12:35:33 +020059 assertThat(new String(input.getContent())).isEqualTo(content);
adonovan22096af2020-01-08 07:09:52 -080060 assertThat(input.getFile()).isEqualTo(pathName);
Ulf Adams89f012d2015-02-26 13:39:28 +000061 }
62
Han-Wen Nienhuys6682a312015-02-26 14:51:57 +000063 @Test
Ulf Adams89f012d2015-02-26 13:39:28 +000064 public void testWillNotTryToReadInputFileIfContentProvidedAsString() {
adonovan22096af2020-01-08 07:09:52 -080065 ParserInput.create("Content provided as string.", "/will/not/try/to/read");
Ulf Adams89f012d2015-02-26 13:39:28 +000066 }
67
Han-Wen Nienhuys6682a312015-02-26 14:51:57 +000068 @Test
Ulf Adams89f012d2015-02-26 13:39:28 +000069 public void testWillNotTryToReadInputFileIfContentProvidedAsChars() {
Ulf Adams89f012d2015-02-26 13:39:28 +000070 char[] content = "Content provided as char array.".toCharArray();
adonovan22096af2020-01-08 07:09:52 -080071 ParserInput.create(content, "/will/not/try/to/read");
Ulf Adams89f012d2015-02-26 13:39:28 +000072 }
Ulf Adams89f012d2015-02-26 13:39:28 +000073}