github.com/ianlewis/go-gitignore@v0.1.1-0.20231110021210-4a0f15cbd56f/token_test.go (about)

     1  // Copyright 2016 Denormal Limited
     2  // Copyright 2023 Google LLC
     3  //
     4  // Licensed under the Apache License, Version 2.0 (the "License");
     5  // you may not use this file except in compliance with the License.
     6  // You may obtain a copy of the License at
     7  //
     8  //      http://www.apache.org/licenses/LICENSE-2.0
     9  //
    10  // Unless required by applicable law or agreed to in writing, software
    11  // distributed under the License is distributed on an "AS IS" BASIS,
    12  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  // See the License for the specific language governing permissions and
    14  // limitations under the License.
    15  
    16  package gitignore_test
    17  
    18  import (
    19  	"fmt"
    20  	"testing"
    21  
    22  	"github.com/ianlewis/go-gitignore"
    23  )
    24  
    25  func TestToken(t *testing.T) {
    26  	for _, _test := range _TOKENS {
    27  		// create the token
    28  		_position := gitignore.Position{
    29  			File:   "file",
    30  			Line:   _test.Line,
    31  			Column: _test.Column,
    32  			Offset: _test.NewLine,
    33  		}
    34  		_token := gitignore.NewToken(
    35  			_test.Type, []rune(_test.Token), _position,
    36  		)
    37  
    38  		// ensure we have a non-nil token
    39  		if _token == nil {
    40  			t.Errorf(
    41  				"unexpected nil Token for type %d %q", _test.Type, _test.Name,
    42  			)
    43  			continue
    44  		}
    45  
    46  		// ensure the token type match
    47  		if _token.Type != _test.Type {
    48  			// if we have a bad token, then we accept token types that
    49  			// are outside the range of permitted token values
    50  			if _token.Type == gitignore.BAD {
    51  				if _test.Type < gitignore.ILLEGAL ||
    52  					_test.Type > gitignore.BAD {
    53  					goto NAME
    54  				}
    55  			}
    56  
    57  			// otherwise, we have a type mismatch
    58  			t.Errorf(
    59  				"token type mismatch for %q; expected %d, got %d",
    60  				_test.Name, _test.Type, _token.Type,
    61  			)
    62  			continue
    63  		}
    64  
    65  	NAME:
    66  		// ensure the token name match
    67  		if _token.Name() != _test.Name {
    68  			t.Errorf(
    69  				"token name mismatch for type %d; expected %s, got %s",
    70  				_test.Type, _test.Name, _token.Name(),
    71  			)
    72  			continue
    73  		}
    74  
    75  		// ensure the positions are the same
    76  		if !coincident(_position, _token.Position) {
    77  			t.Errorf(
    78  				"token position mismatch; expected %s, got %s",
    79  				pos(_position), pos(_token.Position),
    80  			)
    81  			continue
    82  		}
    83  
    84  		// ensure the string form of the token is as expected
    85  		_string := fmt.Sprintf(
    86  			"%s: %s %q", _position, _test.Name, _test.Token,
    87  		)
    88  		if _string != _token.String() {
    89  			t.Errorf(
    90  				"token string mismatch; expected %q, got %q",
    91  				_string, _token.String(),
    92  			)
    93  		}
    94  	}
    95  } // TestToken()