Skip to content

Commit

Permalink
✨!や?が出現した時に、半角や絵文字にランダムで切り替えるように+!?!?など連続する場合も処理できるようにいたしましたわ #40 (#41)
Browse files Browse the repository at this point in the history
* wip

* wip

* wip

* だいたいいけた

* fix test

* fix

* fix make

* add test case

* del unused func

* add test case

* go fmt

* comment

* fix: nilアクセスが発生していたので修正

* fix: CI

* fix: path
  • Loading branch information
jiro4989 authored Jun 23, 2022
1 parent 718371e commit 84f3679
Show file tree
Hide file tree
Showing 6 changed files with 384 additions and 15 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,10 @@ jobs:
fi
- name: Static analysis
run: go vet .
- run: go build
- run: go install
- run: go build ./cmd/ojosama
- run: go install ./cmd/ojosama
- run: go test -cover ./...
- run: ./ojosama README.adoc

coverage:
runs-on: ubuntu-latest
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
bin/ojosama: go.* *.go cmd/*
bin/ojosama: go.* *.go cmd/* internal/*
make test
go vet .
go fmt .
Expand All @@ -10,5 +10,5 @@ test:
go test -cover ./...

.PHONY: install
install: go.* *.go cmd/*
install: go.* *.go cmd/* internal/*
go install ./cmd/ojosama
122 changes: 122 additions & 0 deletions internal/chars/chars.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package chars

import (
"math/rand"
)

type ExclQuesMark struct {
Value string
Style StyleType
Meaning MeaningType
}

type StyleType int
type MeaningType int
type TestMode struct {
Pos int
}

const (
styleTypeUnknown StyleType = iota
styleTypeFullWidth
styleTypeHalfWidth
styleTypeEmoji
styleTypeDoubleEmoji // !!

meaningTypeUnknown = iota
meaningTypeExcl // !
meaningTypeQues // ?
meaningTypeEQ // !?
)

var (
eqMarks = []ExclQuesMark{
newExcl("!", styleTypeFullWidth),
newExcl("!", styleTypeHalfWidth),
newExcl("❗", styleTypeEmoji),
newExcl("‼", styleTypeDoubleEmoji),
newQues("?", styleTypeFullWidth),
newQues("?", styleTypeHalfWidth),
newQues("❓", styleTypeEmoji),
newEQ("!?", styleTypeHalfWidth),
newEQ("⁉", styleTypeEmoji),
}
)

func newExcl(v string, t StyleType) ExclQuesMark {
return ExclQuesMark{
Value: v,
Style: t,
Meaning: meaningTypeExcl,
}
}

func newQues(v string, t StyleType) ExclQuesMark {
return ExclQuesMark{
Value: v,
Style: t,
Meaning: meaningTypeQues,
}
}

func newEQ(v string, t StyleType) ExclQuesMark {
return ExclQuesMark{
Value: v,
Style: t,
Meaning: meaningTypeEQ,
}
}

func IsExclQuesMark(s string) (bool, *ExclQuesMark) {
for _, v := range eqMarks {
if v.Value == s {
return true, &v
}
}
return false, nil
}

func SampleExclQuesByValue(v string, t *TestMode) *ExclQuesMark {
ok, got := IsExclQuesMark(v)
if !ok {
return nil
}

var s []ExclQuesMark
for _, mark := range eqMarks {
if mark.Meaning == got.Meaning {
s = append(s, mark)
}
}
// 到達しないはずだけれど一応いれてる
if len(s) < 1 {
return nil
}

if t != nil {
// テスト用のパラメータがあるときは決め打ちで返す
return &s[t.Pos]
}
rand.Shuffle(len(s), func(i, j int) { s[i], s[j] = s[j], s[i] })
return &s[0]
}

func FindExclQuesByStyleAndMeaning(s StyleType, m MeaningType) *ExclQuesMark {
var eq []ExclQuesMark
for _, mark := range eqMarks {
if mark.Style == s {
eq = append(eq, mark)
}
}
if len(eq) < 1 {
return nil
}

for _, mark := range eq {
if mark.Meaning == m {
return &mark
}
}

return nil
}
136 changes: 136 additions & 0 deletions internal/chars/chars_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
package chars

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestIsExclQuesMark(t *testing.T) {
tests := []struct {
desc string
s string
wantOK bool
wantEQ ExclQuesMark
}{
{
desc: "正常系: !とはマッチいたしますわ",
s: "!",
wantOK: true,
wantEQ: newExcl("!", styleTypeFullWidth),
},
{
desc: "正常系: ❓とはマッチいたしますわ",
s: "❓",
wantOK: true,
wantEQ: newQues("❓", styleTypeEmoji),
},
{
desc: "正常系: 漆とはマッチいたしませんわ",
s: "漆",
wantOK: false,
},
}

for _, tt := range tests {
t.Run(tt.desc, func(t *testing.T) {
assert := assert.New(t)

got, got2 := IsExclQuesMark(tt.s)
assert.Equal(tt.wantOK, got)
if tt.wantOK {
assert.Equal(&tt.wantEQ, got2)
}
})
}
}

func TestSampleExclQuesByValue(t *testing.T) {
tests := []struct {
desc string
v string
t *TestMode
want ExclQuesMark
wantNil bool
}{
{
desc: "正常系: !とはマッチいたしますわ",
v: "!",
t: &TestMode{Pos: 0},
want: newExcl("!", styleTypeFullWidth),
},
{
desc: "正常系: ❓とはマッチいたしますわ",
v: "❓",
t: &TestMode{Pos: 2},
want: newQues("❓", styleTypeEmoji),
},
{
desc: "正常系: 菫とはマッチいたしませんわ",
v: "菫",
t: &TestMode{Pos: 2},
wantNil: true,
},
}

for _, tt := range tests {
t.Run(tt.desc, func(t *testing.T) {
assert := assert.New(t)

got := SampleExclQuesByValue(tt.v, tt.t)
if tt.wantNil {
assert.Nil(got)
return
}
assert.Equal(&tt.want, got)
})
}
}

func TestFindExclQuesByStyleAndMeaning(t *testing.T) {
tests := []struct {
desc string
s StyleType
m MeaningType
want ExclQuesMark
wantNil bool
}{
{
desc: "正常系: ❗を指定いたしますわ",
s: styleTypeEmoji,
m: meaningTypeExcl,
want: newExcl("❗", styleTypeEmoji),
},
{
desc: "正常系: ?を指定いたしますわ",
s: styleTypeFullWidth,
m: meaningTypeQues,
want: newQues("?", styleTypeFullWidth),
},
{
desc: "正常系: 不明な要素の場合は何もお返しいたしませんわ",
s: styleTypeUnknown,
m: meaningTypeExcl,
wantNil: true,
},
{
desc: "正常系: 不明な要素の場合は何もお返しいたしませんわ",
s: styleTypeFullWidth,
m: meaningTypeUnknown,
wantNil: true,
},
}

for _, tt := range tests {
t.Run(tt.desc, func(t *testing.T) {
assert := assert.New(t)

got := FindExclQuesByStyleAndMeaning(tt.s, tt.m)
if tt.wantNil {
assert.Nil(got)
return
}
assert.Equal(&tt.want, got)
})
}
}
Loading

0 comments on commit 84f3679

Please sign in to comment.