Skip to content

Commit

Permalink
fix(DTSS-1817): Address dangling Block/EndBlock statements with speci…
Browse files Browse the repository at this point in the history
…fic errors (#117)

* fix(DTSS-1817): Address dangling Block/EndBlock statements with specific errors

* pr feedback

Co-authored-by: David de Regt <[email protected]>
  • Loading branch information
deregtd and deregtd authored Jul 6, 2022
1 parent 5d2b8c6 commit fa99afe
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 6 deletions.
17 changes: 11 additions & 6 deletions internal/codegen/blocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,22 +46,23 @@ func parseBlocks(filePath string) (map[string]string, error) {
case "Block":
blockName := matches[3]
if curBlockName != "" {
return nil, fmt.Errorf("invalid Block when already inside of a block, at %s:%d", filePath, i)
return nil, fmt.Errorf("invalid Block when already inside of a block, at %s:%d", filePath, i+1)
}
curBlockName = blockName
case "EndBlock":
blockName := matches[3]

if curBlockName == "" {
return nil, fmt.Errorf("invalid EndBlock when not inside of a block, at %s:%d", filePath, i+1)
}

if blockName != curBlockName {
return nil, fmt.Errorf(
"invalid EndBlock, found EndBlock with name %q while inside of block with name %q, at %s:%d",
blockName, curBlockName, filePath, i,
blockName, curBlockName, filePath, i+1,
)
}

if curBlockName == "" {
return nil, fmt.Errorf("invalid EndBlock when not inside of a block, at %s:%d", filePath, i)
}

curBlockName = ""
default:
isCommand = false
Expand All @@ -86,5 +87,9 @@ func parseBlocks(filePath string) (map[string]string, error) {
}
}

if curBlockName != "" {
return nil, fmt.Errorf("found dangling Block (%s) in %s", curBlockName, filePath)
}

return args, nil
}
26 changes: 26 additions & 0 deletions internal/codegen/blocks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,29 @@ func TestParseBlocks(t *testing.T) {
assert.NilError(t, err, "expected parseBlocks() not to fail")
assert.Equal(t, blocks["helloWorld"], "Hello, world!", "expected parseBlocks() to parse basic block")
}

func TestDanglingBlock(t *testing.T) {
_, err := parseBlocks("testdata/danglingblock-test.txt")
assert.Error(t, err, "found dangling Block (dangles) in testdata/danglingblock-test.txt", "expected parseBlocks() to fail")
}

func TestDanglingEndBlock(t *testing.T) {
_, err := parseBlocks("testdata/danglingendblock-test.txt")
assert.Error(t, err,
"invalid EndBlock when not inside of a block, at testdata/danglingendblock-test.txt:8",
"expected parseBlocks() to fail")
}

func TestBlockInsideBlock(t *testing.T) {
_, err := parseBlocks("testdata/blockinsideblock-test.txt")
assert.Error(t, err,
"invalid Block when already inside of a block, at testdata/blockinsideblock-test.txt:3",
"expected parseBlocks() to fail")
}

func TestWrongEndBlock(t *testing.T) {
_, err := parseBlocks("testdata/wrongendblock-test.txt")
assert.Error(t, err,
"invalid EndBlock, found EndBlock with name \"wrongend\" while inside of block with name \"helloWorld\", at testdata/wrongendblock-test.txt:3", //nolint:lll
"expected parseBlocks() to fail")
}
6 changes: 6 additions & 0 deletions internal/codegen/testdata/blockinsideblock-test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
###Block(helloWorld)
a
###Block(boompls)
b
c
###EndBlock(helloWorld)
8 changes: 8 additions & 0 deletions internal/codegen/testdata/danglingblock-test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
###Block(helloWorld)
Hello, world!
###EndBlock(helloWorld)

###Block(dangles)
dsffdsfsd
fdsfds
sd
8 changes: 8 additions & 0 deletions internal/codegen/testdata/danglingendblock-test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
###Block(helloWorld)
Hello, world!
###EndBlock(helloWorld)

dsffdsfsd
fdsfds
sd
###EndBlock(dangles)
3 changes: 3 additions & 0 deletions internal/codegen/testdata/wrongendblock-test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
###Block(helloWorld)
Hello, world!
###EndBlock(wrongend)

0 comments on commit fa99afe

Please sign in to comment.