This source file includes following definitions.
- TEST
- scanSequenceHelper1
- TEST
- scanSequenceHelper2
- TEST
- lowerCaseAlpha
- scanWithPredicate
- TEST
- scanWithInvPredicate
- TEST
- scanRuns
- TEST
- scanRunsToStrings
- TEST
- tailStringExtract
- TEST
- scanDigits1
- scanDigits2
- TEST
- scanFloatValue
- TEST
#include "config.h"
#include "core/html/track/vtt/VTTScanner.h"
#include "wtf/text/WTFString.h"
#include <gtest/gtest.h>
using WebCore::VTTScanner;
namespace {
TEST(VTTScanner, Constructor)
{
String data8("foo");
EXPECT_TRUE(data8.is8Bit());
VTTScanner scanner8(data8);
EXPECT_FALSE(scanner8.isAtEnd());
String data16(data8);
data16.ensure16Bit();
EXPECT_FALSE(data16.is8Bit());
VTTScanner scanner16(data16);
EXPECT_FALSE(scanner16.isAtEnd());
VTTScanner scannerEmpty(emptyString());
EXPECT_TRUE(scannerEmpty.isAtEnd());
}
void scanSequenceHelper1(const String& input)
{
VTTScanner scanner(input);
EXPECT_FALSE(scanner.isAtEnd());
EXPECT_TRUE(scanner.match('f'));
EXPECT_FALSE(scanner.match('o'));
EXPECT_TRUE(scanner.scan('f'));
EXPECT_FALSE(scanner.match('f'));
EXPECT_TRUE(scanner.match('o'));
EXPECT_FALSE(scanner.scan('e'));
EXPECT_TRUE(scanner.scan('o'));
EXPECT_TRUE(scanner.scan('e'));
EXPECT_FALSE(scanner.match('e'));
EXPECT_TRUE(scanner.isAtEnd());
}
#define TEST_WITH(TESTFUNC, DATA) do { \
String data8(DATA); \
EXPECT_TRUE(data8.is8Bit()); \
TESTFUNC(data8); \
\
String data16(data8); \
data16.ensure16Bit(); \
EXPECT_FALSE(data16.is8Bit()); \
TESTFUNC(data16); \
} while (false)
TEST(VTTScanner, BasicOperations1)
{
TEST_WITH(scanSequenceHelper1, "foe");
}
void scanSequenceHelper2(const String& input)
{
VTTScanner scanner(input);
EXPECT_FALSE(scanner.isAtEnd());
EXPECT_FALSE(scanner.scan("fe"));
EXPECT_TRUE(scanner.scan("fo"));
EXPECT_FALSE(scanner.isAtEnd());
EXPECT_FALSE(scanner.scan("ee"));
EXPECT_TRUE(scanner.scan('e'));
EXPECT_TRUE(scanner.isAtEnd());
}
TEST(VTTScanner, BasicOperations2)
{
TEST_WITH(scanSequenceHelper2, "foe");
}
bool lowerCaseAlpha(UChar c)
{
return c >= 'a' && c <= 'z';
}
void scanWithPredicate(const String& input)
{
VTTScanner scanner(input);
EXPECT_FALSE(scanner.isAtEnd());
VTTScanner::Run lcRun = scanner.collectWhile<lowerCaseAlpha>();
EXPECT_TRUE(scanner.match('b'));
scanner.skipWhile<lowerCaseAlpha>();
EXPECT_TRUE(scanner.match('A'));
EXPECT_TRUE(scanner.isAt(lcRun.end()));
EXPECT_TRUE(scanner.scan('A'));
lcRun = scanner.collectWhile<lowerCaseAlpha>();
EXPECT_FALSE(scanner.isAtEnd());
scanner.skipWhile<lowerCaseAlpha>();
EXPECT_TRUE(scanner.isAt(lcRun.end()));
EXPECT_TRUE(scanner.isAtEnd());
}
TEST(VTTScanner, PredicateScanning)
{
TEST_WITH(scanWithPredicate, "badAbing");
}
void scanWithInvPredicate(const String& input)
{
VTTScanner scanner(input);
EXPECT_FALSE(scanner.isAtEnd());
VTTScanner::Run ucRun = scanner.collectUntil<lowerCaseAlpha>();
EXPECT_TRUE(scanner.match('B'));
scanner.skipUntil<lowerCaseAlpha>();
EXPECT_TRUE(scanner.match('a'));
EXPECT_TRUE(scanner.isAt(ucRun.end()));
EXPECT_TRUE(scanner.scan('a'));
ucRun = scanner.collectUntil<lowerCaseAlpha>();
EXPECT_FALSE(scanner.isAtEnd());
scanner.skipUntil<lowerCaseAlpha>();
EXPECT_TRUE(scanner.isAt(ucRun.end()));
EXPECT_TRUE(scanner.isAtEnd());
}
TEST(VTTScanner, InversePredicateScanning)
{
TEST_WITH(scanWithInvPredicate, "BADaBING");
}
void scanRuns(const String& input)
{
String fooString("foo");
String barString("bar");
VTTScanner scanner(input);
EXPECT_FALSE(scanner.isAtEnd());
VTTScanner::Run word = scanner.collectWhile<lowerCaseAlpha>();
EXPECT_FALSE(scanner.scanRun(word, barString));
EXPECT_TRUE(scanner.scanRun(word, fooString));
EXPECT_TRUE(scanner.match(':'));
EXPECT_TRUE(scanner.scan(':'));
scanner.skipRun(scanner.collectWhile<lowerCaseAlpha>());
EXPECT_TRUE(scanner.match(':'));
EXPECT_TRUE(scanner.scan(':'));
word = scanner.collectWhile<lowerCaseAlpha>();
EXPECT_FALSE(scanner.scanRun(word, fooString));
EXPECT_TRUE(scanner.scanRun(word, barString));
EXPECT_TRUE(scanner.isAtEnd());
}
TEST(VTTScanner, RunScanning)
{
TEST_WITH(scanRuns, "foo:baz:bar");
}
void scanRunsToStrings(const String& input)
{
VTTScanner scanner(input);
EXPECT_FALSE(scanner.isAtEnd());
VTTScanner::Run word = scanner.collectWhile<lowerCaseAlpha>();
String fooString = scanner.extractString(word);
EXPECT_EQ(fooString, "foo");
EXPECT_TRUE(scanner.isAt(word.end()));
EXPECT_TRUE(scanner.match(':'));
EXPECT_TRUE(scanner.scan(':'));
word = scanner.collectWhile<lowerCaseAlpha>();
String barString = scanner.extractString(word);
EXPECT_EQ(barString, "bar");
EXPECT_TRUE(scanner.isAt(word.end()));
EXPECT_TRUE(scanner.isAtEnd());
}
TEST(VTTScanner, ExtractString)
{
TEST_WITH(scanRunsToStrings, "foo:bar");
}
void tailStringExtract(const String& input)
{
VTTScanner scanner(input);
EXPECT_TRUE(scanner.scan("foo"));
EXPECT_TRUE(scanner.scan(':'));
String barSuffix = scanner.restOfInputAsString();
EXPECT_EQ(barSuffix, "bar");
EXPECT_TRUE(scanner.isAtEnd());
}
TEST(VTTScanner, ExtractRestAsString)
{
TEST_WITH(tailStringExtract, "foo:bar");
}
void scanDigits1(const String& input)
{
VTTScanner scanner(input);
EXPECT_TRUE(scanner.scan("foo"));
int number;
EXPECT_EQ(scanner.scanDigits(number), 0u);
EXPECT_EQ(number, 0);
EXPECT_TRUE(scanner.scan(' '));
EXPECT_EQ(scanner.scanDigits(number), 3u);
EXPECT_TRUE(scanner.match(' '));
EXPECT_EQ(number, 123);
EXPECT_TRUE(scanner.scan(' '));
EXPECT_TRUE(scanner.scan("bar"));
EXPECT_TRUE(scanner.scan(' '));
EXPECT_EQ(scanner.scanDigits(number), 5u);
EXPECT_EQ(number, 45678);
EXPECT_TRUE(scanner.isAtEnd());
}
void scanDigits2(const String& input)
{
VTTScanner scanner(input);
int number;
EXPECT_EQ(scanner.scanDigits(number), 0u);
EXPECT_EQ(number, 0);
EXPECT_TRUE(scanner.scan('-'));
EXPECT_EQ(scanner.scanDigits(number), 3u);
EXPECT_EQ(number, 654);
EXPECT_TRUE(scanner.scan(' '));
EXPECT_EQ(scanner.scanDigits(number), 19u);
EXPECT_EQ(number, std::numeric_limits<int>::max());
EXPECT_TRUE(scanner.isAtEnd());
}
TEST(VTTScanner, ScanDigits)
{
TEST_WITH(scanDigits1, "foo 123 bar 45678");
TEST_WITH(scanDigits2, "-654 1000000000000000000");
}
void scanFloatValue(const String& input)
{
VTTScanner scanner(input);
float value;
EXPECT_TRUE(scanner.scanFloat(value));
EXPECT_EQ(value, 1.0f);
EXPECT_TRUE(scanner.scan(' '));
EXPECT_TRUE(scanner.scanFloat(value));
EXPECT_EQ(value, 1.0f);
EXPECT_TRUE(scanner.scan(' '));
EXPECT_TRUE(scanner.scanFloat(value));
EXPECT_EQ(value, 0.0f);
EXPECT_TRUE(scanner.scan(' '));
EXPECT_FALSE(scanner.scanFloat(value));
EXPECT_TRUE(scanner.match('.'));
EXPECT_TRUE(scanner.scan('.'));
EXPECT_TRUE(scanner.scan(' '));
EXPECT_TRUE(scanner.scanFloat(value));
EXPECT_EQ(value, 1.0f);
EXPECT_TRUE(scanner.scan(' '));
EXPECT_TRUE(scanner.scanFloat(value));
EXPECT_EQ(value, 1.0f);
EXPECT_TRUE(scanner.isAtEnd());
}
TEST(VTTScanner, ScanFloat)
{
TEST_WITH(scanFloatValue, "1. 1.0 .0 . 1.0000 01.000");
}
#undef TEST_WITH
}