2015-01-23 23:15:10 +01:00
|
|
|
// Copyright 2014 Google Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2020-01-02 18:37:49 +01:00
|
|
|
package proptools
|
2014-05-28 01:34:41 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
2015-06-22 22:38:45 +02:00
|
|
|
"text/scanner"
|
|
|
|
|
|
|
|
"github.com/google/blueprint/parser"
|
2014-05-28 01:34:41 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var validUnpackTestCases = []struct {
|
|
|
|
input string
|
2016-06-07 21:28:16 +02:00
|
|
|
output []interface{}
|
2016-08-06 02:19:36 +02:00
|
|
|
empty []interface{}
|
2015-06-22 22:38:45 +02:00
|
|
|
errs []error
|
2014-05-28 01:34:41 +02:00
|
|
|
}{
|
2016-08-06 02:19:36 +02:00
|
|
|
{
|
|
|
|
input: `
|
|
|
|
m {
|
2020-01-02 18:37:49 +01:00
|
|
|
s: "abc",
|
2016-08-06 02:19:36 +02:00
|
|
|
blank: "",
|
|
|
|
}
|
2015-10-30 23:53:55 +01:00
|
|
|
`,
|
2016-08-06 02:19:36 +02:00
|
|
|
output: []interface{}{
|
2020-01-28 01:14:31 +01:00
|
|
|
&struct {
|
2020-01-02 18:37:49 +01:00
|
|
|
S *string
|
2016-06-07 21:28:16 +02:00
|
|
|
Blank *string
|
|
|
|
Unset *string
|
|
|
|
}{
|
2020-01-02 18:37:49 +01:00
|
|
|
S: StringPtr("abc"),
|
|
|
|
Blank: StringPtr(""),
|
2016-06-07 21:28:16 +02:00
|
|
|
Unset: nil,
|
|
|
|
},
|
2015-10-30 23:53:55 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2016-08-06 02:19:36 +02:00
|
|
|
{
|
|
|
|
input: `
|
|
|
|
m {
|
2020-01-02 18:37:49 +01:00
|
|
|
s: "abc",
|
2016-08-06 02:19:36 +02:00
|
|
|
}
|
2014-05-28 01:34:41 +02:00
|
|
|
`,
|
2016-08-06 02:19:36 +02:00
|
|
|
output: []interface{}{
|
2020-01-28 01:14:31 +01:00
|
|
|
&struct {
|
2020-01-02 18:37:49 +01:00
|
|
|
S string
|
2016-06-07 21:28:16 +02:00
|
|
|
}{
|
2020-01-02 18:37:49 +01:00
|
|
|
S: "abc",
|
2016-06-07 21:28:16 +02:00
|
|
|
},
|
2014-05-28 01:34:41 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2016-08-06 02:19:36 +02:00
|
|
|
{
|
|
|
|
input: `
|
|
|
|
m {
|
|
|
|
isGood: true,
|
|
|
|
}
|
2014-05-28 01:34:41 +02:00
|
|
|
`,
|
2016-08-06 02:19:36 +02:00
|
|
|
output: []interface{}{
|
2020-01-28 01:14:31 +01:00
|
|
|
&struct {
|
2016-06-07 21:28:16 +02:00
|
|
|
IsGood bool
|
|
|
|
}{
|
|
|
|
IsGood: true,
|
|
|
|
},
|
2014-05-28 01:34:41 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2016-08-06 02:19:36 +02:00
|
|
|
{
|
|
|
|
input: `
|
|
|
|
m {
|
|
|
|
isGood: true,
|
|
|
|
isBad: false,
|
|
|
|
}
|
2015-10-30 23:53:55 +01:00
|
|
|
`,
|
2016-08-06 02:19:36 +02:00
|
|
|
output: []interface{}{
|
2020-01-28 01:14:31 +01:00
|
|
|
&struct {
|
2016-06-07 21:28:16 +02:00
|
|
|
IsGood *bool
|
|
|
|
IsBad *bool
|
|
|
|
IsUgly *bool
|
|
|
|
}{
|
2020-01-02 18:37:49 +01:00
|
|
|
IsGood: BoolPtr(true),
|
|
|
|
IsBad: BoolPtr(false),
|
2016-06-07 21:28:16 +02:00
|
|
|
IsUgly: nil,
|
|
|
|
},
|
2015-10-30 23:53:55 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2016-08-06 02:19:36 +02:00
|
|
|
{
|
|
|
|
input: `
|
|
|
|
m {
|
|
|
|
stuff: ["asdf", "jkl;", "qwert",
|
|
|
|
"uiop", "bnm,"],
|
|
|
|
empty: []
|
|
|
|
}
|
2014-05-28 01:34:41 +02:00
|
|
|
`,
|
2016-08-06 02:19:36 +02:00
|
|
|
output: []interface{}{
|
2020-01-28 01:14:31 +01:00
|
|
|
&struct {
|
2018-10-03 06:57:47 +02:00
|
|
|
Stuff []string
|
|
|
|
Empty []string
|
|
|
|
Nil []string
|
|
|
|
NonString []struct{ S string } `blueprint:"mutated"`
|
2016-06-07 21:28:16 +02:00
|
|
|
}{
|
2018-10-03 06:57:47 +02:00
|
|
|
Stuff: []string{"asdf", "jkl;", "qwert", "uiop", "bnm,"},
|
|
|
|
Empty: []string{},
|
|
|
|
Nil: nil,
|
|
|
|
NonString: nil,
|
2016-06-07 21:28:16 +02:00
|
|
|
},
|
2014-05-28 01:34:41 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2016-08-06 02:19:36 +02:00
|
|
|
{
|
|
|
|
input: `
|
|
|
|
m {
|
|
|
|
nested: {
|
2020-01-02 18:37:49 +01:00
|
|
|
s: "abc",
|
2016-08-06 02:19:36 +02:00
|
|
|
}
|
2014-09-30 20:38:25 +02:00
|
|
|
}
|
2014-05-28 01:34:41 +02:00
|
|
|
`,
|
2016-08-06 02:19:36 +02:00
|
|
|
output: []interface{}{
|
2020-01-28 01:14:31 +01:00
|
|
|
&struct {
|
2016-06-07 21:28:16 +02:00
|
|
|
Nested struct {
|
2020-01-02 18:37:49 +01:00
|
|
|
S string
|
2016-06-07 21:28:16 +02:00
|
|
|
}
|
|
|
|
}{
|
2020-01-02 18:37:49 +01:00
|
|
|
Nested: struct{ S string }{
|
|
|
|
S: "abc",
|
2016-06-07 21:28:16 +02:00
|
|
|
},
|
2014-05-28 01:34:41 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2016-08-06 02:19:36 +02:00
|
|
|
{
|
|
|
|
input: `
|
|
|
|
m {
|
|
|
|
nested: {
|
2020-01-02 18:37:49 +01:00
|
|
|
s: "def",
|
2016-08-06 02:19:36 +02:00
|
|
|
}
|
2014-09-30 20:38:25 +02:00
|
|
|
}
|
|
|
|
`,
|
2016-08-06 02:19:36 +02:00
|
|
|
output: []interface{}{
|
2020-01-28 01:14:31 +01:00
|
|
|
&struct {
|
2016-06-07 21:28:16 +02:00
|
|
|
Nested interface{}
|
|
|
|
}{
|
2020-01-02 18:37:49 +01:00
|
|
|
Nested: &struct{ S string }{
|
|
|
|
S: "def",
|
2016-06-07 21:28:16 +02:00
|
|
|
},
|
2014-09-30 20:38:25 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2016-08-06 02:19:36 +02:00
|
|
|
{
|
|
|
|
input: `
|
|
|
|
m {
|
|
|
|
nested: {
|
|
|
|
foo: "abc",
|
|
|
|
},
|
|
|
|
bar: false,
|
|
|
|
baz: ["def", "ghi"],
|
|
|
|
}
|
2014-05-28 01:34:41 +02:00
|
|
|
`,
|
2016-08-06 02:19:36 +02:00
|
|
|
output: []interface{}{
|
2020-01-28 01:14:31 +01:00
|
|
|
&struct {
|
2016-06-07 21:28:16 +02:00
|
|
|
Nested struct {
|
|
|
|
Foo string
|
|
|
|
}
|
|
|
|
Bar bool
|
|
|
|
Baz []string
|
|
|
|
}{
|
|
|
|
Nested: struct{ Foo string }{
|
|
|
|
Foo: "abc",
|
|
|
|
},
|
|
|
|
Bar: false,
|
|
|
|
Baz: []string{"def", "ghi"},
|
2014-05-28 01:34:41 +02:00
|
|
|
},
|
|
|
|
},
|
2015-06-22 22:38:45 +02:00
|
|
|
},
|
|
|
|
|
2016-08-06 02:19:36 +02:00
|
|
|
{
|
|
|
|
input: `
|
|
|
|
m {
|
|
|
|
nested: {
|
|
|
|
foo: "abc",
|
|
|
|
},
|
|
|
|
bar: false,
|
|
|
|
baz: ["def", "ghi"],
|
|
|
|
}
|
2015-06-22 22:38:45 +02:00
|
|
|
`,
|
2016-08-06 02:19:36 +02:00
|
|
|
output: []interface{}{
|
2020-01-28 01:14:31 +01:00
|
|
|
&struct {
|
2016-06-07 21:28:16 +02:00
|
|
|
Nested struct {
|
|
|
|
Foo string `allowNested:"true"`
|
|
|
|
} `blueprint:"filter(allowNested:\"true\")"`
|
|
|
|
Bar bool
|
|
|
|
Baz []string
|
2015-06-22 22:38:45 +02:00
|
|
|
}{
|
2016-06-07 21:28:16 +02:00
|
|
|
Nested: struct {
|
|
|
|
Foo string `allowNested:"true"`
|
|
|
|
}{
|
|
|
|
Foo: "abc",
|
|
|
|
},
|
|
|
|
Bar: false,
|
|
|
|
Baz: []string{"def", "ghi"},
|
2015-06-22 22:38:45 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2015-11-21 02:03:25 +01:00
|
|
|
// Anonymous struct
|
2016-08-06 02:19:36 +02:00
|
|
|
{
|
|
|
|
input: `
|
|
|
|
m {
|
2020-01-02 18:37:49 +01:00
|
|
|
s: "abc",
|
2016-08-06 02:19:36 +02:00
|
|
|
nested: {
|
2020-01-02 18:37:49 +01:00
|
|
|
s: "def",
|
2016-08-06 02:19:36 +02:00
|
|
|
},
|
|
|
|
}
|
2015-11-21 02:03:25 +01:00
|
|
|
`,
|
2016-08-06 02:19:36 +02:00
|
|
|
output: []interface{}{
|
2020-01-28 01:14:31 +01:00
|
|
|
&struct {
|
2015-11-21 02:03:25 +01:00
|
|
|
EmbeddedStruct
|
2016-06-07 21:28:16 +02:00
|
|
|
Nested struct {
|
|
|
|
EmbeddedStruct
|
|
|
|
}
|
2015-11-21 02:03:25 +01:00
|
|
|
}{
|
|
|
|
EmbeddedStruct: EmbeddedStruct{
|
2020-01-02 18:37:49 +01:00
|
|
|
S: "abc",
|
2016-06-07 21:28:16 +02:00
|
|
|
},
|
|
|
|
Nested: struct {
|
|
|
|
EmbeddedStruct
|
|
|
|
}{
|
|
|
|
EmbeddedStruct: EmbeddedStruct{
|
2020-01-02 18:37:49 +01:00
|
|
|
S: "def",
|
2016-06-07 21:28:16 +02:00
|
|
|
},
|
2015-11-21 02:03:25 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
// Anonymous interface
|
2016-08-06 02:19:36 +02:00
|
|
|
{
|
|
|
|
input: `
|
|
|
|
m {
|
2020-01-02 18:37:49 +01:00
|
|
|
s: "abc",
|
2016-08-06 02:19:36 +02:00
|
|
|
nested: {
|
2020-01-02 18:37:49 +01:00
|
|
|
s: "def",
|
2016-08-06 02:19:36 +02:00
|
|
|
},
|
|
|
|
}
|
2015-11-21 02:03:25 +01:00
|
|
|
`,
|
2016-08-06 02:19:36 +02:00
|
|
|
output: []interface{}{
|
2020-01-28 01:14:31 +01:00
|
|
|
&struct {
|
2015-11-21 02:03:25 +01:00
|
|
|
EmbeddedInterface
|
2016-06-07 21:28:16 +02:00
|
|
|
Nested struct {
|
|
|
|
EmbeddedInterface
|
|
|
|
}
|
2015-11-21 02:03:25 +01:00
|
|
|
}{
|
2020-01-02 18:37:49 +01:00
|
|
|
EmbeddedInterface: &struct{ S string }{
|
|
|
|
S: "abc",
|
2016-06-07 21:28:16 +02:00
|
|
|
},
|
|
|
|
Nested: struct {
|
|
|
|
EmbeddedInterface
|
|
|
|
}{
|
2020-01-02 18:37:49 +01:00
|
|
|
EmbeddedInterface: &struct{ S string }{
|
|
|
|
S: "def",
|
2016-06-07 21:28:16 +02:00
|
|
|
},
|
2015-11-21 02:03:25 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
// Anonymous struct with name collision
|
2016-08-06 02:19:36 +02:00
|
|
|
{
|
|
|
|
input: `
|
|
|
|
m {
|
2020-01-02 18:37:49 +01:00
|
|
|
s: "abc",
|
2016-08-06 02:19:36 +02:00
|
|
|
nested: {
|
2020-01-02 18:37:49 +01:00
|
|
|
s: "def",
|
2016-08-06 02:19:36 +02:00
|
|
|
},
|
|
|
|
}
|
2015-11-21 02:03:25 +01:00
|
|
|
`,
|
2016-08-06 02:19:36 +02:00
|
|
|
output: []interface{}{
|
2020-01-28 01:14:31 +01:00
|
|
|
&struct {
|
2020-01-02 18:37:49 +01:00
|
|
|
S string
|
2015-11-21 02:03:25 +01:00
|
|
|
EmbeddedStruct
|
2016-06-07 21:28:16 +02:00
|
|
|
Nested struct {
|
2020-01-02 18:37:49 +01:00
|
|
|
S string
|
2016-06-07 21:28:16 +02:00
|
|
|
EmbeddedStruct
|
|
|
|
}
|
2015-11-21 02:03:25 +01:00
|
|
|
}{
|
2020-01-02 18:37:49 +01:00
|
|
|
S: "abc",
|
2015-11-21 02:03:25 +01:00
|
|
|
EmbeddedStruct: EmbeddedStruct{
|
2020-01-02 18:37:49 +01:00
|
|
|
S: "abc",
|
2016-06-07 21:28:16 +02:00
|
|
|
},
|
|
|
|
Nested: struct {
|
2020-01-02 18:37:49 +01:00
|
|
|
S string
|
2016-06-07 21:28:16 +02:00
|
|
|
EmbeddedStruct
|
|
|
|
}{
|
2020-01-02 18:37:49 +01:00
|
|
|
S: "def",
|
2016-06-07 21:28:16 +02:00
|
|
|
EmbeddedStruct: EmbeddedStruct{
|
2020-01-02 18:37:49 +01:00
|
|
|
S: "def",
|
2016-06-07 21:28:16 +02:00
|
|
|
},
|
2015-11-21 02:03:25 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
// Anonymous interface with name collision
|
2016-08-06 02:19:36 +02:00
|
|
|
{
|
|
|
|
input: `
|
|
|
|
m {
|
2020-01-02 18:37:49 +01:00
|
|
|
s: "abc",
|
2016-08-06 02:19:36 +02:00
|
|
|
nested: {
|
2020-01-02 18:37:49 +01:00
|
|
|
s: "def",
|
2016-08-06 02:19:36 +02:00
|
|
|
},
|
|
|
|
}
|
2015-11-21 02:03:25 +01:00
|
|
|
`,
|
2016-08-06 02:19:36 +02:00
|
|
|
output: []interface{}{
|
2020-01-28 01:14:31 +01:00
|
|
|
&struct {
|
2020-01-02 18:37:49 +01:00
|
|
|
S string
|
2015-11-21 02:03:25 +01:00
|
|
|
EmbeddedInterface
|
2016-06-07 21:28:16 +02:00
|
|
|
Nested struct {
|
2020-01-02 18:37:49 +01:00
|
|
|
S string
|
2016-06-07 21:28:16 +02:00
|
|
|
EmbeddedInterface
|
|
|
|
}
|
2015-11-21 02:03:25 +01:00
|
|
|
}{
|
2020-01-02 18:37:49 +01:00
|
|
|
S: "abc",
|
|
|
|
EmbeddedInterface: &struct{ S string }{
|
|
|
|
S: "abc",
|
2016-06-07 21:28:16 +02:00
|
|
|
},
|
|
|
|
Nested: struct {
|
2020-01-02 18:37:49 +01:00
|
|
|
S string
|
2016-06-07 21:28:16 +02:00
|
|
|
EmbeddedInterface
|
|
|
|
}{
|
2020-01-02 18:37:49 +01:00
|
|
|
S: "def",
|
|
|
|
EmbeddedInterface: &struct{ S string }{
|
|
|
|
S: "def",
|
2016-06-07 21:28:16 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
// Variables
|
2016-08-06 02:19:36 +02:00
|
|
|
{
|
|
|
|
input: `
|
|
|
|
list = ["abc"]
|
|
|
|
string = "def"
|
|
|
|
list_with_variable = [string]
|
|
|
|
m {
|
2020-01-02 18:37:49 +01:00
|
|
|
s: string,
|
2016-08-06 02:19:36 +02:00
|
|
|
list: list,
|
|
|
|
list2: list_with_variable,
|
|
|
|
}
|
2016-06-07 21:28:16 +02:00
|
|
|
`,
|
2016-08-06 02:19:36 +02:00
|
|
|
output: []interface{}{
|
2020-01-28 01:14:31 +01:00
|
|
|
&struct {
|
2020-01-02 18:37:49 +01:00
|
|
|
S string
|
2016-06-07 21:28:16 +02:00
|
|
|
List []string
|
|
|
|
List2 []string
|
|
|
|
}{
|
2020-01-02 18:37:49 +01:00
|
|
|
S: "def",
|
2016-06-07 21:28:16 +02:00
|
|
|
List: []string{"abc"},
|
|
|
|
List2: []string{"def"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
// Multiple property structs
|
2016-08-06 02:19:36 +02:00
|
|
|
{
|
|
|
|
input: `
|
|
|
|
m {
|
|
|
|
nested: {
|
2020-01-02 18:37:49 +01:00
|
|
|
s: "abc",
|
2016-08-06 02:19:36 +02:00
|
|
|
}
|
2016-06-07 21:28:16 +02:00
|
|
|
}
|
|
|
|
`,
|
2016-08-06 02:19:36 +02:00
|
|
|
output: []interface{}{
|
2020-01-28 01:14:31 +01:00
|
|
|
&struct {
|
2016-06-07 21:28:16 +02:00
|
|
|
Nested struct {
|
2020-01-02 18:37:49 +01:00
|
|
|
S string
|
2016-06-07 21:28:16 +02:00
|
|
|
}
|
|
|
|
}{
|
2020-01-02 18:37:49 +01:00
|
|
|
Nested: struct{ S string }{
|
|
|
|
S: "abc",
|
2016-06-07 21:28:16 +02:00
|
|
|
},
|
|
|
|
},
|
2020-01-28 01:14:31 +01:00
|
|
|
&struct {
|
2016-06-07 21:28:16 +02:00
|
|
|
Nested struct {
|
2020-01-02 18:37:49 +01:00
|
|
|
S string
|
2016-06-07 21:28:16 +02:00
|
|
|
}
|
|
|
|
}{
|
2020-01-02 18:37:49 +01:00
|
|
|
Nested: struct{ S string }{
|
|
|
|
S: "abc",
|
2015-11-21 02:03:25 +01:00
|
|
|
},
|
|
|
|
},
|
2020-01-28 01:14:31 +01:00
|
|
|
&struct {
|
2016-06-07 21:28:16 +02:00
|
|
|
}{},
|
2015-11-21 02:03:25 +01:00
|
|
|
},
|
2016-08-06 02:19:36 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
// Nil pointer to struct
|
|
|
|
{
|
|
|
|
input: `
|
|
|
|
m {
|
|
|
|
nested: {
|
2020-01-02 18:37:49 +01:00
|
|
|
s: "abc",
|
2016-08-06 02:19:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
output: []interface{}{
|
2020-01-28 01:14:31 +01:00
|
|
|
&struct {
|
2016-08-06 02:19:36 +02:00
|
|
|
Nested *struct {
|
2020-01-02 18:37:49 +01:00
|
|
|
S string
|
2016-08-06 02:19:36 +02:00
|
|
|
}
|
|
|
|
}{
|
2020-01-02 18:37:49 +01:00
|
|
|
Nested: &struct{ S string }{
|
|
|
|
S: "abc",
|
2016-08-06 02:19:36 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
empty: []interface{}{
|
|
|
|
&struct {
|
|
|
|
Nested *struct {
|
2020-01-02 18:37:49 +01:00
|
|
|
S string
|
2016-08-06 02:19:36 +02:00
|
|
|
}
|
|
|
|
}{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
// Interface containing nil pointer to struct
|
|
|
|
{
|
|
|
|
input: `
|
|
|
|
m {
|
|
|
|
nested: {
|
2020-01-02 18:37:49 +01:00
|
|
|
s: "abc",
|
2016-08-06 02:19:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
output: []interface{}{
|
2020-01-28 01:14:31 +01:00
|
|
|
&struct {
|
2016-08-06 02:19:36 +02:00
|
|
|
Nested interface{}
|
|
|
|
}{
|
|
|
|
Nested: &EmbeddedStruct{
|
2020-01-02 18:37:49 +01:00
|
|
|
S: "abc",
|
2016-08-06 02:19:36 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
empty: []interface{}{
|
|
|
|
&struct {
|
|
|
|
Nested interface{}
|
|
|
|
}{
|
|
|
|
Nested: (*EmbeddedStruct)(nil),
|
|
|
|
},
|
|
|
|
},
|
2015-11-21 02:03:25 +01:00
|
|
|
},
|
2017-07-29 02:51:37 +02:00
|
|
|
|
|
|
|
// Factory set properties
|
|
|
|
{
|
|
|
|
input: `
|
|
|
|
m {
|
|
|
|
string: "abc",
|
|
|
|
string_ptr: "abc",
|
|
|
|
bool: false,
|
|
|
|
bool_ptr: false,
|
|
|
|
list: ["a", "b", "c"],
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
output: []interface{}{
|
2020-01-28 01:14:31 +01:00
|
|
|
&struct {
|
2017-07-29 02:51:37 +02:00
|
|
|
String string
|
|
|
|
String_ptr *string
|
|
|
|
Bool bool
|
|
|
|
Bool_ptr *bool
|
|
|
|
List []string
|
|
|
|
}{
|
|
|
|
String: "012abc",
|
2020-01-02 18:37:49 +01:00
|
|
|
String_ptr: StringPtr("abc"),
|
2017-07-29 02:51:37 +02:00
|
|
|
Bool: true,
|
2020-01-02 18:37:49 +01:00
|
|
|
Bool_ptr: BoolPtr(false),
|
2017-07-29 02:51:37 +02:00
|
|
|
List: []string{"0", "1", "2", "a", "b", "c"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
empty: []interface{}{
|
|
|
|
&struct {
|
|
|
|
String string
|
|
|
|
String_ptr *string
|
|
|
|
Bool bool
|
|
|
|
Bool_ptr *bool
|
|
|
|
List []string
|
|
|
|
}{
|
|
|
|
String: "012",
|
2020-01-02 18:37:49 +01:00
|
|
|
String_ptr: StringPtr("012"),
|
2017-07-29 02:51:37 +02:00
|
|
|
Bool: true,
|
2020-01-02 18:37:49 +01:00
|
|
|
Bool_ptr: BoolPtr(true),
|
2017-07-29 02:51:37 +02:00
|
|
|
List: []string{"0", "1", "2"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2014-05-28 01:34:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestUnpackProperties(t *testing.T) {
|
|
|
|
for _, testCase := range validUnpackTestCases {
|
|
|
|
r := bytes.NewBufferString(testCase.input)
|
2016-06-07 21:28:16 +02:00
|
|
|
file, errs := parser.ParseAndEval("", r, parser.NewScope(nil))
|
2014-05-28 01:34:41 +02:00
|
|
|
if len(errs) != 0 {
|
|
|
|
t.Errorf("test case: %s", testCase.input)
|
|
|
|
t.Errorf("unexpected parse errors:")
|
|
|
|
for _, err := range errs {
|
|
|
|
t.Errorf(" %s", err)
|
|
|
|
}
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
|
2016-06-07 21:28:16 +02:00
|
|
|
for _, def := range file.Defs {
|
|
|
|
module, ok := def.(*parser.Module)
|
|
|
|
if !ok {
|
|
|
|
continue
|
2014-05-28 01:34:41 +02:00
|
|
|
}
|
|
|
|
|
2016-08-06 02:19:36 +02:00
|
|
|
var output []interface{}
|
|
|
|
if len(testCase.empty) > 0 {
|
|
|
|
output = testCase.empty
|
|
|
|
} else {
|
|
|
|
for _, p := range testCase.output {
|
2020-01-02 18:37:49 +01:00
|
|
|
output = append(output, CloneEmptyProperties(reflect.ValueOf(p)).Interface())
|
2016-08-06 02:19:36 +02:00
|
|
|
}
|
2016-06-07 21:28:16 +02:00
|
|
|
}
|
2020-01-02 18:37:49 +01:00
|
|
|
_, errs = UnpackProperties(module.Properties, output...)
|
2016-06-07 21:28:16 +02:00
|
|
|
if len(errs) != 0 && len(testCase.errs) == 0 {
|
|
|
|
t.Errorf("test case: %s", testCase.input)
|
|
|
|
t.Errorf("unexpected unpack errors:")
|
|
|
|
for _, err := range errs {
|
|
|
|
t.Errorf(" %s", err)
|
|
|
|
}
|
|
|
|
t.FailNow()
|
|
|
|
} else if !reflect.DeepEqual(errs, testCase.errs) {
|
|
|
|
t.Errorf("test case: %s", testCase.input)
|
|
|
|
t.Errorf("incorrect errors:")
|
|
|
|
t.Errorf(" expected: %+v", testCase.errs)
|
|
|
|
t.Errorf(" got: %+v", errs)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(output) != len(testCase.output) {
|
|
|
|
t.Fatalf("incorrect number of property structs, expected %d got %d",
|
|
|
|
len(testCase.output), len(output))
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range output {
|
2020-01-28 01:14:31 +01:00
|
|
|
got := reflect.ValueOf(output[i]).Interface()
|
2016-06-07 21:28:16 +02:00
|
|
|
if !reflect.DeepEqual(got, testCase.output[i]) {
|
|
|
|
t.Errorf("test case: %s", testCase.input)
|
|
|
|
t.Errorf("incorrect output:")
|
|
|
|
t.Errorf(" expected: %+v", testCase.output[i])
|
|
|
|
t.Errorf(" got: %+v", got)
|
|
|
|
}
|
|
|
|
}
|
2014-05-28 01:34:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-06-01 00:53:32 +02:00
|
|
|
|
|
|
|
func mkpos(offset, line, column int) scanner.Position {
|
|
|
|
return scanner.Position{
|
|
|
|
Offset: offset,
|
|
|
|
Line: line,
|
|
|
|
Column: column,
|
|
|
|
}
|
|
|
|
}
|