diff --git a/test/wadler_separate_test.rb b/test/wadler_separate_test.rb index cb191d8..3b09c25 100644 --- a/test/wadler_separate_test.rb +++ b/test/wadler_separate_test.rb @@ -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 diff --git a/test/wadler_surround_test.rb b/test/wadler_surround_test.rb index f9dafec..843e20d 100644 --- a/test/wadler_surround_test.rb +++ b/test/wadler_surround_test.rb @@ -3,179 +3,198 @@ require_relative 'lib' describe 'surround' do - it 'prints lft and rg only' do - width = 10 - block = proc { |out| - out.surround('{', '}').text('!') - } - assert_wadler width, '{}!', block - end - - it 'accepts a block' do - width = 10 - block = proc { |out| - out.surround('{', '}') { - out.text '1' - } - } - assert_wadler width, '{1}', block - end - - it 'breaks on left and right inconsistently by default' do - width = 10 - block = proc { |out| - out.text 'A long long string' - out.surround('{', '}') { - out.text '1' - } - } - assert_wadler width, <<~OUT.chomp, block - A long long string{ - 1} - OUT - end - - it 'force breaks on left' do - width = 10 - block = proc { |out| - out.text 'A long long string' - out.surround('{', '}', lft_force_break: true) { - out.text '1' - } - } - assert_wadler width, <<~OUT.chomp, block - A long long string{ - 1} - OUT - block = proc { |out| - out.text 'A long long string' - out.surround('{', '}', indent: 2, lft_force_break: true) { - out.text '1' - } - } - assert_wadler width, <<~OUT.chomp, block - A long long string{ + [ + { + title: 'prints lft and rg only', + block: proc { |out| + out.surround('{', '}').text('!') + }, + expected: '{}!', + }, + { + title: 'accepts a block', + block: proc { |out| + out.surround('{', '}') { + out.text '1' + } + }, + expected: '{1}', + }, + { + title: 'breaks on left and right inconsistently by default', + block: proc { |out| + out.text 'A long long string' + out.surround('{', '}') { + out.text '1' + } + }, + expected: <<~OUT.chomp, + A long long string{ 1} - OUT - end - - it 'force breaks on right' do - width = 10 - block = proc { |out| - out.text 'A long long string' - out.surround('{', '}', rgt_force_break: true) { - out.text '1' - } - } - assert_wadler width, <<~OUT.chomp, block - A long long string{ - 1 - } - OUT - block = proc { |out| - out.text 'A long long string' - out.surround('{', '}', indent: 2, rgt_force_break: true) { - out.text '1' - } - } - assert_wadler width, <<~OUT.chomp, block - A long long string{ + OUT + }, + { + title: 'force breaks on left', + block: proc { |out| + out.text 'A long long string' + out.surround('{', '}', lft_force_break: true) { + out.text '1' + } + }, + expected: <<~OUT.chomp, + A long long string{ + 1} + OUT + }, + { + title: 'force breaks on left with indent', + block: proc { |out| + out.text 'A long long string' + out.surround('{', '}', indent: 2, lft_force_break: true) { + out.text '1' + } + }, + expected: <<~OUT.chomp, + A long long string{ + 1} + OUT + }, + { + title: 'force breaks on right', + block: proc { |out| + out.text 'A long long string' + out.surround('{', '}', rgt_force_break: true) { + out.text '1' + } + }, + expected: <<~OUT.chomp, + A long long string{ 1 } - OUT - end - - it 'prevents breaks on left' do - width = 10 - block = proc { |out| - out.text 'A long long string' - out.surround('{', '}', lft_can_break: false) { - out.text '1' - } - } - assert_wadler width, <<~OUT.chomp, block - A long long string{1 - } - OUT - block = proc { |out| - out.text 'A long long string' - out.surround('{', '}', indent: 2, lft_can_break: false) { - out.text '1' - } - } - assert_wadler width, <<~OUT.chomp, block - A long long string{1 + OUT + }, + { + title: 'force breaks on right with indent', + block: proc { |out| + out.text 'A long long string' + out.surround('{', '}', indent: 2, rgt_force_break: true) { + out.text '1' } - OUT - end - - it 'prevents breaks on right' do - width = 10 - block = proc { |out| - out.text 'A long long string' - out.surround('{', '}', rgt_can_break: false) { - out.text '1' - } - } - assert_wadler width, <<~OUT.chomp, block - A long long string{ - 1} - OUT - block = proc { |out| - out.text 'A long long string' - out.surround('{', '}', indent: 2, rgt_can_break: false) { - out.text '1' - } - } - assert_wadler width, <<~OUT.chomp, block - A long long string{ + }, + expected: <<~OUT.chomp, + A long long string{ + 1 + } + OUT + }, + { + title: 'prevents breaks on left', + block: proc { |out| + out.text 'A long long string' + out.surround('{', '}', lft_can_break: false) { + out.text '1' + } + }, + expected: <<~OUT.chomp, + A long long string{1 + } + OUT + }, + { + title: 'prevents breaks on left with indent', + block: proc { |out| + out.text 'A long long string' + out.surround('{', '}', indent: 2, lft_can_break: false) { + out.text '1' + } + }, + expected: <<~OUT.chomp, + A long long string{1 + } + OUT + }, + { + title: 'prevents breaks on right', + block: proc { |out| + out.text 'A long long string' + out.surround('{', '}', rgt_can_break: false) { + out.text '1' + } + }, + expected: <<~OUT.chomp, + A long long string{ 1} - OUT - end - - it 'can change breakables' do - width = 10 - block = proc { |out| - out.surround('{', '}', lft_breakable: '<', rgt_breakable: '>') { - out.text '1' - } - } - assert_wadler width, '{<1>}', block - block = proc { |out| - out.text 'A long long string' - out.surround('{', '}', lft_breakable: '<', rgt_breakable: '>') { - out.text '1' - } - } - assert_wadler width, <<~OUT.chomp, block - A long long string{ - 1>} - OUT - block = proc { |out| - out.text 'A long long string' - out.surround('{', '}', lft_breakable: '<', lft_can_break: false, rgt_breakable: '>') { - out.text '1' - } - } - assert_wadler width, <<~OUT.chomp, block - A long long string{1 - } - OUT - block = proc { |out| - out.text 'A long long string' - out.surround('{', '}', lft_breakable: '<', rgt_breakable: '>', rgt_can_break: false) { - out.text '1' - } - } - assert_wadler width, <<~OUT.chomp, block - A long long string{ - 1} - OUT + OUT + }, + { + title: 'prevents breaks on right with indent', + block: proc { |out| + out.text 'A long long string' + out.surround('{', '}', indent: 2, rgt_can_break: false) { + out.text '1' + } + }, + expected: <<~OUT.chomp, + A long long string{ + 1} + OUT + }, + { + title: 'can change breakable', + block: proc { |out| + out.surround('{', '}', lft_breakable: '<', rgt_breakable: '>') { + out.text '1' + } + }, + expected: '{<1>}', + }, + { + title: 'can change breakable and breaks on left and right inconsistently by default', + block: proc { |out| + out.text 'A long long string' + out.surround('{', '}', lft_breakable: '<', rgt_breakable: '>') { + out.text '1' + } + }, + expected: <<~OUT.chomp, + A long long string{ + 1>} + OUT + }, + { + title: 'can change breakable and prevent left break', + block: proc { |out| + out.text 'A long long string' + out.surround('{', '}', lft_breakable: '<', lft_can_break: false, rgt_breakable: '>') { + out.text '1' + } + }, + expected: <<~OUT.chomp, + A long long string{1 + } + OUT + }, + { + title: 'can change breakable and prevent right break', + block: proc { |out| + out.text 'A long long string' + out.surround('{', '}', lft_breakable: '<', rgt_breakable: '>', rgt_can_break: false) { + out.text '1' + } + }, + expected: <<~OUT.chomp, + A long long string{ + 1} + OUT + }, + ].each do |test| + it test[:title] do + assert_wadler 10, test[:expected], test[:block] + end end end describe 'helpers for surround' do - width = 10 padding_no = ->(name) { proc { |out| out.send(name) { @@ -196,36 +215,49 @@ [:brackets, %w{[ ]}], [:parens, %w[( )]], ].each do |name, chars| - it "prints #{name} w/o padding" do - assert_wadler width, "#{chars[0]}1#{chars[1]}", padding_no.(name) - end - - it "prints #{name} w padding" do - assert_wadler width, "#{chars[0]}~1~#{chars[1]}", padding_yes.(name) - end - - it "prints #{name}_break_both w/o padding" do - assert_wadler width, <<~OUT.chomp, padding_no.("#{name}_break_both") - #{chars[0]} - 1 - #{chars[1]} - OUT - end - - it "prints #{name}_break_both w padding but removes them" do - assert_wadler width, <<~OUT.chomp, padding_yes.("#{name}_break_both") - #{chars[0]} - 1 - #{chars[1]} - OUT - end - - it "prints #{name}_break_non w/o padding but removes them" do - assert_wadler width, "#{chars[0]}1#{chars[1]}", padding_no.("#{name}_break_none") - end - - it "prints #{name}_break_non w padding but removes them" do - assert_wadler width, "#{chars[0]}1#{chars[1]}", padding_yes.("#{name}_break_none") + [ + { + title: "prints #{name} w/o padding", + block: padding_no.(name), + expected: "#{chars[0]}1#{chars[1]}", + }, + { + title: "prints #{name} w padding", + block: padding_yes.(name), + expected: "#{chars[0]}~1~#{chars[1]}", + }, + { + title: "prints #{name}_break_both w/o padding", + block: padding_no.("#{name}_break_both"), + expected: <<~OUT.chomp, + #{chars[0]} + 1 + #{chars[1]} + OUT + }, + { + title: "prints #{name}_break_both w padding but removes them", + block: padding_yes.("#{name}_break_both"), + expected: <<~OUT.chomp, + #{chars[0]} + 1 + #{chars[1]} + OUT + }, + { + title: "prints #{name}_break_non w/o padding but removes them", + block: padding_no.("#{name}_break_none"), + expected: "#{chars[0]}1#{chars[1]}", + }, + { + title: "prints #{name}_break_non w padding but removes them", + block: padding_yes.("#{name}_break_none"), + expected: "#{chars[0]}1#{chars[1]}", + }, + ].each do |test| + it test[:title] do + assert_wadler 10, test[:expected], test[:block] + end end end @@ -234,8 +266,16 @@ [:quote_double, '"'], [:quote_single, "'"], ].each do |name, char| - it "prints #{name}" do - assert_wadler width, "#{char}1#{char}", padding_no.(name) + [ + { + title: "prints #{name}", + block: padding_no.(name), + expected: "#{char}1#{char}", + }, + ].each do |test| + it test[:title] do + assert_wadler 10, test[:expected], test[:block] + end end end end