File: flatten_spec.rb

package info (click to toggle)
ruby3.1 3.1.2-7%2Bdeb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 132,892 kB
  • sloc: ruby: 1,154,753; ansic: 736,782; yacc: 46,445; pascal: 10,401; sh: 3,931; cpp: 1,158; python: 838; makefile: 787; asm: 462; javascript: 382; lisp: 97; sed: 94; perl: 62; awk: 36; xml: 4
file content (62 lines) | stat: -rw-r--r-- 1,672 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
require_relative '../../spec_helper'

describe "Hash#flatten" do

  before :each do
    @h = {plato: :greek,
          witgenstein: [:austrian, :british],
          russell: :welsh}
  end

  it "returns an Array" do
    {}.flatten.should be_an_instance_of(Array)
  end

  it "returns an empty Array for an empty Hash" do
    {}.flatten.should == []
  end

  it "sets each even index of the Array to a key of the Hash" do
    a = @h.flatten
    a[0].should == :plato
    a[2].should == :witgenstein
    a[4].should == :russell
  end

  it "sets each odd index of the Array to the value corresponding to the previous element" do
    a = @h.flatten
    a[1].should == :greek
    a[3].should == [:austrian, :british]
    a[5].should == :welsh
  end

  it "does not recursively flatten Array values when called without arguments" do
    a = @h.flatten
    a[3].should == [:austrian, :british]
  end

  it "does not recursively flatten Hash values when called without arguments" do
    @h[:russell] = {born: :wales, influenced_by: :mill }
    a = @h.flatten
    a[5].should_not == {born: :wales, influenced_by: :mill }.flatten
  end

  it "recursively flattens Array values when called with an argument >= 2" do
    a = @h.flatten(2)
    a[3].should == :austrian
    a[4].should == :british
  end

  it "recursively flattens Array values to the given depth" do
    @h[:russell] = [[:born, :wales], [:influenced_by, :mill]]
    a = @h.flatten(2)
    a[6].should == [:born, :wales]
    a[7].should == [:influenced_by, :mill]
  end

  it "raises a TypeError if given a non-Integer argument" do
    -> do
      @h.flatten(Object.new)
    end.should raise_error(TypeError)
  end
end