summaryrefslogtreecommitdiff
path: root/decode_test.go
blob: 90729fa6aeddd97a1d6f694182582a675a717af0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// Copyright (C) 2022-2023  Luke Shumaker <lukeshu@lukeshu.com>
//
// SPDX-License-Identifier: GPL-2.0-or-later

package lowmemjson

import (
	"io"
	"strings"
	"testing"

	"github.com/stretchr/testify/assert"
)

func TestDecodeNumber(t *testing.T) {
	r := strings.NewReader(`1{}`)

	var num int
	assert.NoError(t, NewDecoder(r).Decode(&num))
	assert.Equal(t, 1, num)
	assert.Equal(t, 2, r.Len()) // check that it didn't read too far
}

func TestDecodeObject(t *testing.T) {
	err := DecodeObject(strings.NewReader(`{"foo":9}`),
		func(r io.RuneScanner) error {
			return nil
		},
		func(r io.RuneScanner) error {
			var n int
			return NewDecoder(r).Decode(&n)
		})
	assert.ErrorContains(t, err, "did not consume entire")
}