File: definitions_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 (95 lines) | stat: -rw-r--r-- 3,905 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
require "test_helper"

class DefinitionsTest < Minitest::Spec
  NestedBuilder = ->(options) {
    base = options[:_base] || Declarative::Definitions.new(Declarative::Definitions::Definition)
    base.instance_exec(&options[:_block])
    base
  }

  let (:schema) { Declarative::Definitions.new(Declarative::Definitions::Definition).extend(Declarative::Definitions::Inspect) }

  it "what" do
    # #add works with name
    schema.add :id
    # get works with symbol
    _(schema.get(:id).inspect).must_equal '#<Declarative::Definitions::Definition: @options={:name=>"id"}>'
    # get works with string
    _(schema.get("id").inspect).must_equal '#<Declarative::Definitions::Definition: @options={:name=>"id"}>'

    # #add with name and options
    schema.add(:id, unique: true)
    _(schema.get(:id).inspect).must_equal '#<Declarative::Definitions::Definition: @options={:unique=>true, :name=>"id"}>'
  end

  it "overwrites old when called twice" do
    schema.add :id
    schema.add :id, cool: true
    _(schema.inspect).must_equal '{"id"=>#<Declarative::Definitions::Definition: @options={:cool=>true, :name=>"id"}>}'
  end

  it "#add with block" do
    schema.add :artist, _nested_builder: NestedBuilder do
      add :name
      add :band, _nested_builder: NestedBuilder do
        add :location
      end
    end

    _(schema.inspect).must_equal '{"artist"=>#<Declarative::Definitions::Definition: @options={:nested=>{"name"=>#<Declarative::Definitions::Definition: @options={:name=>"name"}>, "band"=>#<Declarative::Definitions::Definition: @options={:nested=>{"location"=>#<Declarative::Definitions::Definition: @options={:name=>"location"}>}, :name=>"band"}>}, :name=>"artist"}>}'
  end

  it "#add with :nested instead of block" do
    nested_schema = Declarative::Definitions.new(Declarative::Definitions::Definition)
    nested_schema.extend(Declarative::Definitions::Inspect)

    nested_schema.add :name

    schema.add :artist, nested: nested_schema

    _(schema.inspect).must_equal '{"artist"=>#<Declarative::Definitions::Definition: @options={:nested=>{"name"=>#<Declarative::Definitions::Definition: @options={:name=>"name"}>}, :name=>"artist"}>}'
  end


  it "#add with inherit: true and block" do
    schema.add :artist, cool: true, _nested_builder: NestedBuilder do
      add :name
      add :band, crazy: nil, _nested_builder: NestedBuilder do
        add :location
      end
    end

    schema.add :id, unique: true, value: 1

    schema.add :artist, uncool: false, _nested_builder: NestedBuilder, inherit: true do
      add :band, normal: false, _nested_builder: NestedBuilder, inherit: true do
        add :genre
      end
    end

    schema.add :id, unique: false, inherit: true

    _(schema.inspect).must_equal '{"artist"=>#<Declarative::Definitions::Definition: @options={:cool=>true, :nested=>{"name"=>#<Declarative::Definitions::Definition: @options={:name=>"name"}>, "band"=>#<Declarative::Definitions::Definition: @options={:crazy=>nil, :nested=>{"location"=>#<Declarative::Definitions::Definition: @options={:name=>"location"}>, "genre"=>#<Declarative::Definitions::Definition: @options={:name=>"genre"}>}, :name=>"band", :normal=>false}>}, :name=>"artist", :uncool=>false}>, "id"=>#<Declarative::Definitions::Definition: @options={:unique=>false, :value=>1, :name=>"id"}>}'
  end

  it "#add with nested options followed by inherit: true" do
    schema.add :id, deserializer: options = { render: false }
    schema.add :id, inherit: true

    schema.get(:id)[:deserializer][:parse] = true

    _(options).must_equal(render: false)
  end
end


class DefinitionTest < Minitest::Spec
  let (:definition) { Declarative::Definitions::Definition.new(:name) }

  it "#merge does return deep copy" do
    options = { render: false }
    merged = definition.merge(options)
    definition.merge!(render: true)
    _(merged).must_equal(:name=>"name", render: false)
  end
end