File: schema_test.rb

package info (click to toggle)
ruby-declarative 0.0.10-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 256 kB
  • sloc: ruby: 595; makefile: 6
file content (115 lines) | stat: -rw-r--r-- 4,248 bytes parent folder | download
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
require "test_helper"

class SchemaTest < Minitest::Spec
  class Decorator
    extend Declarative::Schema

    def self.default_nested_class
      Decorator
    end
  end

  module AddLinks
    def self.included(includer)
      super
      includer.property(:links)
    end
  end

  class Concrete < Decorator
    defaults render_nil: true do |name|
      { as: name.to_s.upcase }
    end
    feature AddLinks

    property :artist, cool: true do
      property :name
      property :band, crazy: nil do
        property :location
      end
    end

    property :id, unique: true, value: 1
  end


  it do
    Concrete.extend(Declarative::Inspect::Schema)
    Concrete.inspect
    _(Concrete.inspect.gsub(/\s/, "")).must_equal 'Schema:{
    "links"=>#<Declarative::Definitions::Definition:@options={:render_nil=>true,:as=>"LINKS",:name=>"links"}>,
    "artist"=>#<Declarative::Definitions::Definition:@options={:render_nil=>true,:as=>"ARTIST",:cool=>true,:nested=>Schema:{
      "links"=>#<Declarative::Definitions::Definition:@options={:name=>"links"}>,
      "name"=>#<Declarative::Definitions::Definition:@options={:name=>"name"}>,
      "band"=>#<Declarative::Definitions::Definition:@options={:crazy=>nil,:nested=>Schema:{
        "links"=>#<Declarative::Definitions::Definition:@options={:name=>"links"}>,
        "location"=>#<Declarative::Definitions::Definition:@options={:name=>"location"}>},:name=>"band"}>},:name=>"artist"}>,
    "id"=>#<Declarative::Definitions::Definition:@options={:render_nil=>true,:as=>"ID",:unique=>true,:value=>1,:name=>"id"}>}'.
     gsub("\n", "").gsub(/\s/, "")
  end

  class InheritingConcrete < Concrete
    property :uuid
  end


  it do
    InheritingConcrete.extend(Declarative::Inspect::Schema)
    InheritingConcrete.inspect
    _(InheritingConcrete.inspect.gsub(/\s/, "")).must_equal 'Schema:{
    "links"=>#<Declarative::Definitions::Definition:@options={:render_nil=>true,:as=>"LINKS",:name=>"links"}>,
    "artist"=>#<Declarative::Definitions::Definition:@options={:render_nil=>true,:as=>"ARTIST",:cool=>true,:nested=>Schema:{
      "links"=>#<Declarative::Definitions::Definition:@options={:name=>"links"}>,
      "name"=>#<Declarative::Definitions::Definition:@options={:name=>"name"}>,
      "band"=>#<Declarative::Definitions::Definition:@options={:crazy=>nil,:nested=>Schema:{
        "links"=>#<Declarative::Definitions::Definition:@options={:name=>"links"}>,
        "location"=>#<Declarative::Definitions::Definition:@options={:name=>"location"}>},:name=>"band"}>},:name=>"artist"}>,
    "id"=>#<Declarative::Definitions::Definition:@options={:render_nil=>true,:as=>"ID",:unique=>true,:value=>1,:name=>"id"}>,
    "uuid"=>#<Declarative::Definitions::Definition:@options={:render_nil=>true,:as=>"UUID",:name=>"uuid"}>}
     '.
     gsub("\n", "").gsub(/\s/, "")
  end


  describe "::property still allows passing internal options" do
    class ConcreteWithOptions < Decorator
      defaults cool: true

      # you can pass your own _nested_builder and it will still receive correct,
      # defaultized options.
      property :artist, _nested_builder: ->(options) { OpenStruct.new(cool: options[:cool]) }
    end

    it do
      _(ConcreteWithOptions.extend(Declarative::Inspect::Schema).inspect).must_equal 'Schema: {"artist"=>#<Declarative::Definitions::Definition: @options={:cool=>true, :nested=>#<OpenStruct cool=true>, :name=>"artist"}>}'
    end
  end

  describe "multiple ::defaults" do
    class Twin < Decorator
      module A; end
      module B; end
      module D; end

      defaults a: "a", _features: [A] do |name|
        { first: 1, _features: [D] }
      end

      # DISCUSS: currently, we only allow one dynamic block.
      defaults b: "b", _features: [B]# do |name, options|
      #  {}
      #end


      property :id do end
    end

    it do
      _(Twin.extend(Declarative::Inspect::Schema).inspect).must_equal 'Schema: {"id"=>#<Declarative::Definitions::Definition: @options={:a=>"a", :b=>"b", :first=>1, :nested=>Schema: {}, :name=>"id"}>}'
      # :_features get merged.
      Twin.definitions.get(:id)[:nested].is_a? Twin::A
      Twin.definitions.get(:id)[:nested].is_a? Twin::B
      Twin.definitions.get(:id)[:nested].is_a? Twin::D
    end
  end
end