-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
6 changed files
with
384 additions
and
15 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
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,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 | ||
} |
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,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) | ||
}) | ||
} | ||
} |
Oops, something went wrong.