Skip to content

Commit

Permalink
test: test scanner
Browse files Browse the repository at this point in the history
  • Loading branch information
petricavalry committed Nov 6, 2024
1 parent 07f1869 commit 87ded5b
Show file tree
Hide file tree
Showing 5 changed files with 213 additions and 85 deletions.
9 changes: 7 additions & 2 deletions go.mod
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
)
9 changes: 9 additions & 0 deletions go.sum
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=
85 changes: 2 additions & 83 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,11 @@ package main

import (
"bufio"
"fmt"
"log"
"log/slog"
"os"
"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 main() {
// slog.SetLogLoggerLevel(slog.LevelDebug)
if len(os.Args) < 2 {
Expand All @@ -35,81 +25,10 @@ func main() {
defer file.Close()

scanner := bufio.NewScanner(file)
inSignatureBlock := false
inCountBlock := false
lineCount := 0
lineNumber := 0
countStartLine := 0
var lines []string
for scanner.Scan() {
line := scanner.Text()
line = strings.TrimSpace(line)
slog.Debug(fmt.Sprintf("Parsing line: %d %s", lineNumber, line))

if line == COUNT_START {
slog.Debug("Found count block start")
if inSignatureBlock {
log.Fatal("Please put count block start outside signature block")
}
countStartLine = lineNumber
inCountBlock = true
goto exit
}
if line == COUNT_END {
slog.Debug("Found count block end")
if !inCountBlock {
log.Fatal("Please put count block start first")
}
inCountBlock = false
goto exit
}
if line == SIGNATURE_START {
slog.Debug("Found signature block start")
if inCountBlock {
log.Fatal("Please put signature block end outside count block")
}
inSignatureBlock = true
goto exit
}
if line == SIGNATURE_END {
slog.Debug("Found signature block end")
if !inSignatureBlock {
log.Fatal("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 {
lines, err := scan(scanner)
if err != nil {
log.Fatal(err)
}

if inCountBlock {
fmt.Println("Please put count block end")
}
if inSignatureBlock {
fmt.Println("Please put signature block end")
}

slog.Debug(fmt.Sprintf("%d lines in signature block", lineCount))
slog.Debug(fmt.Sprintf("count block start at line %d", countStartLine))
lines = slices.Insert(lines, countStartLine+1, fmt.Sprintf("已有%d人签署!", lineCount))

for _, line := range lines {
fmt.Println(line)
}

output := strings.Join(lines, "\n")

err = os.WriteFile(fileName, []byte(output), 644)
Expand Down
96 changes: 96 additions & 0 deletions scanner.go
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
}
99 changes: 99 additions & 0 deletions scanner_test.go
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 -->",
})
}

0 comments on commit 87ded5b

Please sign in to comment.