File: format_test.go

package info (click to toggle)
golang-github-huandu-xstrings 1.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 160 kB
  • sloc: makefile: 2
file content (100 lines) | stat: -rw-r--r-- 2,953 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Copyright 2015 Huan Du. All rights reserved.
// Licensed under the MIT license that can be found in the LICENSE file.

package xstrings

import (
	"strconv"
	"strings"
	"testing"
)

func TestExpandTabs(t *testing.T) {
	runner := func(str string) (result string) {
		defer func() {
			if e := recover(); e != nil {
				result = e.(string)
			}
		}()

		input := strings.Split(str, separator)
		n, _ := strconv.Atoi(input[1])
		return ExpandTabs(input[0], n)
	}

	runTestCases(t, runner, _M{
		sep("a\tbc\tdef\tghij\tk", "4"): "a   bc  def ghij    k",
		sep("abcdefg\thij\nk\tl", "4"):  "abcdefg hij\nk   l",
		sep("z中\t文\tw", "4"):            "z中 文  w",
		sep("abcdef", "4"):              "abcdef",

		sep("abc\td\tef\tghij\nk\tl", "3"): "abc   d  ef ghij\nk  l",
		sep("abc\td\tef\tghij\nk\tl", "1"): "abc d ef ghij\nk l",

		sep("abc", "0"):  "tab size must be positive",
		sep("abc", "-1"): "tab size must be positive",
	})
}

func TestLeftJustify(t *testing.T) {
	runner := func(str string) string {
		input := strings.Split(str, separator)
		n, _ := strconv.Atoi(input[1])
		return LeftJustify(input[0], n, input[2])
	}

	runTestCases(t, runner, _M{
		sep("hello", "4", " "):    "hello",
		sep("hello", "10", " "):   "hello     ",
		sep("hello", "10", "123"): "hello12312",

		sep("hello中文test", "4", " "):    "hello中文test",
		sep("hello中文test", "12", " "):   "hello中文test ",
		sep("hello中文test", "18", "测试!"): "hello中文test测试!测试!测",

		sep("hello中文test", "0", "123"): "hello中文test",
		sep("hello中文test", "18", ""):   "hello中文test",
	})
}

func TestRightJustify(t *testing.T) {
	runner := func(str string) string {
		input := strings.Split(str, separator)
		n, _ := strconv.Atoi(input[1])
		return RightJustify(input[0], n, input[2])
	}

	runTestCases(t, runner, _M{
		sep("hello", "4", " "):    "hello",
		sep("hello", "10", " "):   "     hello",
		sep("hello", "10", "123"): "12312hello",

		sep("hello中文test", "4", " "):    "hello中文test",
		sep("hello中文test", "12", " "):   " hello中文test",
		sep("hello中文test", "18", "测试!"): "测试!测试!测hello中文test",

		sep("hello中文test", "0", "123"): "hello中文test",
		sep("hello中文test", "18", ""):   "hello中文test",
	})
}

func TestCenter(t *testing.T) {
	runner := func(str string) string {
		input := strings.Split(str, separator)
		n, _ := strconv.Atoi(input[1])
		return Center(input[0], n, input[2])
	}

	runTestCases(t, runner, _M{
		sep("hello", "4", " "):    "hello",
		sep("hello", "10", " "):   "  hello   ",
		sep("hello", "10", "123"): "12hello123",

		sep("hello中文test", "4", " "):    "hello中文test",
		sep("hello中文test", "12", " "):   "hello中文test ",
		sep("hello中文test", "18", "测试!"): "测试!hello中文test测试!测",

		sep("hello中文test", "0", "123"): "hello中文test",
		sep("hello中文test", "18", ""):   "hello中文test",
	})
}