Runtime assertions for Ruby literal.fun
ruby
5
fork

Configure Feed

Select the types of activity you want to include in your feed.

Ensure empty properties/datas/structs work as expected (#111)

authored by

Samuel Giddins and committed by
GitHub
4dc042eb 45b15394

+65 -2
+4
lib/literal/data_structure.rb
··· 20 20 instance_variable_set(:"@#{key}", value) 21 21 end 22 22 23 + def to_h 24 + {} 25 + end 26 + 23 27 def deconstruct 24 28 to_h.values 25 29 end
+10 -2
lib/literal/properties.rb
··· 6 6 7 7 include Literal::Types 8 8 9 + def self.extended(base) 10 + super 11 + base.include(base.__send__(:__literal_extension__)) 12 + end 13 + 9 14 def prop(name, type, kind = :keyword, reader: false, writer: false, predicate: false, default: nil, &coercion) 10 15 if default && !(Proc === default || default.frozen?) 11 16 raise Literal::ArgumentError.new("The default must be a frozen object or a Proc.") ··· 46 51 47 52 literal_properties << property 48 53 __define_literal_methods__(property) 49 - include(__literal_extension__) 50 54 end 51 55 52 56 def literal_properties ··· 75 79 @__literal_extension__ 76 80 else 77 81 @__literal_extension__ = Module.new do 78 - set_temporary_name "Literal::Properties(Extension)" 82 + def to_h 83 + {} 84 + end 85 + 86 + set_temporary_name "Literal::Properties(Extension)" if respond_to?(:set_temporary_name) 79 87 end 80 88 end 81 89 end
+18
test/data.test.rb
··· 4 4 prop :name, String 5 5 end 6 6 7 + class Empty < Literal::Data 8 + end 9 + 7 10 test "properties have readers by default" do 8 11 person = Person.new(name: "John") 9 12 expect(person.name) == "John" ··· 53 56 expect(hash[person2]) == "Bob" 54 57 expect(hash[Person.new(name: "John")]) == "John" 55 58 end 59 + 60 + test "empty" do 61 + empty = Empty.new 62 + expect(empty.to_h) == {} 63 + 64 + other = Empty.new 65 + expect(empty) == other 66 + expect(empty).to_eql?(other) 67 + expect(empty.hash) == other.hash 68 + 69 + other_empty = Class.new(Literal::Data).new 70 + expect(empty) == other_empty 71 + expect(empty).to_eql?(other_empty) 72 + expect(empty.hash) != other_empty.hash 73 + end
+33
test/properties.test.rb
··· 102 102 prop :name, Literal::Types::NilableType.new(String), :positional 103 103 end 104 104 105 + class Empty 106 + extend Literal::Properties 107 + end 108 + 109 + test "empty initializer" do 110 + expect { Empty.new }.not_to_raise 111 + end 112 + 105 113 test do 106 114 person = Person.new("John", age: 30) 107 115 ··· 140 148 props = Person.literal_properties 141 149 expect(props.size) == 2 142 150 expect(props.map(&:name)) == [:name, :age] 151 + 152 + props = Empty.literal_properties 153 + expect(props.size) == 0 143 154 end 144 155 145 156 test "introspection" do ··· 184 195 end 185 196 186 197 example.new(name: "John") 198 + 199 + assert callback_called 200 + 201 + callback_called = false 202 + 203 + empty = Class.new do 204 + extend Literal::Properties 205 + 206 + define_method :after_initialize do 207 + callback_called = true 208 + end 209 + end 210 + 211 + empty.new 187 212 188 213 assert callback_called 189 214 end ··· 324 349 expect { Family.new([]) }.not_to_raise 325 350 expect { Family.new([], last_reunion_year: 0) }.not_to_raise 326 351 end 352 + 353 + test "#to_h" do 354 + person = Person.new("John", age: 30) 355 + expect(person.to_h) == { name: "John", age: 30 } 356 + 357 + empty = Empty.new 358 + expect(empty.to_h) == {} 359 + end