Skip to content

Commit

Permalink
test: refactor surround and separate tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Amine Mike committed Jan 7, 2025
1 parent 7d8a4b2 commit 9fbc705
Show file tree
Hide file tree
Showing 2 changed files with 389 additions and 347 deletions.
304 changes: 153 additions & 151 deletions test/wadler_separate_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,163 +3,165 @@
require_relative 'lib'

describe 'separate' do
it 'does nothing for singletons' do
width = 10
block = proc { |out|
out.separate(%w[1], ',') { |i|
out.text i
}
}
assert_wadler width, '1', block
end

it 'adds separator for non-sigletons' do
width = 10
block = proc { |out|
out.separate(%w[1 2 3], ',') { |i|
out.text i
}
}
assert_wadler width, '1, 2, 3', block
end

it 'breaks consistently by default' do
width = 10
block = proc { |out|
out.separate((1..10).map(&:to_s), ',') { |i|
out.text i
}
}
assert_wadler width, <<~OUT.chomp, block
1,
2,
3,
4,
5,
6,
7,
8,
9,
10
OUT
end

it 'breaks inconsistently' do
width = 10
block = proc { |out|
out.separate((1..10).map(&:to_s), ',', break_type: :inconsistent) { |i|
out.text i
}
}
assert_wadler width, <<~OUT.chomp, block
1, 2, 3,
4, 5, 6,
7, 8, 9,
10
OUT
end

it 'breaks consistently before the separator' do
width = 10
block = proc { |out|
out.separate((1..10).map(&:to_s), ',', break_pos: :before) { |i|
out.text i
}
}
assert_wadler width, <<~OUT.chomp, block
1
,2
,3
,4
,5
,6
,7
,8
,9
,10
OUT
end

it 'breaks inconsistently before the separator' do
width = 10
block = proc { |out|
out.separate((1..10).map(&:to_s), ',', break_pos: :before, break_type: :inconsistent) { |i|
out.text i
}
}
assert_wadler width, <<~OUT.chomp, block
1 ,2 ,3 ,4
,5 ,6 ,7
,8 ,9 ,10
OUT
end

it 'indents when using a Boolean' do
width = 10
block = proc { |out|
out.separate((1..10).map(&:to_s), ',', break_type: :inconsistent, indent: true) { |i|
out.text i
}
}
assert_wadler width, <<~OUT.chomp, block, indent: 4
1, 2, 3,
4, 5,
6, 7,
8, 9,
10
OUT
end

it 'indents when using an Integer' do
width = 10
block = proc { |out|
out.separate((1..10).map(&:to_s), ',', break_type: :inconsistent, indent: 2) { |i|
out.text i
}
}
assert_wadler width, <<~OUT.chomp, block
1, 2, 3,
[
{
title: 'does nothing for singletons',
block: proc { |out|
out.separate(%w[1], ',') { |i|
out.text i
}
},
expected: '1',
},
{
title: 'adds separator for non-sigletons',
block: proc { |out|
out.separate(%w[1 2 3], ',') { |i|
out.text i
}
},
expected: '1, 2, 3',
},
{
title: 'breaks consistently by default',
block: proc { |out|
out.separate((1..10).map(&:to_s), ',') { |i|
out.text i
}
},
expected: <<~OUT.chomp,
1,
2,
3,
4,
5,
6,
7,
8,
9,
10
OUT
},
{
title: 'breaks inconsistently',
block: proc { |out|
out.separate((1..10).map(&:to_s), ',', break_type: :inconsistent) { |i|
out.text i
}
},
expected: <<~OUT.chomp,
1, 2, 3,
4, 5, 6,
7, 8, 9,
10
OUT
OUT
},
{
title: 'breaks consistently before the separator',
block: proc { |out|
out.separate((1..10).map(&:to_s), ',', break_pos: :before) { |i|
out.text i
}
},
expected: <<~OUT.chomp,
1
,2
,3
,4
,5
,6
,7
,8
,9
,10
OUT
},
{
title: 'breaks inconsistently before the separator',
block: proc { |out|
out.separate((1..10).map(&:to_s), ',', break_pos: :before, break_type: :inconsistent) { |i|
out.text i
}
},
expected: <<~OUT.chomp,
1 ,2 ,3 ,4
,5 ,6 ,7
,8 ,9 ,10
OUT
},
{
title: 'indents when using a Boolean',
block: proc { |out|
out.separate((1..10).map(&:to_s), ',', break_type: :inconsistent, indent: true) { |i|
out.text i
}
},
expected: <<~OUT.chomp,
1, 2, 3,
4, 5,
6, 7,
8, 9,
10
OUT
indent: 4,
},
{
title: 'indents when using an Integer',
block: proc { |out|
out.separate((1..10).map(&:to_s), ',', break_type: :inconsistent, indent: 2) { |i|
out.text i
}
},
expected: <<~OUT.chomp,
1, 2, 3,
4, 5, 6,
7, 8, 9,
10
OUT
},
].each do |test|
it test[:title] do
assert_wadler 10, test[:expected], test[:block], indent: test[:indent] || 0
end
end

# TODO: the rest of the params
# 1. breaking inconsistently :before deos not make sense.
end

describe 'helpers built on separate' do
it 'creates lines from a list' do
width = 10
block = proc { |out|
out.lines((1..10).map(&:to_s), ',') { |i|
out.text i
}
}
assert_wadler width, <<~OUT.chomp, block
1,
2,
3,
4,
5,
6,
7,
8,
9,
10
OUT
end

it 'concatenates args from a list' do
width = 10
block = proc { |out|
out.concat((1..10).map(&:to_s), ',') { |i|
out.text i
}
}
assert_wadler width, <<~OUT.chomp, block
1,2,3,4,5,6,7,8,9,10
OUT
[
{
title: 'creates lines from a list',
block: proc { |out|
out.lines((1..10).map(&:to_s), ',') { |i|
out.text i
}
},
expected: <<~OUT.chomp,
1,
2,
3,
4,
5,
6,
7,
8,
9,
10
OUT
},
{
title: 'concatenates args from a list',
block: proc { |out|
out.concat((1..10).map(&:to_s), ',') { |i|
out.text i
}
},
expected: <<~OUT.chomp,
1,2,3,4,5,6,7,8,9,10
OUT
},
].each do |test|
it test[:title] do
assert_wadler 10, test[:expected], test[:block], indent: test[:indent] || 0
end
end
end
Loading

0 comments on commit 9fbc705

Please sign in to comment.