github.com/jxskiss/gopkg/v2@v2.14.9-0.20240514120614-899f3e7952b4/perf/json/human_friendly_test.go (about)

     1  package json
     2  
     3  import (
     4  	"bytes"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  func TestHumanFriendlyIndentation(t *testing.T) {
    11  	data := `[
    12      {
    13          "author": {
    14              "avatar_url": "https://github.com/images/error/octocat_happy.gif",
    15              "events_url": "https://api.github.com/users/octocat/events{/privacy}",
    16              "followers_url": "https://api.github.com/users/octocat/followers",
    17              "following_url": "https://api.github.com/users/octocat/following{/other_user}",
    18              "gists_url": "https://api.github.com/users/octocat/gists{/gist_id}",
    19              "gravatar_id": "",
    20              "html_url": "https://github.com/octocat",
    21              "id": 1,
    22              "login": "octocat",
    23              "node_id": "MDQ6VXNlcjE=",
    24              "organizations_url": "https://api.github.com/users/octocat/orgs",
    25              "received_events_url": "https://api.github.com/users/octocat/received_events",
    26              "repos_url": "https://api.github.com/users/octocat/repos",
    27              "site_admin": false,
    28              "starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}",
    29              "subscriptions_url": "https://api.github.com/users/octocat/subscriptions",
    30              "type": "User",
    31              "url": "https://api.github.com/users/octocat"
    32          },
    33          "comments_url": "https://api.github.com/repos/octocat/Hello-World/commits/6dcb09b5b57875f334f61aebed695e2e4193db5e/comments",
    34          "commit": {
    35              "author": {
    36                  "date": "2011-04-14T16:00:49Z",
    37                  "email": "support@github.com",
    38                  "name": "Monalisa Octocat"
    39              },
    40              "comment_count": 0,
    41              "committer": {
    42                  "date": "2011-04-14T16:00:49Z",
    43                  "email": "support@github.com",
    44                  "name": "Monalisa Octocat"
    45              },
    46              "message": "Fix all the bugs",
    47              "tree": {
    48                  "sha": "6dcb09b5b57875f334f61aebed695e2e4193db5e",
    49                  "url": "https://api.github.com/repos/octocat/Hello-World/tree/6dcb09b5b57875f334f61aebed695e2e4193db5e"
    50              },
    51              "url": "https://api.github.com/repos/octocat/Hello-World/git/commits/6dcb09b5b57875f334f61aebed695e2e4193db5e",
    52              "verification": {
    53                  "payload": null,
    54                  "reason": "unsigned",
    55                  "signature": null,
    56                  "verified": false
    57              }
    58          },
    59          "committer": {
    60              "avatar_url": "https://github.com/images/error/octocat_happy.gif",
    61              "events_url": "https://api.github.com/users/octocat/events{/privacy}",
    62              "followers_url": "https://api.github.com/users/octocat/followers",
    63              "following_url": "https://api.github.com/users/octocat/following{/other_user}",
    64              "gists_url": "https://api.github.com/users/octocat/gists{/gist_id}",
    65              "gravatar_id": "",
    66              "html_url": "https://github.com/octocat",
    67              "id": 1,
    68              "login": "octocat",
    69              "node_id": "MDQ6VXNlcjE=",
    70              "organizations_url": "https://api.github.com/users/octocat/orgs",
    71              "received_events_url": "https://api.github.com/users/octocat/received_events",
    72              "repos_url": "https://api.github.com/users/octocat/repos",
    73              "site_admin": false,
    74              "starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}",
    75              "subscriptions_url": "https://api.github.com/users/octocat/subscriptions",
    76              "type": "User",
    77              "url": "https://api.github.com/users/octocat"
    78          },
    79          "html_url": "https://github.com/octocat/Hello-World/commit/6dcb09b5b57875f334f61aebed695e2e4193db5e",
    80          "node_id": "MDY6Q29tbWl0NmRjYjA5YjViNTc4NzVmMzM0ZjYxYWViZWQ2OTVlMmU0MTkzZGI1ZQ==",
    81          "parents": [
    82              {
    83                  "sha": "6dcb09b5b57875f334f61aebed695e2e4193db5e",
    84                  "url": "https://api.github.com/repos/octocat/Hello-World/commits/6dcb09b5b57875f334f61aebed695e2e4193db5e"
    85              }
    86          ],
    87          "sha": "6dcb09b5b57875f334f61aebed695e2e4193db5e",
    88          "url": "https://api.github.com/repos/octocat/Hello-World/commits/6dcb09b5b57875f334f61aebed695e2e4193db5e"
    89      }
    90  ]`
    91  	var m []any
    92  	err := UnmarshalFromString(data, &m)
    93  	assert.Nil(t, err)
    94  
    95  	got, err := HumanFriendly.MarshalIndent(m, "", "    ")
    96  	assert.Nil(t, err)
    97  	assert.True(t, bytes.HasPrefix(got, []byte("[\n    {\n        \"author\": {\n            \"avatar_url\"")))
    98  	assert.True(t, bytes.Contains(got, []byte(",\n        \"comments_url\": \"")))
    99  	assert.True(t, bytes.Contains(got, []byte("        \"commit\": {\n            \"author\": {\n                \"date\": \"")))
   100  	assert.True(t, bytes.Contains(got, []byte(",\n        \"parents\": [\n            {\n                \"sha\": \"")))
   101  
   102  	var buf bytes.Buffer
   103  	err = HumanFriendly.NewEncoder(&buf).SetIndent("", "    ").Encode(m)
   104  	assert.Nil(t, err)
   105  	assert.True(t, bytes.HasPrefix(got, []byte("[\n    {\n        \"author\": {\n            \"avatar_url\"")))
   106  	assert.True(t, bytes.Contains(got, []byte(",\n        \"comments_url\": \"")))
   107  	assert.True(t, bytes.Contains(got, []byte("        \"commit\": {\n            \"author\": {\n                \"date\": \"")))
   108  	assert.True(t, bytes.Contains(got, []byte(",\n        \"parents\": [\n            {\n                \"sha\": \"")))
   109  }