Skip to content

Commit a07d56c

Browse files
author
Kazuma Arimura
authored
Merge pull request #17 from pakio/feature/support-private
Support Public/Public Default/Private Function And Sub Procedure
2 parents 68801ad + 47a637f commit a07d56c

13 files changed

+112
-46
lines changed

domain/vbscript.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +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-
VBScriptFunctionPattern = `(?i)^(\s*?)(\t*?)(Function)`
11-
VBScriptEndFunctionPattern = `(?i)^(\s*?)(\t*?)(End Function)`
12-
VBScriptProcedurePattern = `[\r\n](\s*?)(private|public)?\s*(function|sub|property +(get|set|let))\s\[?([^(\r\n\]]+).*?[\r\n]([\s\S]*?)end +(function|sub|Property)`
10+
VBScriptFunctionPattern = `(?i)^(\s*?)(\t*?)(Private |Public (Default )?)?(Function|Sub)`
11+
VBScriptEndFunctionPattern = `(?i)^(\s*?)(\t*?)(End (Function|Sub))`
1312
)
1413

1514
// VBScriptFileExtension defines the VBSCript file extensions.

test/function_statements_test.go

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,35 +8,22 @@ import (
88
)
99

1010
func TestFunctionStatements(t *testing.T) {
11-
var expectedCognitiveComplexity = 3
12-
filename := "testdata/function.vbs"
13-
vbscript := domain.VBScript{}
14-
15-
vbscript.CognitiveComplexity = internal.Read(filename, &vbscript)
16-
17-
if expectedCognitiveComplexity != vbscript.CognitiveComplexity {
18-
t.Errorf("wrong output: got %v, expected %v", vbscript.CognitiveComplexity, expectedCognitiveComplexity)
19-
}
20-
}
21-
22-
func TestTwoFunctionStatements(t *testing.T) {
23-
var expectedCognitiveComplexity = 3
24-
var expectedFirstCognitiveComplexity = 2
25-
var expectedSecondCognitiveComplexity = 1
26-
filename := "testdata/two_function.vbs"
27-
vbscript := domain.VBScript{}
28-
29-
vbscript.CognitiveComplexity = internal.Read(filename, &vbscript)
30-
31-
if expectedCognitiveComplexity != vbscript.CognitiveComplexity {
32-
t.Errorf("wrong output: got %v, expected %v", vbscript.CognitiveComplexity, expectedCognitiveComplexity)
33-
}
34-
35-
if expectedFirstCognitiveComplexity != vbscript.Functions[0].CognitiveComplexity {
36-
t.Errorf("wrong output: got %v, expected %v", vbscript.Functions[0].CognitiveComplexity, expectedFirstCognitiveComplexity)
11+
tests := []struct {
12+
filename string
13+
expectedValue int
14+
}{
15+
{"testdata/function/function.vbs", 2},
16+
{"testdata/function/public_function.vbs", 3},
17+
{"testdata/function/public_default_function.vbs", 4},
18+
{"testdata/function/private_function.vbs", 1},
19+
{"testdata/function/two_function.vbs", 3},
3720
}
21+
for _, test := range tests {
22+
vbscript := domain.VBScript{}
23+
internal.Read(test.filename, &vbscript)
3824

39-
if expectedSecondCognitiveComplexity != vbscript.Functions[1].CognitiveComplexity {
40-
t.Errorf("wrong output: got %v, expected %v", vbscript.Functions[1].CognitiveComplexity, expectedSecondCognitiveComplexity)
25+
if test.expectedValue != vbscript.CognitiveComplexity {
26+
t.Errorf("wrong cognitive complexity output: got %v, expected %v (%s)", vbscript.CognitiveComplexity, test.expectedValue, test.filename)
27+
}
4128
}
4229
}

test/sub_statements_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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 TestSubStatements(t *testing.T) {
11+
tests := []struct {
12+
filename string
13+
expectedValue int
14+
}{
15+
{"testdata/sub/sub.vbs", 2},
16+
{"testdata/sub/public_sub.vbs", 3},
17+
{"testdata/sub/public_default_sub.vbs", 4},
18+
{"testdata/sub/private_sub.vbs", 1},
19+
}
20+
for _, test := range tests {
21+
vbscript := domain.VBScript{}
22+
internal.Read(test.filename, &vbscript)
23+
24+
if test.expectedValue != vbscript.CognitiveComplexity {
25+
t.Errorf("wrong cognitive complexity output: got %v, expected %v (%s)", vbscript.CognitiveComplexity, test.expectedValue, test.filename)
26+
}
27+
}
28+
}

test/testdata/function.vbs

Lines changed: 0 additions & 15 deletions
This file was deleted.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
' Cognitive Complexity 10
2+
Function Min(nOne, nTwo)
3+
If nOne <= nTwo Then ' +1
4+
Min = nOne
5+
Else ' +1
6+
Min = nTwo
7+
End If
8+
End Function
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Private Function Min(nOne, nTwo)
2+
If nOne <= nTwo Then ' +1
3+
Min = nOne
4+
End If
5+
End Function
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Public Default Function Min(nOne, nTwo, Three)
2+
If nOne <= nTwo Then ' +1
3+
Min = nOne
4+
If nThree <= nOne Then ' +2
5+
Min = nThree
6+
Else ' +1
7+
Min = nOne
8+
End If
9+
End If
10+
End Function
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Public Function Min(nOne, nTwo, nThree)
2+
If nOne <= nTwo Then ' +1
3+
Min = nOne
4+
Else ' +1
5+
Min = nTwo
6+
End If
7+
8+
If nThree < Min Then ' +1
9+
Min = nThree
10+
End If
11+
End Function

test/testdata/sub/private_sub.vbs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Private Sub Min(nOne, nTwo)
2+
If nOne <= nTwo Then ' +1
3+
Min = nOne
4+
End If
5+
End Sub

0 commit comments

Comments
 (0)