Skip to content

Commit c082aaa

Browse files
author
Kazuma Arimura
authored
Merge pull request #18 from deresmos/feature/add_do_statement
Support do loop statement
2 parents a07d56c + ea04571 commit c082aaa

File tree

10 files changed

+84
-2
lines changed

10 files changed

+84
-2
lines changed

domain/vbscript.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ const (
77
VBScriptEndIfPattern = `(?i)^(\s*?)(\t*?)(End if)`
88
VBScriptForPattern = `(?i)^(\s*?)(\t*?)(For )`
99
VBScriptNextPattern = `(?i)^(\s*?)(\t*?)(Next)($|(\s+?)(\t+?))`
10+
VBScriptDoPattern = `(?i)^(\s*?)(\t*?)(Do)($|(\s+?)|(\t+?)| While | Until )`
11+
VBScriptLoopPattern = `(?i)^(\s*?)(\t*?)(Loop)($|(\s+?)|(\t+?)| While | Until )`
1012
VBScriptFunctionPattern = `(?i)^(\s*?)(\t*?)(Private |Public (Default )?)?(Function|Sub)`
1113
VBScriptEndFunctionPattern = `(?i)^(\s*?)(\t*?)(End (Function|Sub))`
1214
)

internal/process_control.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,12 @@ func isBeginNestStatement(str string) bool {
7070
fmt.Printf("err: %v", err)
7171
}
7272

73-
return isIf || isFor
73+
isDo, err := regexp.MatchString(domain.VBScriptDoPattern, str)
74+
if err != nil {
75+
fmt.Printf("err: %v", err)
76+
}
77+
78+
return isIf || isFor || isDo
7479
}
7580

7681
func isEndNestStatement(str string) bool {
@@ -84,7 +89,12 @@ func isEndNestStatement(str string) bool {
8489
fmt.Printf("err: %v", err)
8590
}
8691

87-
return isEndIf || isNext
92+
isLoop, err := regexp.MatchString(domain.VBScriptLoopPattern, str)
93+
if err != nil {
94+
fmt.Printf("err: %v", err)
95+
}
96+
97+
return isEndIf || isNext || isLoop
8898
}
8999

90100
func isIncrementStatement(str string) bool {

test/do_statements_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package test_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/st-tech/refactoring-conductor/domain"
7+
"github.com/st-tech/refactoring-conductor/internal"
8+
)
9+
10+
func TestDoStatements(t *testing.T) {
11+
tests := []struct {
12+
filename string
13+
expectedValue int
14+
}{
15+
{"testdata/do/do.vbs", 1},
16+
{"testdata/do/do_while.vbs", 1},
17+
{"testdata/do/do_until.vbs", 1},
18+
{"testdata/do/do_nest.vbs", 3},
19+
{"testdata/do/do_multi.vbs", 2},
20+
{"testdata/do/do_whitespace.vbs", 2},
21+
{"testdata/do/do_triple_nest.vbs", 6},
22+
}
23+
for _, test := range tests {
24+
vbscript := domain.VBScript{}
25+
internal.Read(test.filename, &vbscript)
26+
27+
if test.expectedValue != vbscript.CognitiveComplexity {
28+
t.Errorf("wrong cognitive complexity output: got %v, expected %v (%s)", vbscript.CognitiveComplexity, test.expectedValue, test.filename)
29+
}
30+
}
31+
}

test/testdata/do/do.vbs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Do ' +1
2+
index += 1
3+
Loop While index <= 10

test/testdata/do/do_multi.vbs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Do Until index > 10 ' +1
2+
index += 1
3+
Loop
4+
5+
Do ' +1
6+
index += 1
7+
Loop While index <= 10

test/testdata/do/do_nest.vbs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Do ' +1
2+
index += 1
3+
Do While j <= 10 ' +2
4+
j += 1
5+
Loop
6+
Loop While index <= 10
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Do ' +1
2+
index += 1
3+
Do While j <= 10 ' +2
4+
Do ' +3
5+
k += 1
6+
doTest += 1
7+
Loop Until k > 10
8+
j += 1
9+
Loop
10+
Loop While index <= 10

test/testdata/do/do_until.vbs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Do Until index > 10 ' +1
2+
index += 1
3+
Loop

test/testdata/do/do_while.vbs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Do While index <= 10 ' +1
2+
index += 1
3+
Loop

test/testdata/do/do_whitespace.vbs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Do Until index > 10 ' +1
2+
index += 1
3+
Loop ' whitespace check
4+
5+
Do ' +1
6+
index += 1
7+
Loop While index <= 10

0 commit comments

Comments
 (0)