Runtime assertions for Ruby literal.fun
ruby
5
fork

Configure Feed

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

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

This reverts commit 4dc042ebf049aa06f070701500208f1fc84b471d which broke enums.

+2 -65
-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 - 27 23 def deconstruct 28 24 to_h.values 29 25 end
+2 -10
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 - 14 9 def prop(name, type, kind = :keyword, reader: false, writer: false, predicate: false, default: nil, &coercion) 15 10 if default && !(Proc === default || default.frozen?) 16 11 raise Literal::ArgumentError.new("The default must be a frozen object or a Proc.") ··· 51 46 52 47 literal_properties << property 53 48 __define_literal_methods__(property) 49 + include(__literal_extension__) 54 50 end 55 51 56 52 def literal_properties ··· 79 75 @__literal_extension__ 80 76 else 81 77 @__literal_extension__ = Module.new do 82 - def to_h 83 - {} 84 - end 85 - 86 - set_temporary_name "Literal::Properties(Extension)" if respond_to?(:set_temporary_name) 78 + set_temporary_name "Literal::Properties(Extension)" 87 79 end 88 80 end 89 81 end
-18
test/data.test.rb
··· 4 4 prop :name, String 5 5 end 6 6 7 - class Empty < Literal::Data 8 - end 9 - 10 7 test "properties have readers by default" do 11 8 person = Person.new(name: "John") 12 9 expect(person.name) == "John" ··· 56 53 expect(hash[person2]) == "Bob" 57 54 expect(hash[Person.new(name: "John")]) == "John" 58 55 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 - 113 105 test do 114 106 person = Person.new("John", age: 30) 115 107 ··· 148 140 props = Person.literal_properties 149 141 expect(props.size) == 2 150 142 expect(props.map(&:name)) == [:name, :age] 151 - 152 - props = Empty.literal_properties 153 - expect(props.size) == 0 154 143 end 155 144 156 145 test "introspection" do ··· 195 184 end 196 185 197 186 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 212 187 213 188 assert callback_called 214 189 end ··· 349 324 expect { Family.new([]) }.not_to_raise 350 325 expect { Family.new([], last_reunion_year: 0) }.not_to_raise 351 326 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