Runtime assertions for Ruby literal.fun
ruby
5
fork

Configure Feed

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

Begin testing value objects

+103 -30
+8
lib/literal.rb
··· 15 15 class ArgumentError < ::ArgumentError 16 16 include Error 17 17 end 18 + 19 + def self.Enum(type, &block) 20 + Enum.define(type, &block) 21 + end 22 + 23 + def self.Value(type, &block) 24 + Value.define(type, &block) 25 + end 18 26 end
+1 -1
lib/literal/enum.rb
··· 17 17 @members.each(&block) 18 18 end 19 19 20 - def define(&block) 20 + def define(type, &block) 21 21 Class.new(self, &block).freeze 22 22 end 23 23
+4 -1
lib/literal/value.rb
··· 29 29 end 30 30 31 31 value_class.class_exec(&block) if block 32 - value_class.freeze 33 32 value_class 34 33 end 35 34 end ··· 42 41 end 43 42 44 43 attr_reader :value 44 + 45 + def inspect 46 + "#{self.class.name}(#{value.inspect})" 47 + end 45 48 end
-7
test/literal/attributes.rb
··· 1 - # frozen_string_literal: true 2 - 3 - class Person 4 - extend Literal::Attributes 5 - 6 - attribute :name, _Maybe(String) 7 - end
+1 -1
test/literal/enum.rb
··· 1 1 # frozen_string_literal: true 2 2 3 - Color = Literal::Enum.define do 3 + Color = Literal::Enum(String) do 4 4 Red("#ff0000") 5 5 Green("#00ff00") 6 6 Blue("#0000ff")
-20
test/literal/struct.rb
··· 1 - # frozen_string_literal: true 2 - 3 - class Person < Literal::Struct 4 - attribute :name, String 5 - attribute :age, Integer 6 - end 7 - 8 - test do 9 - expect { 10 - Person.new(name: "John", age: 42) 11 - }.not_to_raise 12 - end 13 - 14 - test do 15 - expect { 16 - Person.new(name: 1, age: "Hello") 17 - }.to_raise(Literal::TypeError) do |error| 18 - expect(error.message) == "Expected name: `1` to be: `String`." 19 - end 20 - end
+89
test/literal/value.rb
··· 1 + let def type = Literal::Value(Integer) 2 + let def example = type.new(1) 3 + 4 + context "with Integer" do 5 + let def type = Literal::Value(Integer) 6 + let def example = type.new(1) 7 + 8 + test "invalid type" do 9 + expect { type.new(1.0) }.to_raise(Literal::TypeError) 10 + end 11 + 12 + test do 13 + expect(example.value) == 1 14 + expect(example.to_i) == 1 15 + end 16 + end 17 + 18 + context "with String" do 19 + let def type = Literal::Value(String) 20 + let def example = type.new("foo") 21 + 22 + test do 23 + expect(example.value) == "foo" 24 + expect(example.to_s) == "foo" 25 + expect(example.to_str) == "foo" 26 + end 27 + end 28 + 29 + context "with Symbol" do 30 + let def type = Literal::Value(Symbol) 31 + let def example = type.new(:foo) 32 + 33 + test do 34 + expect(example.value) == :foo 35 + expect(example.to_sym) == :foo 36 + end 37 + end 38 + 39 + context "with Float" do 40 + let def type = Literal::Value(Float) 41 + let def example = type.new(1.0) 42 + 43 + test do 44 + expect(example.value) == 1.0 45 + expect(example.to_f) == 1.0 46 + end 47 + end 48 + 49 + context "with Set" do 50 + let def type = Literal::Value(Set) 51 + let def example = type.new(Set[1, 2, 3]) 52 + 53 + test do 54 + expect(example.value) == Set[1, 2, 3] 55 + expect(example.to_set) == Set[1, 2, 3] 56 + end 57 + end 58 + 59 + context "with Array" do 60 + let def type = Literal::Value(Array) 61 + let def example = type.new([1, 2, 3]) 62 + 63 + test do 64 + expect(example.value) == [1, 2, 3] 65 + expect(example.to_a) == [1, 2, 3] 66 + expect(example.to_ary) == [1, 2, 3] 67 + end 68 + end 69 + 70 + context "with Hash" do 71 + let def type = Literal::Value(Hash) 72 + let def example = type.new({ foo: :bar }) 73 + 74 + test do 75 + expect(example.value) == { foo: :bar } 76 + expect(example.to_h) == { foo: :bar } 77 + end 78 + end 79 + 80 + context "with Proc" do 81 + let def type = Literal::Value(Proc) 82 + let def value = -> { :foo } 83 + let def example = type.new(value) 84 + 85 + test do 86 + expect(example.value) == value 87 + expect(example.to_proc) == value 88 + end 89 + end