123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- // This package contains a reusable recursive descent parser
- // Copyright Beoran beoran@gmail.com 2021
- // Licenced under the MIT licence.
- package rdpar
- import "testing"
- // import "os"
- // import "bufio"
- import "strings"
- import "strconv"
- func FatalIfErr(t *testing.T, v Value) {
- if v != nil {
- if err, isErr := v.(error); isErr {
- t.Fatalf("Error: %s", err)
- }
- }
- }
- func TestParseWord(t *testing.T) {
- in := strings.NewReader(`foo `)
- p := &Parser{}
- p.RuneScanner = in
- res := p.ParseWord("\r\n ")
- FatalIfErr(t, res)
- want := string("foo")
- if res != want {
- t.Errorf("error: expected: %s, got %s.", want, res)
- } else {
- t.Logf("ok: %s, %s", want, res)
- }
- }
- func TestParseInt(t *testing.T) {
- in := strings.NewReader(`123 `)
- p := &Parser{}
- p.RuneScanner = in
- res := p.ParseNum("\r\n ")
- FatalIfErr(t, res)
- want := int64(123)
- if res != want {
- t.Errorf("error: expected: %d, got %d.", want, res)
- } else {
- t.Logf("ok: %d, %d", want, res)
- }
- }
- func TestParseFloat(t *testing.T) {
- in := strings.NewReader(`123.456 `)
- p := &Parser{}
- p.RuneScanner = in
- res := p.ParseNum("\r\n ")
- FatalIfErr(t, res)
- want := float64(123.456)
- if res != want {
- t.Errorf("error: expected: %f, got %f.", want, res)
- } else {
- t.Logf("ok: %f, %f", want, res)
- }
- }
- func TestParseString(t *testing.T) {
- instr := `"Hello\nWorld\\\""`
- in := strings.NewReader(instr)
- p := &Parser{}
- p.RuneScanner = in
- res := p.ParseString('"', '"')
- FatalIfErr(t, res)
- want, err := strconv.Unquote(instr)
- FatalIfErr(t, err)
- if res != want {
- t.Errorf("error: expected: %s, got %v.", want, res)
- } else {
- t.Logf("ok: %s, %s", want, res)
- }
- }
- func TestParsRawString(t *testing.T) {
- instr := `[Hello&]World&&]`
- in := strings.NewReader(instr)
- p := &Parser{}
- p.RuneScanner = in
- res := p.ParseRawString('[', ']', '&')
- FatalIfErr(t, res)
- want := `Hello]World&`
- if res != want {
- t.Errorf("error: expected: %s, got %v.", want, res)
- } else {
- t.Logf("ok: %s, %s", want, res)
- }
- }
- /*
- func TestTabpa(t *testing.T) {
- fin, err := os.Open("testdata/test1.b1cfg")
- FatalIfErr(t, err)
- defer fin.Close()
- res, err := ParseB1cfg(bufio.NewReader(fin))
- FatalIfErr(t, err)
- t.Logf("res: %v", res)
- }*/
|