github.com/Lephar/snapd@v0.0.0-20210825215435-c7fba9cef4d2/testutil/jsonchecker_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2021 Canonical Ltd
     5   *
     6   * This program is free software: you can redistribute it and/or modify
     7   * it under the terms of the GNU General Public License version 3 as
     8   * published by the Free Software Foundation.
     9   *
    10   * This program is distributed in the hope that it will be useful,
    11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13   * GNU General Public License for more details.
    14   *
    15   * You should have received a copy of the GNU General Public License
    16   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17   *
    18   */
    19  
    20  package testutil_test
    21  
    22  import (
    23  	"gopkg.in/check.v1"
    24  
    25  	. "github.com/snapcore/snapd/testutil"
    26  )
    27  
    28  type jsonCheckerSuite struct{}
    29  
    30  var _ = check.Suite(&jsonCheckerSuite{})
    31  
    32  func (*jsonCheckerSuite) TestFilePresent(c *check.C) {
    33  	testInfo(c, JsonEquals, "JsonEqual", []string{"obtained", "expected"})
    34  
    35  	type subRef struct {
    36  		Foo    string    `json:"foo"`
    37  		Baz    []float32 `json:"baz"`
    38  		hidden bool
    39  	}
    40  	type ref struct {
    41  		Number int      `json:"number"`
    42  		String string   `json:"string,omitempty"`
    43  		List   []string `json:"list"`
    44  		Map    *subRef  `json:"map,omitempty"`
    45  		hidden bool
    46  	}
    47  
    48  	testCheck(c, JsonEquals, true, "",
    49  		map[string]interface{}{
    50  			"number": 42,
    51  			"string": "foo",
    52  			"list":   []string{"123", "456"},
    53  			"map": map[string]interface{}{
    54  				"foo": "bar",
    55  				"baz": []interface{}{0.2, 0.3},
    56  			},
    57  		}, ref{
    58  			Number: 42,
    59  			String: "foo",
    60  			List:   []string{"123", "456"},
    61  			Map: &subRef{
    62  				Foo: "bar",
    63  				Baz: []float32{0.2, 0.3},
    64  				// this is transparent to the checker
    65  				hidden: true,
    66  			},
    67  			// this is transparent to the checker
    68  			hidden: true,
    69  		})
    70  	testCheck(c, JsonEquals, true, "",
    71  		map[string]interface{}{
    72  			"number": 42,
    73  			"string": "foo",
    74  			"list":   []interface{}(nil),
    75  		}, ref{
    76  			Number: 42,
    77  			String: "foo",
    78  		})
    79  	testCheck(c, JsonEquals, true, "",
    80  		map[string]interface{}{
    81  			"number": 42,
    82  			"list":   []interface{}(nil),
    83  		}, ref{
    84  			Number: 42,
    85  		})
    86  	testCheck(c, JsonEquals, false, "", 12, "12")
    87  	testCheck(c, JsonEquals, false, "Difference:\n...     [0]: 12 != 24\n", []int{12}, []int{24})
    88  	testCheck(c, JsonEquals, false, "Difference:\n...     [0]: string != float64\n", []string{"abc"}, []int{24})
    89  }