Fumitoshi Ukai | 76b609e | 2015-06-10 14:56:49 +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 | package kati |
Fumitoshi Ukai | 76b609e | 2015-06-10 14:56:49 +0900 | [diff] [blame] | 16 | |
Fumitoshi Ukai | 44ae8cf | 2015-06-24 16:44:15 +0900 | [diff] [blame] | 17 | import ( |
| 18 | "testing" |
| 19 | "time" |
| 20 | ) |
Fumitoshi Ukai | 76b609e | 2015-06-10 14:56:49 +0900 | [diff] [blame] | 21 | |
| 22 | func TestRot13(t *testing.T) { |
| 23 | for _, tc := range []struct { |
| 24 | in string |
| 25 | want string |
| 26 | }{ |
| 27 | { |
| 28 | in: "PRODUCT_PACKAGE_OVERLAYS", |
| 29 | want: "CEBQHPG_CNPXNTR_BIREYNLF", |
| 30 | }, |
| 31 | { |
| 32 | in: "product_name", |
| 33 | want: "cebqhpg_anzr", |
| 34 | }, |
| 35 | } { |
| 36 | buf := []byte(tc.in) |
| 37 | rot13(buf) |
| 38 | if got, want := string(buf), tc.want; got != want { |
| 39 | t.Errorf("rot13(%q) got=%q; want=%q", tc.in, got, want) |
| 40 | } |
| 41 | } |
| 42 | } |
Fumitoshi Ukai | 44ae8cf | 2015-06-24 16:44:15 +0900 | [diff] [blame] | 43 | |
| 44 | func TestShellDate(t *testing.T) { |
Fumitoshi Ukai | 744bb2b | 2015-06-25 00:10:52 +0900 | [diff] [blame] | 45 | ts := ShellDateTimestamp |
| 46 | ShellDateTimestamp = time.Now() |
Fumitoshi Ukai | 44ae8cf | 2015-06-24 16:44:15 +0900 | [diff] [blame] | 47 | defer func() { |
Fumitoshi Ukai | 744bb2b | 2015-06-25 00:10:52 +0900 | [diff] [blame] | 48 | ShellDateTimestamp = ts |
Fumitoshi Ukai | 44ae8cf | 2015-06-24 16:44:15 +0900 | [diff] [blame] | 49 | }() |
| 50 | for _, tc := range []struct { |
| 51 | sharg literal |
| 52 | format string |
| 53 | }{ |
| 54 | { |
| 55 | sharg: literal("date +%Y-%m-%d"), |
| 56 | format: "2006-01-02", |
| 57 | }, |
| 58 | { |
| 59 | sharg: literal("date +%Y%m%d.%H%M%S"), |
| 60 | format: "20060102.150405", |
| 61 | }, |
| 62 | { |
| 63 | sharg: literal(`date "+%d %b %Y %k:%M"`), |
| 64 | format: "02 Jan 2006 15:04", |
| 65 | }, |
| 66 | } { |
| 67 | var matched bool |
| 68 | for _, b := range shBuiltins { |
| 69 | if b.name != "shell-date" && b.name != "shell-date-quoted" { |
| 70 | continue |
| 71 | } |
Fumitoshi Ukai | 55c8fa9 | 2015-06-25 15:56:10 +0900 | [diff] [blame] | 72 | m, ok := matchExpr(expr{tc.sharg}, b.pattern) |
Fumitoshi Ukai | 44ae8cf | 2015-06-24 16:44:15 +0900 | [diff] [blame] | 73 | if !ok { |
| 74 | t.Logf("%s not match with %s", b.name, tc.sharg) |
| 75 | continue |
| 76 | } |
| 77 | f := &funcShell{ |
| 78 | fclosure: fclosure{ |
| 79 | args: []Value{ |
| 80 | literal("(shell"), |
| 81 | tc.sharg, |
| 82 | }, |
| 83 | }, |
| 84 | } |
| 85 | v := b.compact(f, m) |
| 86 | sd, ok := v.(*funcShellDate) |
| 87 | if !ok { |
| 88 | t.Errorf("%s: matched %s but not compacted", tc.sharg, b.name) |
| 89 | continue |
| 90 | } |
| 91 | if got, want := sd.format, tc.format; got != want { |
| 92 | t.Errorf("%s: format=%q, want=%q - %s", tc.sharg, got, want, b.name) |
| 93 | continue |
| 94 | } |
| 95 | matched = true |
| 96 | break |
| 97 | } |
| 98 | if !matched { |
| 99 | t.Errorf("%s: not matched", tc.sharg) |
| 100 | } |
| 101 | } |
| 102 | } |