summaryrefslogtreecommitdiff
path: root/test/unit/t_format.cc
blob: 142db92c01db3e3cb8150aba8acb2df16b0db7cf (plain)
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#define BOOST_TEST_DYN_LINK

#include <boost/test/unit_test.hpp>

#include <system.hh>

#include "format.h"

using namespace ledger;

struct format_fixture {
  format_fixture() {
    format_t::default_style = format_t::TRUNCATE_TRAILING;
    format_t::default_style_changed = false;
  }
  ~format_fixture() {
    format_t::default_style = format_t::TRUNCATE_TRAILING;
    format_t::default_style_changed = false;
  }
};

BOOST_FIXTURE_TEST_SUITE(format, format_fixture)

BOOST_AUTO_TEST_CASE(testTruncateTrailing)
{
  format_t::default_style = format_t::TRUNCATE_TRAILING;
  unistring str("abcd:1234:ABCD");
  BOOST_CHECK_EQUAL(format_t::truncate(str, 0, 0), "abcd:1234:ABCD");
  BOOST_CHECK_EQUAL(format_t::truncate(str, 99, 0), "abcd:1234:ABCD");

  BOOST_CHECK_EQUAL(format_t::truncate(str, 14, 0), "abcd:1234:ABCD");
  BOOST_CHECK_EQUAL(format_t::truncate(str, 13, 0), "abcd:1234:A..");
  BOOST_CHECK_EQUAL(format_t::truncate(str, 12, 0), "abcd:1234:..");
  BOOST_CHECK_EQUAL(format_t::truncate(str, 11, 0), "abcd:1234..");
  BOOST_CHECK_EQUAL(format_t::truncate(str, 10, 0), "abcd:123..");

  unistring ustr("中文:中文:中文");
  BOOST_CHECK_EQUAL(format_t::truncate(ustr, 14, 0), "中文:中文:中文");
  BOOST_CHECK_EQUAL(format_t::truncate(ustr, 13, 0), "中文:中文:...");
  BOOST_CHECK_EQUAL(format_t::truncate(ustr, 12, 0), "中文:中文:..");
  BOOST_CHECK_EQUAL(format_t::truncate(ustr, 11, 0), "中文:中文..");
  BOOST_CHECK_EQUAL(format_t::truncate(ustr, 10, 0), "中文:中...");
}

BOOST_AUTO_TEST_CASE(testTruncateMiddle)
{
  format_t::default_style = format_t::TRUNCATE_MIDDLE;
  unistring str("abcd:1234:ABCD");
  BOOST_CHECK_EQUAL(format_t::truncate(str, 0, 0), "abcd:1234:ABCD");
  BOOST_CHECK_EQUAL(format_t::truncate(str, 99, 0), "abcd:1234:ABCD");

  BOOST_CHECK_EQUAL(format_t::truncate(str, 14, 0), "abcd:1234:ABCD");
  BOOST_CHECK_EQUAL(format_t::truncate(str, 13, 0), "abcd:..4:ABCD");
  BOOST_CHECK_EQUAL(format_t::truncate(str, 12, 0), "abcd:..:ABCD");
  BOOST_CHECK_EQUAL(format_t::truncate(str, 11, 0), "abcd..:ABCD");
  BOOST_CHECK_EQUAL(format_t::truncate(str, 10, 0), "abcd..ABCD");

  unistring ustr("中文:中文:中文");
  BOOST_CHECK_EQUAL(format_t::truncate(ustr, 14, 0), "中文:中文:中文");
  BOOST_CHECK_EQUAL(format_t::truncate(ustr, 13, 0), "中文:...:中文");
  BOOST_CHECK_EQUAL(format_t::truncate(ustr, 12, 0), "中文:..:中文");
  BOOST_CHECK_EQUAL(format_t::truncate(ustr, 11, 0), "中文..:中文");
  BOOST_CHECK_EQUAL(format_t::truncate(ustr, 10, 0), "中文..中文");
}

BOOST_AUTO_TEST_CASE(testTruncateLeading)
{
  format_t::default_style = format_t::TRUNCATE_LEADING;
  unistring str("abcd:1234:ABCD");
  BOOST_CHECK_EQUAL(format_t::truncate(str, 0, 0), "abcd:1234:ABCD");
  BOOST_CHECK_EQUAL(format_t::truncate(str, 99, 0), "abcd:1234:ABCD");

  BOOST_CHECK_EQUAL(format_t::truncate(str, 14, 0), "abcd:1234:ABCD");
  BOOST_CHECK_EQUAL(format_t::truncate(str, 13, 0), "..d:1234:ABCD");
  BOOST_CHECK_EQUAL(format_t::truncate(str, 12, 0), "..:1234:ABCD");
  BOOST_CHECK_EQUAL(format_t::truncate(str, 11, 0), "..1234:ABCD");
  BOOST_CHECK_EQUAL(format_t::truncate(str, 10, 0), "..234:ABCD");

  unistring ustr("中文:中文:中文");
  BOOST_CHECK_EQUAL(format_t::truncate(ustr, 14, 0), "中文:中文:中文");
  BOOST_CHECK_EQUAL(format_t::truncate(ustr, 13, 0), "...:中文:中文");
  BOOST_CHECK_EQUAL(format_t::truncate(ustr, 12, 0), "..:中文:中文");
  BOOST_CHECK_EQUAL(format_t::truncate(ustr, 11, 0), "..中文:中文");
  BOOST_CHECK_EQUAL(format_t::truncate(ustr, 10, 0), "...文:中文");
}

BOOST_AUTO_TEST_CASE(testTruncateAbbreviate)
{
  format_t::default_style = format_t::ABBREVIATE;
  unistring str("abcd:1234:ABCD");
  BOOST_CHECK_EQUAL(format_t::truncate(str, 0, 2), "abcd:1234:ABCD");
  BOOST_CHECK_EQUAL(format_t::truncate(str, 99, 2), "abcd:1234:ABCD");
  BOOST_CHECK_EQUAL(format_t::truncate(str, 14, 2), "abcd:1234:ABCD");
  BOOST_CHECK_EQUAL(format_t::truncate(str, 13, 2), "abc:1234:ABCD");
  BOOST_CHECK_EQUAL(format_t::truncate(str, 12, 2), "ab:1234:ABCD");
  BOOST_CHECK_EQUAL(format_t::truncate(str, 11, 2), "ab:123:ABCD");
  BOOST_CHECK_EQUAL(format_t::truncate(str, 10, 2), "ab:12:ABCD");
  BOOST_CHECK_EQUAL(format_t::truncate(str, 9, 2), "..12:ABCD");
  BOOST_CHECK_EQUAL(format_t::truncate(str, 8, 2), "..2:ABCD");
  BOOST_CHECK_EQUAL(format_t::truncate(str, 7, 2), "..:ABCD");
  BOOST_CHECK_EQUAL(format_t::truncate(str, 6, 2), "..ABCD");
  BOOST_CHECK_EQUAL(format_t::truncate(str, 5, 2), "..BCD");

  unistring ustr("中文:中文:中文");
  BOOST_CHECK_EQUAL(format_t::truncate(ustr, 14, 2), "中文:中文:中文");
  BOOST_CHECK_EQUAL(format_t::truncate(ustr, 13, 2), "中.:中文:中文");
  BOOST_CHECK_EQUAL(format_t::truncate(ustr, 12, 2), "中:中文:中文");
  BOOST_CHECK_EQUAL(format_t::truncate(ustr, 11, 2), "中:中.:中文");
  BOOST_CHECK_EQUAL(format_t::truncate(ustr, 10, 2), "中:中:中文");
  BOOST_CHECK_EQUAL(format_t::truncate(ustr, 9, 2), "..中:中文");
  BOOST_CHECK_EQUAL(format_t::truncate(ustr, 8, 2), "...:中文");

  BOOST_CHECK_EQUAL(format_t::truncate(ustr, 14, 1), "中文:中文:中文");
  BOOST_CHECK_EQUAL(format_t::truncate(ustr, 13, 1), "中.:中文:中文");
  BOOST_CHECK_EQUAL(format_t::truncate(ustr, 12, 1), "中:中文:中文");
  BOOST_CHECK_EQUAL(format_t::truncate(ustr, 11, 1), ".:中文:中文");
  BOOST_CHECK_EQUAL(format_t::truncate(ustr, 10, 1), ".:中.:中文");
  BOOST_CHECK_EQUAL(format_t::truncate(ustr, 9, 1), ".:中:中文");
  BOOST_CHECK_EQUAL(format_t::truncate(ustr, 8, 1), ".:.:中文");
}

BOOST_AUTO_TEST_SUITE_END()