Skip to content

Commit

Permalink
enhancing input file flexibility
Browse files Browse the repository at this point in the history
  • Loading branch information
hiroaki committed Jul 1, 2024
1 parent a424436 commit bdd5951
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 30 deletions.
2 changes: 1 addition & 1 deletion lib/gpx/gpx_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def initialize(opts = {})
if opts[:gpx_file] || opts[:gpx_data]
if opts[:gpx_file]
gpx_file = opts[:gpx_file]
gpx_file = File.open(gpx_file) unless gpx_file.is_a?(File)
gpx_file = File.open(gpx_file) unless gpx_file.respond_to?(:read)
@xml = Nokogiri::XML(gpx_file)
else
@xml = Nokogiri::XML(opts[:gpx_data])
Expand Down
63 changes: 34 additions & 29 deletions tests/gpx_file_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require 'minitest/autorun'
require 'gpx'
require 'stringio'

class GPXFileTest < Minitest::Test
ONE_TRACK_FILE = File.join(File.dirname(__FILE__), 'gpx_files/one_track.gpx')
Expand All @@ -11,39 +12,12 @@ class GPXFileTest < Minitest::Test

def test_load_data_from_string
gpx_file = GPX::GPXFile.new(gpx_data: File.open(ONE_TRACK_FILE).read)
assert_equal(1, gpx_file.tracks.size)
assert_equal(8, gpx_file.tracks.first.segments.size)
assert_equal('ACTIVE LOG', gpx_file.tracks.first.name)
assert_equal('active_log.gpx', gpx_file.name)
assert_equal('2006-04-08T16:44:28Z', gpx_file.time.xmlschema)
assert_equal(38.681488, gpx_file.bounds.min_lat)
assert_equal(-109.606948, gpx_file.bounds.min_lon)
assert_equal(38.791759, gpx_file.bounds.max_lat)
assert_equal(-109.447045, gpx_file.bounds.max_lon)
assert_equal('description of my GPX file with special char like &, <, >', gpx_file.description)
assert_equal('description of my GPX file with special char like &, <, >', gpx_file.description)
assert_equal(3.0724966849262554, gpx_file.distance)
assert_equal(15_237.0, gpx_file.duration)
assert_equal(3036.0, gpx_file.moving_duration)
assert_equal(3.6432767014935834, gpx_file.average_speed)
shared_assertions_for_one_track_file(gpx_file)
end

def test_load_data_from_file
gpx_file = GPX::GPXFile.new(gpx_file: ONE_TRACK_FILE)
assert_equal(1, gpx_file.tracks.size)
assert_equal(8, gpx_file.tracks.first.segments.size)
assert_equal('ACTIVE LOG', gpx_file.tracks.first.name)
assert_equal('active_log.gpx', gpx_file.name)
assert_equal('2006-04-08T16:44:28Z', gpx_file.time.xmlschema)
assert_equal(38.681488, gpx_file.bounds.min_lat)
assert_equal(-109.606948, gpx_file.bounds.min_lon)
assert_equal(38.791759, gpx_file.bounds.max_lat)
assert_equal(-109.447045, gpx_file.bounds.max_lon)
assert_equal('description of my GPX file with special char like &, <, >', gpx_file.description)
assert_equal(3.0724966849262554, gpx_file.distance)
assert_equal(15_237.0, gpx_file.duration)
assert_equal(3036.0, gpx_file.moving_duration)
assert_equal(3.6432767014935834, gpx_file.average_speed)
shared_assertions_for_one_track_file(gpx_file)
end

def test_big_file
Expand Down Expand Up @@ -74,4 +48,35 @@ def test_with_empty_tracks
assert_equal(21.0, gpx_file.moving_duration)
assert_equal(6.674040636626879, gpx_file.average_speed)
end

def test_load_data_from_stringio_assigned_gpx_file
str = File.open(ONE_TRACK_FILE).read
io = StringIO.new(str, 'r+')
gpx_file = GPX::GPXFile.new(gpx_file: io)
shared_assertions_for_one_track_file(gpx_file)
end

def test_load_data_from_stringio_assigned_gpx_data
str = File.open(ONE_TRACK_FILE).read
io = StringIO.new(str, 'r+')
gpx_file = GPX::GPXFile.new(gpx_data: io)
shared_assertions_for_one_track_file(gpx_file)
end

def shared_assertions_for_one_track_file(gpx_file)
assert_equal(1, gpx_file.tracks.size)
assert_equal(8, gpx_file.tracks.first.segments.size)
assert_equal('ACTIVE LOG', gpx_file.tracks.first.name)
assert_equal('active_log.gpx', gpx_file.name)
assert_equal('2006-04-08T16:44:28Z', gpx_file.time.xmlschema)
assert_equal(38.681488, gpx_file.bounds.min_lat)
assert_equal(-109.606948, gpx_file.bounds.min_lon)
assert_equal(38.791759, gpx_file.bounds.max_lat)
assert_equal(-109.447045, gpx_file.bounds.max_lon)
assert_equal('description of my GPX file with special char like &, <, >', gpx_file.description)
assert_equal(3.0724966849262554, gpx_file.distance)
assert_equal(15_237.0, gpx_file.duration)
assert_equal(3036.0, gpx_file.moving_duration)
assert_equal(3.6432767014935834, gpx_file.average_speed)
end
end

0 comments on commit bdd5951

Please sign in to comment.