github.com/hashicorp/hcl/v2@v2.20.0/hclsyntax/peeker_test.go (about) 1 // Copyright (c) HashiCorp, Inc. 2 // SPDX-License-Identifier: MPL-2.0 3 4 package hclsyntax 5 6 import ( 7 "reflect" 8 "testing" 9 ) 10 11 func init() { 12 // see the documentation of this variable for more information 13 tracePeekerNewlinesStack = true 14 } 15 16 func TestPeeker(t *testing.T) { 17 tokens := Tokens{ 18 { 19 Type: TokenIdent, 20 }, 21 { 22 Type: TokenComment, 23 }, 24 { 25 Type: TokenIdent, 26 }, 27 { 28 Type: TokenComment, 29 }, 30 { 31 Type: TokenIdent, 32 }, 33 { 34 Type: TokenNewline, 35 }, 36 { 37 Type: TokenIdent, 38 }, 39 { 40 Type: TokenNewline, 41 }, 42 { 43 Type: TokenIdent, 44 }, 45 { 46 Type: TokenNewline, 47 }, 48 { 49 Type: TokenEOF, 50 }, 51 } 52 53 { 54 peeker := newPeeker(tokens, true) 55 56 wantTypes := []TokenType{ 57 TokenIdent, 58 TokenComment, 59 TokenIdent, 60 TokenComment, 61 TokenIdent, 62 TokenNewline, 63 TokenIdent, 64 TokenNewline, 65 TokenIdent, 66 TokenNewline, 67 TokenEOF, 68 } 69 var gotTypes []TokenType 70 71 for { 72 peeked := peeker.Peek() 73 read := peeker.Read() 74 if peeked.Type != read.Type { 75 t.Errorf("mismatched Peek %s and Read %s", peeked, read) 76 } 77 78 gotTypes = append(gotTypes, read.Type) 79 80 if read.Type == TokenEOF { 81 break 82 } 83 } 84 85 if !reflect.DeepEqual(gotTypes, wantTypes) { 86 t.Errorf("wrong types\ngot: %#v\nwant: %#v", gotTypes, wantTypes) 87 } 88 } 89 90 { 91 peeker := newPeeker(tokens, false) 92 93 wantTypes := []TokenType{ 94 TokenIdent, 95 TokenIdent, 96 TokenIdent, 97 TokenNewline, 98 TokenIdent, 99 TokenNewline, 100 TokenIdent, 101 TokenNewline, 102 TokenEOF, 103 } 104 var gotTypes []TokenType 105 106 for { 107 peeked := peeker.Peek() 108 read := peeker.Read() 109 if peeked.Type != read.Type { 110 t.Errorf("mismatched Peek %s and Read %s", peeked, read) 111 } 112 113 gotTypes = append(gotTypes, read.Type) 114 115 if read.Type == TokenEOF { 116 break 117 } 118 } 119 120 if !reflect.DeepEqual(gotTypes, wantTypes) { 121 t.Errorf("wrong types\ngot: %#v\nwant: %#v", gotTypes, wantTypes) 122 } 123 } 124 125 { 126 peeker := newPeeker(tokens, false) 127 128 peeker.PushIncludeNewlines(false) 129 130 wantTypes := []TokenType{ 131 TokenIdent, 132 TokenIdent, 133 TokenIdent, 134 TokenIdent, 135 TokenIdent, 136 TokenNewline, // we'll pop off the PushIncludeNewlines before we get here 137 TokenEOF, 138 } 139 var gotTypes []TokenType 140 141 idx := 0 142 for { 143 peeked := peeker.Peek() 144 read := peeker.Read() 145 if peeked.Type != read.Type { 146 t.Errorf("mismatched Peek %s and Read %s", peeked, read) 147 } 148 149 gotTypes = append(gotTypes, read.Type) 150 151 if read.Type == TokenEOF { 152 break 153 } 154 155 if idx == 4 { 156 peeker.PopIncludeNewlines() 157 } 158 159 idx++ 160 } 161 162 if !reflect.DeepEqual(gotTypes, wantTypes) { 163 t.Errorf("wrong types\ngot: %#v\nwant: %#v", gotTypes, wantTypes) 164 } 165 } 166 }