replacer_test.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package stringx
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/assert"
  5. )
  6. func TestReplacer_Replace(t *testing.T) {
  7. mapping := map[string]string{
  8. "一二三四": "1234",
  9. "二三": "23",
  10. "二": "2",
  11. }
  12. assert.Equal(t, "零1234五", NewReplacer(mapping).Replace("零一二三四五"))
  13. }
  14. func TestReplacer_ReplaceSingleChar(t *testing.T) {
  15. mapping := map[string]string{
  16. "二": "2",
  17. }
  18. assert.Equal(t, "零一2三四五", NewReplacer(mapping).Replace("零一二三四五"))
  19. }
  20. func TestReplacer_ReplaceExceedRange(t *testing.T) {
  21. mapping := map[string]string{
  22. "二三四五六": "23456",
  23. }
  24. assert.Equal(t, "零一二三四五", NewReplacer(mapping).Replace("零一二三四五"))
  25. }
  26. func TestReplacer_ReplacePartialMatch(t *testing.T) {
  27. mapping := map[string]string{
  28. "二三四七": "2347",
  29. }
  30. assert.Equal(t, "零一二三四五", NewReplacer(mapping).Replace("零一二三四五"))
  31. }
  32. func TestReplacer_ReplaceMultiMatches(t *testing.T) {
  33. mapping := map[string]string{
  34. "二三": "23",
  35. }
  36. assert.Equal(t, "零一23四五一23四五", NewReplacer(mapping).Replace("零一二三四五一二三四五"))
  37. }