go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/common/flag/nestedflagset/lexer_test.go (about)

     1  // Copyright 2016 The LUCI Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package nestedflagset
    16  
    17  import (
    18  	"testing"
    19  
    20  	. "github.com/smartystreets/goconvey/convey"
    21  )
    22  
    23  func TestLexer(t *testing.T) {
    24  	Convey("Given a lexer with an empty value", t, func() {
    25  		l := lexer("", ':')
    26  
    27  		Convey("The lexer should start in a finished state.", func() {
    28  			So(l.finished(), ShouldBeTrue)
    29  		})
    30  
    31  		Convey("The next token should be empty.", func() {
    32  
    33  			So(l.nextToken(), ShouldEqual, token(""))
    34  		})
    35  
    36  		Convey("Splitting should yield a zero-length token slice.", func() {
    37  			So(l.split(), ShouldResemble, []token{})
    38  		})
    39  	})
    40  
    41  	s := `a: b c:d":e:ł:f":g:ü:h\"i:j\":k`
    42  	Convey(`Given a complex test string: `+s, t, func() {
    43  		l := lexer(s, ':')
    44  
    45  		Convey("Should yield the expected token set and be finished.", func() {
    46  			expectedTokens := []token{
    47  				`a`,
    48  				` b c`,
    49  				`d:e:ł:f`,
    50  				`g`,
    51  				`ü`,
    52  				`h"i`,
    53  				`j"`,
    54  				`k`,
    55  			}
    56  			for _, expected := range expectedTokens {
    57  				So(l.nextToken(), ShouldEqual, expected)
    58  			}
    59  
    60  			So(l.finished(), ShouldBeTrue)
    61  			So(l.nextToken(), ShouldEqual, token(""))
    62  		})
    63  	})
    64  
    65  	Convey("Given a simple lexer string: a:b:c", t, func() {
    66  		l := lexer(`a:b:c`, ':')
    67  
    68  		Convey("Split should yield three elements.", func() {
    69  			So(l.split(), ShouldResemble, []token{"a", "b", "c"})
    70  		})
    71  	})
    72  }