github.com/rusq/gomojimoji@v0.0.1/mojimoji_test.go (about)

     1  // Package mojimoji is a port of mojimoji package to Go.
     2  // Original: https://github.com/studio-ousia/mojimoji
     3  
     4  package gomojimoji
     5  
     6  import (
     7  	"fmt"
     8  	"strings"
     9  	"testing"
    10  	"time"
    11  )
    12  
    13  func TestZenToHan(t *testing.T) {
    14  	type args struct {
    15  		text string
    16  		opt  []Option
    17  	}
    18  	tests := []struct {
    19  		name string
    20  		args args
    21  		want string
    22  	}{
    23  		{"kana", args{"アイウエオ", nil}, "アイウエオ"},
    24  		{"ten", args{"ガギグゲゴ", nil}, "ガギグゲゴ"},
    25  		{"maru", args{"パピプペポ", nil}, "パピプペポ"},
    26  		{"digits", args{"0123", nil}, "0123"},
    27  		{"ASCII", args{"abcABC", nil}, "abcABC"},
    28  		{"symbols", args{"#?!¥", nil}, "#?!¥"},
    29  		{"hiragana", args{"あいうえお", nil}, "あいうえお"},
    30  	}
    31  	for _, tt := range tests {
    32  		t.Run(tt.name, func(t *testing.T) {
    33  			if got := ZenToHan(tt.args.text, tt.args.opt...); got != tt.want {
    34  				t.Errorf("ZenToHan() = %v, want %v", got, tt.want)
    35  			}
    36  		})
    37  	}
    38  }
    39  
    40  func TestHanToZen(t *testing.T) {
    41  	type args struct {
    42  		text string
    43  		opt  []Option
    44  	}
    45  	tests := []struct {
    46  		name string
    47  		args args
    48  		want string
    49  	}{
    50  		{"kana", args{"アイウエオ", nil}, "アイウエオ"},
    51  		{"ten", args{"ガギグゲゴ", nil}, "ガギグゲゴ"},
    52  		{"maru", args{"パピプペポ", nil}, "パピプペポ"},
    53  		{"digits", args{"0123", nil}, "0123"},
    54  		{"ascii", args{"abcABC", nil}, "abcABC"},
    55  		{"symbols", args{"#?!¥", nil}, "#?!¥"},
    56  		{"hiragana", args{"あいうえお", nil}, "あいうえお"},
    57  		{"mixed", args{"ア゙イ゙ヴエ゙オ゙", nil}, "ア゛イ゛ヴエ゛オ゛"},
    58  	}
    59  	for _, tt := range tests {
    60  		t.Run(tt.name, func(t *testing.T) {
    61  			if got := HanToZen(tt.args.text, tt.args.opt...); got != tt.want {
    62  				t.Errorf("HanToZen() = %v, want %v", got, tt.want)
    63  			}
    64  		})
    65  	}
    66  }
    67  
    68  func ExampleZenToHan() {
    69  	fmt.Println(ZenToHan("ニュージーランド"))
    70  	fmt.Println(ZenToHan("ニュージーランド Auckland 0123", Kana(false), Digits(true)))
    71  	//Output:
    72  	// ニュージーランド
    73  	// ニュージーランド Auckland 0123
    74  
    75  }
    76  
    77  func ExampleHanToZen() {
    78  	fmt.Println(HanToZen("ニュージーランド"))
    79  	fmt.Println(HanToZen("ニュージーランド Auckland 6012", ASCII(true), Digits(false), Kana(false)))
    80  	//Output:
    81  	//ニュージーランド
    82  	//ニュージーランド Auckland 6012
    83  }
    84  
    85  func BenchmarkZenToHan(b *testing.B) {
    86  	s := strings.Repeat("ABCDEFG012345", 10)
    87  	for n := 0; n < b.N; n++ {
    88  		ZenToHan(s)
    89  	}
    90  }
    91  
    92  func BenchmarkZenToHanConv(b *testing.B) {
    93  	// this benchmark is similar to that for python mojimoji library.
    94  	s := strings.Repeat("ABCDEFG012345", 10)
    95  	start := time.Now()
    96  	for n := 0; n < 1000000; n++ {
    97  		ZenToHan(s)
    98  	}
    99  	b.Log(time.Since(start))
   100  }
   101  
   102  func BenchmarkHanToZen(b *testing.B) {
   103  	s := strings.Repeat("ABCDEFG012345", 10)
   104  	for n := 0; n < b.N; n++ {
   105  		HanToZen(s)
   106  	}
   107  }
   108  
   109  func BenchmarkHanToZenConv(b *testing.B) {
   110  	// this benchmark is similar to that for python mojimoji library.
   111  	s := strings.Repeat("ABCDEFG012345", 10)
   112  	start := time.Now()
   113  	for n := 0; n < 1000000; n++ {
   114  		HanToZen(s)
   115  	}
   116  	b.Log(time.Since(start))
   117  }