Skip to content

Commit

Permalink
Added tests for most of the examples in the README.
Browse files Browse the repository at this point in the history
  • Loading branch information
johncarney committed Mar 6, 2015
1 parent 4a5aeb9 commit a2b0967
Show file tree
Hide file tree
Showing 3 changed files with 185 additions and 6 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ addresses:

We can describe it as follows:

definition = Pikelet.define signature_field: :record_type do
record_type 0...4
definition = Pikelet.define signature_field: :type do
type 0...4

record "NAME" do
first_name 4...14
Expand All @@ -126,13 +126,13 @@ record's type signature as a parameter and a block describing its fields.
When we parse the data, we end up with this:

#<struct
record_type="NAME",
type="NAME",
first_name="Frida",
last_name="Kahlo">,
#<struct
record_type="ADDR",
type="ADDR",
street_address="123 South Street",
city="Sometoen",
city="Sometown",
postal_code="45678Y",
state="Someplace">

Expand Down Expand Up @@ -215,7 +215,7 @@ You can supply a custom formatter for a field.

definition = Pikelet.define do
username 0...10, format: :downcase
password 10...53, format: ->(v) { Digest::SHA1.hexdigest(v) }
password 10...50, format: ->(v) { Digest::SHA1.hexdigest(v) }
end

definition.format([
Expand Down
177 changes: 177 additions & 0 deletions spec/readme_examples_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
require "spec_helper"
require "pikelet"

describe "README Examples:" do
def strip_data(text)
text.gsub(/^#{text.scan(/^[ \t]*(?=\S)/).min || ''}/, '').gsub('|', '').split("\n")
end

RSpec::Matchers.define :match do |expected|
def record_matches_hash?(record, hash)
hash.all? { |attr, value| record.send(attr) == value }
end

match do |actual|
actual.zip(expected).all? do |actual, expected|
record_matches_hash?(actual, expected)
end
end
end

shared_examples_for "parse the data" do
subject { definition.parse(strip_data(data)) }

it { is_expected.to match expected_records }
end

shared_examples_for "format the records" do
subject { definition.format(records) }

it { is_expected.to eq strip_data(expected_data) }
end

describe "Homogeneous records" do
let(:definition) do
Pikelet.define do
first_name 0...10
last_name 10...20
end
end

let(:data) do
<<-DATA
|Grace |Hopper |
|Ada |Lovelace |
DATA
end

let(:expected_records) do
[
{ first_name: "Grace", last_name: "Hopper" },
{ first_name: "Ada", last_name: "Lovelace" }
]
end

it_will "parse the data"
end

describe "Heterogeneous records" do
let(:definition) do
Pikelet.define signature_field: :type do
type 0...4

record "NAME" do
first_name 4...14
last_name 14...24
end

record "ADDR" do
street_address 4...24
city 24...44
postal_code 44...54
state 54...74
end
end
end

let(:data) do
<<-DATA
|NAME|Frida |Kahlo |
|ADDR|123 South Street |Sometown |45678Y |Someplace |
DATA
end

let(:expected_records) do
[
{ type: "NAME", first_name: "Frida", last_name: "Kahlo" },
{ type: "ADDR", street_address: "123 South Street", city: "Sometown", postal_code: "45678Y", state: "Someplace" }
]
end

it_will "parse the data"
end

describe "Inheritance" do
let(:definition) do
Pikelet.define signature_field: :record_type do
record_type 0...5

record "NAME" do
first_name 5...15
last_name 15...25

record "NAME+" do
middle_name 25...35
end
end
end
end

let(:data) do
<<-DATA
|NAME |Rosa |Parks |
|NAME+|Rosalind |Franklin |Elsie |
DATA
end

let(:expected_records) do
[
{ record_type: "NAME", first_name: "Rosa", last_name: "Parks" },
{ record_type: "NAME+", first_name: "Rosalind", last_name: "Franklin", middle_name: "Elsie" }
]
end

it_will "parse the data"
end

describe "Custom field parsing" do
let(:definition) do
Pikelet.define do
a_number(0... 4) { |value| value.to_i }

another_number 4... 8, &:to_i
yet_another_number 8...12, parse: ->(value) { value.to_i }
some_text 12...20, parse: :upcase
end
end

let(:data) do
<<-DATA
| 67| 3| 999|blah |
DATA
end

let(:expected_records) do
[
{ a_number: 67, another_number: 3, yet_another_number: 999, some_text: "BLAH" }
]
end

it_will "parse the data"
end

describe "Custom field formatting" do
let(:definition) do
Pikelet.define do
username 0...10, format: :downcase
password 10...50, format: ->(v) { Digest::SHA1.hexdigest(v) }
end
end

let(:records) do
[
OpenStruct.new(username: "Coleman", password: "password"),
OpenStruct.new(username: "Savitskaya", password: "sekrit" )
]
end

let(:expected_data) do
<<-DATA
|coleman |5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8|
|savitskaya|8d42e738c7adee551324955458b5e2c0b49ee655|
DATA
end

it_will "format the records"
end
end
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,6 @@
# a real object. This is generally recommended.
mocks.verify_partial_doubles = true
end

config.alias_it_behaves_like_to :it_will, "it will"
end

0 comments on commit a2b0967

Please sign in to comment.