...

Source file src/cloud.google.com/go/internal/pretty/pretty_test.go

Documentation: cloud.google.com/go/internal/pretty

     1  // Copyright 2016 Google LLC
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package pretty
    16  
    17  import (
    18  	"fmt"
    19  	"strings"
    20  	"testing"
    21  )
    22  
    23  type S struct {
    24  	X int
    25  	Y bool
    26  	z *string
    27  }
    28  
    29  func TestSprint(t *testing.T) {
    30  	Indent = "~"
    31  	i := 17
    32  
    33  	for _, test := range []struct {
    34  		value interface{}
    35  		want  string
    36  	}{
    37  		// primitives and pointer
    38  		{nil, "nil"},
    39  		{3, "3"},
    40  		{9.8, "9.8"},
    41  		{true, "true"},
    42  		{"foo", `"foo"`},
    43  		{&i, "&17"},
    44  		// array and slice
    45  		{[3]int{1, 2, 3}, "[3]int{\n~1,\n~2,\n~3,\n}"},
    46  		{[]int{1, 2, 3}, "[]int{\n~1,\n~2,\n~3,\n}"},
    47  		{[]int{}, "[]int{}"},
    48  		{[]string{"foo"}, "[]string{\n~\"foo\",\n}"},
    49  		// map
    50  		{map[int]bool{}, "map[int]bool{}"},
    51  		{map[int]bool{1: true, 2: false, 3: true},
    52  			"map[int]bool{\n~1: true,\n~3: true,\n}"},
    53  		// struct
    54  		{S{}, "pretty.S{\n}"},
    55  		{S{3, true, ptr("foo")},
    56  			"pretty.S{\n~X: 3,\n~Y: true,\n~z: &\"foo\",\n}"},
    57  		// interface
    58  		{[]interface{}{&i}, "[]interface {}{\n~&17,\n}"},
    59  		// nesting
    60  		{[]S{{1, false, ptr("a")}, {2, true, ptr("b")}},
    61  			`[]pretty.S{
    62  ~pretty.S{
    63  ~~X: 1,
    64  ~~z: &"a",
    65  ~},
    66  ~pretty.S{
    67  ~~X: 2,
    68  ~~Y: true,
    69  ~~z: &"b",
    70  ~},
    71  }`},
    72  	} {
    73  		got := fmt.Sprintf("%v", Value(test.value))
    74  		if got != test.want {
    75  			t.Errorf("%v: got:\n%q\nwant:\n%q", test.value, got, test.want)
    76  		}
    77  	}
    78  }
    79  
    80  func TestWithDefaults(t *testing.T) {
    81  	Indent = "~"
    82  	for _, test := range []struct {
    83  		value interface{}
    84  		want  string
    85  	}{
    86  		{map[int]bool{1: true, 2: false, 3: true},
    87  			"map[int]bool{\n~1: true,\n~2: false,\n~3: true,\n}"},
    88  		{S{}, "pretty.S{\n~X: 0,\n~Y: false,\n~z: nil,\n}"},
    89  	} {
    90  		got := fmt.Sprintf("%+v", Value(test.value))
    91  		if got != test.want {
    92  			t.Errorf("%v: got:\n%q\nwant:\n%q", test.value, got, test.want)
    93  		}
    94  	}
    95  }
    96  
    97  func TestBadVerb(t *testing.T) {
    98  	got := fmt.Sprintf("%d", Value(8))
    99  	want := "%!d("
   100  	if !strings.HasPrefix(got, want) {
   101  		t.Errorf("got %q, want prefix %q", got, want)
   102  	}
   103  }
   104  
   105  func ptr(s string) *string { return &s }
   106  

View as plain text