Skip to content
This repository
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 233 lines (184 sloc) 5.532 kb
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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
require 'test/unit'
require 'rubygems'
require 'wunderbar'

# these tests were "borrowed"/adapted from jbuilder so as to demonstrate
# functional parity:
# https://github.com/rails/jbuilder/blob/master/test/jbuilder_test.rb

class JbuilderTest < Test::Unit::TestCase
  def setup
    @j = Wunderbar::JsonBuilder.new(Struct.new(:params).new({}))
  end

  def test_single_key
    parsed = @j.encode do
      _content "hello"
    end
    
    assert_equal "hello", parsed["content"]
  end

  def test_single_key_with_false_value
    parsed = @j.encode do
      _content false
    end

    assert_equal false, parsed["content"]
  end

  def test_single_key_with_nil_value
    parsed = @j.encode do
      _content nil
    end

    assert parsed.has_key?("content")
    assert_equal nil, parsed["content"]
  end

  def test_multiple_keys
    parsed = @j.encode do
      _title "hello"
      _content "world"
    end
    
    assert_equal "hello", parsed["title"]
    assert_equal "world", parsed["content"]
  end
  
  def test_extracting_from_object
    person = Struct.new(:name, :age).new("David", 32)
    
    parsed = @j.encode do
      _ person, :name, :age
    end
    
    assert_equal "David", parsed["name"]
    assert_equal 32, parsed["age"]
  end
  
  def test_nesting_single_child_with_block
    parsed = @j.encode do
      _author do
        _name "David"
        _age 32
      end
    end
    
    assert_equal "David", parsed["author"]["name"]
    assert_equal 32, parsed["author"]["age"]
  end
  
  def test_nesting_multiple_children_with_block
    parsed = @j.encode do
      _comments do
        _ { _content "hello" }
        _ { _content "world" }
      end
    end

    assert_equal "hello", parsed["comments"][0]["content"]
    assert_equal "world", parsed["comments"][1]["content"]
  end
  
  def test_nesting_single_child_with_inline_extract
    person = Class.new do
      attr_reader :name, :age
      
      def initialize(name, age)
        @name, @age = name, age
      end
    end.new("David", 32)
    
    parsed = @j.encode do
      _author person, :name, :age
    end
    
    assert_equal "David", parsed["author"]["name"]
    assert_equal 32, parsed["author"]["age"]
  end
  
  def test_nesting_multiple_children_from_array
    comments = [ Struct.new(:content, :id).new("hello", 1),
                 Struct.new(:content, :id).new("world", 2) ]
    
    parsed = @j.encode do
      _comments comments, :content
    end
    
    assert_equal ["content"], parsed["comments"].first.keys
    assert_equal "hello", parsed["comments"][0]["content"]
    assert_equal "world", parsed["comments"][1]["content"]
  end
  
  def test_nesting_multiple_children_from_array_when_child_array_is_empty
    comments = []
    
    parsed = @j.encode do
      _name "Parent"
      _comments comments, :content
    end
    
    assert_equal "Parent", parsed["name"]
    assert_equal [], parsed["comments"]
  end
  
  def test_nesting_multiple_children_from_array_with_inline_loop
    comments = [ Struct.new(:content, :id).new("hello", 1),
                 Struct.new(:content, :id).new("world", 2) ]
    
    parsed = @j.encode do
      _comments comments do |comment|
        _content comment.content
      end
    end
    
    assert_equal ["content"], parsed["comments"].first.keys
    assert_equal "hello", parsed["comments"][0]["content"]
    assert_equal "world", parsed["comments"][1]["content"]
  end

  def test_nesting_multiple_children_from_array_with_inline_loop_on_root
    comments = [ Struct.new(:content, :id).new("hello", 1), Struct.new(:content, :id).new("world", 2) ]
    
    parsed = @j.encode do
      _ comments do |comment|
        _content comment.content
      end
    end
    
    assert_equal "hello", parsed[0]["content"]
    assert_equal "world", parsed[1]["content"]
  end
  
  def test_array_nested_inside_nested_hash
    parsed = @j.encode do
      _author do
        _name "David"
        _age 32
        
        _comments do
          _ { _content "hello" }
          _ { _content "world" }
        end
      end
    end
    
    assert_equal "hello", parsed["author"]["comments"][0]["content"]
    assert_equal "world", parsed["author"]["comments"][1]["content"]
  end
  
  def test_array_nested_inside_array
    parsed = @j.encode do
      _comments do
        _ do
          _authors do
            _ do
              _name "david"
            end
          end
        end
      end
    end
    
    assert_equal "david", parsed["comments"].first["authors"].first["name"]
  end
  
  def test_top_level_array
    comments = [ Struct.new(:content, :id).new("hello", 1),
                 Struct.new(:content, :id).new("world", 2) ]

    parsed = @j.encode do
      _ comments do |comment|
        _content comment.content
      end
    end
    
    assert_equal "hello", parsed[0]["content"]
    assert_equal "world", parsed[1]["content"]
  end
  
  def test_empty_top_level_array
    comments = []
    
    parsed = @j.encode do
      _ comments do |comment|
        _content comment.content
      end
    end
    
    assert_equal [], parsed
  end
  
  def test_dynamically_set_a_key_value
    parsed = @j.encode do
      _["each"] = "stuff"
    end
    
    assert_equal "stuff", parsed["each"]
  end

  def test_dynamically_set_a_key_nested_child_with_block
    parsed = @j.encode do
      _ :author do
        _name "David"
        _age 32
      end
    end
    
    assert_equal "David", parsed["author"]["name"]
    assert_equal 32, parsed["author"]["age"]
  end
end
Something went wrong with that request. Please try again.