blob: c52231e04ed2d072233658eeb5b5c5df4889bec9 [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) {
32 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");
39}
40
41TEST_F(SourceFileContentTest, CopyCtor) {
42 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");
51}
52
53TEST_F(SourceFileContentTest, MoveCtor) {
54 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");
63}
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) {
76 std::string src = "line one";
77 src += GetParam();
78 src += "line two";
79
80 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");
84}
85TEST_P(LineBreakTest, Double) {
86 std::string src = "line one";
87 src += GetParam();
88 src += GetParam();
89 src += "line two";
90
91 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");
96}
97INSTANTIATE_TEST_SUITE_P(
98 SourceFileContentTest,
99 LineBreakTest,
100 testing::Values(kVTab, kFF, kNL, kLS, kPS, kLF, kCR, kCR kLF));
101
Ryan Harrisondbc13af2022-02-21 15:19:07 +0000102} // namespace
103} // namespace tint