Skip to content

Commit

Permalink
Added func colorjson for actual value and delta in defaultFailureTemp…
Browse files Browse the repository at this point in the history
…late
  • Loading branch information
dminkovski committed Apr 26, 2023
1 parent 305ca01 commit 3bb920b
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 5 deletions.
32 changes: 30 additions & 2 deletions formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"testing"
"text/template"

"github.com/TylerBrock/colorjson"
"github.com/fatih/color"
"github.com/mattn/go-isatty"
"github.com/mitchellh/go-wordwrap"
Expand Down Expand Up @@ -988,6 +989,33 @@ var defaultTemplateFuncs = template.FuncMap{

return sb.String()
},
"colorjson": func(enable bool, stringColor, input string) string {
if !enable {
return input
}
var colorObj map[string]interface{}
err := json.Unmarshal([]byte(input), &colorObj)
if err != nil {
return input
}
colorInput := "HiMagenta"
if _, ok := defaultColors[stringColor]; ok {
colorInput = stringColor
}
cyanColor := color.New(defaultColors["Cyan"])

formatter := colorjson.NewFormatter()
formatter.StringColor = color.New(defaultColors[colorInput])
formatter.NumberColor = cyanColor
formatter.BoolColor = cyanColor
formatter.NullColor = cyanColor

s, err := formatter.Marshal(colorObj)
if err != nil {
return input
}
return string(s)
},
}

var defaultSuccessTemplate = `[OK] {{ join .LineWidth .AssertPath }}`
Expand Down Expand Up @@ -1034,7 +1062,7 @@ assertion:
{{- if .HaveActual }}
actual value:
{{ .Actual | indent | color .EnableColors "HiMagenta" }}
{{ .Actual | indent | colorjson .EnableColors "HiMagenta"}}
{{- end -}}
{{- if .HaveReference }}
Expand All @@ -1044,7 +1072,7 @@ reference value:
{{- if .HaveDelta }}
allowed delta:
{{ .Delta | indent | color .EnableColors "HiMagenta" }}
{{ .Delta | indent | colorjson .EnableColors "Cyan" }}
{{- end -}}
{{- if .HaveDiff }}
Expand Down
7 changes: 4 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ go 1.17
require (
github.com/ajg/form v1.5.1
github.com/fasthttp/websocket v1.4.3-rc.6
github.com/fatih/color v1.13.0
github.com/fatih/color v1.15.0
github.com/fatih/structs v1.1.0
github.com/gobwas/glob v0.2.3
github.com/google/go-querystring v1.1.0
github.com/gorilla/websocket v1.4.2
github.com/imkira/go-interpol v1.1.0
github.com/mattn/go-isatty v0.0.17
github.com/mattn/go-isatty v0.0.18
github.com/mitchellh/go-wordwrap v1.0.1
github.com/sanity-io/litter v1.5.5
github.com/stretchr/testify v1.5.0
Expand All @@ -24,6 +24,7 @@ require (
)

require (
github.com/TylerBrock/colorjson v0.0.0-20200706003622-8a50f05110d2 // indirect
github.com/andybalholm/brotli v1.0.4 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/klauspost/compress v1.15.0 // indirect
Expand All @@ -38,6 +39,6 @@ require (
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
github.com/yudai/golcs v0.0.0-20170316035057-ecda9a501e82 // indirect
github.com/yudai/pp v2.0.1+incompatible // indirect
golang.org/x/sys v0.5.0 // indirect
golang.org/x/sys v0.7.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)
9 changes: 9 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/TylerBrock/colorjson v0.0.0-20200706003622-8a50f05110d2 h1:ZBbLwSJqkHBuFDA6DUhhse0IGJ7T5bemHyNILUjvOq4=
github.com/TylerBrock/colorjson v0.0.0-20200706003622-8a50f05110d2/go.mod h1:VSw57q4QFiWDbRnjdX8Cb3Ow0SFncRw+bA/ofY6Q83w=
github.com/ajg/form v1.5.1 h1:t9c7v8JUKu/XxOGBU0yjNpaMloxGEJhUkqFRq0ibGeU=
github.com/ajg/form v1.5.1/go.mod h1:uL1WgH+h2mgNtvBq0339dVnzXdBETtL2LeUXaIv25UY=
github.com/andybalholm/brotli v1.0.2/go.mod h1:loMXtMfwqflxFJPmdbJO0a3KNoPuLBgiu3qAvBg8x/Y=
Expand All @@ -11,6 +13,8 @@ github.com/fasthttp/websocket v1.4.3-rc.6 h1:omHqsl8j+KXpmzRjF8bmzOSYJ8GnS0E3efi
github.com/fasthttp/websocket v1.4.3-rc.6/go.mod h1:43W9OM2T8FeXpCWMsBd9Cb7nE2CACNqNvCqQCoty/Lc=
github.com/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w=
github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk=
github.com/fatih/color v1.15.0 h1:kOqh6YHBtK8aywxGerMG2Eq3H6Qgoqeo13Bk2Mv/nBs=
github.com/fatih/color v1.15.0/go.mod h1:0h5ZqXfHYED7Bhv2ZJamyIOUej9KtShiJESRwBDUSsw=
github.com/fatih/structs v1.1.0 h1:Q7juDM0QtcnhCpeyLGQKyg4TOIghuNXrkL32pHAUMxo=
github.com/fatih/structs v1.1.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga6PJ7M=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
Expand Down Expand Up @@ -40,6 +44,8 @@ github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27k
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng=
github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.18 h1:DOKFKCQ7FNG2L1rbrmstDN4QVRdS89Nkh85u68Uwp98=
github.com/mattn/go-isatty v0.0.18/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mitchellh/go-wordwrap v1.0.1 h1:TLuKupo69TCn6TQSyGxwI1EblZZEsQ0vMlAFQflz0v0=
github.com/mitchellh/go-wordwrap v1.0.1/go.mod h1:R62XHJLzvMFRBbcrT7m7WgmE1eOyTSsCt+hzestvNj0=
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
Expand Down Expand Up @@ -129,6 +135,9 @@ golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.7.0 h1:3jlCCIQZPdOYu1h8BkNvLz8Kgwtae2cagcG/VamtZRU=
golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
Expand Down

0 comments on commit 3bb920b

Please sign in to comment.