dawn-cmake/src/reader/wgsl/token_test.cc
Ben Clayton 580d6c7f3e Produce end ranges for tokens and AST nodes
This includes a couple of position fixes in `lexer.cc`.

The source for identifiers (vars, params, etc) now refer to the identifier, not the first token of the construct.

Bug: tint:282
Change-Id: I58cb8422a4af1c7dc5f84431fd7f06b823b514c5
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/31444
Commit-Queue: Ben Clayton <bclayton@google.com>
Reviewed-by: dan sinclair <dsinclair@chromium.org>
2020-11-02 15:25:18 +00:00

88 lines
2.3 KiB
C++

// Copyright 2020 The Tint Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "src/reader/wgsl/token.h"
#include <limits>
#include "gtest/gtest.h"
namespace tint {
namespace reader {
namespace wgsl {
namespace {
using TokenTest = testing::Test;
TEST_F(TokenTest, ReturnsStr) {
Token t(Token::Type::kStringLiteral, Source{}, "test string");
EXPECT_EQ(t.to_str(), "test string");
}
TEST_F(TokenTest, ReturnsF32) {
Token t1(Source{}, -2.345f);
EXPECT_EQ(t1.to_f32(), -2.345f);
Token t2(Source{}, 2.345f);
EXPECT_EQ(t2.to_f32(), 2.345f);
}
TEST_F(TokenTest, ReturnsI32) {
Token t1(Source{}, -2345);
EXPECT_EQ(t1.to_i32(), -2345);
Token t2(Source{}, 2345);
EXPECT_EQ(t2.to_i32(), 2345);
}
TEST_F(TokenTest, HandlesMaxI32) {
Token t1(Source{}, std::numeric_limits<int32_t>::max());
EXPECT_EQ(t1.to_i32(), std::numeric_limits<int32_t>::max());
}
TEST_F(TokenTest, HandlesMinI32) {
Token t1(Source{}, std::numeric_limits<int32_t>::min());
EXPECT_EQ(t1.to_i32(), std::numeric_limits<int32_t>::min());
}
TEST_F(TokenTest, ReturnsU32) {
Token t2(Source{}, 2345u);
EXPECT_EQ(t2.to_u32(), 2345u);
}
TEST_F(TokenTest, ReturnsMaxU32) {
Token t1(Source{}, std::numeric_limits<uint32_t>::max());
EXPECT_EQ(t1.to_u32(), std::numeric_limits<uint32_t>::max());
}
TEST_F(TokenTest, Source) {
Source::File file("", "");
Source src;
src.file = &file;
src.range.begin = Source::Location{3, 9};
src.range.end = Source::Location{4, 3};
Token t(Token::Type::kUintLiteral, src);
EXPECT_EQ(t.source().range.begin.line, 3u);
EXPECT_EQ(t.source().range.begin.column, 9u);
EXPECT_EQ(t.source().range.end.line, 4u);
EXPECT_EQ(t.source().range.end.column, 3u);
EXPECT_EQ(t.source().file, &file);
}
} // namespace
} // namespace wgsl
} // namespace reader
} // namespace tint