Skip to content

Commit

Permalink
fix: bad indentation (#7)
Browse files Browse the repository at this point in the history
* fix: bad indentation
  • Loading branch information
ilaif committed Sep 17, 2022
1 parent 8f3f213 commit f6291ed
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 10 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
name: ci
on:
push:
branches: [main]
pull_request:
permissions:
contents: read
Expand Down
18 changes: 14 additions & 4 deletions pkg/blocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,23 @@ func (b *Block) SetLines(lines []string) {

// padLines add a base indentation to match the one in this block (according to the first line)
func (b *Block) padLines(lines []string) []string {
paddedLines := make([]string, len(lines))
indent := 0
ourIndent := 0
if len(b.Lines) > 0 {
indent = utils.CountLeadingSpaces(b.Lines[0])
ourIndent = utils.CountLeadingSpaces(b.Lines[0])
}
theirIndent := 0
if len(lines) > 0 {
theirIndent = utils.CountLeadingSpaces(lines[0])
}
indentAddition := ourIndent - theirIndent

paddedLines := make([]string, len(lines))
for i, l := range lines {
paddedLines[i] = strings.Repeat(" ", indent) + l
if indentAddition > 0 {
paddedLines[i] = strings.Repeat(" ", indentAddition) + l
} else {
paddedLines[i] = l[-indentAddition:]
}
}

return paddedLines
Expand Down
83 changes: 79 additions & 4 deletions pkg/blocks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import (
"testing"

"github.com/stretchr/testify/assert"

"github.com/ilaif/goplicate/pkg/utils"
)

func TestParseBlocks(t *testing.T) {
assert := assert.New(t)
func TestParseBlocksFromFile(t *testing.T) {
a := assert.New(t)

tests := []struct {
file string
Expand Down Expand Up @@ -75,9 +77,82 @@ func TestParseBlocks(t *testing.T) {
for _, test := range tests {
t.Run(test.file, func(t *testing.T) {
blocks, err := parseBlocksFromFile(test.file, nil)
assert.NoError(err)
a.NoError(err)

assert.Equal(test.expectedBlocks, blocks)
a.Equal(test.expectedBlocks, blocks)
})
}
}

func TestBlocksPadding(t *testing.T) {
a := assert.New(t)

tests := []struct {
targetBlock Block
sourceBlock Block
}{
{
targetBlock: Block{
Name: "common",
Lines: []string{
" # goplicate-start:common",
" value",
" # goplicate-end:common",
},
},
sourceBlock: Block{
Name: "common",
Lines: []string{
" # goplicate-start:common",
" value",
" # goplicate-end:common",
},
},
},
{
targetBlock: Block{
Name: "common",
Lines: []string{
" # goplicate-start:common",
" value",
" # goplicate-end:common",
},
},
sourceBlock: Block{
Name: "common",
Lines: []string{
" # goplicate-start:common",
" value",
" # goplicate-end:common",
},
},
},
{
targetBlock: Block{
Name: "common",
Lines: []string{
" # goplicate-start:common",
" value",
" # goplicate-end:common",
},
},
sourceBlock: Block{
Name: "common",
Lines: []string{
" # goplicate-start:common",
" value",
" # goplicate-end:common",
},
},
},
}

for _, test := range tests {
lines := test.targetBlock.padLines(test.sourceBlock.Lines)
expectedLinePadding := utils.CountLeadingSpaces(test.targetBlock.Lines[0])
for _, line := range lines {
actualLinePadding := utils.CountLeadingSpaces(line)
a.Equal(expectedLinePadding, actualLinePadding)
}
}
}

0 comments on commit f6291ed

Please sign in to comment.