randomJSONValue builds a random nested Go value suitable for encoding/json.Marshal. The shape (depth, breadth, alternatives) is chosen by the supplied RNG so the generator is independent of the parser undertest.
(r *mathrand.Rand, depth int)
| 42 | // encoding/json.Marshal. The shape (depth, breadth, alternatives) is chosen |
| 43 | // by the supplied RNG so the generator is independent of the parser undertest. |
| 44 | func randomJSONValue(r *mathrand.Rand, depth int) interface{} { |
| 45 | if depth <= 0 { |
| 46 | // Leaves: choose a scalar. |
| 47 | switch r.Intn(6) { |
| 48 | case 0: |
| 49 | return r.Int63() |
| 50 | case 1: |
| 51 | return r.Float64() |
| 52 | case 2: |
| 53 | return r.Intn(2) == 0 |
| 54 | case 3: |
| 55 | return nil |
| 56 | case 4: |
| 57 | return randJSONString(r) |
| 58 | default: |
| 59 | return r.Int63() |
| 60 | } |
| 61 | } |
| 62 | switch r.Intn(3) { |
| 63 | case 0: |
| 64 | return randJSONString(r) |
| 65 | case 1: |
| 66 | n := r.Intn(4) + 1 |
| 67 | arr := make([]interface{}, n) |
| 68 | for i := range arr { |
| 69 | arr[i] = randomJSONValue(r, depth-1) |
| 70 | } |
| 71 | return arr |
| 72 | default: |
| 73 | n := r.Intn(4) + 1 |
| 74 | obj := make(map[string]interface{}, n) |
| 75 | for i := 0; i < n; i++ { |
| 76 | obj[randKey(r)] = randomJSONValue(r, depth-1) |
| 77 | } |
| 78 | return obj |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | func randKey(r *mathrand.Rand) string { |
| 83 | letters := "abcdefghijklmnopqrstuvwx" |
no test coverage detected