-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
07f1869
commit 87ded5b
Showing
5 changed files
with
213 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,10 @@ | ||
module github.com/LGBT-CN/signature-counter | ||
|
||
// use slices.Insert and log/slog module in Go 1.21 | ||
// use slog.SetLogLoggerLevel in Go 1.22 | ||
go 1.21 | ||
|
||
require ( | ||
github.com/davecgh/go-spew v1.1.1 // indirect | ||
github.com/pmezard/go-difflib v1.0.0 // indirect | ||
github.com/stretchr/testify v1.9.0 // indirect | ||
gopkg.in/yaml.v3 v3.0.1 // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,11 @@ | ||
github.com/KevinZonda/GoX v0.0.15 h1:u+G41/srg8c6bmI6Oqw9iBwYiSQk9FKp9fv/YBZadFk= | ||
github.com/KevinZonda/GoX v0.0.15/go.mod h1:WHV3YUyG+ou3wuUgwO4SRhMiKxLTVK5inajmtR1MUXo= | ||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= | ||
github.com/davecgh/go-spew v1.1.1/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/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= | ||
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= | ||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package main | ||
|
||
import ( | ||
"bufio" | ||
"errors" | ||
"fmt" | ||
"log" | ||
"slices" | ||
"strings" | ||
) | ||
|
||
const ( | ||
SIGNATURE_START = "<!-- BEGIN LGBT-CN SIGNATURE -->" | ||
SIGNATURE_END = "<!-- END LGBT-CN SIGNATURE -->" | ||
COUNT_START = "<!-- BEGIN LGBT-CN COUNT -->" | ||
COUNT_END = "<!-- END LGBT-CN COUNT -->" | ||
) | ||
|
||
func scan(scanner *bufio.Scanner) ([]string, error) { | ||
inSignatureBlock := false | ||
inCountBlock := false | ||
lineCount := 0 | ||
lineNumber := 0 | ||
// count block does not exists if count equal to zero | ||
countStartLine := 0 | ||
var lines []string | ||
for scanner.Scan() { | ||
line := scanner.Text() | ||
line = strings.TrimSpace(line) | ||
|
||
if line == COUNT_START { | ||
if inCountBlock { | ||
return nil, errors.New("Please don't use nested count block") | ||
} | ||
if inSignatureBlock { | ||
return nil, errors.New("Please put count block start outside signature block") | ||
} | ||
// insert count in next line | ||
countStartLine = lineNumber + 1 | ||
inCountBlock = true | ||
goto exit | ||
} | ||
if line == COUNT_END { | ||
if !inCountBlock { | ||
return nil, errors.New("Please put count block start first") | ||
} | ||
inCountBlock = false | ||
goto exit | ||
} | ||
if line == SIGNATURE_START { | ||
if inSignatureBlock { | ||
return nil, errors.New("Please don't use nested signature block") | ||
} | ||
if inCountBlock { | ||
return nil, errors.New("Please put signature block end outside count block") | ||
} | ||
inSignatureBlock = true | ||
goto exit | ||
} | ||
if line == SIGNATURE_END { | ||
if !inSignatureBlock { | ||
return nil, errors.New("Please put signature block start first") | ||
} | ||
inSignatureBlock = false | ||
goto exit | ||
} | ||
if inSignatureBlock && line != "" { | ||
lineCount++ | ||
} | ||
// skip content in count block | ||
if inCountBlock { | ||
continue | ||
} | ||
exit: | ||
lines = append(lines, line) | ||
lineNumber++ | ||
} | ||
|
||
if err := scanner.Err(); err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
if inCountBlock { | ||
return nil, errors.New("Please put count block end") | ||
} | ||
if inSignatureBlock { | ||
return nil, errors.New("Please put signature block end") | ||
} | ||
|
||
if countStartLine == 0 { | ||
return nil, errors.New("Please put count block") | ||
} | ||
lines = slices.Insert(lines, countStartLine, fmt.Sprintf("已有%d人签署!", lineCount)) | ||
|
||
return lines, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package main | ||
|
||
import ( | ||
"bufio" | ||
"github.com/stretchr/testify/assert" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestMissCountStart(t *testing.T) { | ||
code := `<!-- END LGBT-CN COUNT -->` | ||
scanner := bufio.NewScanner(strings.NewReader(code)) | ||
_, err := scan(scanner) | ||
assert.EqualError(t, err, "Please put count block start first") | ||
} | ||
|
||
func TestMissCountEnd(t *testing.T) { | ||
code := `<!-- BEGIN LGBT-CN COUNT -->` | ||
scanner := bufio.NewScanner(strings.NewReader(code)) | ||
_, err := scan(scanner) | ||
assert.EqualError(t, err, "Please put count block end") | ||
} | ||
|
||
func TestDuplicateCountStart(t *testing.T) { | ||
code := `<!-- BEGIN LGBT-CN COUNT --> | ||
<!-- BEGIN LGBT-CN COUNT -->` | ||
scanner := bufio.NewScanner(strings.NewReader(code)) | ||
_, err := scan(scanner) | ||
assert.EqualError(t, err, "Please don't use nested count block") | ||
} | ||
|
||
func TestMissSignatureStart(t *testing.T) { | ||
code := `<!-- END LGBT-CN SIGNATURE -->` | ||
scanner := bufio.NewScanner(strings.NewReader(code)) | ||
_, err := scan(scanner) | ||
assert.EqualError(t, err, "Please put signature block start first") | ||
} | ||
|
||
func TestMissSignatureEnd(t *testing.T) { | ||
code := `<!-- BEGIN LGBT-CN SIGNATURE -->` | ||
scanner := bufio.NewScanner(strings.NewReader(code)) | ||
_, err := scan(scanner) | ||
assert.EqualError(t, err, "Please put signature block end") | ||
} | ||
|
||
func TestDuplicateSignatureStart(t *testing.T) { | ||
code := `<!-- BEGIN LGBT-CN SIGNATURE --> | ||
<!-- BEGIN LGBT-CN SIGNATURE -->` | ||
scanner := bufio.NewScanner(strings.NewReader(code)) | ||
_, err := scan(scanner) | ||
assert.EqualError(t, err, "Please don't use nested signature block") | ||
} | ||
|
||
func TestMissCount(t *testing.T) { | ||
code := `<!-- BEGIN LGBT-CN SIGNATURE --> | ||
<!-- END LGBT-CN SIGNATURE -->` | ||
scanner := bufio.NewScanner(strings.NewReader(code)) | ||
_, err := scan(scanner) | ||
assert.EqualError(t, err, "Please put count block") | ||
} | ||
|
||
// Count should be zero when signature block not exists. | ||
func TestMissSignature(t *testing.T) { | ||
code := `<!-- BEGIN LGBT-CN COUNT --> | ||
<!-- END LGBT-CN COUNT -->` | ||
scanner := bufio.NewScanner(strings.NewReader(code)) | ||
lines, err := scan(scanner) | ||
assert.Nil(t, err) | ||
assert.Equal(t, lines, []string{ | ||
"<!-- BEGIN LGBT-CN COUNT -->", | ||
"已有0人签署!", | ||
"<!-- END LGBT-CN COUNT -->", | ||
}) | ||
} | ||
|
||
func TestSkipEmptyInSignature(t *testing.T) { | ||
code := `<!-- BEGIN LGBT-CN COUNT --> | ||
<!-- END LGBT-CN COUNT --> | ||
<!-- BEGIN LGBT-CN SIGNATURE --> | ||
unrivaled scalded | ||
reputable overripe | ||
<!-- END LGBT-CN SIGNATURE -->` | ||
scanner := bufio.NewScanner(strings.NewReader(code)) | ||
lines, err := scan(scanner) | ||
assert.Nil(t, err) | ||
assert.Equal(t, lines, []string{ | ||
"<!-- BEGIN LGBT-CN COUNT -->", | ||
"已有2人签署!", | ||
"<!-- END LGBT-CN COUNT -->", | ||
"", | ||
"<!-- BEGIN LGBT-CN SIGNATURE -->", | ||
"unrivaled scalded", | ||
"", | ||
"reputable overripe", | ||
"<!-- END LGBT-CN SIGNATURE -->", | ||
}) | ||
} |