From ab6ed0df62d4d163eadbbe9777517afb822829d6 Mon Sep 17 00:00:00 2001 From: Arpad Ryszka Date: Thu, 26 Nov 2020 16:02:43 +0100 Subject: [PATCH] setup CI --- .github/workflows/check.yaml | 8 ++++++++ .gitignore | 1 + Makefile | 13 ++++++++----- fprint.go | 8 ++++++++ sprint_test.go | 6 +++--- 5 files changed, 28 insertions(+), 8 deletions(-) create mode 100644 .github/workflows/check.yaml diff --git a/.github/workflows/check.yaml b/.github/workflows/check.yaml new file mode 100644 index 0000000..9e6183f --- /dev/null +++ b/.github/workflows/check.yaml @@ -0,0 +1,8 @@ +on: [push, pull_request] +jobs: + check: + runs-on: ubuntu-latest + steps: + - uses: actions/setup-go@v2 + - uses: actions/checkout@v2 + - run: make ci-check diff --git a/.gitignore b/.gitignore index 223cec9..316958a 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ +.bin .coverprofile diff --git a/Makefile b/Makefile index 8bb8f75..c09cdd4 100644 --- a/Makefile +++ b/Makefile @@ -3,10 +3,10 @@ SOURCES = $(shell find . -name "*.go") default: build build: - go build ./... + @go build ./... check: - go test -count 1 ./... + @go test -count 1 ./... imports: @goimports -w $(SOURCES) @@ -15,10 +15,13 @@ fmt: @gofmt -w -s $(SOURCES) .coverprofile: $(SOURCES) - go test -count 1 -coverprofile .coverprofile + @go test -count 1 -coverprofile .coverprofile cover: .coverprofile - go tool cover -func .coverprofile + @go tool cover -func .coverprofile showcover: .coverprofile - go tool cover -html .coverprofile + @go tool cover -html .coverprofile + +check-fmt: + @! ( gofmt -s -l . | grep . ) diff --git a/fprint.go b/fprint.go index c6dabbd..21c1949 100644 --- a/fprint.go +++ b/fprint.go @@ -65,6 +65,7 @@ func nodeLen(t int, n node) node { return n } + // measure all parts: for i := range n.parts { switch pt := n.parts[i].(type) { case node: @@ -76,6 +77,7 @@ func nodeLen(t int, n node) node { } } + // measure the unwrapped length: for _, p := range n.parts { switch pt := p.(type) { case node: @@ -94,6 +96,7 @@ func nodeLen(t int, n node) node { } } + // measure the wrapped and the fully wrapped length: var w, f int for _, p := range n.parts { switch pt := p.(type) { @@ -192,6 +195,7 @@ func wrapNode(t, cf0, c0, c1 int, n node) node { return n } + // otherwise, we need to wrap the node: n.wrap = true // We assume here that an str is always contained @@ -203,6 +207,10 @@ func wrapNode(t, cf0, c0, c1 int, n node) node { return n } + // before iterating over the parts, take a copy of + // the available column width and modify only the + // copy, to support trackback. + // cc0, cc1 := c0, c1 lastWrapperIndex := -1 var trackBack bool diff --git a/sprint_test.go b/sprint_test.go index 65a6aed..9c943c9 100644 --- a/sprint_test.go +++ b/sprint_test.go @@ -1004,9 +1004,9 @@ func TestLongString(t *testing.T) { func TestCyclicReferences(t *testing.T) { t.Run("slice", func(t *testing.T) { - const expect = `r0=[]{r0}` - l := []interface{}{"foo"} - l[0] = l + const expect = `r0=[]{"foo", r0}` + l := []interface{}{"foo", "bar"} + l[1] = l s := Sprint(l) if s != expect { t.Fatalf("expected: %s, got: %s", expect, s)