github.com/pyroscope-io/pyroscope@v0.37.3-0.20230725203016-5f6947968bd0/scripts/decode-resp/decode_test.go (about) 1 package main 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "strings" 7 8 "github.com/pyroscope-io/pyroscope/pkg/storage/tree" 9 10 . "github.com/onsi/ginkgo/v2" 11 . "github.com/onsi/gomega" 12 ) 13 14 var _ = Describe("Decode", func() { 15 Context("simple case", func() { 16 It("should decode correctly", func() { 17 names := strings.Split("total,a,b,c", ",") 18 levels := [][]int{ 19 {0, 3, 0, 0}, // total 20 {0, 3, 0, 1}, // a 21 {0, 1, 1, 3, 2, 2, 2, 2}, // b, c 22 } 23 in := &Input{ 24 Flamebearer: &tree.Flamebearer{ 25 Names: names, 26 Levels: levels, 27 }, 28 } 29 out := decodeLevels(in) 30 outJSON := marshal(out) 31 expected := `{ 32 "flamebearer": { 33 "levels": [ 34 [ 35 { 36 "_row": 0, 37 "_col": 0, 38 "name": "total", 39 "total": 3, 40 "self": 0, 41 "offset": 0 42 } 43 ], 44 [ 45 { 46 "_row": 1, 47 "_col": 0, 48 "name": "a", 49 "total": 3, 50 "self": 0, 51 "offset": 0 52 } 53 ], 54 [ 55 { 56 "_row": 2, 57 "_col": 0, 58 "name": "c", 59 "total": 1, 60 "self": 1, 61 "offset": 0 62 }, 63 { 64 "_row": 2, 65 "_col": 1, 66 "name": "b", 67 "total": 2, 68 "self": 2, 69 "offset": 2 70 } 71 ] 72 ] 73 } 74 }` 75 fmt.Println(outJSON) 76 Expect(outJSON).To(Equal(expected)) 77 }) 78 }) 79 }) 80 81 func marshal(out interface{}) string { 82 data, _ := json.MarshalIndent(out, "", " ") 83 return strings.TrimSpace(string(data)) 84 }