require 'spec_helper' describe FitParser::File::Data do describe '.generate' do context 'standard definition' do let(:definition) do FitParser::File::Definition.read( example_file('record/message/definition') ) end subject { described_class.generate(definition) } its(:ancestors) { should include(BinData::Record) } its('new.record_type') { should eq(:file_id) } end context 'definition with multiple time the same field' do before :all do @fields = FitParser::File::Definitions.fields @dyn_fields = FitParser::File::Definitions.dyn_fields # force a fake definition for scaling of arrays FitParser::File::Definitions.fields[2].delete(2) FitParser::File::Definitions.add_field( 2, 2, 'field_array', type: 6, scale: 10, offset: 0 ) end after :all do FitParser::File::Definitions.fields = @fields FitParser::File::Definitions.dyn_fields = @dyn_fields end before :each do def_file = example_file('record/message/definition_field_array.fit') data_file = example_file('record/message/data_field_array.fit') definition = described_class.generate( FitParser::File::Definition.read(def_file) ) @result = definition.read(data_file) end it 'reads the entire record' do # read first the record definition expect(@result.raw_field_array).to be == [123_456_789, 987_654_321] expect(@result.raw_time_mode).to be == [1, 3] expect(@result.raw_field_8).to be == 1539 expect(@result.raw_active_time_zone).to be == 0 end it 'does not apply the scale equal to 1 for integer' do expect(@result.raw_active_time_zone).to be == 0 expect(@result.active_time_zone.to_s).to be_eql '0' end it 'does not apply the scale equal to 1 for arrays' do expect(@result.raw_time_mode).to be == [1, 3] expect(@result.time_mode.to_s).to be_eql('[1, 3]') end it 'does apply scale on each element of an array' do expect(@result.raw_field_array).to be == [123_456_789, 987_654_321] expect(@result.field_array.to_s).to be_eql '[12345678.9, 98765432.1]' end end context 'definition with dynamic fields' do before :each do def_file = example_file('record/message/definition_dynamic_fields.fit') data_file = example_file('record/message/data_dynamic_fields.fit') definition = described_class.generate( FitParser::File::Definition.read(def_file) ) @result = definition.read(data_file) end it 'uses dynamic field value' do expect(@result.raw_product).to be == 1499 expect(@result.product).to be == 'swim' end end context 'definition with non basic types' do before :each do def_file = example_file('record/message/definition_dynamic_fields.fit') data_file = example_file('record/message/data_dynamic_fields.fit') definition = described_class.generate( FitParser::File::Definition.read(def_file) ) @result = definition.read(data_file) end it 'returns the real value' do expect(@result.raw_type).to be == 1 expect(@result.type).to be == 'device' end end context 'definition with bit field types' do before :each do def_file = example_file( 'record/message/definition_file_capabilities.fit' ) @definition = described_class.generate( FitParser::File::Definition.read(def_file) ) end context 'when only 1 bit set' do it 'returns the single value' do res = @definition.read( example_file('record/message/data_file_capabilities_activities.fit') ) expect(res.raw_flags).to eq(2) expect(res.flags).to eq('read') end end context 'when several bits set' do it 'returns the compound value' do res = @definition.read( example_file('record/message/data_file_capabilities_settings.fit') ) expect(res.raw_flags).to eq(6) expect(res.flags).to eq('read/write') end end end end end