github.com/tada-team/tdproto@v1.51.57/tdmarkup/scanner_test.go (about) 1 package tdmarkup 2 3 import "testing" 4 5 func TestScanner(t *testing.T) { 6 t.Run("empty", func(t *testing.T) { 7 s := NewScanner("") 8 9 if s.Position() != 0 { 10 t.Error("pos must be 0") 11 } 12 13 if s.Rest() != 0 { 14 t.Error("must be end") 15 } 16 17 if s.TakeNext() != EOF { 18 t.Error("pop result must be EOF") 19 } 20 }) 21 22 t.Run("one", func(t *testing.T) { 23 r := 'хов' 24 s := NewScanner(string(r)) 25 26 if s.Rest() != 1 { 27 t.Error("must be not end") 28 } 29 30 if s.Position() != 0 { 31 t.Error("Index() must be 0") 32 } 33 34 if s.Next() != r { 35 t.Errorf("Next() must be %s got %s at %d", string(r), string(s.Next()), s.Position()) 36 } 37 38 if s.TakeNext() != r { 39 t.Errorf("Pop() must be %s got %s at %d", string(r), string(s.Next()), s.Position()) 40 } 41 42 if s.Rest() != 0 { 43 t.Error("must be end") 44 } 45 46 if s.Next() != EOF { 47 t.Errorf("Next() must be EOF got %s at %d", string(s.Next()), s.Position()) 48 } 49 }) 50 51 t.Run("text", func(t *testing.T) { 52 s := NewScanner("123") 53 54 if ch := s.TakeNext(); ch != '1' { 55 t.Error("next result must be 1") 56 } 57 if ch := s.Prev(); ch != EOF { 58 t.Error("prev must be eof, got:", string(ch)) 59 } 60 if ch := s.Next(); ch != '2' { 61 t.Error("prev must be 2, got:", string(ch)) 62 } 63 64 if ch := s.TakeNext(); ch != '2' { 65 t.Error("next result must be 1") 66 } 67 if ch := s.Prev(); ch != '1' { 68 t.Error("prev must be 1, got:", string(ch)) 69 } 70 if ch := s.Next(); ch != '3' { 71 t.Error("prev must be 3, got:", string(ch)) 72 } 73 74 if ch := s.TakeNext(); ch != '3' { 75 t.Error("next result must be 1") 76 } 77 if ch := s.Prev(); ch != '2' { 78 t.Error("prev must be 2, got:", string(ch)) 79 } 80 if ch := s.Next(); ch != EOF { 81 t.Error("prev must be EOF, got:", string(ch)) 82 } 83 }) 84 }