...

Source file src/github.com/andybalholm/cascadia/parser_test.go

Documentation: github.com/andybalholm/cascadia

     1  package cascadia
     2  
     3  import (
     4  	"testing"
     5  )
     6  
     7  var identifierTests = map[string]string{
     8  	"x":             "x",
     9  	"96":            "",
    10  	"-x":            "-x",
    11  	`r\e9 sumé`:     "résumé",
    12  	`r\0000e9 sumé`: "résumé",
    13  	`r\0000e9sumé`:  "résumé",
    14  	`a\"b`:          `a"b`,
    15  }
    16  
    17  func TestParseIdentifier(t *testing.T) {
    18  	for source, want := range identifierTests {
    19  		p := &parser{s: source}
    20  		got, err := p.parseIdentifier()
    21  		if err != nil {
    22  			if want == "" {
    23  				// It was supposed to be an error.
    24  				continue
    25  			}
    26  			t.Errorf("parsing %q: got error (%s), want %q", source, err, want)
    27  			continue
    28  		}
    29  
    30  		if want == "" {
    31  			if err == nil {
    32  				t.Errorf("parsing %q: got %q, want error", source, got)
    33  			}
    34  			continue
    35  		}
    36  
    37  		if p.i < len(source) {
    38  			t.Errorf("parsing %q: %d bytes left over", source, len(source)-p.i)
    39  			continue
    40  		}
    41  
    42  		if got != want {
    43  			t.Errorf("parsing %q: got %q, want %q", source, got, want)
    44  		}
    45  	}
    46  }
    47  
    48  var stringTests = map[string]string{
    49  	`"x"`:             "x",
    50  	`'x'`:             "x",
    51  	`'x`:              "",
    52  	"'x\\\r\nx'":      "xx",
    53  	`"r\e9 sumé"`:     "résumé",
    54  	`"r\0000e9 sumé"`: "résumé",
    55  	`"r\0000e9sumé"`:  "résumé",
    56  	`"a\"b"`:          `a"b`,
    57  }
    58  
    59  func TestParseString(t *testing.T) {
    60  	for source, want := range stringTests {
    61  		p := &parser{s: source}
    62  		got, err := p.parseString()
    63  		if err != nil {
    64  			if want == "" {
    65  				// It was supposed to be an error.
    66  				continue
    67  			}
    68  			t.Errorf("parsing %q: got error (%s), want %q", source, err, want)
    69  			continue
    70  		}
    71  
    72  		if want == "" {
    73  			if err == nil {
    74  				t.Errorf("parsing %q: got %q, want error", source, got)
    75  			}
    76  			continue
    77  		}
    78  
    79  		if p.i < len(source) {
    80  			t.Errorf("parsing %q: %d bytes left over", source, len(source)-p.i)
    81  			continue
    82  		}
    83  
    84  		if got != want {
    85  			t.Errorf("parsing %q: got %q, want %q", source, got, want)
    86  		}
    87  	}
    88  }
    89  

View as plain text