@@ -5,9 +5,16 @@ import (
55 "strings"
66 "testing"
77
8+ "github.com/google/go-cmp/cmp"
9+ "github.com/google/go-cmp/cmp/cmpopts"
810 "github.com/stretchr/testify/assert"
911)
1012
13+ // floatComparer returns a cmp.Option for floating-point comparisons with tolerance.
14+ func floatComparer () cmp.Option {
15+ return cmpopts .EquateApprox (0 , 1e-5 )
16+ }
17+
1118func TestKiwiVersion (t * testing.T ) {
1219 assert .Equal (t , KiwiVersion (), "0.21.0" )
1320}
@@ -59,7 +66,9 @@ func TestAnalyze(t *testing.T) {
5966 },
6067 }
6168
62- assert .Equal (t , expected , res )
69+ if diff := cmp .Diff (expected , res , floatComparer ()); diff != "" {
70+ t .Errorf ("Analyze result mismatch (-want +got):\n %s" , diff )
71+ }
6372 assert .Equal (t , 0 , kiwi .Close ())
6473}
6574
@@ -143,7 +152,9 @@ func TestAddWord(t *testing.T) {
143152 },
144153 }
145154
146- assert .Equal (t , expected , res )
155+ if diff := cmp .Diff (expected , res , floatComparer ()); diff != "" {
156+ t .Errorf ("AddWord result mismatch (-want +got):\n %s" , diff )
157+ }
147158 assert .Equal (t , 0 , kiwi .Close ())
148159}
149160
@@ -202,7 +213,9 @@ func TestLoadDict(t *testing.T) {
202213 },
203214 }
204215
205- assert .Equal (t , expected , res )
216+ if diff := cmp .Diff (expected , res , floatComparer ()); diff != "" {
217+ t .Errorf ("LoadDict result mismatch (-want +got):\n %s" , diff )
218+ }
206219 assert .Equal (t , 0 , kiwi .Close ())
207220}
208221
@@ -242,7 +255,9 @@ func TestLoadDict2(t *testing.T) {
242255 },
243256 }
244257
245- assert .Equal (t , expected , res )
258+ if diff := cmp .Diff (expected , res , floatComparer ()); diff != "" {
259+ t .Errorf ("LoadDict2 result mismatch (-want +got):\n %s" , diff )
260+ }
246261 assert .Equal (t , 0 , kiwi .Close ())
247262}
248263
@@ -258,7 +273,7 @@ func TestExtractWord(t *testing.T) {
258273가능성이 크다. 다만 가사의 경우 만약 윤치호가 실제 작사한 것이 사실이라고 하더라도 일제시대가 되기도 이전인 대한제국 시절 작사된 것이기
259274때문에 친일의 산물은 아니다.` )
260275 wordInfos , _ := kb .ExtractWords (rs , 3 /*=minCnt*/ , 3 /*=maxWordLen*/ , 0.0 /*=minScore*/ , - 3.0 /*=posThreshold*/ )
261- assert . Equal ( t , []WordInfo {
276+ expected := []WordInfo {
262277 {
263278 Form : "안익" ,
264279 Freq : 3 ,
@@ -271,7 +286,10 @@ func TestExtractWord(t *testing.T) {
271286 POSScore : - 0.23702252 ,
272287 Score : 0 ,
273288 },
274- }, wordInfos )
289+ }
290+ if diff := cmp .Diff (expected , wordInfos , floatComparer ()); diff != "" {
291+ t .Errorf ("ExtractWord result mismatch (-want +got):\n %s" , diff )
292+ }
275293 assert .Equal (t , 0 , kb .Close ())
276294}
277295
@@ -280,8 +298,11 @@ func TestExtractWordwithFile(t *testing.T) {
280298 file , _ := os .Open ("./example/test.txt" )
281299
282300 wordInfos , _ := kb .ExtractWords (file , 10 /*=minCnt*/ , 5 /*=maxWordLen*/ , 0.0 /*=minScore*/ , - 25.0 /*=posThreshold*/ )
283- assert . Equal ( t , WordInfo {
301+ expectedWordInfo := WordInfo {
284302 Form : "무위원" , Freq : 17 , POSScore : - 1.7342134 , Score : 0.69981515 ,
285- }, wordInfos [0 ])
303+ }
304+ if diff := cmp .Diff (expectedWordInfo , wordInfos [0 ], floatComparer ()); diff != "" {
305+ t .Errorf ("ExtractWordwithFile result mismatch (-want +got):\n %s" , diff )
306+ }
286307 assert .Equal (t , 0 , kb .Close ())
287308}
0 commit comments