Skip to content Search Apps Documentation Source Content File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

Community realm. Read the code first. Community realm, deployed by its author. Read the code before you interact or send coins.

humanize_test.gno

2.13 Kb · 85 lines
 1package humanize
 2
 3import (
 4	"testing"
 5
 6	"gno.land/p/nt/uassert/v0"
 7)
 8
 9func TestBytes(t *testing.T) {
10	cases := []struct {
11		in   int64
12		want string
13	}{
14		{0, "0 B"},
15		{999, "999 B"},
16		{1023, "1023 B"},
17		{1024, "1 KiB"},
18		{1536, "1.5 KiB"},
19		{1048576, "1 MiB"},
20		{1572864, "1.5 MiB"},
21		{1073741824, "1 GiB"},
22		{-1536, "-1.5 KiB"},
23		{-10, "-10 B"},
24	}
25	for _, c := range cases {
26		uassert.Equal(t, c.want, Bytes(c.in))
27	}
28}
29
30func TestComma(t *testing.T) {
31	cases := []struct {
32		in   int64
33		want string
34	}{
35		{0, "0"}, {1, "1"}, {999, "999"}, {1000, "1,000"},
36		{1234567, "1,234,567"}, {-1234567, "-1,234,567"},
37	}
38	for _, c := range cases {
39		uassert.Equal(t, c.want, Comma(c.in))
40	}
41}
42
43// The teens are the trap: 11/12/13 take "th" despite ending in 1/2/3.
44func TestOrdinalTeens(t *testing.T) {
45	cases := []struct {
46		in   int64
47		want string
48	}{
49		{1, "1st"}, {2, "2nd"}, {3, "3rd"}, {4, "4th"},
50		{11, "11th"}, {12, "12th"}, {13, "13th"},
51		{21, "21st"}, {22, "22nd"}, {23, "23rd"},
52		{111, "111th"}, {112, "112th"}, {113, "113th"},
53		{101, "101st"}, {0, "0th"},
54	}
55	for _, c := range cases {
56		uassert.Equal(t, c.want, Ordinal(c.in))
57	}
58}
59
60func TestPlural(t *testing.T) {
61	uassert.Equal(t, "1 block", Plural(1, "block", ""))
62	uassert.Equal(t, "2 blocks", Plural(2, "block", ""))
63	uassert.Equal(t, "0 blocks", Plural(0, "block", ""))
64	uassert.Equal(t, "2 mice", Plural(2, "mouse", "mice"))
65	uassert.Equal(t, "-1 block", Plural(-1, "block", ""))
66}
67
68func TestBlocks(t *testing.T) {
69	uassert.Equal(t, "now", Blocks(0))
70	uassert.Equal(t, "1 block", Blocks(1))
71	uassert.Equal(t, "9 blocks", Blocks(9))
72	uassert.Equal(t, "~10 blocks", Blocks(15))
73	uassert.Equal(t, "~1,200 blocks", Blocks(1234))
74	uassert.Equal(t, "in the past", Blocks(-5))
75}
76
77// Truncate counts runes, so multi-byte text is never cut mid-character.
78func TestTruncateCountsRunes(t *testing.T) {
79	uassert.Equal(t, "hello", Truncate("hello", 10))
80	uassert.Equal(t, "hell…", Truncate("hello!", 5))
81	uassert.Equal(t, "…", Truncate("hello", 1))
82	uassert.Equal(t, "", Truncate("hello", 0))
83	uassert.Equal(t, "éé", Truncate("éé", 2))
84	uassert.Equal(t, "é…", Truncate("ééé", 2)) // not sliced mid-rune
85}