github.com/hashicorp/hcl/v2@v2.20.0/hclsyntax/navigation_test.go (about)

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: MPL-2.0
     3  
     4  package hclsyntax
     5  
     6  import (
     7  	"fmt"
     8  	"strconv"
     9  	"testing"
    10  
    11  	"github.com/hashicorp/hcl/v2"
    12  )
    13  
    14  func TestNavigationContextString(t *testing.T) {
    15  	cfg := `
    16  
    17  
    18  resource {
    19  }
    20  
    21  resource "random_type" {
    22  }
    23  
    24  resource "null_resource" "baz" {
    25    name = "foo"
    26    boz = {
    27    	one = "111"
    28    	two = "22222"
    29    }
    30  }
    31  
    32  data "another" "baz" {
    33    name = "foo"
    34    boz = {
    35    	one = "111"
    36    	two = "22222"
    37    }
    38  }
    39  `
    40  	file, diags := ParseConfig([]byte(cfg), "", hcl.Pos{Byte: 0, Line: 1, Column: 1})
    41  	if len(diags) != 0 {
    42  		fmt.Printf("offset %d\n", diags[0].Subject.Start.Byte)
    43  		t.Errorf("Unexpected diagnostics: %s", diags)
    44  	}
    45  	if file == nil {
    46  		t.Fatalf("Got nil file")
    47  	}
    48  	nav := file.Nav.(navigation)
    49  
    50  	testCases := []struct {
    51  		Offset int
    52  		Want   string
    53  	}{
    54  		{0, ``},
    55  		{2, ``},
    56  		{4, `resource`},
    57  		{17, `resource "random_type"`},
    58  		{25, `resource "random_type"`},
    59  		{45, `resource "null_resource" "baz"`},
    60  		{142, `data "another" "baz"`},
    61  		{180, `data "another" "baz"`},
    62  		{99999, ``},
    63  	}
    64  
    65  	for _, tc := range testCases {
    66  		t.Run(strconv.Itoa(tc.Offset), func(t *testing.T) {
    67  			got := nav.ContextString(tc.Offset)
    68  
    69  			if got != tc.Want {
    70  				t.Errorf("wrong result\ngot:  %s\nwant: %s", got, tc.Want)
    71  			}
    72  		})
    73  	}
    74  }
    75  
    76  func TestNavigationContextDefRange(t *testing.T) {
    77  	cfg := `
    78  
    79  
    80  resource {
    81  }
    82  
    83  resource "random_type" {
    84  }
    85  
    86  resource "null_resource" "baz" {
    87    name = "foo"
    88    boz = {
    89    	one = "111"
    90    	two = "22222"
    91    }
    92  }
    93  
    94  data "another" "baz" {
    95    name = "foo"
    96    boz = {
    97    	one = "111"
    98    	two = "22222"
    99    }
   100  }
   101  `
   102  	file, diags := ParseConfig([]byte(cfg), "", hcl.Pos{Byte: 0, Line: 1, Column: 1})
   103  	if len(diags) != 0 {
   104  		fmt.Printf("offset %d\n", diags[0].Subject.Start.Byte)
   105  		t.Errorf("Unexpected diagnostics: %s", diags)
   106  	}
   107  	if file == nil {
   108  		t.Fatalf("Got nil file")
   109  	}
   110  	nav := file.Nav.(navigation)
   111  
   112  	testCases := []struct {
   113  		Offset    int
   114  		WantRange hcl.Range
   115  	}{
   116  		{0, hcl.Range{}},
   117  		{2, hcl.Range{}},
   118  		{4, hcl.Range{Filename: "", Start: hcl.Pos{Line: 4, Column: 1, Byte: 3}, End: hcl.Pos{Line: 4, Column: 9, Byte: 11}}},
   119  		{17, hcl.Range{Filename: "", Start: hcl.Pos{Line: 7, Column: 1, Byte: 17}, End: hcl.Pos{Line: 7, Column: 23, Byte: 39}}},
   120  		{25, hcl.Range{Filename: "", Start: hcl.Pos{Line: 7, Column: 1, Byte: 17}, End: hcl.Pos{Line: 7, Column: 23, Byte: 39}}},
   121  		{45, hcl.Range{Filename: "", Start: hcl.Pos{Line: 10, Column: 1, Byte: 45}, End: hcl.Pos{Line: 10, Column: 31, Byte: 75}}},
   122  		{142, hcl.Range{Filename: "", Start: hcl.Pos{Line: 18, Column: 1, Byte: 142}, End: hcl.Pos{Line: 18, Column: 21, Byte: 162}}},
   123  		{180, hcl.Range{Filename: "", Start: hcl.Pos{Line: 18, Column: 1, Byte: 142}, End: hcl.Pos{Line: 18, Column: 21, Byte: 162}}},
   124  		{99999, hcl.Range{}},
   125  	}
   126  
   127  	for _, tc := range testCases {
   128  		t.Run(strconv.Itoa(tc.Offset), func(t *testing.T) {
   129  			got := nav.ContextDefRange(tc.Offset)
   130  
   131  			if got != tc.WantRange {
   132  				t.Errorf("wrong range\ngot:  %#v\nwant: %#v", got, tc.WantRange)
   133  			}
   134  		})
   135  	}
   136  }