Class: SlowBlink::Message::TestData

Inherits:
Object
  • Object
show all
Defined in:
lib/slow_blink/message/test_data.rb

Instance Method Summary collapse

Constructor Details

#initialize(model) ⇒ TestData

generate test data for a particular model



27
28
29
# File 'lib/slow_blink/message/test_data.rb', line 27

def initialize(model)
    @model = model
end

Instance Method Details

#fieldData(field, **opts) ⇒ Object

return a random valid test value for a given field type



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
# File 'lib/slow_blink/message/test_data.rb', line 55

def fieldData(field, **opts)


    if !field.is_a? SlowBlink::Field
        ArgumentError.new "field argument must be an instance of SlowBlink::Field"
    end

    case field.type.class
    when SlowBlink::FIXED
        s = (0...field.type.size).map { (65 + rand(26)).chr }.join
        
        s
        
    when SlowBlink::BINARY, SlowBlink::STRING
        if field.type.size
            s = (0...rand(field.type.size)).map { (65 + rand(26)).chr }.join
        else
            s = (0...rand(50)).map { (65 + rand(26)).chr }.join
        end
        
        s
        
    when SlowBlink::BOOLEAN
        
        [true, false].sample
        
    when SlowBlink::U8, SlowBlink::U16, SlowBlink::U32, SlowBlink::U64, SlowBlink::I8, SlowBlink::I16, SlowBlink::I32, SlowBlink::I64
        
            rand(field.type.class::RANGE)
        
    when SlowBlink::FLOATING_POINT, SlowBlink::DECIMAL
        
            rand(Float::MIN, Float::MAX)
        
    when SlowBlink::ENUM
        
            field.type.symbols.sample.name
        
    when SlowBlink::MILLI_TIME, SlowBlink::NANO_TIME, SlowBlink::DATE

            0
        
    when SlowBlink::TIME_OF_DAY_MILLI, SlowBlink::TIME_OF_DAY_NANO
        
            0
                            
    when SlowBlink::StaticGroup

        
        set(field.type.name)
        

    when SlowBlink::DynamicGroup

        set(field.type.groups.select{|g|g.id}.sample.name)
        
    else
        raise "no support for #{field.type}"
    end
        
end

#set(name) ⇒ Object

create an instance of group (name) and initialise the fields



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/slow_blink/message/test_data.rb', line 33

def set(name)
    group = @model.group(name).new
    @model.schema.groups.detect{|g|g.name == name}.fields.each do |f|

        data = fieldData(f)

        if !f.optional? and data.nil?
            raise "what the its #{f.type}"
        end
        
        if f.type.sequence? and data
            group[f.name] = [data]
        else
            group[f.name] = data
        end                
    end
    group
end