summaryrefslogtreecommitdiffstats
path: root/spec/options_spec.rb
blob: 0fd84d57b49504645adf8f2eaff8361fd8516707 (plain)
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
#! /usr/bin/env ruby
# -*- coding: UTF-8 -*-

require 'ayk/options'
#
describe AYK::Options do
    #
    describe 'simple usage' do
        before(:all) do
            @opt = AYK::Options.new 'Spec tests'
        end
        it "should set option key1" do
            @opt.o("key 1 doc", :key1, 666) { puts 'trigger' }
        end
        it "should raise override existing key" do
            lambda { @opt.o("key 1 doc", :key1, 666) { puts 'trigger' } }.should raise_error AYK::OptionsError
        end
        it "should set sub options" do
            @opt.sub(:sub) do
                o 'var 1', :var1, 69
                o 'var 2', :var2, 999
            end
            @opt[:sub][:var1].should eql 69
            @opt[:sub][:var2].should eql 999
        end
        it "should access subeys through [,]" do
            @opt[:sub,:var1].should eql 69
            @opt[:sub,:var2].should eql 999
        end
        it "should raise calling sub on existing option" do
            lambda { @opt.sub :key1 }.should raise_error AYK::OptionsError
        end
        it "should raise unknown option when setting trigger" do
            lambda { @opt.trigger :unknonwn }.should raise_error AYK::OptionsError
        end
        it "Options.get is private" do
            lambda{ @opt.get(:key1) }.should raise_error NoMethodError
        end
        it "should set a trigger on assignment" do
            @opt[:sub][:var1].should eql 69
            @opt[:sub].trigger :var1 do raise Exception.new "trigger" end
            lambda{ @opt[:sub][:var1] = 96}.should raise_error Exception
            @opt[:sub][:var1].should eql 69
            begin
                @opt[:sub][:var1] = 96
            rescue Exception
                $!.message.should eql "trigger"
            end
            @opt[:sub][:var1].should eql 69
        end
        it "should get option key1 through method missing" do
            @opt.key1.should eql 666
        end
        it "should get option key1 through []" do
            @opt[:key1].should eql 666
        end
        it "should get option nokey through method missing" do
            @opt.nokey.should eql nil
        end
        it "should get option nokey through []" do
            @opt[:nokey].should eql nil
        end
        it "should return default valuei through method missing" do
            @opt.default 'default value', 'none'
            @opt.nokey.should eql 'none'
        end
        it "should return default value through []" do
            @opt.default 'default value', 'none'
            @opt[:nokey].should eql 'none'
        end
        it "should assign new value through []=" do
            @opt[:sub][:var2].should eql 999
            @opt[:sub][:var2] = 666
            @opt[:sub][:var2].should eql 666
        end
        it "should nice init" do
            opts = AYK::Options.new('Spec nice') do |opt|
                opt.dsl do 
                    o 'var doc', :var, 666
                end
            end
            opts.var.should eql 666
        end
        it "should be nicely included" do
            class A__A; include AYK::Optioned end
            A__A.options.name.should eql 'a__a'
        end
    end
    #
end
#