1/*
2Copyright 2017 WALLIX
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8    http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package ast
18
19import (
20	"reflect"
21	"testing"
22
23	"github.com/wallix/awless/template/env"
24	"github.com/wallix/awless/template/params"
25)
26
27func TestCloneAST(t *testing.T) {
28	tree := &AST{}
29
30	cmd := new(fakeCmd)
31
32	tree.Statements = append(tree.Statements, &Statement{Node: &DeclarationNode{
33		Ident: "myvar",
34		Expr: &CommandNode{
35			Action: "create", Entity: "vpc",
36			ParamNodes: map[string]interface{}{"count": InterfaceNode{i: 1}, "myname": RefNode{key: "name"}},
37			Refs:       map[string]interface{}{"here": "there"},
38		}}}, &Statement{Node: &DeclarationNode{
39		Ident: "myothervar",
40		Expr: &CommandNode{
41			Command: cmd,
42			Action:  "create", Entity: "subnet",
43			ParamNodes: map[string]interface{}{"vpc": HoleNode{key: "myvar"}},
44			Refs:       map[string]interface{}{},
45		}}}, &Statement{Node: &CommandNode{
46		Action: "create", Entity: "instance",
47		ParamNodes: map[string]interface{}{"subnet": HoleNode{key: "myothervar"}},
48		Refs:       map[string]interface{}{"donald": "duck"},
49	}},
50	)
51
52	clone := tree.Clone()
53
54	if got, want := clone, tree; !reflect.DeepEqual(got, want) {
55		t.Fatalf("\ngot %#v\n\nwant %#v", got, want)
56	}
57
58	clone.Statements[0].Node.(*DeclarationNode).Expr.(*CommandNode).ParamNodes["new"] = InterfaceNode{i: "mynode"}
59
60	if got, want := clone.Statements, tree.Statements; reflect.DeepEqual(got, want) {
61		t.Fatalf("\ngot %s\n\nwant %s", got, want)
62	}
63}
64
65func TestIsQuoted(t *testing.T) {
66	tcases := []struct {
67		in  string
68		out bool
69	}{
70		{"", false},
71		{"'", false},
72		{"\"", false},
73		{"''", true},
74		{"\"\"", true},
75		{"\"'", false},
76		{"'\"", false},
77		{"'test\"", false},
78		{"\"test'", false},
79		{"\"test\"", true},
80		{"'test'", true},
81	}
82	for i, tcase := range tcases {
83		if got, want := isQuoted(tcase.in), tcase.out; got != want {
84			t.Fatalf("%d: got %t, want %t", i+1, got, want)
85		}
86	}
87}
88
89type fakeCmd struct{}
90
91func (*fakeCmd) ParamsSpec() params.Spec                                      { return nil }
92func (*fakeCmd) Run(env.Running, map[string]interface{}) (interface{}, error) { return nil, nil }
93