blob: 39c2c64a72e5cbd37b933fe4f26317c03afa001e [file] [log] [blame]
Fumitoshi Ukai76b609e2015-06-10 14:56:49 +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 +090015package kati
Fumitoshi Ukai76b609e2015-06-10 14:56:49 +090016
Fumitoshi Ukai44ae8cf2015-06-24 16:44:15 +090017import (
18 "testing"
19 "time"
20)
Fumitoshi Ukai76b609e2015-06-10 14:56:49 +090021
22func 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 Ukai44ae8cf2015-06-24 16:44:15 +090043
44func TestShellDate(t *testing.T) {
Fumitoshi Ukai744bb2b2015-06-25 00:10:52 +090045 ts := ShellDateTimestamp
46 ShellDateTimestamp = time.Now()
Fumitoshi Ukai44ae8cf2015-06-24 16:44:15 +090047 defer func() {
Fumitoshi Ukai744bb2b2015-06-25 00:10:52 +090048 ShellDateTimestamp = ts
Fumitoshi Ukai44ae8cf2015-06-24 16:44:15 +090049 }()
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 Ukai55c8fa92015-06-25 15:56:10 +090072 m, ok := matchExpr(expr{tc.sharg}, b.pattern)
Fumitoshi Ukai44ae8cf2015-06-24 16:44:15 +090073 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}