File: gyoku_spec.rb

package info (click to toggle)
ruby-gyoku 1.3.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 184 kB
  • sloc: ruby: 771; makefile: 2
file content (84 lines) | stat: -rw-r--r-- 2,530 bytes parent folder | download | duplicates (3)
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
require "spec_helper"

describe Gyoku do

  describe ".xml_tag" do
    it "translates Symbols to lowerCamelCase by default" do
      tag = Gyoku.xml_tag(:user_name)
      expect(tag).to eq("userName")
    end

    it "does not translate Strings" do
      tag = Gyoku.xml_tag("user_name")
      expect(tag).to eq("user_name")
    end

    it "translates Symbols by a given key_converter" do
      tag = Gyoku.xml_tag(:user_name, :key_converter => :upcase)
      expect(tag).to eq("USER_NAME")
    end

    it "does not translates Strings with a given key_converter" do
      tag = Gyoku.xml_tag("user_name", :key_converter => :upcase)
      expect(tag).to eq("user_name")
    end
  end

  describe ".xml" do
    it "translates a given Hash to XML" do
      hash = { :id => 1 }
      xml = Gyoku.xml(hash, :element_form_default => :qualified)

      expect(xml).to eq("<id>1</id>")
    end

    it "accepts a key_converter for the Hash keys" do
      hash = { :user_name => "finn", :pass_word => "secret" }
      xml = Gyoku.xml(hash, {key_converter: :upcase})

      expect(xml).to include("<USER_NAME>finn</USER_NAME>")
      expect(xml).to include("<PASS_WORD>secret</PASS_WORD>")
    end

    it "don't converts Strings keys" do
      hash = { :user_name => "finn", "pass_word" => "secret" }
      xml = Gyoku.xml(hash, {key_converter: :upcase})

      expect(xml).to include("<USER_NAME>finn</USER_NAME>")
      expect(xml).to include("<pass_word>secret</pass_word>")
    end

    it "when defined key_to_convert only convert this key" do
      hash = { user_name: "finn", pass_word: "secret" }
      options = {key_converter: :upcase, key_to_convert: 'user_name'}
      xml = Gyoku.xml(hash, options)

      expect(xml).to include("<USER_NAME>finn</USER_NAME>")
      expect(xml).to include("<passWord>secret</passWord>")
    end

    it "accepts key_converter for nested hash" do
      hash = { user: { user_name: "finn", pass_word: "secret" }}
      xml = Gyoku.xml(hash, {key_converter: :upcase})

      expect(xml).to include("<USER><USER_NAME>finn</USER_NAME>")
      expect(xml).to include("<PASS_WORD>secret</PASS_WORD></USER>")
    end

    it "does not modify the original Hash" do
      hash = {
        :person => {
          :first_name => "Lucy",
          :last_name => "Sky",
          :order! => [:first_name, :last_name]
        },
        :attributes! => { :person => { :id => "666" } }
      }
      original_hash = hash.dup

      Gyoku.xml(hash)
      expect(original_hash).to eq(hash)
    end
  end

end