Skip to content

Commit

Permalink
Add key/value pairs (hashes) data table conversion (close #12)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbpros committed Oct 19, 2011
1 parent f91f3a7 commit 2ed060a
Show file tree
Hide file tree
Showing 7 changed files with 194 additions and 13 deletions.
2 changes: 1 addition & 1 deletion features/cucumber-features
Submodule cucumber-features updated from 7ef49e to 29bd62
22 changes: 13 additions & 9 deletions features/step_definitions/cucumber_js_mappings.rb
Expand Up @@ -61,7 +61,7 @@ def write_mapping_calling_world_function(step_name)
append_step_definition(step_name, step_def)
end

def write_mapping_receiving_data_table(step_name)
def write_mapping_receiving_data_table_as_raw(step_name)
body = <<-EOF
var dataTableArray = dataTable.raw();
var dataTableJSON = JSON.stringify(dataTableArray);
Expand All @@ -71,6 +71,16 @@ def write_mapping_receiving_data_table(step_name)
append_step_definition(step_name, body, ["dataTable"])
end

def write_mapping_receiving_data_table_as_hashes(step_name)
body = <<-EOF
var dataTableHashes = dataTable.hashes();
var dataTableJSON = JSON.stringify(dataTableHashes);
fs.writeFileSync("#{DATA_TABLE_LOG_FILE}", "" + dataTableJSON);
callback();
EOF
append_step_definition(step_name, body, ["dataTable"])
end

def write_calculator_code
rpn_calculator_code = get_file_contents('../support/rpn_calculator.js')
create_dir 'features/support'
Expand Down Expand Up @@ -142,11 +152,11 @@ def assert_world_function_called
check_file_presence [WORLD_FUNCTION_LOG_FILE], true
end

def assert_data_table_equals_array_source(array_source)
def assert_data_table_equals_json(json)
prep_for_fs_check do
log_file_contents = IO.read(DATA_TABLE_LOG_FILE)
actual_array = JSON.parse(log_file_contents)
expected_array = JSON.parse(array_source)
expected_array = JSON.parse(json)
actual_array.should == expected_array
end
end
Expand Down Expand Up @@ -198,11 +208,5 @@ def get_file_contents(file_path)
f.read
end
end

def indent_code(code, levels = 1)
indented_code = ''
code.each_line { |line| indented_code += "#{' ' * levels}#{line}" }
indented_code
end
end
World(CucumberJsMappings)
7 changes: 7 additions & 0 deletions lib/cucumber/ast/data_table.js
Expand Up @@ -19,6 +19,13 @@ var DataTable = function() {
rawRows.push(rawRow);
});
return rawRows;
},

hashes: function hashes() {
var raw = self.raw();
var hashDataTable = Cucumber.Type.HashDataTable(raw);
var rawHashDataTable = hashDataTable.raw();
return rawHashDataTable;
}
};
return self;
Expand Down
7 changes: 4 additions & 3 deletions lib/cucumber/type.js
@@ -1,3 +1,4 @@
var Type = {};
Type.Collection = require('./type/collection');
module.exports = Type;
var Type = {};
Type.Collection = require('./type/collection');
Type.HashDataTable = require('./type/hash_data_table');
module.exports = Type;
41 changes: 41 additions & 0 deletions lib/cucumber/type/hash_data_table.js
@@ -0,0 +1,41 @@
var HashDataTable = function(rawArray) {
var self = {
raw: function raw() {
var hashKeys = self.getHashKeys();
var hashValueArrays = self.getHashValueArrays();
var hashes = self.createHashesFromKeysAndValueArrays(hashKeys, hashValueArrays);
return hashes;
},

getHashKeys: function getHashKeys() {
return rawArray[0];
},

getHashValueArrays: function getHashValueArrays() {
var _rawArray = [].concat(rawArray);
_rawArray.shift();
return _rawArray;
},

createHashesFromKeysAndValueArrays: function createHashesFromKeysAndValueArrays(keys, valueArrays) {
var hashes = [];
valueArrays.forEach(function(values) {
var hash = self.createHashFromKeysAndValues(keys, values);
hashes.push(hash);
});
return hashes;
},

createHashFromKeysAndValues: function createHashFromKeysAndValues(keys, values) {
var hash = {};
var len = keys.length;
for (var i = 0; i < len; i++) {
hash[keys[i]] = values[i];
}
return hash;
}
};
return self;
};

module.exports = HashDataTable;
31 changes: 31 additions & 0 deletions spec/cucumber/ast/data_table_spec.js
Expand Up @@ -62,4 +62,35 @@ describe("Cucumber.Ast.DataTable", function() {
expect(dataTable.raw()).toEqual(rawRows);
});
});

describe("hashes", function() {
var raw, hashDataTable;

beforeEach(function() {
raw = createSpy("raw data table");
rawHashDataTable = createSpy("raw hash data table");
hashDataTable = createSpyWithStubs("hash data table", {raw: rawHashDataTable});
spyOn(dataTable, 'raw').andReturn(raw);
spyOn(Cucumber.Type, 'HashDataTable').andReturn(hashDataTable);
});

it("gets the raw representation of the data table", function() {
dataTable.hashes();
expect(dataTable.raw).toHaveBeenCalled();
});

it("creates a hash data table based on the raw representation", function() {
dataTable.hashes();
expect(Cucumber.Type.HashDataTable).toHaveBeenCalledWith(raw);
});

it("gets the raw representation of the hash data table", function() {
dataTable.hashes();
expect(hashDataTable.raw).toHaveBeenCalled();
});

it("returns the raw hash data table", function() {
expect(dataTable.hashes()).toBe(rawHashDataTable);
});
});
});
97 changes: 97 additions & 0 deletions spec/cucumber/type/hash_data_table_spec.js
@@ -0,0 +1,97 @@
require('../../support/spec_helper');

describe("Cucumber.Type.HashDataTable", function() {
var Cucumber = require('cucumber');

var hashDataTable, rawArray;

beforeEach(function() {
rawArray = [
['key1', 'key2'],
['value1-1', 'value1-2'],
['value2-1', 'value2-2']
];
hashDataTable = Cucumber.Type.HashDataTable(rawArray);
});

describe("raw()", function() {
var hashKeys, hashValueArrays, hashes;

beforeEach(function() {
hashKeys = createSpy("hash keys");
hashValueArrays = createSpy("hash value arrays");
hashes = createSpy("hashes");
spyOn(hashDataTable, 'getHashKeys').andReturn(hashKeys);
spyOn(hashDataTable, 'getHashValueArrays').andReturn(hashValueArrays);
spyOn(hashDataTable, 'createHashesFromKeysAndValueArrays').andReturn(hashes);
});

it("gets the keys of the hashes", function() {
hashDataTable.raw();
expect(hashDataTable.getHashKeys).toHaveBeenCalled();
});

it("gets the values of the hashes", function() {
hashDataTable.raw();
expect(hashDataTable.getHashValueArrays).toHaveBeenCalled();
});

it("creates the hashes from the keys and values", function() {
hashDataTable.raw();
expect(hashDataTable.createHashesFromKeysAndValueArrays).toHaveBeenCalledWith(hashKeys, hashValueArrays);
});

it("returns the hashes", function() {
expect(hashDataTable.raw()).toBe(hashes);
});
});

describe("getHashKeys()", function() {
it("returns the first row of the raw array", function() {
expect(hashDataTable.getHashKeys()).toBe(rawArray[0]);
});
});

describe("getHashValueArrays()", function() {
it("returns all but the first raw of the raw array", function() {
expect(hashDataTable.getHashValueArrays()).toEqual([rawArray[1], rawArray[2]]);
});

it("does not alter the original raw array", function() {
hashDataTable.getHashValueArrays();
expect(rawArray.length).toBe(3);
});
});

describe("createHashesFromKeysAndValueArrays()", function() {
var hashes;

beforeEach(function() {
keys = [createSpy("key 1"), createSpy("key 2")];
valueArrays = [createSpy("value array 1"), createSpy("value array 2")];
hashes = [createSpy("first hash"), createSpy("second hash")];
spyOn(hashDataTable, 'createHashFromKeysAndValues').andReturnSeveral(hashes);
});

it("creates a hash for each keys/values", function() {
hashDataTable.createHashesFromKeysAndValueArrays(keys, valueArrays);
expect(hashDataTable.createHashFromKeysAndValues).toHaveBeenCalledWith(keys, valueArrays[0]);
expect(hashDataTable.createHashFromKeysAndValues).toHaveBeenCalledWith(keys, valueArrays[1]);
});

it("returns the hashes", function() {
var actual = hashDataTable.createHashesFromKeysAndValueArrays(keys, valueArrays);
expect(actual).toEqual(hashes);
});
});

describe("createHashFromKeysAndValues()", function() {
it("returns the combined keys and values as a hash", function() {
var keys = ["key1", "key2"];
var values = ["value1", "value2"];
var actual = hashDataTable.createHashFromKeysAndValues(keys, values);
var expected = { key1: "value1", key2: "value2" };
expect(actual).toEqual(expected);
});
});
});

0 comments on commit 2ed060a

Please sign in to comment.