mirror of
https://github.com/jimeh/undent.git
synced 2026-02-19 11:56:39 +00:00
Compare commits
10 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
369ec87ddd
|
|||
|
5a4b199462
|
|||
| 86be1bf614 | |||
|
6a2254e918
|
|||
|
98946bf286
|
|||
|
f0855f3a83
|
|||
| c3e2bd98b0 | |||
|
fe6ba9c1c4
|
|||
|
d1c5735041
|
|||
|
5cae4bc420
|
6
.github/workflows/ci.yml
vendored
6
.github/workflows/ci.yml
vendored
@@ -11,7 +11,7 @@ jobs:
|
|||||||
- name: golangci-lint
|
- name: golangci-lint
|
||||||
uses: golangci/golangci-lint-action@v2
|
uses: golangci/golangci-lint-action@v2
|
||||||
with:
|
with:
|
||||||
version: v1.31
|
version: v1.37
|
||||||
env:
|
env:
|
||||||
VERBOSE: "true"
|
VERBOSE: "true"
|
||||||
|
|
||||||
@@ -35,7 +35,7 @@ jobs:
|
|||||||
benchmark:
|
benchmark:
|
||||||
name: Benchmarks
|
name: Benchmarks
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
if: github.ref != 'refs/heads/master'
|
if: github.ref != 'refs/heads/main'
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v2
|
- uses: actions/checkout@v2
|
||||||
- uses: actions/setup-go@v2
|
- uses: actions/setup-go@v2
|
||||||
@@ -111,7 +111,7 @@ jobs:
|
|||||||
benchmark-store:
|
benchmark-store:
|
||||||
name: Store benchmarks
|
name: Store benchmarks
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
if: github.ref == 'refs/heads/master'
|
if: github.ref == 'refs/heads/main'
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v2
|
- uses: actions/checkout@v2
|
||||||
- uses: actions/setup-go@v2
|
- uses: actions/setup-go@v2
|
||||||
|
|||||||
@@ -2,6 +2,14 @@
|
|||||||
|
|
||||||
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
|
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
|
||||||
|
|
||||||
|
## [1.1.0](https://github.com/jimeh/undent/compare/v1.0.2...v1.1.0) (2021-02-22)
|
||||||
|
|
||||||
|
|
||||||
|
### Features
|
||||||
|
|
||||||
|
* **performance:** improve core undenting performance by around 20-30x ([6a2254e](https://github.com/jimeh/undent/commit/6a2254e918944e5ead4d5a4d6d1b95b0e971c4b7))
|
||||||
|
* **print** add Print, Printf, Fprint, and Fprintf functions ([5cae4bc](https://github.com/jimeh/undent/commit/5cae4bc420f7bec12efd6071149eac88c37cfd9f))
|
||||||
|
|
||||||
### [1.0.2](https://github.com/jimeh/undent/compare/v1.0.1...v1.0.2) (2020-12-14)
|
### [1.0.2](https://github.com/jimeh/undent/compare/v1.0.1...v1.0.2) (2020-12-14)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
25
Makefile
25
Makefile
@@ -1,3 +1,4 @@
|
|||||||
|
GOMODNAME := $(shell grep 'module' go.mod | sed -e 's/^module //')
|
||||||
SOURCES := $(shell find . -name "*.go" -or -name "go.mod" -or -name "go.sum" \
|
SOURCES := $(shell find . -name "*.go" -or -name "go.mod" -or -name "go.sum" \
|
||||||
-or -name "Makefile")
|
-or -name "Makefile")
|
||||||
|
|
||||||
@@ -18,7 +19,7 @@ SHELL ?= /bin/bash
|
|||||||
SHELL := env \
|
SHELL := env \
|
||||||
GO111MODULE=on \
|
GO111MODULE=on \
|
||||||
GOBIN=$(CURDIR)/$(TOOLDIR) \
|
GOBIN=$(CURDIR)/$(TOOLDIR) \
|
||||||
CGO_ENABLED=1 \
|
CGO_ENABLED=0 \
|
||||||
PATH='$(CURDIR)/$(BINDIR):$(CURDIR)/$(TOOLDIR):$(PATH)' \
|
PATH='$(CURDIR)/$(BINDIR):$(CURDIR)/$(TOOLDIR):$(PATH)' \
|
||||||
$(SHELL)
|
$(SHELL)
|
||||||
|
|
||||||
@@ -34,7 +35,6 @@ SHELL := env \
|
|||||||
#
|
#
|
||||||
|
|
||||||
TOOLS += $(TOOLDIR)/gobin
|
TOOLS += $(TOOLDIR)/gobin
|
||||||
gobin: $(TOOLDIR)/gobin
|
|
||||||
$(TOOLDIR)/gobin:
|
$(TOOLDIR)/gobin:
|
||||||
GO111MODULE=off go get -u github.com/myitcv/gobin
|
GO111MODULE=off go get -u github.com/myitcv/gobin
|
||||||
|
|
||||||
@@ -42,16 +42,13 @@ $(TOOLDIR)/gobin:
|
|||||||
define tool # 1: binary-name, 2: go-import-path
|
define tool # 1: binary-name, 2: go-import-path
|
||||||
TOOLS += $(TOOLDIR)/$(1)
|
TOOLS += $(TOOLDIR)/$(1)
|
||||||
|
|
||||||
.PHONY: $(1)
|
|
||||||
$(1): $(TOOLDIR)/$(1)
|
|
||||||
|
|
||||||
$(TOOLDIR)/$(1): $(TOOLDIR)/gobin Makefile
|
$(TOOLDIR)/$(1): $(TOOLDIR)/gobin Makefile
|
||||||
gobin $(V) "$(2)"
|
gobin $(V) "$(2)"
|
||||||
endef
|
endef
|
||||||
|
|
||||||
$(eval $(call tool,godoc,golang.org/x/tools/cmd/godoc))
|
$(eval $(call tool,godoc,golang.org/x/tools/cmd/godoc))
|
||||||
$(eval $(call tool,gofumports,mvdan.cc/gofumpt/gofumports))
|
$(eval $(call tool,gofumports,mvdan.cc/gofumpt/gofumports))
|
||||||
$(eval $(call tool,golangci-lint,github.com/golangci/golangci-lint/cmd/golangci-lint@v1.31))
|
$(eval $(call tool,golangci-lint,github.com/golangci/golangci-lint/cmd/golangci-lint@v1.37))
|
||||||
|
|
||||||
.PHONY: tools
|
.PHONY: tools
|
||||||
tools: $(TOOLS)
|
tools: $(TOOLS)
|
||||||
@@ -75,25 +72,18 @@ clean-golden:
|
|||||||
|
|
||||||
.PHONY: test
|
.PHONY: test
|
||||||
test:
|
test:
|
||||||
go test $(V) -count=1 -race $(TESTARGS) $(TEST)
|
CGO_ENABLED=1 go test $(V) -count=1 -race $(TESTARGS) $(TEST)
|
||||||
|
|
||||||
.PHONY: test-update-golden
|
|
||||||
test-update-golden:
|
|
||||||
@$(MAKE) test UPDATE_GOLDEN=1
|
|
||||||
|
|
||||||
.PHONY: regen-golden
|
|
||||||
regen-golden: clean-golden test-update-golden
|
|
||||||
|
|
||||||
.PHONY: test-deps
|
.PHONY: test-deps
|
||||||
test-deps:
|
test-deps:
|
||||||
go test all
|
go test all
|
||||||
|
|
||||||
.PHONY: lint
|
.PHONY: lint
|
||||||
lint: golangci-lint
|
lint: $(TOOLDIR)/golangci-lint
|
||||||
GOGC=off golangci-lint $(V) run
|
GOGC=off golangci-lint $(V) run
|
||||||
|
|
||||||
.PHONY: format
|
.PHONY: format
|
||||||
format: gofumports
|
format: $(TOOLDIR)/gofumports
|
||||||
gofumports -w .
|
gofumports -w .
|
||||||
|
|
||||||
.SILENT: bench
|
.SILENT: bench
|
||||||
@@ -161,7 +151,8 @@ check-tidy:
|
|||||||
|
|
||||||
# Serve docs
|
# Serve docs
|
||||||
.PHONY: docs
|
.PHONY: docs
|
||||||
docs: godoc
|
docs: $(TOOLDIR)/godoc
|
||||||
|
$(info serving docs on http://127.0.0.1:6060/pkg/$(GOMODNAME)/)
|
||||||
@godoc -http=127.0.0.1:6060
|
@godoc -http=127.0.0.1:6060
|
||||||
|
|
||||||
#
|
#
|
||||||
|
|||||||
@@ -4,8 +4,7 @@
|
|||||||
|
|
||||||
<p align="center">
|
<p align="center">
|
||||||
<strong>
|
<strong>
|
||||||
Go package which removes leading indentation/white-space from strings and
|
Go package which removes leading indentation/white-space from strings.
|
||||||
byte slices.
|
|
||||||
</strong>
|
</strong>
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
@@ -30,7 +29,7 @@ alt="GitHub issues">
|
|||||||
<a href="https://github.com/jimeh/undent/pulls">
|
<a href="https://github.com/jimeh/undent/pulls">
|
||||||
<img src="https://img.shields.io/github/issues-pr-raw/jimeh/undent.svg?style=flat&logo=github&logoColor=white" alt="GitHub pull requests">
|
<img src="https://img.shields.io/github/issues-pr-raw/jimeh/undent.svg?style=flat&logo=github&logoColor=white" alt="GitHub pull requests">
|
||||||
</a>
|
</a>
|
||||||
<a href="https://github.com/jimeh/undent/blob/master/LICENSE">
|
<a href="https://github.com/jimeh/undent/blob/main/LICENSE">
|
||||||
<img src="https://img.shields.io/github/license/jimeh/undent.svg?style=flat" alt="License Status">
|
<img src="https://img.shields.io/github/license/jimeh/undent.svg?style=flat" alt="License Status">
|
||||||
</a>
|
</a>
|
||||||
</p>
|
</p>
|
||||||
@@ -63,4 +62,4 @@ https://jimeh.me/undent/dev/bench/
|
|||||||
|
|
||||||
## License
|
## License
|
||||||
|
|
||||||
[MIT](https://github.com/jimeh/undent/blob/master/LICENSE)
|
[MIT](https://github.com/jimeh/undent/blob/main/LICENSE)
|
||||||
|
|||||||
5
go.mod
5
go.mod
@@ -2,4 +2,7 @@ module github.com/jimeh/undent
|
|||||||
|
|
||||||
go 1.15
|
go 1.15
|
||||||
|
|
||||||
require github.com/stretchr/testify v1.6.1
|
require (
|
||||||
|
github.com/rhysd/go-fakeio v1.0.0
|
||||||
|
github.com/stretchr/testify v1.6.1
|
||||||
|
)
|
||||||
|
|||||||
2
go.sum
2
go.sum
@@ -2,6 +2,8 @@ github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8
|
|||||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
|
github.com/rhysd/go-fakeio v1.0.0 h1:+TjiKCOs32dONY7DaoVz/VPOdvRkPfBkEyUDIpM8FQY=
|
||||||
|
github.com/rhysd/go-fakeio v1.0.0/go.mod h1:joYxF906trVwp2JLrE4jlN7A0z6wrz8O6o1UjarbFzE=
|
||||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||||
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
|
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
|
||||||
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||||
|
|||||||
141
undent.go
141
undent.go
@@ -4,15 +4,86 @@ package undent
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"regexp"
|
"io"
|
||||||
)
|
)
|
||||||
|
|
||||||
var matcher = regexp.MustCompile(`(?m)^([ \t]*)(?:\S)`)
|
const (
|
||||||
|
tab = 9
|
||||||
|
lf = 10
|
||||||
|
spc = 32
|
||||||
|
)
|
||||||
|
|
||||||
// Bytes removes leading indentation/white-space from given string and returns
|
// Bytes removes leading indentation/white-space from given string and returns
|
||||||
// it as a byte slice.
|
// it as a byte slice.
|
||||||
func Bytes(s string) []byte {
|
func Bytes(s string) []byte {
|
||||||
return []byte(String(s))
|
if len(s) == 0 {
|
||||||
|
return []byte{}
|
||||||
|
}
|
||||||
|
|
||||||
|
// find line feeds
|
||||||
|
lfs := []int{}
|
||||||
|
|
||||||
|
if s[0] != lf {
|
||||||
|
lfs = append(lfs, -1)
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := 0; i < len(s); i++ {
|
||||||
|
if s[i] == lf {
|
||||||
|
lfs = append(lfs, i)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// find smallest indent relative to each line-feed
|
||||||
|
min := 99999999999
|
||||||
|
count := 0
|
||||||
|
|
||||||
|
for i := 0; i < len(lfs); i++ {
|
||||||
|
offset := lfs[i]
|
||||||
|
end := len(s) - 1
|
||||||
|
if i+1 < len(lfs) {
|
||||||
|
end = lfs[i+1]
|
||||||
|
}
|
||||||
|
|
||||||
|
if offset+1 >= end {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
indent := 0
|
||||||
|
lineSeek:
|
||||||
|
for n := offset + 1; n < end && indent < min; n++ {
|
||||||
|
switch s[n] {
|
||||||
|
case spc, tab:
|
||||||
|
indent++
|
||||||
|
default:
|
||||||
|
break lineSeek
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if indent < min {
|
||||||
|
min = indent
|
||||||
|
}
|
||||||
|
if indent > 0 {
|
||||||
|
count++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// extract each line without indentation
|
||||||
|
out := make([]byte, 0, len(s)-(min*count))
|
||||||
|
|
||||||
|
for i := 0; i < len(lfs); i++ {
|
||||||
|
offset := lfs[i] + 1
|
||||||
|
end := len(s)
|
||||||
|
if i+1 < len(lfs) {
|
||||||
|
end = lfs[i+1] + 1
|
||||||
|
}
|
||||||
|
|
||||||
|
if offset+min < end {
|
||||||
|
out = append(out, s[offset+min:end]...)
|
||||||
|
} else if offset < end {
|
||||||
|
out = append(out, s[offset:end]...)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return out
|
||||||
}
|
}
|
||||||
|
|
||||||
// Bytesf removes leading indentation/white-space from given format string
|
// Bytesf removes leading indentation/white-space from given format string
|
||||||
@@ -24,34 +95,7 @@ func Bytesf(format string, a ...interface{}) []byte {
|
|||||||
|
|
||||||
// String removes leading indentation/white-space from given string.
|
// String removes leading indentation/white-space from given string.
|
||||||
func String(s string) string {
|
func String(s string) string {
|
||||||
if len(s) > 0 && s[0] == '\n' {
|
return string(Bytes(s))
|
||||||
s = s[1:]
|
|
||||||
}
|
|
||||||
|
|
||||||
matches := matcher.FindAllString(s, -1)
|
|
||||||
if len(matches) == 0 {
|
|
||||||
return s
|
|
||||||
}
|
|
||||||
|
|
||||||
index := 0
|
|
||||||
length := len(matches[0])
|
|
||||||
|
|
||||||
for i, s := range matches[1:] {
|
|
||||||
l := len(s)
|
|
||||||
if l < length {
|
|
||||||
index = i + 1
|
|
||||||
length = l
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if length <= 1 {
|
|
||||||
return s
|
|
||||||
}
|
|
||||||
indent := matches[index][0 : length-1]
|
|
||||||
|
|
||||||
return regexp.MustCompile(
|
|
||||||
`(?m)^`+regexp.QuoteMeta(indent),
|
|
||||||
).ReplaceAllLiteralString(s, "")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Stringf removes leading indentation/white-space from given format string
|
// Stringf removes leading indentation/white-space from given format string
|
||||||
@@ -60,3 +104,38 @@ func String(s string) string {
|
|||||||
func Stringf(format string, a ...interface{}) string {
|
func Stringf(format string, a ...interface{}) string {
|
||||||
return fmt.Sprintf(String(format), a...)
|
return fmt.Sprintf(String(format), a...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Print will undent any string arguments before passing them to fmt.Print.
|
||||||
|
func Print(a ...interface{}) (n int, err error) {
|
||||||
|
return fmt.Print(undentInterfaces(a)...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Printf will undent the given format string before passing it and all
|
||||||
|
// arguments to fmt.Printf.
|
||||||
|
func Printf(format string, a ...interface{}) (n int, err error) {
|
||||||
|
return fmt.Printf(String(format), a...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fprint will undent any string arguments before passing them to fmt.Fprint.
|
||||||
|
func Fprint(w io.Writer, a ...interface{}) (n int, err error) {
|
||||||
|
return fmt.Fprint(w, undentInterfaces(a)...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fprintf will undent the given format string before passing it and all
|
||||||
|
// arguments to fmt.Fprintf.
|
||||||
|
func Fprintf(w io.Writer, format string, a ...interface{}) (n int, err error) {
|
||||||
|
return fmt.Fprintf(w, String(format), a...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func undentInterfaces(a []interface{}) []interface{} {
|
||||||
|
var r []interface{}
|
||||||
|
|
||||||
|
for _, v := range a {
|
||||||
|
if s, ok := v.(string); ok {
|
||||||
|
v = String(s)
|
||||||
|
}
|
||||||
|
r = append(r, v)
|
||||||
|
}
|
||||||
|
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package undent_test
|
package undent_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/jimeh/undent"
|
"github.com/jimeh/undent"
|
||||||
@@ -60,3 +61,57 @@ func ExampleStringf() {
|
|||||||
// "hello": "world"
|
// "hello": "world"
|
||||||
// }
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ExamplePrint() {
|
||||||
|
undent.Print(`
|
||||||
|
{
|
||||||
|
"hello": "world"
|
||||||
|
}`,
|
||||||
|
)
|
||||||
|
// Output:
|
||||||
|
// {
|
||||||
|
// "hello": "world"
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExamplePrintf() {
|
||||||
|
undent.Printf(`
|
||||||
|
{
|
||||||
|
"hello": "%s"
|
||||||
|
}`,
|
||||||
|
"world",
|
||||||
|
)
|
||||||
|
// Output:
|
||||||
|
// {
|
||||||
|
// "hello": "world"
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExampleFprint() {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
undent.Fprint(&buf, `
|
||||||
|
{
|
||||||
|
"hello": "world"
|
||||||
|
}`,
|
||||||
|
)
|
||||||
|
fmt.Println(buf.String())
|
||||||
|
// Output:
|
||||||
|
// {
|
||||||
|
// "hello": "world"
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExampleFprintf() {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
undent.Fprintf(&buf, `
|
||||||
|
{
|
||||||
|
"hello": "%s"
|
||||||
|
}`,
|
||||||
|
"world",
|
||||||
|
)
|
||||||
|
fmt.Println(buf.String())
|
||||||
|
// Output:
|
||||||
|
// {
|
||||||
|
// "hello": "world"
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
|||||||
328
undent_test.go
328
undent_test.go
@@ -1,9 +1,12 @@
|
|||||||
package undent
|
package undent
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/rhysd/go-fakeio"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
var stringTestCases = []struct {
|
var stringTestCases = []struct {
|
||||||
@@ -56,6 +59,21 @@ var stringTestCases = []struct {
|
|||||||
"foo": [
|
"foo": [
|
||||||
"bar"
|
"bar"
|
||||||
]
|
]
|
||||||
|
}`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "multi-line space indented without any leading line-breaks",
|
||||||
|
s: ` {
|
||||||
|
"hello": "world",
|
||||||
|
"foo": [
|
||||||
|
"bar"
|
||||||
|
]
|
||||||
|
}`,
|
||||||
|
want: `{
|
||||||
|
"hello": "world",
|
||||||
|
"foo": [
|
||||||
|
"bar"
|
||||||
|
]
|
||||||
}`,
|
}`,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -95,7 +113,22 @@ var stringTestCases = []struct {
|
|||||||
}`,
|
}`,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "multi-line tab indented with leading line breaks",
|
name: "multi-line tab indented without any leading line-breaks",
|
||||||
|
s: ` {
|
||||||
|
"hello": "world",
|
||||||
|
"foo": [
|
||||||
|
"bar"
|
||||||
|
]
|
||||||
|
}`,
|
||||||
|
want: `{
|
||||||
|
"hello": "world",
|
||||||
|
"foo": [
|
||||||
|
"bar"
|
||||||
|
]
|
||||||
|
}`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "multi-line tab indented with leading line-breaks",
|
||||||
s: `
|
s: `
|
||||||
|
|
||||||
|
|
||||||
@@ -194,6 +227,108 @@ world
|
|||||||
foo
|
foo
|
||||||
bar`,
|
bar`,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "long block of text",
|
||||||
|
s: `
|
||||||
|
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc
|
||||||
|
ornare, tellus vel luctus tristique, ipsum ante varius mauris, non
|
||||||
|
hendrerit tellus urna quis ex. Donec efficitur arcu sed iaculis
|
||||||
|
lobortis. Phasellus facilisis vitae mi quis accumsan. Suspendisse
|
||||||
|
rhoncus viverra odio ultricies rhoncus. Cras laoreet tortor
|
||||||
|
vestibulum pharetra luctus. Vivamus sit amet volutpat elit.
|
||||||
|
Suspendisse feugiat lectus id arcu sollicitudin tincidunt. Duis ut
|
||||||
|
sem auctor orci sodales varius in ac odio. Nullam finibus odio at
|
||||||
|
lacus tristique malesuada.
|
||||||
|
|
||||||
|
Morbi nisl nulla, euismod eu enim in, tincidunt varius turpis. Morbi
|
||||||
|
ullamcorper tortor mi, ut aliquam metus posuere vel. Etiam vel dui
|
||||||
|
at quam placerat sollicitudin. Proin aliquam justo vitae mauris
|
||||||
|
gravida porta. Praesent hendrerit egestas ligula, faucibus tincidunt
|
||||||
|
tortor aliquet at. Ut luctus vehicula arcu eget cursus. Suspendisse
|
||||||
|
eget enim mollis, condimentum lacus eu, viverra nulla. Aenean vel
|
||||||
|
sapien eget enim convallis accumsan. Donec dictum ullamcorper leo
|
||||||
|
placerat sollicitudin. Pellentesque habitant morbi tristique
|
||||||
|
senectus et netus et malesuada fames ac turpis egestas. Vestibulum
|
||||||
|
volutpat mattis est, a feugiat purus feugiat at. Quisque at velit ut
|
||||||
|
mauris convallis sodales a et erat. Mauris condimentum augue sit
|
||||||
|
amet arcu sodales, aliquet ultrices ipsum tempor. Donec scelerisque
|
||||||
|
mi ligula, vel volutpat velit posuere a. Sed faucibus dui pulvinar
|
||||||
|
lorem commodo egestas. In facilisis suscipit lacus non suscipit.
|
||||||
|
|
||||||
|
Nunc dictum est nulla, a rhoncus mi posuere id. Morbi at tempus
|
||||||
|
augue. Quisque ac nibh auctor velit auctor placerat id non eros.
|
||||||
|
Nulla condimentum quam id risus suscipit, ut fermentum mauris
|
||||||
|
lacinia. Vestibulum suscipit rutrum ex, sed vulputate nisi tempus
|
||||||
|
ultricies. Sed id ante pretium, accumsan sapien eget, malesuada
|
||||||
|
quam. Phasellus quis commodo enim. Vivamus in purus ac lorem ornare
|
||||||
|
posuere non eu quam. Sed arcu tortor, gravida quis fringilla nec,
|
||||||
|
ultricies et sem. Pellentesque arcu enim, tempor id nisl at, rhoncus
|
||||||
|
efficitur sem. Ut quis placerat quam. Donec maximus a risus sed
|
||||||
|
posuere. Curabitur pretium a diam non aliquet.
|
||||||
|
|
||||||
|
Class aptent taciti sociosqu ad litora torquent per conubia nostra,
|
||||||
|
per inceptos himenaeos. Morbi volutpat felis leo, vel ultrices orci
|
||||||
|
bibendum ac. Integer eu mattis urna. Donec dictum vehicula
|
||||||
|
fermentum. Pellentesque a rutrum ipsum. Donec ultricies elit purus,
|
||||||
|
eget viverra tellus tristique sed. In hac habitasse platea dictumst.
|
||||||
|
Duis elementum semper elit, sit amet rhoncus lacus dictum ac. Nunc
|
||||||
|
pretium enim ac urna efficitur, eget facilisis enim interdum.
|
||||||
|
|
||||||
|
Vivamus vel lectus lacus. Praesent vestibulum vel ligula eget
|
||||||
|
cursus. Quisque eu dignissim erat. Quisque maximus arcu eu turpis
|
||||||
|
pulvinar egestas. Nam aliquet neque sed tellus finibus mollis.
|
||||||
|
Phasellus consequat nibh nec ornare egestas. Donec at pellentesque
|
||||||
|
lorem.`,
|
||||||
|
want: `Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc
|
||||||
|
ornare, tellus vel luctus tristique, ipsum ante varius mauris, non
|
||||||
|
hendrerit tellus urna quis ex. Donec efficitur arcu sed iaculis
|
||||||
|
lobortis. Phasellus facilisis vitae mi quis accumsan. Suspendisse
|
||||||
|
rhoncus viverra odio ultricies rhoncus. Cras laoreet tortor
|
||||||
|
vestibulum pharetra luctus. Vivamus sit amet volutpat elit.
|
||||||
|
Suspendisse feugiat lectus id arcu sollicitudin tincidunt. Duis ut
|
||||||
|
sem auctor orci sodales varius in ac odio. Nullam finibus odio at
|
||||||
|
lacus tristique malesuada.
|
||||||
|
|
||||||
|
Morbi nisl nulla, euismod eu enim in, tincidunt varius turpis. Morbi
|
||||||
|
ullamcorper tortor mi, ut aliquam metus posuere vel. Etiam vel dui
|
||||||
|
at quam placerat sollicitudin. Proin aliquam justo vitae mauris
|
||||||
|
gravida porta. Praesent hendrerit egestas ligula, faucibus tincidunt
|
||||||
|
tortor aliquet at. Ut luctus vehicula arcu eget cursus. Suspendisse
|
||||||
|
eget enim mollis, condimentum lacus eu, viverra nulla. Aenean vel
|
||||||
|
sapien eget enim convallis accumsan. Donec dictum ullamcorper leo
|
||||||
|
placerat sollicitudin. Pellentesque habitant morbi tristique
|
||||||
|
senectus et netus et malesuada fames ac turpis egestas. Vestibulum
|
||||||
|
volutpat mattis est, a feugiat purus feugiat at. Quisque at velit ut
|
||||||
|
mauris convallis sodales a et erat. Mauris condimentum augue sit
|
||||||
|
amet arcu sodales, aliquet ultrices ipsum tempor. Donec scelerisque
|
||||||
|
mi ligula, vel volutpat velit posuere a. Sed faucibus dui pulvinar
|
||||||
|
lorem commodo egestas. In facilisis suscipit lacus non suscipit.
|
||||||
|
|
||||||
|
Nunc dictum est nulla, a rhoncus mi posuere id. Morbi at tempus
|
||||||
|
augue. Quisque ac nibh auctor velit auctor placerat id non eros.
|
||||||
|
Nulla condimentum quam id risus suscipit, ut fermentum mauris
|
||||||
|
lacinia. Vestibulum suscipit rutrum ex, sed vulputate nisi tempus
|
||||||
|
ultricies. Sed id ante pretium, accumsan sapien eget, malesuada
|
||||||
|
quam. Phasellus quis commodo enim. Vivamus in purus ac lorem ornare
|
||||||
|
posuere non eu quam. Sed arcu tortor, gravida quis fringilla nec,
|
||||||
|
ultricies et sem. Pellentesque arcu enim, tempor id nisl at, rhoncus
|
||||||
|
efficitur sem. Ut quis placerat quam. Donec maximus a risus sed
|
||||||
|
posuere. Curabitur pretium a diam non aliquet.
|
||||||
|
|
||||||
|
Class aptent taciti sociosqu ad litora torquent per conubia nostra,
|
||||||
|
per inceptos himenaeos. Morbi volutpat felis leo, vel ultrices orci
|
||||||
|
bibendum ac. Integer eu mattis urna. Donec dictum vehicula
|
||||||
|
fermentum. Pellentesque a rutrum ipsum. Donec ultricies elit purus,
|
||||||
|
eget viverra tellus tristique sed. In hac habitasse platea dictumst.
|
||||||
|
Duis elementum semper elit, sit amet rhoncus lacus dictum ac. Nunc
|
||||||
|
pretium enim ac urna efficitur, eget facilisis enim interdum.
|
||||||
|
|
||||||
|
Vivamus vel lectus lacus. Praesent vestibulum vel ligula eget
|
||||||
|
cursus. Quisque eu dignissim erat. Quisque maximus arcu eu turpis
|
||||||
|
pulvinar egestas. Nam aliquet neque sed tellus finibus mollis.
|
||||||
|
Phasellus consequat nibh nec ornare egestas. Donec at pellentesque
|
||||||
|
lorem.`,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
var stringfTestCases = []struct {
|
var stringfTestCases = []struct {
|
||||||
@@ -251,6 +386,22 @@ var stringfTestCases = []struct {
|
|||||||
"foo": [
|
"foo": [
|
||||||
42
|
42
|
||||||
]
|
]
|
||||||
|
}`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "multi-line space indented without any leading line-breaks",
|
||||||
|
s: ` {
|
||||||
|
"hello": "%s",
|
||||||
|
"foo": [
|
||||||
|
%d
|
||||||
|
]
|
||||||
|
}`,
|
||||||
|
a: []interface{}{"world", 42},
|
||||||
|
want: `{
|
||||||
|
"hello": "world",
|
||||||
|
"foo": [
|
||||||
|
42
|
||||||
|
]
|
||||||
}`,
|
}`,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -289,6 +440,22 @@ var stringfTestCases = []struct {
|
|||||||
"foo": [
|
"foo": [
|
||||||
42
|
42
|
||||||
]
|
]
|
||||||
|
}`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "multi-line tab indented without any leading line-breaks",
|
||||||
|
s: ` {
|
||||||
|
"hello": "%s",
|
||||||
|
"foo": [
|
||||||
|
%d
|
||||||
|
]
|
||||||
|
}`,
|
||||||
|
a: []interface{}{"world", 42},
|
||||||
|
want: `{
|
||||||
|
"hello": "world",
|
||||||
|
"foo": [
|
||||||
|
42
|
||||||
|
]
|
||||||
}`,
|
}`,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -397,6 +564,109 @@ world
|
|||||||
foo
|
foo
|
||||||
42`,
|
42`,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "long block of text",
|
||||||
|
s: `
|
||||||
|
Lorem %s dolor sit amet, consectetur adipiscing elit. Nunc
|
||||||
|
ornare, tellus vel luctus tristique, ipsum ante varius mauris, non
|
||||||
|
hendrerit tellus urna quis ex. %s efficitur arcu sed iaculis
|
||||||
|
lobortis. Phasellus facilisis vitae mi quis accumsan. Suspendisse
|
||||||
|
rhoncus viverra odio ultricies rhoncus. Cras laoreet tortor
|
||||||
|
vestibulum pharetra luctus. Vivamus sit amet volutpat elit.
|
||||||
|
Suspendisse feugiat lectus id arcu sollicitudin tincidunt. Duis ut
|
||||||
|
sem auctor orci sodales varius in ac odio. Nullam finibus odio at
|
||||||
|
lacus tristique malesuada.
|
||||||
|
|
||||||
|
Morbi nisl nulla, euismod eu enim in, tincidunt varius turpis. Morbi
|
||||||
|
ullamcorper tortor mi, ut aliquam metus posuere vel. Etiam vel dui
|
||||||
|
at quam placerat sollicitudin. Proin aliquam justo vitae mauris
|
||||||
|
gravida porta. Praesent hendrerit egestas ligula, faucibus tincidunt
|
||||||
|
tortor aliquet at. Ut luctus vehicula arcu eget cursus. Suspendisse
|
||||||
|
eget enim mollis, condimentum lacus eu, viverra nulla. Aenean vel
|
||||||
|
sapien eget enim convallis accumsan. Donec dictum ullamcorper leo
|
||||||
|
placerat sollicitudin. Pellentesque habitant morbi tristique
|
||||||
|
senectus et netus et malesuada fames ac turpis egestas. Vestibulum
|
||||||
|
volutpat mattis est, a feugiat purus feugiat at. Quisque at velit ut
|
||||||
|
mauris convallis sodales a et erat. Mauris condimentum augue sit
|
||||||
|
amet arcu sodales, aliquet ultrices ipsum tempor. Donec scelerisque
|
||||||
|
mi ligula, vel volutpat velit posuere a. Sed faucibus dui pulvinar
|
||||||
|
lorem commodo egestas. In facilisis suscipit lacus non suscipit.
|
||||||
|
|
||||||
|
Nunc dictum est nulla, a rhoncus mi posuere id. Morbi at tempus
|
||||||
|
augue. Quisque ac nibh auctor velit auctor placerat id non eros.
|
||||||
|
Nulla condimentum quam id risus suscipit, ut fermentum mauris
|
||||||
|
lacinia. Vestibulum suscipit rutrum ex, sed vulputate nisi tempus
|
||||||
|
ultricies. Sed id ante pretium, accumsan sapien eget, malesuada
|
||||||
|
quam. Phasellus quis commodo enim. Vivamus in purus ac lorem ornare
|
||||||
|
posuere non eu quam. Sed arcu tortor, gravida quis fringilla nec,
|
||||||
|
ultricies et sem. Pellentesque arcu enim, tempor id nisl at, rhoncus
|
||||||
|
efficitur sem. Ut quis placerat quam. Donec maximus a risus sed
|
||||||
|
posuere. Curabitur pretium a diam non aliquet.
|
||||||
|
|
||||||
|
Class aptent taciti sociosqu ad litora torquent per conubia nostra,
|
||||||
|
per inceptos himenaeos. Morbi volutpat felis leo, vel ultrices orci
|
||||||
|
bibendum ac. Integer eu mattis urna. Donec dictum vehicula
|
||||||
|
fermentum. Pellentesque a rutrum ipsum. Donec ultricies elit purus,
|
||||||
|
eget viverra tellus tristique sed. In hac habitasse platea dictumst.
|
||||||
|
Duis elementum semper elit, sit amet rhoncus lacus dictum ac. Nunc
|
||||||
|
pretium enim ac urna efficitur, eget facilisis enim interdum.
|
||||||
|
|
||||||
|
Vivamus vel lectus lacus. Praesent vestibulum vel ligula eget
|
||||||
|
cursus. Quisque eu dignissim erat. Quisque maximus arcu eu turpis
|
||||||
|
pulvinar egestas. Nam aliquet neque sed tellus finibus mollis.
|
||||||
|
Phasellus consequat nibh nec ornare egestas. Donec at pellentesque
|
||||||
|
lorem.`,
|
||||||
|
a: []interface{}{"ipsum", "Donec"},
|
||||||
|
want: `Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc
|
||||||
|
ornare, tellus vel luctus tristique, ipsum ante varius mauris, non
|
||||||
|
hendrerit tellus urna quis ex. Donec efficitur arcu sed iaculis
|
||||||
|
lobortis. Phasellus facilisis vitae mi quis accumsan. Suspendisse
|
||||||
|
rhoncus viverra odio ultricies rhoncus. Cras laoreet tortor
|
||||||
|
vestibulum pharetra luctus. Vivamus sit amet volutpat elit.
|
||||||
|
Suspendisse feugiat lectus id arcu sollicitudin tincidunt. Duis ut
|
||||||
|
sem auctor orci sodales varius in ac odio. Nullam finibus odio at
|
||||||
|
lacus tristique malesuada.
|
||||||
|
|
||||||
|
Morbi nisl nulla, euismod eu enim in, tincidunt varius turpis. Morbi
|
||||||
|
ullamcorper tortor mi, ut aliquam metus posuere vel. Etiam vel dui
|
||||||
|
at quam placerat sollicitudin. Proin aliquam justo vitae mauris
|
||||||
|
gravida porta. Praesent hendrerit egestas ligula, faucibus tincidunt
|
||||||
|
tortor aliquet at. Ut luctus vehicula arcu eget cursus. Suspendisse
|
||||||
|
eget enim mollis, condimentum lacus eu, viverra nulla. Aenean vel
|
||||||
|
sapien eget enim convallis accumsan. Donec dictum ullamcorper leo
|
||||||
|
placerat sollicitudin. Pellentesque habitant morbi tristique
|
||||||
|
senectus et netus et malesuada fames ac turpis egestas. Vestibulum
|
||||||
|
volutpat mattis est, a feugiat purus feugiat at. Quisque at velit ut
|
||||||
|
mauris convallis sodales a et erat. Mauris condimentum augue sit
|
||||||
|
amet arcu sodales, aliquet ultrices ipsum tempor. Donec scelerisque
|
||||||
|
mi ligula, vel volutpat velit posuere a. Sed faucibus dui pulvinar
|
||||||
|
lorem commodo egestas. In facilisis suscipit lacus non suscipit.
|
||||||
|
|
||||||
|
Nunc dictum est nulla, a rhoncus mi posuere id. Morbi at tempus
|
||||||
|
augue. Quisque ac nibh auctor velit auctor placerat id non eros.
|
||||||
|
Nulla condimentum quam id risus suscipit, ut fermentum mauris
|
||||||
|
lacinia. Vestibulum suscipit rutrum ex, sed vulputate nisi tempus
|
||||||
|
ultricies. Sed id ante pretium, accumsan sapien eget, malesuada
|
||||||
|
quam. Phasellus quis commodo enim. Vivamus in purus ac lorem ornare
|
||||||
|
posuere non eu quam. Sed arcu tortor, gravida quis fringilla nec,
|
||||||
|
ultricies et sem. Pellentesque arcu enim, tempor id nisl at, rhoncus
|
||||||
|
efficitur sem. Ut quis placerat quam. Donec maximus a risus sed
|
||||||
|
posuere. Curabitur pretium a diam non aliquet.
|
||||||
|
|
||||||
|
Class aptent taciti sociosqu ad litora torquent per conubia nostra,
|
||||||
|
per inceptos himenaeos. Morbi volutpat felis leo, vel ultrices orci
|
||||||
|
bibendum ac. Integer eu mattis urna. Donec dictum vehicula
|
||||||
|
fermentum. Pellentesque a rutrum ipsum. Donec ultricies elit purus,
|
||||||
|
eget viverra tellus tristique sed. In hac habitasse platea dictumst.
|
||||||
|
Duis elementum semper elit, sit amet rhoncus lacus dictum ac. Nunc
|
||||||
|
pretium enim ac urna efficitur, eget facilisis enim interdum.
|
||||||
|
|
||||||
|
Vivamus vel lectus lacus. Praesent vestibulum vel ligula eget
|
||||||
|
cursus. Quisque eu dignissim erat. Quisque maximus arcu eu turpis
|
||||||
|
pulvinar egestas. Nam aliquet neque sed tellus finibus mollis.
|
||||||
|
Phasellus consequat nibh nec ornare egestas. Donec at pellentesque
|
||||||
|
lorem.`,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestBytes(t *testing.T) {
|
func TestBytes(t *testing.T) {
|
||||||
@@ -443,6 +713,62 @@ func TestStringf(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestPrint(t *testing.T) {
|
||||||
|
for _, tt := range stringTestCases {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
got, err := fakeio.Stdout().Do(func() {
|
||||||
|
Print(tt.s, 5, tt.s)
|
||||||
|
})
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
assert.IsType(t, "", got)
|
||||||
|
assert.Equal(t, tt.want+"5"+tt.want, got)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPrintf(t *testing.T) {
|
||||||
|
for _, tt := range stringfTestCases {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
got, err := fakeio.Stdout().Do(func() {
|
||||||
|
Printf(tt.s, tt.a...)
|
||||||
|
})
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
assert.IsType(t, "", got)
|
||||||
|
assert.Equal(t, tt.want, got)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFprint(t *testing.T) {
|
||||||
|
for _, tt := range stringTestCases {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
|
||||||
|
Fprint(&buf, tt.s, 5, tt.s)
|
||||||
|
got := buf.String()
|
||||||
|
|
||||||
|
assert.IsType(t, "", got)
|
||||||
|
assert.Equal(t, tt.want+"5"+tt.want, got)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFprintf(t *testing.T) {
|
||||||
|
for _, tt := range stringfTestCases {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
|
||||||
|
Fprintf(&buf, tt.s, tt.a...)
|
||||||
|
got := buf.String()
|
||||||
|
|
||||||
|
assert.IsType(t, "", got)
|
||||||
|
assert.Equal(t, tt.want, got)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func BenchmarkBytes(b *testing.B) {
|
func BenchmarkBytes(b *testing.B) {
|
||||||
for _, tt := range stringTestCases {
|
for _, tt := range stringTestCases {
|
||||||
b.Run(tt.name, func(b *testing.B) {
|
b.Run(tt.name, func(b *testing.B) {
|
||||||
|
|||||||
Reference in New Issue
Block a user