github.com/denormal/go-gitignore@v0.0.0-20180930084346-ae8ad1d07817/token_test.go (about)

     1  package gitignore_test
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/denormal/go-gitignore"
     8  )
     9  
    10  func TestToken(t *testing.T) {
    11  	for _, _test := range _TOKENS {
    12  		// create the token
    13  		_position := gitignore.Position{
    14  			File:   "file",
    15  			Line:   _test.Line,
    16  			Column: _test.Column,
    17  			Offset: _test.NewLine,
    18  		}
    19  		_token := gitignore.NewToken(
    20  			_test.Type, []rune(_test.Token), _position,
    21  		)
    22  
    23  		// ensure we have a non-nil token
    24  		if _token == nil {
    25  			t.Errorf(
    26  				"unexpected nil Token for type %d %q", _test.Type, _test.Name,
    27  			)
    28  			continue
    29  		}
    30  
    31  		// ensure the token type match
    32  		if _token.Type != _test.Type {
    33  			// if we have a bad token, then we accept token types that
    34  			// are outside the range of permitted token values
    35  			if _token.Type == gitignore.BAD {
    36  				if _test.Type < gitignore.ILLEGAL ||
    37  					_test.Type > gitignore.BAD {
    38  					goto NAME
    39  				}
    40  			}
    41  
    42  			// otherwise, we have a type mismatch
    43  			t.Errorf(
    44  				"token type mismatch for %q; expected %d, got %d",
    45  				_test.Name, _test.Type, _token.Type,
    46  			)
    47  			continue
    48  		}
    49  
    50  	NAME:
    51  		// ensure the token name match
    52  		if _token.Name() != _test.Name {
    53  			t.Errorf(
    54  				"token name mismatch for type %d; expected %s, got %s",
    55  				_test.Type, _test.Name, _token.Name(),
    56  			)
    57  			continue
    58  		}
    59  
    60  		// ensure the positions are the same
    61  		if !coincident(_position, _token.Position) {
    62  			t.Errorf(
    63  				"token position mismatch; expected %s, got %s",
    64  				pos(_position), pos(_token.Position),
    65  			)
    66  			continue
    67  		}
    68  
    69  		// ensure the string form of the token is as expected
    70  		_string := fmt.Sprintf(
    71  			"%s: %s %q", _position, _test.Name, _test.Token,
    72  		)
    73  		if _string != _token.String() {
    74  			t.Errorf(
    75  				"token string mismatch; expected %q, got %q",
    76  				_string, _token.String(),
    77  			)
    78  		}
    79  	}
    80  } // TestToken()