...

Source file src/github.com/google/shlex/shlex_test.go

Documentation: github.com/google/shlex

     1  /*
     2  Copyright 2012 Google Inc. All Rights Reserved.
     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  
    17  package shlex
    18  
    19  import (
    20  	"strings"
    21  	"testing"
    22  )
    23  
    24  var (
    25  	// one two "three four" "five \"six\"" seven#eight # nine # ten
    26  	// eleven 'twelve\'
    27  	testString = "one two \"three four\" \"five \\\"six\\\"\" seven#eight # nine # ten\n eleven 'twelve\\' thirteen=13 fourteen/14"
    28  )
    29  
    30  func TestClassifier(t *testing.T) {
    31  	classifier := newDefaultClassifier()
    32  	tests := map[rune]runeTokenClass{
    33  		' ':  spaceRuneClass,
    34  		'"':  escapingQuoteRuneClass,
    35  		'\'': nonEscapingQuoteRuneClass,
    36  		'#':  commentRuneClass}
    37  	for runeChar, want := range tests {
    38  		got := classifier.ClassifyRune(runeChar)
    39  		if got != want {
    40  			t.Errorf("ClassifyRune(%v) -> %v. Want: %v", runeChar, got, want)
    41  		}
    42  	}
    43  }
    44  
    45  func TestTokenizer(t *testing.T) {
    46  	testInput := strings.NewReader(testString)
    47  	expectedTokens := []*Token{
    48  		&Token{WordToken, "one"},
    49  		&Token{WordToken, "two"},
    50  		&Token{WordToken, "three four"},
    51  		&Token{WordToken, "five \"six\""},
    52  		&Token{WordToken, "seven#eight"},
    53  		&Token{CommentToken, " nine # ten"},
    54  		&Token{WordToken, "eleven"},
    55  		&Token{WordToken, "twelve\\"},
    56  		&Token{WordToken, "thirteen=13"},
    57  		&Token{WordToken, "fourteen/14"}}
    58  
    59  	tokenizer := NewTokenizer(testInput)
    60  	for i, want := range expectedTokens {
    61  		got, err := tokenizer.Next()
    62  		if err != nil {
    63  			t.Error(err)
    64  		}
    65  		if !got.Equal(want) {
    66  			t.Errorf("Tokenizer.Next()[%v] of %q -> %v. Want: %v", i, testString, got, want)
    67  		}
    68  	}
    69  }
    70  
    71  func TestLexer(t *testing.T) {
    72  	testInput := strings.NewReader(testString)
    73  	expectedStrings := []string{"one", "two", "three four", "five \"six\"", "seven#eight", "eleven", "twelve\\", "thirteen=13", "fourteen/14"}
    74  
    75  	lexer := NewLexer(testInput)
    76  	for i, want := range expectedStrings {
    77  		got, err := lexer.Next()
    78  		if err != nil {
    79  			t.Error(err)
    80  		}
    81  		if got != want {
    82  			t.Errorf("Lexer.Next()[%v] of %q -> %v. Want: %v", i, testString, got, want)
    83  		}
    84  	}
    85  }
    86  
    87  func TestSplit(t *testing.T) {
    88  	want := []string{"one", "two", "three four", "five \"six\"", "seven#eight", "eleven", "twelve\\", "thirteen=13", "fourteen/14"}
    89  	got, err := Split(testString)
    90  	if err != nil {
    91  		t.Error(err)
    92  	}
    93  	if len(want) != len(got) {
    94  		t.Errorf("Split(%q) -> %v. Want: %v", testString, got, want)
    95  	}
    96  	for i := range got {
    97  		if got[i] != want[i] {
    98  			t.Errorf("Split(%q)[%v] -> %v. Want: %v", testString, i, got[i], want[i])
    99  		}
   100  	}
   101  }
   102  

View as plain text