Shinichiro Hamaji | 1d545aa | 2015-06-23 15:29:13 +0900 | [diff] [blame] | 1 | // 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 Ukai | 744bb2b | 2015-06-25 00:10:52 +0900 | [diff] [blame] | 15 | // +build ignore |
| 16 | |
Shinichiro Hamaji | 63e68fc | 2015-06-16 17:36:53 +0900 | [diff] [blame] | 17 | #include "strutil.h" |
| 18 | |
| 19 | #include <assert.h> |
| 20 | |
| 21 | #include <string> |
| 22 | #include <vector> |
| 23 | |
| 24 | #include "string_piece.h" |
| 25 | |
| 26 | using namespace std; |
| 27 | |
| 28 | void 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 Hamaji | f91d682 | 2015-06-16 17:51:23 +0900 | [diff] [blame] | 39 | void TestHasPrefix() { |
| 40 | assert(HasPrefix("foo", "foo")); |
| 41 | assert(HasPrefix("foo", "fo")); |
| 42 | assert(HasPrefix("foo", "")); |
| 43 | assert(!HasPrefix("foo", "fooo")); |
| 44 | } |
| 45 | |
| 46 | void TestHasSuffix() { |
| 47 | assert(HasSuffix("bar", "bar")); |
| 48 | assert(HasSuffix("bar", "ar")); |
| 49 | assert(HasSuffix("bar", "")); |
| 50 | assert(!HasSuffix("bar", "bbar")); |
| 51 | } |
| 52 | |
| 53 | string SubstPattern(StringPiece str, StringPiece pat, StringPiece subst) { |
| 54 | string r; |
Shinichiro Hamaji | a6a17a4 | 2015-06-18 20:11:19 +0900 | [diff] [blame] | 55 | Pattern(pat).AppendSubst(str, subst, &r); |
Shinichiro Hamaji | f91d682 | 2015-06-16 17:51:23 +0900 | [diff] [blame] | 56 | return r; |
| 57 | } |
| 58 | |
| 59 | void 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 Hamaji | 2b674c4 | 2015-06-24 17:49:35 +0900 | [diff] [blame^] | 70 | void TestNoLineBreak() { |
| 71 | assert(NoLineBreak("a\nb") == "a\\nb"); |
| 72 | assert(NoLineBreak("a\nb\nc") == "a\\nb\\nc"); |
| 73 | } |
| 74 | |
Shinichiro Hamaji | 63e68fc | 2015-06-16 17:36:53 +0900 | [diff] [blame] | 75 | int main() { |
| 76 | TestWordScanner(); |
Shinichiro Hamaji | f91d682 | 2015-06-16 17:51:23 +0900 | [diff] [blame] | 77 | TestHasPrefix(); |
| 78 | TestHasSuffix(); |
| 79 | TestSubstPattern(); |
Shinichiro Hamaji | 2b674c4 | 2015-06-24 17:49:35 +0900 | [diff] [blame^] | 80 | TestNoLineBreak(); |
Shinichiro Hamaji | 63e68fc | 2015-06-16 17:36:53 +0900 | [diff] [blame] | 81 | } |