summaryrefslogtreecommitdiffstats
path: root/src/test/test_texttable.cc
blob: 290f6f85fa9fd537da6908c8090eab7ac11ba4f3 (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
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
/*
 * Ceph - scalable distributed file system
 *
 * Copyright (C) 2012 Inktank Storage, Inc.
 *
 * This is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License version 2.1, as published by the Free Software
 * Foundation.  See file COPYING.
 *
 */

#include "common/TextTable.h"
#include <iostream>
#include "gtest/gtest.h"
#include "include/coredumpctl.h"

TEST(TextTable, Alignment) {
  TextTable t;

  // test alignment
  // 3 5-character columns
  t.define_column("HEAD1", TextTable::LEFT, TextTable::LEFT);
  t.define_column("HEAD2", TextTable::LEFT, TextTable::CENTER);
  t.define_column("HEAD3", TextTable::LEFT, TextTable::RIGHT);

  t << "1" << 2 << 3 << TextTable::endrow;
  std::ostringstream oss;
  oss << t;
  ASSERT_STREQ("HEAD1  HEAD2  HEAD3\n1        2        3\n", oss.str().c_str());
}

TEST(TextTable, WidenAndClearShrink) {
  TextTable t;

  t.define_column("1", TextTable::LEFT, TextTable::LEFT);

  // default column size is 1, widen to 5
  t << "wider";

  // validate wide output
  std::ostringstream oss;
  oss << t;
  ASSERT_STREQ("1    \nwider\n", oss.str().c_str());
  oss.str("");

  // reset, validate single-char width output
  t.clear();
  t << "s";
  oss << t;
  ASSERT_STREQ("1\ns\n", oss.str().c_str());
}

TEST(TextTable, Indent) {
  TextTable t;

  t.define_column("1", TextTable::LEFT, TextTable::LEFT);
  t.set_indent(10);
  t << "s";
  std::ostringstream oss;
  oss << t;
  ASSERT_STREQ("          1\n          s\n", oss.str().c_str());
}


TEST(TextTable, TooManyItems) {
  TextTable t;

  t.define_column("1", TextTable::LEFT, TextTable::LEFT);
  t.define_column("2", TextTable::LEFT, TextTable::LEFT);
  t.define_column("3", TextTable::LEFT, TextTable::LEFT);

  // expect assertion failure on this, which throws FailedAssertion
  PrCtl unset_dumpable;
  ASSERT_DEATH((t << "1" << "2" << "3" << "4" << TextTable::endrow), "");
}