summaryrefslogtreecommitdiff
path: root/hclsyntax/navigation_test.go
blob: 1218739815d6a7434829388ea0ffaea4e04d9927 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package hclsyntax

import (
	"fmt"
	"strconv"
	"testing"

	"github.com/hashicorp/hcl/v2"
)

func TestNavigationContextString(t *testing.T) {
	cfg := `


resource {
}

resource "random_type" {
}

resource "null_resource" "baz" {
  name = "foo"
  boz = {
  	one = "111"
  	two = "22222"
  }
}

data "another" "baz" {
  name = "foo"
  boz = {
  	one = "111"
  	two = "22222"
  }
}
`
	file, diags := ParseConfig([]byte(cfg), "", hcl.Pos{Byte: 0, Line: 1, Column: 1})
	if len(diags) != 0 {
		fmt.Printf("offset %d\n", diags[0].Subject.Start.Byte)
		t.Errorf("Unexpected diagnostics: %s", diags)
	}
	if file == nil {
		t.Fatalf("Got nil file")
	}
	nav := file.Nav.(navigation)

	testCases := []struct {
		Offset int
		Want   string
	}{
		{0, ``},
		{2, ``},
		{4, `resource`},
		{17, `resource "random_type"`},
		{25, `resource "random_type"`},
		{45, `resource "null_resource" "baz"`},
		{142, `data "another" "baz"`},
		{180, `data "another" "baz"`},
		{99999, ``},
	}

	for _, tc := range testCases {
		t.Run(strconv.Itoa(tc.Offset), func(t *testing.T) {
			got := nav.ContextString(tc.Offset)

			if got != tc.Want {
				t.Errorf("wrong result\ngot:  %s\nwant: %s", got, tc.Want)
			}
		})
	}
}

func TestNavigationContextDefRange(t *testing.T) {
	cfg := `


resource {
}

resource "random_type" {
}

resource "null_resource" "baz" {
  name = "foo"
  boz = {
  	one = "111"
  	two = "22222"
  }
}

data "another" "baz" {
  name = "foo"
  boz = {
  	one = "111"
  	two = "22222"
  }
}
`
	file, diags := ParseConfig([]byte(cfg), "", hcl.Pos{Byte: 0, Line: 1, Column: 1})
	if len(diags) != 0 {
		fmt.Printf("offset %d\n", diags[0].Subject.Start.Byte)
		t.Errorf("Unexpected diagnostics: %s", diags)
	}
	if file == nil {
		t.Fatalf("Got nil file")
	}
	nav := file.Nav.(navigation)

	testCases := []struct {
		Offset    int
		WantRange hcl.Range
	}{
		{0, hcl.Range{}},
		{2, hcl.Range{}},
		{4, hcl.Range{Filename: "", Start: hcl.Pos{Line: 4, Column: 1, Byte: 3}, End: hcl.Pos{Line: 4, Column: 9, Byte: 11}}},
		{17, hcl.Range{Filename: "", Start: hcl.Pos{Line: 7, Column: 1, Byte: 17}, End: hcl.Pos{Line: 7, Column: 23, Byte: 39}}},
		{25, hcl.Range{Filename: "", Start: hcl.Pos{Line: 7, Column: 1, Byte: 17}, End: hcl.Pos{Line: 7, Column: 23, Byte: 39}}},
		{45, hcl.Range{Filename: "", Start: hcl.Pos{Line: 10, Column: 1, Byte: 45}, End: hcl.Pos{Line: 10, Column: 31, Byte: 75}}},
		{142, hcl.Range{Filename: "", Start: hcl.Pos{Line: 18, Column: 1, Byte: 142}, End: hcl.Pos{Line: 18, Column: 21, Byte: 162}}},
		{180, hcl.Range{Filename: "", Start: hcl.Pos{Line: 18, Column: 1, Byte: 142}, End: hcl.Pos{Line: 18, Column: 21, Byte: 162}}},
		{99999, hcl.Range{}},
	}

	for _, tc := range testCases {
		t.Run(strconv.Itoa(tc.Offset), func(t *testing.T) {
			got := nav.ContextDefRange(tc.Offset)

			if got != tc.WantRange {
				t.Errorf("wrong range\ngot:  %#v\nwant: %#v", got, tc.WantRange)
			}
		})
	}
}