Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 1 | // 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 | |
| 22 | namespace tint { |
| 23 | namespace { |
| 24 | |
| 25 | static constexpr const char* kSource = R"(line one |
| 26 | line two |
| 27 | line three)"; |
| 28 | |
| 29 | using SourceFileContentTest = testing::Test; |
| 30 | |
dan sinclair | 6e77b47 | 2022-10-20 13:38:28 +0000 | [diff] [blame] | 31 | TEST_F(SourceFileContentTest, Init) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 32 | Source::FileContent fc(kSource); |
| 33 | EXPECT_EQ(fc.data, kSource); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 34 | 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 Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 38 | } |
| 39 | |
dan sinclair | 6e77b47 | 2022-10-20 13:38:28 +0000 | [diff] [blame] | 40 | TEST_F(SourceFileContentTest, CopyInit) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 41 | auto src = std::make_unique<Source::FileContent>(kSource); |
| 42 | Source::FileContent fc{*src}; |
| 43 | src.reset(); |
| 44 | EXPECT_EQ(fc.data, kSource); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 45 | 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 Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 49 | } |
| 50 | |
dan sinclair | 6e77b47 | 2022-10-20 13:38:28 +0000 | [diff] [blame] | 51 | TEST_F(SourceFileContentTest, MoveInit) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 52 | 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 sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 56 | 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 Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 60 | } |
| 61 | |
Antonio Maiorano | 2577530 | 2022-04-25 19:49:01 +0000 | [diff] [blame] | 62 | // 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 | |
| 71 | using LineBreakTest = testing::TestWithParam<const char*>; |
| 72 | TEST_P(LineBreakTest, Single) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 73 | std::string src = "line one"; |
| 74 | src += GetParam(); |
| 75 | src += "line two"; |
Antonio Maiorano | 2577530 | 2022-04-25 19:49:01 +0000 | [diff] [blame] | 76 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 77 | 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 Maiorano | 2577530 | 2022-04-25 19:49:01 +0000 | [diff] [blame] | 81 | } |
| 82 | TEST_P(LineBreakTest, Double) { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 83 | std::string src = "line one"; |
| 84 | src += GetParam(); |
| 85 | src += GetParam(); |
| 86 | src += "line two"; |
Antonio Maiorano | 2577530 | 2022-04-25 19:49:01 +0000 | [diff] [blame] | 87 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 88 | 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 Maiorano | 2577530 | 2022-04-25 19:49:01 +0000 | [diff] [blame] | 93 | } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 94 | INSTANTIATE_TEST_SUITE_P(SourceFileContentTest, |
| 95 | LineBreakTest, |
| 96 | testing::Values(kVTab, kFF, kNL, kLS, kPS, kLF, kCR, kCR kLF)); |
Antonio Maiorano | 2577530 | 2022-04-25 19:49:01 +0000 | [diff] [blame] | 97 | |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 98 | } // namespace |
| 99 | } // namespace tint |