File: json_test.go

package info (click to toggle)
golang-code.forgejo-f3-gof3 3.11.0-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 1,952 kB
  • sloc: sh: 100; makefile: 65
file content (39 lines) | stat: -rw-r--r-- 1,047 bytes parent folder | download
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
// Copyright Earl Warren <contact@earl-warren.org>
// Copyright Loïc Dachary <loic@dachary.org>
// SPDX-License-Identifier: MIT

package util

import (
	"encoding/json"
	"testing"

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

func TestJSON(t *testing.T) {
	var j any
	content := []byte(`
{
  "A": "B",
  "C": 1,
  "D": 2.1,
  "E": true,
  "F": null,
  "G": ["A", 1]
}
`)
	assert.NoError(t, json.Unmarshal(content, &j))
	assert.EqualValues(t, "B", jsonMap[string](jsonGetObject(j), "A"))
	assert.EqualValues(t, 1, int(jsonMap[float64](jsonGetObject(j), "C")))
	assert.EqualValues(t, 2.1, jsonMap[float64](jsonGetObject(j), "D"))
	assert.EqualValues(t, true, jsonMap[bool](jsonGetObject(j), "E"))

	assert.EqualValues(t, nil, jsonMap[any](jsonGetObject(j), "F"))
	assert.EqualValues(t, "", jsonMap[string](jsonGetObject(j), "F"))
	assert.EqualValues(t, 0, jsonMap[int](jsonGetObject(j), "F"))

	a := jsonMap[any](jsonGetObject(j), "G")
	assert.EqualValues(t, "A", jsonElement[string](a, 0))
	assert.EqualValues(t, 1, int(jsonElement[float64](a, 1)))
}