Skip to content

Instantly share code, notes, and snippets.

@arthurschreiber
Created August 7, 2008 22:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arthurschreiber/4512 to your computer and use it in GitHub Desktop.
Save arthurschreiber/4512 to your computer and use it in GitHub Desktop.
ActiveSupport::ArrayExtensions::flatten()
- should correctly flatten the passed multi-dimensional array down to a single-dimensional array
- should correctly flatten associative arrays
ActiveSupport::ArrayExtensions::first()
- should return the first item of the passed array
ActiveSupport::ArrayExtensions::last()
- should return the last item of the passed array
ActiveSupport::ArrayExtensions::extract_options()
- extracts the last element from the array and returns it if it's an associative array
ActiveSupport::ArrayExtensions::is_associative()
- should return true when the passed array is purely associative
- should return true when the passed array is not purely associative
- should return true when the passed array is not associative at all
Finished in 0.0022270679473877 seconds
8 examples, 0 failures
<?php
namespace FsckUnit;
require_once dirname(__FILE__) . "/spec_helper.php";
use ActiveSupport::ArrayExtensions::flatten;
describe("ActiveSupport::ArrayExtensions::flatten()", function() {
it("should correctly flatten the passed multi-dimensional array down to a single-dimensional array", function() {
$flattened = ActiveSupport::ArrayExtensions::flatten(array(1, 2, 3, array(4, 5, 6)));
expect($flattened)->to("equal", array(1, 2, 3, 4, 5, 6));
});
it("should correctly flatten associative arrays", function() {
$flattened = ActiveSupport::ArrayExtensions::flatten(array("a" => 1, "b" => 2, "c" => 3));
expect($flattened)->to("equal", array(1, 2, 3));
});
});
describe("ActiveSupport::ArrayExtensions::first()", function() {
it("should return the first item of the passed array", function() {
$first = ActiveSupport::ArrayExtensions::first(array(1, 2, 3));
expect($first)->to("equal", 1);
});
});
describe("ActiveSupport::ArrayExtensions::last()", function() {
it("should return the last item of the passed array", function() {
$last = ActiveSupport::ArrayExtensions::last(array(1, 2, 3));
expect($last)->to("equal", 3);
});
});
describe("ActiveSupport::ArrayExtensions::extract_options()", function() {
it("extracts the last element from the array and returns it if it's an associative array", function() {
$array = array("some", "elements", array("key" => "value"));
$options = ActiveSupport::ArrayExtensions::extract_options($array);
expect($array)->to("equal", array("some", "elements"));
expect($options)->to("equal", array("key" => "value"));
});
});
describe("ActiveSupport::ArrayExtensions::is_associative()", function() {
it("should return true when the passed array is purely associative", function() {
$is_associative = ActiveSupport::ArrayExtensions::is_associative(array("a" => "b", "c" => "d"));
expect($is_associative)->to("===", true);
});
it("should return true when the passed array is not purely associative", function() {
$is_associative = ActiveSupport::ArrayExtensions::is_associative(array("a", "b", "c" => "d"));
expect($is_associative)->to("===", false);
});
it("should return true when the passed array is not associative at all", function() {
$is_associative = ActiveSupport::ArrayExtensions::is_associative(array("a", "b", "c", "d"));
expect($is_associative)->to("===", false);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment