File: jsoniter_any_string_test.go

package info (click to toggle)
golang-github-json-iterator-go 1.1.12-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie, trixie-proposed-updates
  • size: 916 kB
  • sloc: sh: 19; makefile: 3
file content (58 lines) | stat: -rw-r--r-- 1,597 bytes parent folder | download | duplicates (3)
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package any_tests

import (
	"testing"

	"github.com/json-iterator/go"
	"github.com/stretchr/testify/require"
)

var stringConvertMap = map[string]string{
	"null":                   "",
	"321.1":                  "321.1",
	`"1.1"`:                  "1.1",
	`"-123.1"`:               "-123.1",
	"0.0":                    "0.0",
	"0":                      "0",
	`"0"`:                    "0",
	`"0.0"`:                  "0.0",
	`"00.0"`:                 "00.0",
	"true":                   "true",
	"false":                  "false",
	`"true"`:                 "true",
	`"false"`:                "false",
	`"true123"`:              "true123",
	`"+1"`:                   "+1",
	"[]":                     "[]",
	"[1,2]":                  "[1,2]",
	"{}":                     "{}",
	`{"a":1, "stream":true}`: `{"a":1, "stream":true}`,
}

func Test_read_any_to_string(t *testing.T) {
	should := require.New(t)
	for k, v := range stringConvertMap {
		any := jsoniter.Get([]byte(k))
		should.Equal(v, any.ToString(), "original val "+k)
	}
}

func Test_read_string_as_any(t *testing.T) {
	should := require.New(t)
	any := jsoniter.Get([]byte(`"hello"`))
	should.Equal("hello", any.ToString())
	should.True(any.ToBool())
	any = jsoniter.Get([]byte(`" "`))
	should.False(any.ToBool())
	any = jsoniter.Get([]byte(`"false"`))
	should.True(any.ToBool())
	any = jsoniter.Get([]byte(`"123"`))
	should.Equal(123, any.ToInt())
}

func Test_wrap_string(t *testing.T) {
	should := require.New(t)
	any := jsoniter.Get([]byte("-32000")).MustBeValid()
	should.Equal(-32000, any.ToInt())
	should.NoError(any.LastError())
}