cloudflare/pint
Publicmirrored fromhttps://github.com/cloudflare/pintAvailable
internal/diags/position_test.go
197lines · modecode
| 1 | package diags |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "io" |
| 6 | "strconv" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | |
| 10 | "github.com/google/go-cmp/cmp" |
| 11 | "github.com/stretchr/testify/require" |
| 12 | "go.yaml.in/yaml/v3" |
| 13 | ) |
| 14 | |
| 15 | func parseYaml(input string) (key, val *yaml.Node) { |
| 16 | dec := yaml.NewDecoder(strings.NewReader(input)) |
| 17 | for { |
| 18 | var doc yaml.Node |
| 19 | err := dec.Decode(&doc) |
| 20 | if errors.Is(err, io.EOF) { |
| 21 | break |
| 22 | } |
| 23 | for _, root := range doc.Content { |
| 24 | if root.Kind == yaml.MappingNode { |
| 25 | key = root.Content[0] |
| 26 | val = root.Content[1] |
| 27 | } |
| 28 | } |
| 29 | } |
| 30 | return key, val |
| 31 | } |
| 32 | |
| 33 | func TestNewPositions(t *testing.T) { |
| 34 | type testCaseT struct { |
| 35 | input string |
| 36 | output PositionRanges |
| 37 | } |
| 38 | |
| 39 | testCases := []testCaseT{ |
| 40 | { |
| 41 | input: `foo: my very long string`, |
| 42 | output: PositionRanges{ |
| 43 | {Line: 1, FirstColumn: 6, LastColumn: 24}, |
| 44 | }, |
| 45 | }, |
| 46 | { |
| 47 | input: ` |
| 48 | |
| 49 | foo: my |
| 50 | very long |
| 51 | string |
| 52 | `, |
| 53 | output: PositionRanges{ |
| 54 | {Line: 3, FirstColumn: 6, LastColumn: 8}, |
| 55 | {Line: 4, FirstColumn: 3, LastColumn: 12}, |
| 56 | {Line: 5, FirstColumn: 3, LastColumn: 8}, |
| 57 | }, |
| 58 | }, |
| 59 | { |
| 60 | input: ` |
| 61 | foo: | |
| 62 | my |
| 63 | very long |
| 64 | string |
| 65 | `, |
| 66 | output: PositionRanges{ |
| 67 | {Line: 3, FirstColumn: 3, LastColumn: 5}, |
| 68 | {Line: 4, FirstColumn: 3, LastColumn: 12}, |
| 69 | {Line: 5, FirstColumn: 3, LastColumn: 8}, |
| 70 | }, |
| 71 | }, |
| 72 | { |
| 73 | input: ` |
| 74 | expr: |
| 75 | sum(bar{job="foo"}) |
| 76 | / on(c,d) |
| 77 | sum(bar) |
| 78 | `, |
| 79 | output: PositionRanges{ |
| 80 | {Line: 3, FirstColumn: 3, LastColumn: 22}, |
| 81 | {Line: 4, FirstColumn: 3, LastColumn: 12}, |
| 82 | {Line: 5, FirstColumn: 3, LastColumn: 10}, |
| 83 | }, |
| 84 | }, |
| 85 | { |
| 86 | input: ` |
| 87 | for: |+ |
| 88 | 11m |
| 89 | bar: xxx |
| 90 | `, |
| 91 | output: PositionRanges{ |
| 92 | {Line: 3, FirstColumn: 3, LastColumn: 5}, |
| 93 | }, |
| 94 | }, |
| 95 | { |
| 96 | input: ` |
| 97 | expr: |
| 98 | ( |
| 99 | xxx |
| 100 | - |
| 101 | yyy |
| 102 | ) * bar > 0 |
| 103 | and on(instance, device) baz |
| 104 | `, |
| 105 | output: PositionRanges{ |
| 106 | {Line: 3, FirstColumn: 3, LastColumn: 4}, |
| 107 | {Line: 4, FirstColumn: 5, LastColumn: 8}, |
| 108 | {Line: 5, FirstColumn: 5, LastColumn: 6}, |
| 109 | {Line: 6, FirstColumn: 5, LastColumn: 8}, |
| 110 | {Line: 7, FirstColumn: 3, LastColumn: 14}, |
| 111 | {Line: 8, FirstColumn: 3, LastColumn: 30}, |
| 112 | }, |
| 113 | }, |
| 114 | { |
| 115 | input: ` |
| 116 | expr: | |
| 117 | sum without (name) ( |
| 118 | bird_protocol_prefix_export_count{ip_version="4",name=~".*external.*",proto!="Kernel"} |
| 119 | * on (instance) group_left (profile,cluster) |
| 120 | cf_node_role{kubernetes_role="director",role="kubernetes"} |
| 121 | ) <= 0 |
| 122 | `, |
| 123 | output: PositionRanges{ |
| 124 | {Line: 3, FirstColumn: 5, LastColumn: 25}, |
| 125 | {Line: 4, FirstColumn: 5, LastColumn: 95}, |
| 126 | {Line: 5, FirstColumn: 5, LastColumn: 51}, |
| 127 | {Line: 6, FirstColumn: 5, LastColumn: 67}, |
| 128 | {Line: 7, FirstColumn: 5, LastColumn: 10}, |
| 129 | }, |
| 130 | }, |
| 131 | } |
| 132 | |
| 133 | for i, tc := range testCases { |
| 134 | t.Run(strconv.Itoa(i+1), func(t *testing.T) { |
| 135 | key, val := parseYaml(tc.input) |
| 136 | t.Logf("KEY [%s] %+v", key.Value, key) |
| 137 | t.Logf("VAL [%s] %+v", val.Value, val) |
| 138 | output := NewPositionRange(strings.Split(tc.input, "\n"), val, key.Column+2) |
| 139 | if diff := cmp.Diff(tc.output, output); diff != "" { |
| 140 | t.Errorf("NewPositions() returned wrong output (-want +got):\n%s", diff) |
| 141 | return |
| 142 | } |
| 143 | |
| 144 | require.Equal(t, len(strings.TrimSuffix(val.Value, "\n")), output.Len()) |
| 145 | }) |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | func TestReadRange(t *testing.T) { |
| 150 | type testCaseT struct { |
| 151 | input string |
| 152 | output PositionRanges |
| 153 | first int |
| 154 | last int |
| 155 | } |
| 156 | |
| 157 | testCases := []testCaseT{ |
| 158 | { |
| 159 | input: ` |
| 160 | |
| 161 | foo: my |
| 162 | very long |
| 163 | string |
| 164 | `, |
| 165 | first: 4, |
| 166 | last: 7, |
| 167 | output: PositionRanges{ |
| 168 | {Line: 4, FirstColumn: 3, LastColumn: 6}, |
| 169 | }, |
| 170 | }, |
| 171 | { |
| 172 | input: ` |
| 173 | expr: | |
| 174 | sum(bar{job="foo"}) |
| 175 | / on(c,d) |
| 176 | sum(bar) |
| 177 | `, |
| 178 | first: 23, |
| 179 | last: 24, |
| 180 | output: PositionRanges{ |
| 181 | {Line: 4, FirstColumn: 5, LastColumn: 6}, |
| 182 | }, |
| 183 | }, |
| 184 | } |
| 185 | |
| 186 | for i, tc := range testCases { |
| 187 | t.Run(strconv.Itoa(i+1), func(t *testing.T) { |
| 188 | key, val := parseYaml(tc.input) |
| 189 | pos := NewPositionRange(strings.Split(tc.input, "\n"), val, key.Column+2) |
| 190 | output := readRange(tc.first, tc.last, pos) |
| 191 | if diff := cmp.Diff(tc.output, output); diff != "" { |
| 192 | t.Errorf("ReadRange() returned wrong output (-want +got):\n%s", diff) |
| 193 | return |
| 194 | } |
| 195 | }) |
| 196 | } |
| 197 | } |
| 198 | |