blob: df14a31af5f84693c67fba85e23395cd4a4b86e4 [file] [log] [blame]
Ryan Harrisondbc13af2022-02-21 15:19:07 +00001// Copyright 2022 The Tint Authors.
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
15#include "src/tint/source.h"
16
17#include <memory>
18#include <utility>
19
20#include "gtest/gtest.h"
21
22namespace tint {
23namespace {
24
25static constexpr const char* kSource = R"(line one
26line two
27line three)";
28
29using SourceFileContentTest = testing::Test;
30
dan sinclair6e77b472022-10-20 13:38:28 +000031TEST_F(SourceFileContentTest, Init) {
dan sinclair41e4d9a2022-05-01 14:40:55 +000032 Source::FileContent fc(kSource);
33 EXPECT_EQ(fc.data, kSource);
dan sinclair41e4d9a2022-05-01 14:40:55 +000034 ASSERT_EQ(fc.lines.size(), 3u);
35 EXPECT_EQ(fc.lines[0], "line one");
36 EXPECT_EQ(fc.lines[1], "line two");
37 EXPECT_EQ(fc.lines[2], "line three");
Ryan Harrisondbc13af2022-02-21 15:19:07 +000038}
39
dan sinclair6e77b472022-10-20 13:38:28 +000040TEST_F(SourceFileContentTest, CopyInit) {
dan sinclair41e4d9a2022-05-01 14:40:55 +000041 auto src = std::make_unique<Source::FileContent>(kSource);
42 Source::FileContent fc{*src};
43 src.reset();
44 EXPECT_EQ(fc.data, kSource);
dan sinclair41e4d9a2022-05-01 14:40:55 +000045 ASSERT_EQ(fc.lines.size(), 3u);
46 EXPECT_EQ(fc.lines[0], "line one");
47 EXPECT_EQ(fc.lines[1], "line two");
48 EXPECT_EQ(fc.lines[2], "line three");
Ryan Harrisondbc13af2022-02-21 15:19:07 +000049}
50
dan sinclair6e77b472022-10-20 13:38:28 +000051TEST_F(SourceFileContentTest, MoveInit) {
dan sinclair41e4d9a2022-05-01 14:40:55 +000052 auto src = std::make_unique<Source::FileContent>(kSource);
53 Source::FileContent fc{std::move(*src)};
54 src.reset();
55 EXPECT_EQ(fc.data, kSource);
dan sinclair41e4d9a2022-05-01 14:40:55 +000056 ASSERT_EQ(fc.lines.size(), 3u);
57 EXPECT_EQ(fc.lines[0], "line one");
58 EXPECT_EQ(fc.lines[1], "line two");
59 EXPECT_EQ(fc.lines[2], "line three");
Ryan Harrisondbc13af2022-02-21 15:19:07 +000060}
61
Antonio Maiorano25775302022-04-25 19:49:01 +000062// Line break code points
63#define kCR "\r"
64#define kLF "\n"
65#define kVTab "\x0B"
66#define kFF "\x0C"
67#define kNL "\xC2\x85"
68#define kLS "\xE2\x80\xA8"
69#define kPS "\xE2\x80\xA9"
70
71using LineBreakTest = testing::TestWithParam<const char*>;
72TEST_P(LineBreakTest, Single) {
dan sinclair41e4d9a2022-05-01 14:40:55 +000073 std::string src = "line one";
74 src += GetParam();
75 src += "line two";
Antonio Maiorano25775302022-04-25 19:49:01 +000076
dan sinclair41e4d9a2022-05-01 14:40:55 +000077 Source::FileContent fc(src);
78 EXPECT_EQ(fc.lines.size(), 2u);
79 EXPECT_EQ(fc.lines[0], "line one");
80 EXPECT_EQ(fc.lines[1], "line two");
Antonio Maiorano25775302022-04-25 19:49:01 +000081}
82TEST_P(LineBreakTest, Double) {
dan sinclair41e4d9a2022-05-01 14:40:55 +000083 std::string src = "line one";
84 src += GetParam();
85 src += GetParam();
86 src += "line two";
Antonio Maiorano25775302022-04-25 19:49:01 +000087
dan sinclair41e4d9a2022-05-01 14:40:55 +000088 Source::FileContent fc(src);
89 EXPECT_EQ(fc.lines.size(), 3u);
90 EXPECT_EQ(fc.lines[0], "line one");
91 EXPECT_EQ(fc.lines[1], "");
92 EXPECT_EQ(fc.lines[2], "line two");
Antonio Maiorano25775302022-04-25 19:49:01 +000093}
dan sinclair41e4d9a2022-05-01 14:40:55 +000094INSTANTIATE_TEST_SUITE_P(SourceFileContentTest,
95 LineBreakTest,
96 testing::Values(kVTab, kFF, kNL, kLS, kPS, kLF, kCR, kCR kLF));
Antonio Maiorano25775302022-04-25 19:49:01 +000097
Ryan Harrisondbc13af2022-02-21 15:19:07 +000098} // namespace
99} // namespace tint