blob: 5cc00781650cc6e6764eedc1e60227454903c980 [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
31TEST_F(SourceFileContentTest, Ctor) {
dan sinclair41e4d9a2022-05-01 14:40:55 +000032 Source::FileContent fc(kSource);
33 EXPECT_EQ(fc.data, kSource);
34 EXPECT_EQ(fc.data_view, kSource);
35 ASSERT_EQ(fc.lines.size(), 3u);
36 EXPECT_EQ(fc.lines[0], "line one");
37 EXPECT_EQ(fc.lines[1], "line two");
38 EXPECT_EQ(fc.lines[2], "line three");
Ryan Harrisondbc13af2022-02-21 15:19:07 +000039}
40
41TEST_F(SourceFileContentTest, CopyCtor) {
dan sinclair41e4d9a2022-05-01 14:40:55 +000042 auto src = std::make_unique<Source::FileContent>(kSource);
43 Source::FileContent fc{*src};
44 src.reset();
45 EXPECT_EQ(fc.data, kSource);
46 EXPECT_EQ(fc.data_view, kSource);
47 ASSERT_EQ(fc.lines.size(), 3u);
48 EXPECT_EQ(fc.lines[0], "line one");
49 EXPECT_EQ(fc.lines[1], "line two");
50 EXPECT_EQ(fc.lines[2], "line three");
Ryan Harrisondbc13af2022-02-21 15:19:07 +000051}
52
53TEST_F(SourceFileContentTest, MoveCtor) {
dan sinclair41e4d9a2022-05-01 14:40:55 +000054 auto src = std::make_unique<Source::FileContent>(kSource);
55 Source::FileContent fc{std::move(*src)};
56 src.reset();
57 EXPECT_EQ(fc.data, kSource);
58 EXPECT_EQ(fc.data_view, kSource);
59 ASSERT_EQ(fc.lines.size(), 3u);
60 EXPECT_EQ(fc.lines[0], "line one");
61 EXPECT_EQ(fc.lines[1], "line two");
62 EXPECT_EQ(fc.lines[2], "line three");
Ryan Harrisondbc13af2022-02-21 15:19:07 +000063}
64
Antonio Maiorano25775302022-04-25 19:49:01 +000065// Line break code points
66#define kCR "\r"
67#define kLF "\n"
68#define kVTab "\x0B"
69#define kFF "\x0C"
70#define kNL "\xC2\x85"
71#define kLS "\xE2\x80\xA8"
72#define kPS "\xE2\x80\xA9"
73
74using LineBreakTest = testing::TestWithParam<const char*>;
75TEST_P(LineBreakTest, Single) {
dan sinclair41e4d9a2022-05-01 14:40:55 +000076 std::string src = "line one";
77 src += GetParam();
78 src += "line two";
Antonio Maiorano25775302022-04-25 19:49:01 +000079
dan sinclair41e4d9a2022-05-01 14:40:55 +000080 Source::FileContent fc(src);
81 EXPECT_EQ(fc.lines.size(), 2u);
82 EXPECT_EQ(fc.lines[0], "line one");
83 EXPECT_EQ(fc.lines[1], "line two");
Antonio Maiorano25775302022-04-25 19:49:01 +000084}
85TEST_P(LineBreakTest, Double) {
dan sinclair41e4d9a2022-05-01 14:40:55 +000086 std::string src = "line one";
87 src += GetParam();
88 src += GetParam();
89 src += "line two";
Antonio Maiorano25775302022-04-25 19:49:01 +000090
dan sinclair41e4d9a2022-05-01 14:40:55 +000091 Source::FileContent fc(src);
92 EXPECT_EQ(fc.lines.size(), 3u);
93 EXPECT_EQ(fc.lines[0], "line one");
94 EXPECT_EQ(fc.lines[1], "");
95 EXPECT_EQ(fc.lines[2], "line two");
Antonio Maiorano25775302022-04-25 19:49:01 +000096}
dan sinclair41e4d9a2022-05-01 14:40:55 +000097INSTANTIATE_TEST_SUITE_P(SourceFileContentTest,
98 LineBreakTest,
99 testing::Values(kVTab, kFF, kNL, kLS, kPS, kLF, kCR, kCR kLF));
Antonio Maiorano25775302022-04-25 19:49:01 +0000100
Ryan Harrisondbc13af2022-02-21 15:19:07 +0000101} // namespace
102} // namespace tint