/* NAME lex_test.go AUTHOR Trek Hopton LICENSE This file is Copyright (C) 2019 the Australian Ocean Lab (AusOcean) It is free software: you can redistribute it and/or modify them under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. It is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License in gpl.txt. If not, see [GNU licenses](http://www.gnu.org/licenses). */ package codecutil import ( "bytes" "io" "strconv" "testing" "time" ) var lexTests = []struct { data []byte t time.Duration n int isValid bool // Whether or not this test should fail. }{ {[]byte{0x10, 0x00, 0xf3, 0x45, 0xfe, 0xd2, 0xaa, 0x4e}, time.Millisecond, 4, true}, {[]byte{0x10, 0x00, 0xf3, 0x45, 0xfe, 0xd2, 0xaa, 0x4e}, time.Millisecond, 3, true}, {[]byte{0x10, 0x00, 0xf3, 0x45, 0xfe, 0xd2, 0xaa, 0x4e}, 0, 2, true}, {[]byte{0x10, 0x00, 0xf3, 0x45, 0xfe, 0xd2, 0xaa, 0x4e}, 0, 1, true}, {[]byte{0x10, 0x00, 0xf3, 0x45, 0xfe, 0xd2, 0xaa, 0x4e}, time.Nanosecond, 0, false}, {[]byte{0x10, 0x00, 0xf3, 0x45, 0xfe, 0xd2, 0xaa, 0x4e}, time.Millisecond, -1, false}, {[]byte{0x10, 0x00, 0xf3, 0x45, 0xfe, 0xd2, 0xaa, 0x4e}, time.Millisecond, 15, true}, } func TestByteLexer(t *testing.T) { for i, tt := range lexTests { t.Run(strconv.Itoa(i), func(t *testing.T) { dst := bytes.NewBuffer([]byte{}) l, err := NewByteLexer(tt.n) if err != nil { if tt.isValid { t.Errorf("unexpected error: %v", err) } else { t.Skip() } } err = l.Lex(dst, bytes.NewReader(tt.data), tt.t) if err != nil && err != io.EOF { if tt.isValid { t.Errorf("unexpected error: %v", err) } } else if !bytes.Equal(dst.Bytes(), tt.data) { t.Errorf("data before and after lex are not equal: want %v, got %v", tt.data, dst.Bytes()) } }) } }