aboutsummaryrefslogtreecommitdiffstats
path: root/tests/justify.bats
blob: 525c5d8e78ba662dcbbe3fb5ed064628ba7472cd (plain) (blame)
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
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/usr/bin/env bats

load 'vendor/bats-support/load'
load 'vendor/bats-assert/load'
load 'vendor/bats-file/load'

JUSTIFY_BIN="./build/justify"

@test "-h should show the usage mesage" {
  run "${JUSTIFY_BIN}" -h
  assert_success
  assert_output --partial "Usage:"
}

@test "-w should reject invalid values" {
  run "${JUSTIFY_BIN}" -w 0
  assert_failure
  assert_output --partial "Usage:"
  run "${JUSTIFY_BIN}" -w -1
  assert_failure
  assert_output --partial "Usage:"
}

@test "last line of the output is left aligned" {
  run "${JUSTIFY_BIN}" <<EOF
This is a test of the emergency broadcast system.
EOF
  assert_success
  assert_output "This is a test of the emergency broadcast system."
}

@test "lines are justified to both margins except last line" {
  run "${JUSTIFY_BIN}" <<EOF
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt
EOF
  assert_success
  expected_output="Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do  eiusmod
tempor incididunt"
  assert_output "$expected_output"
}

@test "lines are justified to both margins except last line with custom line width" {
  run "${JUSTIFY_BIN}" -w 30 <<EOF
This is a test of the emergency broadcast system.
EOF
  assert_success
  expected_output="This  is   a   test   of   the
emergency broadcast system."
  assert_output "$expected_output"
}

@test "words are not split if they fit" {
  run "${JUSTIFY_BIN}" -w 60 <<EOF
Hello world
EOF
  assert_success
  assert_output "Hello world"
}

@test "word longer than buffer is wrapped to next line" {
  run "${JUSTIFY_BIN}" -w 60 <<EOF
123456789012345678901234567890123456789012345678901234567890EXTRA
EOF
  assert_success
  expected_output="123456789012345678901234567890123456789012345678901234567890
EXTRA"
  assert_output "$expected_output"
}

@test "word longer than buffer is wrapped across multiple lines" {
  run "${JUSTIFY_BIN}" -w 20 <<EOF
verylongwordthatdoesnotfitinasinglelineandshouldbesplitacrossmultiplelines
EOF
  assert_success
  expected_output="verylongwordthatdoes
notfitinasinglelinea
ndshouldbesplitacros
smultiplelines"
  assert_output "$expected_output"
}

@test "multiple spaces in input are collapsed when on last left-aligned line" {
  run "${JUSTIFY_BIN}" <<EOF
This    is     spaced
EOF
  assert_success
  assert_output --partial "This is spaced"
}

@test "empty input produces no output" {
  run "${JUSTIFY_BIN}" <<EOF

EOF
  assert_success
  assert_output ""
}

@test "unicode text with accented characters is justified correctly" {
  run "${JUSTIFY_BIN}" -w 30 <<EOF
café résumé naïve Zürich another word
EOF
  assert_success
  expected_output="café   résumé   naïve   Zürich
another word"
  assert_output "$expected_output"
}

@test "unicode text with CJK characters is justified correctly" {
  run "${JUSTIFY_BIN}" -w 20 <<EOF
Hello 世界 test this is a longer line
EOF
  assert_success
  expected_output="Hello 世界 test this
is a longer line"
  assert_output "$expected_output"
}

@test "mixed ASCII and unicode text wraps correctly" {
  run "${JUSTIFY_BIN}" -w 25 <<EOF
This is café and résumé text that should wrap properly
EOF
  assert_success
  expected_output="This is café  and  résumé
text  that  should   wrap
properly"
  assert_output "$expected_output"
}

@test "unicode text on last line is left-aligned" {
  run "${JUSTIFY_BIN}" <<EOF
café résumé
EOF
  assert_success
  assert_output "café résumé"
}