@@ -17,10 +17,32 @@ package ast
17
17
import (
18
18
"testing"
19
19
20
+ "github.com/hashicorp/hcl/v2"
20
21
"github.com/pulumi/esc/syntax"
21
22
"github.com/stretchr/testify/assert"
22
23
)
23
24
25
+ type SimpleScalar string
26
+
27
+ func (s SimpleScalar ) Range () * hcl.Range {
28
+ return & hcl.Range {
29
+ Start : hcl.Pos {},
30
+ End : hcl.Pos {Byte : len (s )},
31
+ }
32
+ }
33
+
34
+ func (s SimpleScalar ) Path () string {
35
+ return "test"
36
+ }
37
+
38
+ func (s SimpleScalar ) ScalarRange (start , end int ) * hcl.Range {
39
+ r := s .Range ()
40
+ return & hcl.Range {
41
+ Start : hcl.Pos {Byte : r .Start .Byte + start },
42
+ End : hcl.Pos {Byte : r .Start .Byte + end },
43
+ }
44
+ }
45
+
24
46
func mkInterp (text string , accessors ... PropertyAccessor ) Interpolation {
25
47
return Interpolation {
26
48
Text : text ,
@@ -35,12 +57,24 @@ func mkAccess(accessors ...PropertyAccessor) *PropertyAccess {
35
57
return & PropertyAccess {Accessors : accessors }
36
58
}
37
59
38
- func mkPropertyName (name string ) * PropertyName {
39
- return & PropertyName {Name : name }
60
+ func mkPropertyName (name string , start , end int ) * PropertyName {
61
+ return & PropertyName {
62
+ Name : name ,
63
+ AccessorRange : & hcl.Range {
64
+ Start : hcl.Pos {Byte : start },
65
+ End : hcl.Pos {Byte : end },
66
+ },
67
+ }
40
68
}
41
69
42
- func mkPropertySubscript [T string | int ](index T ) * PropertySubscript {
43
- return & PropertySubscript {Index : index }
70
+ func mkPropertySubscript [T string | int ](index T , start , end int ) * PropertySubscript {
71
+ return & PropertySubscript {
72
+ Index : index ,
73
+ AccessorRange : & hcl.Range {
74
+ Start : hcl.Pos {Byte : start },
75
+ End : hcl.Pos {Byte : end },
76
+ },
77
+ }
44
78
}
45
79
46
80
func TestInvalidInterpolations (t * testing.T ) {
@@ -50,51 +84,51 @@ func TestInvalidInterpolations(t *testing.T) {
50
84
}{
51
85
{
52
86
text : "${foo" ,
53
- parts : []Interpolation {mkInterp ("" , mkPropertyName ("foo" ))},
87
+ parts : []Interpolation {mkInterp ("" , mkPropertyName ("foo" , 2 , 5 ))},
54
88
},
55
89
{
56
90
text : "${foo " ,
57
91
parts : []Interpolation {
58
- mkInterp ("" , mkPropertyName ("foo" )),
92
+ mkInterp ("" , mkPropertyName ("foo" , 2 , 5 )),
59
93
mkInterp (" " ),
60
94
},
61
95
},
62
96
{
63
97
text : `${foo} ${["baz} bar` ,
64
98
parts : []Interpolation {
65
- mkInterp ("" , mkPropertyName ("foo" )),
66
- mkInterp (" " , mkPropertySubscript ("baz} bar" )),
99
+ mkInterp ("" , mkPropertyName ("foo" , 2 , 5 )),
100
+ mkInterp (" " , mkPropertySubscript ("baz} bar" , 9 , 19 )),
67
101
},
68
102
},
69
103
{
70
104
text : `missing ${property[} subscript` ,
71
105
parts : []Interpolation {
72
- mkInterp ("missing " , mkPropertyName ("property" ), mkPropertySubscript ("" )),
106
+ mkInterp ("missing " , mkPropertyName ("property" , 10 , 18 ), mkPropertySubscript ("" , 18 , 19 )),
73
107
mkInterp (" subscript" ),
74
108
},
75
109
},
76
110
{
77
111
text : `${[bar].baz}` ,
78
112
parts : []Interpolation {
79
- mkInterp ("" , mkPropertySubscript ("bar" ), mkPropertyName ("baz" )),
113
+ mkInterp ("" , mkPropertySubscript ("bar" , 2 , 7 ), mkPropertyName ("baz" , 7 , 11 )),
80
114
},
81
115
},
82
116
{
83
117
text : `${foo.` ,
84
118
parts : []Interpolation {
85
- mkInterp ("" , mkPropertyName ("foo" ), mkPropertyName ("" )),
119
+ mkInterp ("" , mkPropertyName ("foo" , 2 , 5 ), mkPropertyName ("" , 5 , 6 )),
86
120
},
87
121
},
88
122
{
89
123
text : `${foo[` ,
90
124
parts : []Interpolation {
91
- mkInterp ("" , mkPropertyName ("foo" ), mkPropertySubscript ("" )),
125
+ mkInterp ("" , mkPropertyName ("foo" , 2 , 5 ), mkPropertySubscript ("" , 5 , 6 )),
92
126
},
93
127
},
94
128
}
95
129
for _ , c := range cases {
96
130
t .Run (c .text , func (t * testing.T ) {
97
- node := syntax .String ( c .text )
131
+ node := syntax .StringSyntax ( SimpleScalar ( c . text ), c .text )
98
132
parts , diags := parseInterpolate (node , c .text )
99
133
assert .NotEmpty (t , diags )
100
134
assert .Equal (t , c .parts , parts )
0 commit comments