| 275 | } |
| 276 | |
| 277 | func TestParseExpressionWithStartPos(t *testing.T) { |
| 278 | src := `{ |
| 279 | "foo": "bar" |
| 280 | }` |
| 281 | part := `"bar"` |
| 282 | |
| 283 | file, diags := Parse([]byte(src), "") |
| 284 | partExpr, partDiags := ParseExpressionWithStartPos([]byte(part), "", hcl.Pos{Byte: 0, Line: 2, Column: 10}) |
| 285 | if len(diags) != 0 { |
| 286 | t.Errorf("got %d diagnostics on parse src; want 0", len(diags)) |
| 287 | for _, diag := range diags { |
| 288 | t.Logf("- %s", diag.Error()) |
| 289 | } |
| 290 | } |
| 291 | if len(partDiags) != 0 { |
| 292 | t.Errorf("got %d diagnostics on parse part src; want 0", len(partDiags)) |
| 293 | for _, diag := range partDiags { |
| 294 | t.Logf("- %s", diag.Error()) |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | if file == nil { |
| 299 | t.Fatalf("got nil File; want actual file") |
| 300 | } |
| 301 | if file.Body == nil { |
| 302 | t.Fatalf("got nil Body: want actual body") |
| 303 | } |
| 304 | if partExpr == nil { |
| 305 | t.Fatalf("got nil Expression; want actual expression") |
| 306 | } |
| 307 | |
| 308 | content, diags := file.Body.Content(&hcl.BodySchema{ |
| 309 | Attributes: []hcl.AttributeSchema{{Name: "foo"}}, |
| 310 | }) |
| 311 | if len(diags) != 0 { |
| 312 | t.Errorf("got %d diagnostics on decode; want 0", len(diags)) |
| 313 | for _, diag := range diags { |
| 314 | t.Logf("- %s", diag.Error()) |
| 315 | } |
| 316 | } |
| 317 | expr := content.Attributes["foo"].Expr |
| 318 | |
| 319 | if expr.Range().String() != partExpr.Range().String() { |
| 320 | t.Errorf("The two ranges did not match: src=%s, part=%s", expr.Range(), partExpr.Range()) |
| 321 | } |
| 322 | } |