github.com/splunk/dan1-qbec@v0.7.3/internal/diff/diff_test.go (about) 1 /* 2 Copyright 2019 Splunk Inc. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package diff 18 19 import ( 20 "testing" 21 22 "github.com/stretchr/testify/assert" 23 "github.com/stretchr/testify/require" 24 ) 25 26 type addr struct { 27 Line string `json:"line"` 28 City string `json:"city"` 29 } 30 31 type contact struct { 32 Name string `json:"name"` 33 Address addr `json:"address"` 34 } 35 36 func TestBasicDiffs(t *testing.T) { 37 a := assert.New(t) 38 left := contact{ 39 Name: "John Doe", 40 Address: addr{ 41 Line: "1st st", 42 City: "San Jose", 43 }, 44 } 45 opts := Options{} 46 right := left 47 out, err := Objects(left, right, opts) 48 require.Nil(t, err) 49 a.Equal("", string(out)) 50 51 right.Name = "Jane Doe" 52 right.Address.City = "San Francisco" 53 out, err = Objects(left, right, opts) 54 require.Nil(t, err) 55 outStr := string(out) 56 a.Contains(outStr, "address:\n") 57 a.Contains(outStr, " line: 1st st\n") 58 a.Contains(outStr, "- city: San Jose\n") 59 a.Contains(outStr, "+ city: San Francisco\n") 60 a.Contains(outStr, "-name: John Doe\n") 61 a.Contains(outStr, "+name: Jane Doe\n") 62 63 out, err = Objects(left, nil, opts) 64 require.Nil(t, err) 65 outStr = string(out) 66 a.Contains(outStr, "-name: John Doe\n") 67 a.Contains(outStr, "-address:\n") 68 a.Contains(outStr, "- city: San Jose\n") 69 a.Contains(outStr, "- line: 1st st\n") 70 71 out, err = Objects([]byte{}, right, opts) 72 require.Nil(t, err) 73 outStr = string(out) 74 a.Contains(outStr, "+name: Jane Doe\n") 75 a.Contains(outStr, "+address:\n") 76 a.Contains(outStr, "+ city: San Francisco\n") 77 a.Contains(outStr, "+ line: 1st st\n") 78 } 79 80 func TestContextLines(t *testing.T) { 81 a := assert.New(t) 82 left := []contact{ 83 { 84 Name: "John Doe", 85 Address: addr{ 86 Line: "1st st", 87 City: "San Jose", 88 }, 89 }, 90 { 91 Name: "Jane Doe", 92 Address: addr{ 93 Line: "1st st", 94 City: "San Francisco", 95 }, 96 }, 97 } 98 right := []contact{left[0], left[1]} 99 right[1].Address.Line = "2nd st" 100 101 out, err := Objects(left, right, Options{Context: 2}) 102 require.Nil(t, err) 103 outStr := string(out) 104 a.NotContains(outStr, "John Doe") 105 a.Contains(outStr, "- address:\n") 106 a.Contains(outStr, " city: San Francisco\n") 107 a.Contains(outStr, "- line: 1st st\n") 108 a.Contains(outStr, "+ line: 2nd st\n") 109 a.Contains(outStr, " city: San Francisco\n") 110 } 111 112 func TestColorize(t *testing.T) { 113 a := assert.New(t) 114 left := contact{ 115 Name: "John Doe", 116 Address: addr{ 117 Line: "1st st", 118 City: "San Jose", 119 }, 120 } 121 opts := Options{Colorize: true} 122 right := left 123 right.Address.Line = "2nd st" 124 out, err := Objects(left, right, opts) 125 require.Nil(t, err) 126 outStr := string(out) 127 a.Contains(outStr, escRed+"- line: 1st st\n"+escReset) 128 a.Contains(outStr, escGreen+"+ line: 2nd st\n"+escReset) 129 }