blob: 281041205ef097ec18113091d52131ebc7a91e36 [file] [log] [blame]
Shinichiro Hamaji1d545aa2015-06-23 15:29:13 +09001// Copyright 2015 Google Inc. All rights reserved
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
Fumitoshi Ukai744bb2b2015-06-25 00:10:52 +090015// +build ignore
16
Shinichiro Hamaji63e68fc2015-06-16 17:36:53 +090017#include "strutil.h"
18
19#include <assert.h>
20
21#include <string>
22#include <vector>
23
24#include "string_piece.h"
25
26using namespace std;
27
28void TestWordScanner() {
29 vector<StringPiece> ss;
30 for (StringPiece tok : WordScanner("foo bar baz")) {
31 ss.push_back(tok);
32 }
33 assert(ss.size() == 3LU);
34 assert(ss[0] == "foo");
35 assert(ss[1] == "bar");
36 assert(ss[2] == "baz");
37}
38
Shinichiro Hamajif91d6822015-06-16 17:51:23 +090039void TestHasPrefix() {
40 assert(HasPrefix("foo", "foo"));
41 assert(HasPrefix("foo", "fo"));
42 assert(HasPrefix("foo", ""));
43 assert(!HasPrefix("foo", "fooo"));
44}
45
46void TestHasSuffix() {
47 assert(HasSuffix("bar", "bar"));
48 assert(HasSuffix("bar", "ar"));
49 assert(HasSuffix("bar", ""));
50 assert(!HasSuffix("bar", "bbar"));
51}
52
53string SubstPattern(StringPiece str, StringPiece pat, StringPiece subst) {
54 string r;
Shinichiro Hamajia6a17a42015-06-18 20:11:19 +090055 Pattern(pat).AppendSubst(str, subst, &r);
Shinichiro Hamajif91d6822015-06-16 17:51:23 +090056 return r;
57}
58
59void TestSubstPattern() {
60 assert(SubstPattern("x.c", "%.c", "%.o") == "x.o");
61 assert(SubstPattern("c.x", "c.%", "o.%") == "o.x");
62 assert(SubstPattern("x.c.c", "%.c", "%.o") == "x.c.o");
63 assert(SubstPattern("x.x y.c", "%.c", "%.o") == "x.x y.o");
64 assert(SubstPattern("x.%.c", "%.%.c", "OK") == "OK");
65 assert(SubstPattern("x.c", "x.c", "OK") == "OK");
66 assert(SubstPattern("x.c.c", "x.c", "XX") == "x.c.c");
67 assert(SubstPattern("x.x.c", "x.c", "XX") == "x.x.c");
68}
69
Shinichiro Hamaji2b674c42015-06-24 17:49:35 +090070void TestNoLineBreak() {
71 assert(NoLineBreak("a\nb") == "a\\nb");
72 assert(NoLineBreak("a\nb\nc") == "a\\nb\\nc");
73}
74
Shinichiro Hamaji63e68fc2015-06-16 17:36:53 +090075int main() {
76 TestWordScanner();
Shinichiro Hamajif91d6822015-06-16 17:51:23 +090077 TestHasPrefix();
78 TestHasSuffix();
79 TestSubstPattern();
Shinichiro Hamaji2b674c42015-06-24 17:49:35 +090080 TestNoLineBreak();
Shinichiro Hamaji63e68fc2015-06-16 17:36:53 +090081}