From 2409f21063c5aeff7fc8cf4ba703ee07bd797bee Mon Sep 17 00:00:00 2001 From: Shulhan Date: Mon, 10 Apr 2023 20:27:00 +0700 Subject: lib/bytes: move unit Test for Read and SkipLine to Example Using an example not only test the code but also provide example snippet when opened in the godoc. --- lib/bytes/parser_example_test.go | 39 ++++++++++++++++++++ lib/bytes/parser_test.go | 77 ---------------------------------------- 2 files changed, 39 insertions(+), 77 deletions(-) delete mode 100644 lib/bytes/parser_test.go (limited to 'lib/bytes') diff --git a/lib/bytes/parser_example_test.go b/lib/bytes/parser_example_test.go index a30bda5a..a81e9fc7 100644 --- a/lib/bytes/parser_example_test.go +++ b/lib/bytes/parser_example_test.go @@ -37,6 +37,26 @@ func ExampleParser_Delimiters() { // =; } +func ExampleParser_Read() { + var ( + content = []byte("a = b; ") + delims = []byte{'=', ';'} + parser = libbytes.NewParser(content, delims) + ) + + token, c := parser.Read() + fmt.Printf("token:'%s' c:'%c'\n", token, c) + token, c = parser.Read() + fmt.Printf("token:'%s' c:'%c'\n", token, c) + token, c = parser.Read() + fmt.Printf("token:'%s' c:%d\n", token, c) + + // Output: + // token:'a ' c:'=' + // token:' b' c:';' + // token:' ' c:0 +} + func ExampleParser_ReadLine() { var ( content = []byte("a=b;\nc=d;") @@ -191,6 +211,25 @@ func ExampleParser_Skip() { // } +func ExampleParser_SkipLine() { + var ( + content = []byte("a\nb\nc\nd e\n") + delims = []byte("\n") + parser = libbytes.NewParser(content, delims) + ) + + parser.SkipLine() + token, _ := parser.Read() + fmt.Printf("token:'%s'\n", token) + + parser.SkipLine() + token, _ = parser.Read() + fmt.Printf("token:'%s'\n", token) + // Output: + // token:'b' + // token:'d e' +} + func ExampleParser_SkipN() { var ( content = []byte(`a=b;c=d;`) diff --git a/lib/bytes/parser_test.go b/lib/bytes/parser_test.go deleted file mode 100644 index 4df84830..00000000 --- a/lib/bytes/parser_test.go +++ /dev/null @@ -1,77 +0,0 @@ -// Copyright 2023, Shulhan . All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package bytes - -import ( - "testing" - - "github.com/shuLhan/share/lib/test" -) - -func TestParserRead(t *testing.T) { - type testCase struct { - expToken []byte - expDelim byte - } - - var ( - parser = NewParser([]byte("a b\tc"), []byte(" \t")) - - cases = []testCase{{ - expToken: []byte(`a`), - expDelim: ' ', - }, { - expToken: []byte(`b`), - expDelim: '\t', - }, { - expToken: []byte(`c`), - expDelim: 0, - }, { - // empty. - }} - - c testCase - token []byte - d byte - ) - - for _, c = range cases { - token, d = parser.Read() - test.Assert(t, `token`, c.expToken, token) - test.Assert(t, `delimiter`, c.expDelim, d) - } -} - -func TestParserSkipLine(t *testing.T) { - type testCase struct { - expToken []byte - expDelim byte - } - - var ( - parser = NewParser([]byte("a\nb\nc\nd e\n"), []byte("\n")) - - cases = []testCase{{ - expToken: []byte(`b`), - expDelim: '\n', - }, { - expToken: []byte(`d e`), - expDelim: '\n', - }, { - // empty. - }} - - c testCase - token []byte - d byte - ) - - for _, c = range cases { - parser.SkipLine() - token, d = parser.Read() - test.Assert(t, `token`, c.expToken, token) - test.Assert(t, `delimiter`, c.expDelim, d) - } -} -- cgit v1.3-5-g9baa