Programming Elixir 1.3: Functional |> Concurrent |> Pragmatic |> Fun by Dave Thomas
Author:Dave Thomas [Thomas, Dave]
Language: eng
Format: azw3, pdf
Tags: Pragmatic Bookshelf
Publisher: Pragmatic Bookshelf
Published: 2016-10-24T04:00:00+00:00
And here are the tests for it:
project/4/issues/test/table_formatter_test.exs
defmodule TableFormatterTest do
use ExUnit.Case # bring in the test functionality
import ExUnit.CaptureIO # And allow us to capture stuff sent to stdout
alias Issues.TableFormatter, as: TF
def simple_test_data do
[ [ c1: "r1 c1", c2: "r1 c2", c3: "r1 c3", c4: "r1+++c4" ],
[ c1: "r2 c1", c2: "r2 c2", c3: "r2 c3", c4: "r2 c4" ],
[ c1: "r3 c1", c2: "r3 c2", c3: "r3 c3", c4: "r3 c4" ],
[ c1: "r4 c1", c2: "r4++c2", c3: "r4 c3", c4: "r4 c4" ] ]
end
def headers, do: [ :c1, :c2, :c4 ]
def split_with_three_columns,
do: TF.split_into_columns(simple_test_data, headers)
test "split_into_columns" do
columns = split_with_three_columns
assert length(columns) == length(headers)
assert List.first(columns) == ["r1 c1", "r2 c1", "r3 c1", "r4 c1"]
assert List.last(columns) == ["r1+++c4", "r2 c4", "r3 c4", "r4 c4"]
end
test "column_widths" do
widths = TF.widths_of(split_with_three_columns)
assert widths == [ 5, 6, 7 ]
end
test "correct format string returned" do
assert TF.format_for([9, 10, 11]) == "~-9s | ~-10s | ~-11s~n"
end
test "Output is correct" do
result = capture_io fn ->
TF.print_table_for_columns(simple_test_data, headers)
end
assert result == """
c1 | c2 | c4
------+--------+--------
r1 c1 | r1 c2 | r1+++c4
r2 c1 | r2 c2 | r2 c4
r3 c1 | r3 c2 | r3 c4
r4 c1 | r4++c2 | r4 c4
"""
end
end
(Although you can’t see it here, the output we compare against in the last test contains trailing whitespace.)
Rather than clutter the process function in the CLI module with a long module name, I chose to use import to make the print function available without a module qualifier. This goes near the top of cli.ex.
import Issues.TableFormatter, only: [ print_table_for_columns: 2 ]
This code also uses a great Elixir testing feature. By importing ExUnit.CaptureIO, we get access to the capture_io function. This runs the code passed to it, but captures anything written to standard output, returning it as a string.
Download
Programming Elixir 1.3: Functional |> Concurrent |> Pragmatic |> Fun by Dave Thomas.pdf
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
The Mikado Method by Ola Ellnestam Daniel Brolund(20975)
Hello! Python by Anthony Briggs(20254)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(18615)
Dependency Injection in .NET by Mark Seemann(18408)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(17931)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(17690)
Kotlin in Action by Dmitry Jemerov(17589)
Adobe Camera Raw For Digital Photographers Only by Rob Sheppard(16939)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(16509)
Grails in Action by Glen Smith Peter Ledbrook(15653)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(10485)
Becoming a Dynamics 365 Finance and Supply Chain Solution Architect by Brent Dawson(8059)
Microservices with Go by Alexander Shuiskov(7823)
Practical Design Patterns for Java Developers by Miroslav Wengner(7725)
Test Automation Engineering Handbook by Manikandan Sambamurthy(7677)
Angular Projects - Third Edition by Aristeidis Bampakos(7166)
The Art of Crafting User Stories by The Art of Crafting User Stories(6613)
NetSuite for Consultants - Second Edition by Peter Ries(6536)
Demystifying Cryptography with OpenSSL 3.0 by Alexei Khlebnikov(6308)