blob: ce9ba54858588fa9bdd357ecd62e435ea0dbc074 [file] [log] [blame]
Colin Cross7e0eaf12017-05-05 16:16:24 -07001// 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
15package android
16
17import (
18 "reflect"
19 "testing"
20)
21
22type printfIntoPropertyTestCase struct {
23 in string
24 val interface{}
25 out string
26 err bool
27}
28
29var printfIntoPropertyTestCases = []printfIntoPropertyTestCase{
30 {
31 in: "%d",
32 val: 0,
33 out: "0",
34 },
35 {
36 in: "%d",
37 val: 1,
38 out: "1",
39 },
40 {
41 in: "%d",
42 val: 2,
43 out: "2",
44 },
45 {
46 in: "%d",
47 val: false,
48 out: "0",
49 },
50 {
51 in: "%d",
52 val: true,
53 out: "1",
54 },
55 {
56 in: "%d",
57 val: -1,
58 out: "-1",
59 },
60
61 {
62 in: "-DA=%d",
63 val: 1,
64 out: "-DA=1",
65 },
66 {
67 in: "-DA=%du",
68 val: 1,
69 out: "-DA=1u",
70 },
71 {
72 in: "-DA=%s",
73 val: "abc",
74 out: "-DA=abc",
75 },
76 {
77 in: `-DA="%s"`,
78 val: "abc",
79 out: `-DA="abc"`,
80 },
81
82 {
83 in: "%%",
84 err: true,
85 },
86 {
87 in: "%d%s",
88 err: true,
89 },
90 {
91 in: "%d,%s",
92 err: true,
93 },
94 {
95 in: "%d",
96 val: "",
97 err: true,
98 },
99 {
100 in: "%d",
101 val: 1.5,
102 err: true,
103 },
104 {
105 in: "%f",
106 val: 1.5,
107 err: true,
108 },
109}
110
111func TestPrintfIntoProperty(t *testing.T) {
112 for _, testCase := range printfIntoPropertyTestCases {
113 s := testCase.in
114 v := reflect.ValueOf(&s).Elem()
115 err := printfIntoProperty(v, testCase.val)
116 if err != nil && !testCase.err {
117 t.Errorf("unexpected error %s", err)
118 } else if err == nil && testCase.err {
119 t.Errorf("expected error")
120 } else if err == nil && v.String() != testCase.out {
121 t.Errorf("expected %q got %q", testCase.out, v.String())
122 }
123 }
124}