github.com/emcfarlane/larking@v0.0.0-20220605172417-1704b45ee6c3/lexer_test.go (about) 1 // Copyright 2021 Edward McFarlane. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package larking 6 7 import ( 8 "testing" 9 ) 10 11 func TestLexer(t *testing.T) { 12 tests := []struct { 13 name string 14 tmpl string 15 want tokens 16 wantErr bool 17 }{{ 18 name: "one", 19 tmpl: "/v1/messages/{name=name/*}", 20 want: tokens{ 21 {tokenSlash, "/"}, 22 {tokenValue, "v1"}, 23 {tokenSlash, "/"}, 24 {tokenValue, "messages"}, 25 {tokenSlash, "/"}, 26 {tokenVariableStart, "{"}, 27 {tokenValue, "name"}, 28 {tokenEqual, "="}, 29 {tokenValue, "name"}, 30 {tokenSlash, "/"}, 31 {tokenStar, "*"}, 32 {tokenVariableEnd, "}"}, 33 {tokenEOF, ""}, 34 }, 35 }} 36 37 for _, tt := range tests { 38 t.Run(tt.name, func(t *testing.T) { 39 40 l := &lexer{ 41 input: tt.tmpl, 42 } 43 err := lexTemplate(l) 44 if tt.wantErr { 45 if err == nil { 46 t.Error("wanted failure but succeeded") 47 } 48 return 49 } 50 if err != nil { 51 t.Error(err) 52 } 53 if n, m := len(tt.want), len(l.toks); n != m { 54 t.Errorf("mismatch length %v != %v:\n\t%v\n\t%v", n, m, tt.want, l.toks) 55 return 56 } 57 for i, want := range tt.want { 58 tok := l.toks[i] 59 if want != tok { 60 t.Errorf("%d: %v != %v", i, tok, want) 61 } 62 } 63 }) 64 } 65 }