Runtime assertions for Ruby literal.fun
ruby
5
fork

Configure Feed

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

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

This reverts commit 797b227e1cb554a7726e8aa97c3879ea067ba6d6.

authored by

Samuel Giddins and committed by
GitHub
3b436f6d 4d44af0c

+64
+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
+9
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.") ··· 75 80 @__literal_extension__ 76 81 else 77 82 @__literal_extension__ = Module.new do 83 + def to_h 84 + {} 85 + end 86 + 78 87 set_temporary_name "Literal::Properties(Extension)" if respond_to?(:set_temporary_name) 79 88 end 80 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
··· 100 100 prop :name, Literal::Types::NilableType.new(String), :positional 101 101 end 102 102 103 + class Empty 104 + extend Literal::Properties 105 + end 106 + 107 + test "empty initializer" do 108 + expect { Empty.new }.not_to_raise 109 + end 110 + 103 111 test do 104 112 person = Person.new("John", age: 30) 105 113 ··· 138 146 props = Person.literal_properties 139 147 expect(props.size) == 2 140 148 expect(props.map(&:name)) == [:name, :age] 149 + 150 + props = Empty.literal_properties 151 + expect(props.size) == 0 141 152 end 142 153 143 154 test "introspection" do ··· 182 193 end 183 194 184 195 example.new(name: "John") 196 + 197 + assert callback_called 198 + 199 + callback_called = false 200 + 201 + empty = Class.new do 202 + extend Literal::Properties 203 + 204 + define_method :after_initialize do 205 + callback_called = true 206 + end 207 + end 208 + 209 + empty.new 185 210 186 211 assert callback_called 187 212 end ··· 322 347 expect { Family.new([]) }.not_to_raise 323 348 expect { Family.new([], last_reunion_year: 0) }.not_to_raise 324 349 end 350 + 351 + test "#to_h" do 352 + person = Person.new("John", age: 30) 353 + expect(person.to_h) == { name: "John", age: 30 } 354 + 355 + empty = Empty.new 356 + expect(empty.to_h) == {} 357 + end