framecurve 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem "term-ansicolor"
4
+ group :development do
5
+ gem "jeweler", "~> 1.6.4"
6
+ gem "rake"
7
+ gem "cli_test"
8
+ end
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Julik
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,27 @@
1
+ = framecurve
2
+
3
+ framecurve (http://framecurve.org) file handling gem for Ruby.
4
+
5
+ It currently includes only one binary, framecurve_validator, that will validate framecurve files passed in the arguments and report
6
+ any errors found in the files to STDERR.
7
+
8
+ $framecurve_validator somefile.framecurve.txt another.framecurve.txt
9
+
10
+ For the rest this gem includes a handful of classes for manipulating framecurves and is intended as a library for when you want to do something
11
+ with framecurves from within Ruby. This will also power the online framecurve validation service once it's online.
12
+
13
+ == Contributing to framecurve
14
+
15
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
16
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
17
+ * Fork the project
18
+ * Start a feature/bugfix branch
19
+ * Commit and push until you are happy with your contribution
20
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
21
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
22
+
23
+ == Copyright
24
+
25
+ Copyright (c) 2011 Julik. See LICENSE.txt for
26
+ further details.
27
+
@@ -0,0 +1,27 @@
1
+ # encoding: utf-8
2
+
3
+ require 'jeweler'
4
+ require './lib/framecurve'
5
+
6
+ Jeweler::Tasks.new do |gem|
7
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
8
+ gem.version = Framecurve::VERSION
9
+ gem.name = "framecurve"
10
+ gem.homepage = "http://github.com/guerilla-di/framecurve"
11
+ gem.license = "MIT"
12
+ gem.summary = %Q{ Handles Framecurve files }
13
+ gem.description = %Q{ Parser, validation and interpolation}
14
+ gem.email = "me@julik.nl"
15
+ gem.authors = ["Julik"]
16
+ # dependencies defined in Gemfile
17
+ end
18
+ Jeweler::RubygemsDotOrgTasks.new
19
+
20
+ require 'rake/testtask'
21
+ Rake::TestTask.new(:test) do |test|
22
+ test.libs << 'lib' << 'test'
23
+ test.pattern = 'test/**/test_*.rb'
24
+ test.verbose = true
25
+ end
26
+
27
+ task :default => :test
@@ -0,0 +1,36 @@
1
+ #!/usr/bin/env ruby
2
+ require File.dirname(__FILE__) + "/../lib/framecurve"
3
+ require 'term/ansicolor'
4
+
5
+ class Color
6
+ extend Term::ANSIColor
7
+ end
8
+
9
+ raise "Pass at least one path to a framecurve file as a argument" if ARGV.empty?
10
+
11
+ def print_list(destination, of)
12
+ of.each do | elem |
13
+ destination.puts " * %s" % elem
14
+ end
15
+ end
16
+
17
+ ARGV.each do | arg |
18
+ validator = Framecurve::Validator.new
19
+ validator.parse_and_validate(arg)
20
+
21
+ unless validator.any_errors? || validator.any_warnings?
22
+ puts Color.green { "Framecurve file %s TOTALLY OK! Good job!" % arg }
23
+ else
24
+ $stderr.puts Color.bold{ arg }
25
+ if validator.any_errors?
26
+ $stderr.puts Color.red{ Color.bold { " The file had the following ERRORS flagged:" % arg }}
27
+ print_list($stderr, validator.errors)
28
+ end
29
+
30
+ if validator.any_warnings?
31
+ $stderr.puts Color.orange{ Color.bold { " The file was sort of OK but did cause the following warnings:" % arg }}
32
+ print_list($stderr, validator.warnings)
33
+ end
34
+ exit 10
35
+ end
36
+ end
@@ -0,0 +1,71 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "framecurve"
8
+ s.version = "1.0.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Julik"]
12
+ s.date = "2011-12-30"
13
+ s.description = " Parser, validation and interpolation"
14
+ s.email = "me@julik.nl"
15
+ s.executables = ["framecurve_validator"]
16
+ s.extra_rdoc_files = [
17
+ "LICENSE.txt",
18
+ "README.rdoc"
19
+ ]
20
+ s.files = [
21
+ ".document",
22
+ "Gemfile",
23
+ "LICENSE.txt",
24
+ "README.rdoc",
25
+ "Rakefile",
26
+ "bin/framecurve_validator",
27
+ "framecurve.gemspec",
28
+ "lib/framecurve.rb",
29
+ "lib/framecurve/comment.rb",
30
+ "lib/framecurve/curve.rb",
31
+ "lib/framecurve/parser.rb",
32
+ "lib/framecurve/serializer.rb",
33
+ "lib/framecurve/tuple.rb",
34
+ "lib/framecurve/validator.rb",
35
+ "test/helper.rb",
36
+ "test/test_framecurve_comment.rb",
37
+ "test/test_framecurve_curve.rb",
38
+ "test/test_framecurve_parser.rb",
39
+ "test/test_framecurve_serializer.rb",
40
+ "test/test_framecurve_tuple.rb",
41
+ "test/test_framecurve_validator.rb",
42
+ "test/test_framecurve_validator_binary.rb"
43
+ ]
44
+ s.homepage = "http://github.com/guerilla-di/framecurve"
45
+ s.licenses = ["MIT"]
46
+ s.require_paths = ["lib"]
47
+ s.rubygems_version = "1.8.11"
48
+ s.summary = "Handles Framecurve files"
49
+
50
+ if s.respond_to? :specification_version then
51
+ s.specification_version = 3
52
+
53
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
54
+ s.add_runtime_dependency(%q<term-ansicolor>, [">= 0"])
55
+ s.add_development_dependency(%q<jeweler>, ["~> 1.6.4"])
56
+ s.add_development_dependency(%q<rake>, [">= 0"])
57
+ s.add_development_dependency(%q<cli_test>, [">= 0"])
58
+ else
59
+ s.add_dependency(%q<term-ansicolor>, [">= 0"])
60
+ s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
61
+ s.add_dependency(%q<rake>, [">= 0"])
62
+ s.add_dependency(%q<cli_test>, [">= 0"])
63
+ end
64
+ else
65
+ s.add_dependency(%q<term-ansicolor>, [">= 0"])
66
+ s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
67
+ s.add_dependency(%q<rake>, [">= 0"])
68
+ s.add_dependency(%q<cli_test>, [">= 0"])
69
+ end
70
+ end
71
+
@@ -0,0 +1,11 @@
1
+ module Framecurve
2
+ VERSION = "1.0.0"
3
+
4
+ # Is raised when a malformed framecurve bit has occurred in the system
5
+ class Malformed < RuntimeError
6
+ end
7
+ end
8
+
9
+ %w( tuple comment curve parser validator serializer ).each do | f |
10
+ require File.join(File.dirname(__FILE__), "framecurve", f)
11
+ end
@@ -0,0 +1,14 @@
1
+ # Represents a framecurve comment
2
+ class Framecurve::Comment < Struct.new(:text)
3
+ def tuple?
4
+ false
5
+ end
6
+
7
+ def comment?
8
+ true
9
+ end
10
+
11
+ def to_s
12
+ ['#', text.to_s.gsub(/\r\n?/, '')].join(' ')
13
+ end
14
+ end
@@ -0,0 +1,108 @@
1
+ # Represents a curve file with comments and frame correlation records
2
+ class Framecurve::Curve
3
+ include Enumerable
4
+
5
+ # If this curve has been generated or parsed from a file, the parser
6
+ # will preserve the filename here
7
+ attr_accessor :filename
8
+
9
+ def initialize(*elements)
10
+ @elements = elements.flatten
11
+ end
12
+
13
+ # Iterates over all the tuples in the curve
14
+ def each_tuple
15
+ @elements.each do | e |
16
+ yield(e) if e.tuple?
17
+ end
18
+ end
19
+
20
+ # Return the tuples in this curve
21
+ def only_tuples
22
+ @elements.select{|e| e.tuple? }
23
+ end
24
+
25
+ # Iterates over all the elements in the curve
26
+ def each
27
+ @elements.each(&Proc.new)
28
+ end
29
+
30
+ # Iterates over all the comments in the curve
31
+ def each_comment
32
+ @elements.each do | e |
33
+ yield(e) if e.comment?
34
+ end
35
+ end
36
+
37
+ # Adds a comment line
38
+ def comment!(text)
39
+ @elements.push(Framecurve::Comment.new(text.strip))
40
+ end
41
+
42
+ # Adds a tuple
43
+ def tuple!(at, value)
44
+ t = Framecurve::Tuple.new(at.to_i, value.to_f)
45
+ # Validate for sequencing
46
+ if any_tuples?
47
+ last_frame = only_tuples[-1].at
48
+ if t.at <= last_frame
49
+ raise Framecurve::Malformed, "Cannot add a frame that comes before or at the same frame as the previous one (%d after %d)" % [t.at, last_frame]
50
+ end
51
+ end
52
+
53
+ @elements.push(t)
54
+ end
55
+
56
+ # Returns the number of lines in this curve file
57
+ def length
58
+ @elements.length
59
+ end
60
+
61
+ # Tells whether the curve contains any elements
62
+ def empty?
63
+ @elements.empty?
64
+ end
65
+
66
+ # Get a record by offset (line number 0-based)
67
+ def [](at)
68
+ @elements[at]
69
+ end
70
+
71
+ # Tells whether the curve has any tuples at all
72
+ def any_tuples?
73
+ @elements.any? {|e| e.tuple? }
74
+ end
75
+
76
+ # Returns a new curve with the same data with all the intermediate frames interpolated properly
77
+ # and all the comments except for the preamble removed
78
+ def to_materialized_curve
79
+ c = self.class.new
80
+ c.comment! "http://framecurve.org/specification-v1"
81
+ c.comment! "at_frame\tuse_frame_of_source"
82
+ each_defined_tuple {|t| c.tuple!(t.at, t.value) }
83
+ return c
84
+ end
85
+
86
+ # Yields each tuple that is defined by this framecurve in succession.
87
+ # For example, if the curve contains tuples at (1, 123.45) and (10, 167.89)
88
+ # this method will yield 10 times for each defined integer frame value
89
+ def each_defined_tuple
90
+ tuples = select{|e| e.tuple? }
91
+ tuples.each_with_index do | tuple, idx |
92
+ next_tuple = tuples[idx + 1]
93
+ if next_tuple.nil?
94
+ yield(tuple)
95
+ else # Apply linear interpolation
96
+ dt = next_tuple.at - tuple.at
97
+ if dt > 1
98
+ dy = next_tuple.value - tuple.value
99
+ delta = dy / dt
100
+ dt.times do | increment |
101
+ value_inc = delta * increment
102
+ yield(Framecurve::Tuple.new(tuple.at + increment, tuple.value + value_inc))
103
+ end
104
+ end
105
+ end
106
+ end
107
+ end
108
+ end
@@ -0,0 +1,49 @@
1
+ # Parses the data in the passed file/IO into a Curve object
2
+ class Framecurve::Parser
3
+ COMMENT = /^#(.+)$/
4
+ CORRELATION_RECORD = /^([-]?\d+)\t([-]?(\d+(\.\d*)?)|\.\d+)([eE][+-]?[0-9]+)?$/
5
+
6
+ # Parse from file path or IO. If the passed object is an IO it will be read from.
7
+ # If the passed object is a string it will be interpreted as a path and the file at that path
8
+ # will be parsed instead
9
+ def parse(path_or_io)
10
+ # If the first argument is a path parse from the opened file,
11
+ # and record the filename in the curve as well
12
+ unless path_or_io.respond_to?(:read)
13
+ curve = File.open(path_or_io, "r", &method(:parse))
14
+ curve.filename = File.basename(path_or_io)
15
+ return curve
16
+ end
17
+
18
+ @line_counter = 0 # IO#lineno is not exactly super-reliable
19
+ elements = []
20
+ until path_or_io.eof?
21
+ str = path_or_io.gets("\n")
22
+ @line_counter += 1
23
+
24
+ str = str.strip
25
+ item = if str =~ COMMENT
26
+ extract_comment(str)
27
+ elsif str =~ CORRELATION_RECORD
28
+ extract_tuple(str)
29
+ else
30
+ raise Framecurve::Malformed, "Malformed line #{str.inspect} at offset #{path_or_io.pos}, line #{@line_counter}"
31
+ end
32
+ elements.push(item)
33
+ end
34
+
35
+ return Framecurve::Curve.new(elements)
36
+ end
37
+
38
+ private
39
+ def extract_comment(line)
40
+ comment_txt = line.scan(COMMENT).flatten[0].strip
41
+ Framecurve::Comment.new(comment_txt)
42
+ end
43
+
44
+ def extract_tuple(line)
45
+ slots = line.scan(CORRELATION_RECORD).flatten
46
+ Framecurve::Tuple.new(slots[0].to_i, slots[1].to_f)
47
+ end
48
+
49
+ end
@@ -0,0 +1,13 @@
1
+ # Writes out a Curve object to the passed IO
2
+ class Framecurve::Serializer
3
+ # Serialize the passed curve into io. Will use the materialized curve version.
4
+ # Will write the file with CRLF linebreaks instead of LF
5
+ def serialize(io, curve)
6
+ io.write("# http://framecurve.org/specification-v1\n")
7
+ io.write("# at_frame\tuse_frame_of_source\n")
8
+ curve.to_materialized_curve.each_tuple do | t |
9
+ io.write(t)
10
+ io.write("\r\n")
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,20 @@
1
+ # Represents one Framecurve frame correlation record
2
+ class Framecurve::Tuple < Struct.new(:at, :value)
3
+ include Comparable
4
+
5
+ def tuple?
6
+ true
7
+ end
8
+
9
+ def comment?
10
+ false
11
+ end
12
+
13
+ def to_s
14
+ "%d\t%.5f" % [at, value]
15
+ end
16
+
17
+ def <=>(another)
18
+ to_s <=> another.to_s
19
+ end
20
+ end
@@ -0,0 +1,94 @@
1
+ # Validates a Curve object for well-formedness and completeness.
2
+ # v = Validator.new
3
+ # v.parse(io_handle)
4
+ # v.errors => []
5
+ # v.warnings => ["Do not put cusswords in your framecurves"]
6
+ class Framecurve::Validator
7
+ attr_reader :warnings, :errors
8
+
9
+ def initialize
10
+ @warnings, @errors = [], []
11
+ end
12
+
13
+ def any_errors?
14
+ @errors.any?
15
+ end
16
+
17
+ def any_warnings?
18
+ @warnings.any?
19
+ end
20
+
21
+ # Parse and validate a file (API similar to Parser#parse)
22
+ def parse_and_validate(path_or_io)
23
+ begin
24
+ validate(Framecurve::Parser.new.parse(path_or_io))
25
+ rescue Framecurve::Malformed => e
26
+ @errors.push(e.message)
27
+ end
28
+ end
29
+
30
+ # Validate a passed Curve object
31
+ def validate(curve)
32
+ initialize # reset
33
+ methods_matching(/^(verify|recommend)/).each do | method_name |
34
+ method(method_name).call(curve)
35
+ end
36
+ end
37
+
38
+ private
39
+
40
+ def methods_matching(pattern)
41
+ private_methods.select { |m| m.to_s =~ pattern }
42
+ end
43
+
44
+ def verify_at_least_one_line(curve)
45
+ @errors.push("The framecurve did not contain any lines at all") if curve.empty?
46
+ end
47
+
48
+ def verify_at_least_one_tuple(curve)
49
+ first_tuple = curve.find{|e| e.tuple? }
50
+ @errors.push("The framecurve did not contain any frame correlation records") unless first_tuple
51
+ end
52
+
53
+ def verify_proper_sequencing(curve)
54
+ tuples = curve.select{|e| e.tuple? }
55
+ frame_numbers = tuples.map{|t| t.at }
56
+ proper_sequence = frame_numbers.sort
57
+
58
+ unless frame_numbers == proper_sequence
59
+ @errors.push("The frame sequencing is out of order " +
60
+ "(expected #{proper_sequence.inspect} but got #{frame_numbers.inspect})." +
61
+ " The framecurve spec mandates that frames are recorded sequentially")
62
+ end
63
+ end
64
+
65
+ def verify_file_naming(curve)
66
+ return unless curve.respond_to?(:filename) && curve.filename
67
+ unless curve.filename =~ /\.framecurve\.txt$/
68
+ @errors.push("The framecurve file has to have the .framecurve.txt double extension, but had %s" % File.extname(curve.filename).inspect)
69
+ end
70
+ end
71
+
72
+ def verify_no_duplicate_records(curve)
73
+ detected_dupes = []
74
+ curve.each do | t |
75
+ next unless t.tuple?
76
+ next if detected_dupes.include?(t.at)
77
+ elements = curve.select{|e| e.tuple? && e.at == t.at }
78
+ if elements.length > 1
79
+ detected_dupes.push(t.at)
80
+ @errors.push("The framecurve contains the same frame (%d) twice or more (%d times)" % [t.at, elements.length])
81
+ end
82
+ end
83
+ end
84
+
85
+ def recommend_proper_preamble(curve)
86
+ first_comments = curve.map do | e |
87
+ break unless e.comment?
88
+ e
89
+ end
90
+ end
91
+
92
+ def recommend_proper_column_headers(curve)
93
+ end
94
+ end
@@ -0,0 +1,7 @@
1
+ require 'test/unit'
2
+ require 'stringio'
3
+
4
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
5
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
6
+ require 'framecurve'
7
+ require 'cli_test'
@@ -0,0 +1,19 @@
1
+ require 'helper'
2
+
3
+ class TestFramecurveComment < Test::Unit::TestCase
4
+ def test_not_tuple
5
+ assert Framecurve::Comment.new.comment?
6
+ assert !Framecurve::Comment.new.tuple?
7
+ end
8
+
9
+ def test_initialization
10
+ c = Framecurve::Comment.new("Very interesting \r\n comment")
11
+ assert_equal "Very interesting \r\n comment", c.text
12
+ assert_equal "# Very interesting comment", c.to_s
13
+ end
14
+
15
+ def test_initialization_with_nil_produces_usable_to_s
16
+ c = Framecurve::Comment.new(nil)
17
+ assert_equal "# ", c.to_s
18
+ end
19
+ end
@@ -0,0 +1,107 @@
1
+ require 'helper'
2
+
3
+ class TestFramecurveCurve < Test::Unit::TestCase
4
+ def test_init_with_empty_array
5
+ c = Framecurve::Curve.new([])
6
+ assert c.empty?
7
+ assert !c.any?
8
+ end
9
+
10
+ def test_init_with_no_arguments
11
+ c = Framecurve::Curve.new
12
+ assert c.empty?
13
+ assert !c.any?
14
+ end
15
+
16
+ def test_init_with_one_tuple
17
+ c = Framecurve::Curve.new( Framecurve::Tuple.new(10, 123))
18
+ assert !c.empty?
19
+ assert_equal 1, c.length
20
+ items = c.to_a
21
+ assert_equal [Framecurve::Tuple.new(10, 123)], c.to_a
22
+ end
23
+
24
+ def test_each_tuple
25
+ c = Framecurve::Curve.new( Framecurve::Comment.new("Welcome"), Framecurve::Tuple.new(10, 123))
26
+ assert !c.empty?
27
+ assert_equal 2, c.length
28
+ tuples = []
29
+ c.each_tuple(&tuples.method(:push))
30
+ assert_equal [Framecurve::Tuple.new(10, 123)], tuples
31
+ end
32
+
33
+ def test_only_tuples
34
+ c = Framecurve::Curve.new( Framecurve::Comment.new("Welcome"), Framecurve::Tuple.new(10, 123))
35
+ assert !c.empty?
36
+ tuples = c.only_tuples
37
+ assert_equal [Framecurve::Tuple.new(10, 123)], tuples
38
+ end
39
+
40
+ def test_each_comment
41
+ c = Framecurve::Curve.new( Framecurve::Comment.new("Welcome"), Framecurve::Tuple.new(10, 123))
42
+ assert !c.empty?
43
+ comments = []
44
+ c.each_comment(&comments.method(:push))
45
+ assert_equal [Framecurve::Comment.new("Welcome")], comments
46
+ end
47
+
48
+ def test_subscript
49
+ c = Framecurve::Curve.new( Framecurve::Comment.new("Welcome"), Framecurve::Tuple.new(10, 123))
50
+ assert_equal Framecurve::Comment.new("Welcome"), c[0]
51
+ assert_equal Framecurve::Tuple.new(10, 123), c[1]
52
+ end
53
+
54
+ def test_comment!
55
+ c = Framecurve::Curve.new
56
+ c.comment!("Also")
57
+ assert !c.empty?
58
+ assert_equal Framecurve::Comment.new("Also"), c[0]
59
+ end
60
+
61
+ def test_tuple!
62
+ c = Framecurve::Curve.new
63
+ c.tuple!(10, 123.45)
64
+ assert !c.empty?
65
+ assert_equal Framecurve::Tuple.new(10, 123.45), c[0]
66
+ end
67
+
68
+ def test_raises_malformed_with_tuple_at_duplicate_frame
69
+ c = Framecurve::Curve.new
70
+ c.tuple!(10, 123.45)
71
+ assert_raise(Framecurve::Malformed) do
72
+ c.tuple!(10, 456.45)
73
+ end
74
+ end
75
+
76
+ def test_filaneme_accessor
77
+ c = Framecurve::Curve.new
78
+ assert_nil c.filename
79
+ c.filename = "foo.framecurve.txt"
80
+ assert_equal "foo.framecurve.txt", c.filename
81
+ end
82
+
83
+ def test_teach_defined_tuple
84
+ c = Framecurve::Curve.new(Framecurve::Tuple.new(1, 123.45), Framecurve::Tuple.new(10, 567.89))
85
+ concrete = []
86
+ c.each_defined_tuple(&concrete.method(:push))
87
+ ref = [
88
+ Framecurve::Tuple.new(1, 123.450000),
89
+ Framecurve::Tuple.new(2, 172.83222222222224),
90
+ Framecurve::Tuple.new(3, 222.21444444444444),
91
+ Framecurve::Tuple.new(4, 271.5966666666667),
92
+ Framecurve::Tuple.new(5, 320.9788888888889),
93
+ Framecurve::Tuple.new(6, 370.36111111111114),
94
+ Framecurve::Tuple.new(7, 419.74333333333334),
95
+ Framecurve::Tuple.new(8, 469.12555555555554),
96
+ Framecurve::Tuple.new(9, 518.5077777777778),
97
+ Framecurve::Tuple.new(10, 567.89),
98
+ ]
99
+ assert_equal ref, concrete
100
+ end
101
+
102
+ def test_materialized_curve
103
+ c = Framecurve::Curve.new(Framecurve::Tuple.new(1, 123.45), Framecurve::Tuple.new(10, 567.89))
104
+ materialized = c.to_materialized_curve
105
+ assert_equal 12, materialized.length
106
+ end
107
+ end
@@ -0,0 +1,57 @@
1
+ require 'helper'
2
+
3
+ class TestFramecurveParser < Test::Unit::TestCase
4
+ def test_parser
5
+ data = ["# Framecurve data", "10\t1293.12", "#Some useful info", "10\t145"].join("\r\n")
6
+ p = Framecurve::Parser.new
7
+ elements = p.parse(StringIO.new(data))
8
+ assert_kind_of Framecurve::Curve, elements
9
+
10
+ assert_equal 4, elements.length
11
+
12
+ assert_kind_of Framecurve::Comment, elements[0]
13
+ assert_kind_of Framecurve::Tuple, elements[1]
14
+ assert_kind_of Framecurve::Comment, elements[2]
15
+ assert_kind_of Framecurve::Tuple, elements[3]
16
+
17
+ assert_equal "Framecurve data", elements[0].text
18
+ assert_equal Framecurve::Tuple.new(10, 1293.12), elements[1]
19
+ assert_equal "Some useful info", elements[2].text
20
+ end
21
+
22
+ def test_parse_with_neg_source_frame
23
+ data = "10\t-1293.12"
24
+ elements = Framecurve::Parser.new.parse(StringIO.new(data))
25
+ assert_kind_of Framecurve::Curve, elements
26
+
27
+ assert_equal 1, elements.length
28
+ assert_kind_of Framecurve::Tuple, elements[0]
29
+ assert_equal Framecurve::Tuple.new(10, -1293.12), elements[0]
30
+ end
31
+
32
+ def test_parse_with_neg_dest_frame
33
+ data = "-123\t-1293.12"
34
+ elements = Framecurve::Parser.new.parse(StringIO.new(data))
35
+ assert_kind_of Framecurve::Curve, elements
36
+
37
+ assert_equal 1, elements.length
38
+ assert_kind_of Framecurve::Tuple, elements[0]
39
+ assert_equal Framecurve::Tuple.new(-123, -1293.12), elements[0]
40
+ end
41
+
42
+ def test_should_try_to_open_file_at_path_if_string_passed_to_parse
43
+ v = Framecurve::Parser.new
44
+ assert !File.exist?("/tmp/some_file.framecurve.txt")
45
+ assert_raise(Errno::ENOENT) do
46
+ v.parse("/tmp/some_file.framecurve.txt")
47
+ end
48
+ end
49
+
50
+ def test_parser_fails_on_malformed_lines
51
+ data = "Sachlich gesehen\nbambam"
52
+ assert_raise(Framecurve::Malformed) do
53
+ Framecurve::Parser.new.parse(StringIO.new(data))
54
+ end
55
+ end
56
+
57
+ end
@@ -0,0 +1,17 @@
1
+ require 'helper'
2
+
3
+ class TestFramecurveSerializer < Test::Unit::TestCase
4
+ def test_output
5
+ f = Framecurve::Curve.new(Framecurve::Tuple.new(10, 123))
6
+ s = StringIO.new
7
+ Framecurve::Serializer.new.serialize(s, f)
8
+ assert_equal "# http://framecurve.org/specification-v1\n# at_frame\tuse_frame_of_source\n10\t123.00000\r\n", s.string
9
+ end
10
+
11
+ def test_materializes_frames
12
+ f = Framecurve::Curve.new(Framecurve::Tuple.new(10, 123), Framecurve::Tuple.new(12, 456))
13
+ s = StringIO.new
14
+ Framecurve::Serializer.new.serialize(s, f)
15
+ assert_equal "# http://framecurve.org/specification-v1\n# at_frame\tuse_frame_of_source\n10\t123.00000\r\n11\t289.00000\r\n12\t456.00000\r\n", s.string
16
+ end
17
+ end
@@ -0,0 +1,24 @@
1
+ require 'helper'
2
+
3
+ class TestFramecurveTuple < Test::Unit::TestCase
4
+ def test_initialization
5
+ t = Framecurve::Tuple.new(123, 645.1458)
6
+ assert_equal 123, t.at
7
+ assert_equal 645.1458, t.value
8
+ assert_equal "123\t645.14580", t.to_s
9
+ end
10
+
11
+ def test_not_comment
12
+ assert !Framecurve::Tuple.new.comment?
13
+ assert Framecurve::Tuple.new.tuple?
14
+ end
15
+
16
+ def test_vlidates_equality_to_5_decimal_places
17
+ t = Framecurve::Tuple.new(1, 1234.567890)
18
+ t2 = Framecurve::Tuple.new(1, 1234.567891)
19
+ t3 = Framecurve::Tuple.new(1, 1234.567884)
20
+
21
+ assert_equal t, t2
22
+ assert_not_equal t, t3
23
+ end
24
+ end
@@ -0,0 +1,78 @@
1
+ require 'helper'
2
+
3
+ class TestFramecurveValidator < Test::Unit::TestCase
4
+
5
+ def test_should_error_out_with_malformed_input_to_parse_and_validate
6
+ v = Framecurve::Validator.new
7
+ io = StringIO.new("foobar")
8
+ v.parse_and_validate(io)
9
+ assert v.any_errors?
10
+ assert_equal ["Malformed line \"foobar\" at offset 6, line 1"], v.errors
11
+ end
12
+
13
+ def test_should_not_error_out_with_good_input_to_parse_and_validate
14
+ v = Framecurve::Validator.new
15
+ io = StringIO.new("# Nice framecurve\r\n1\t146.0")
16
+ v.parse_and_validate(io)
17
+ assert !v.any_errors?
18
+ end
19
+
20
+ def test_should_try_to_open_file_at_path_if_string_passed_to_parse_and_validate
21
+ v = Framecurve::Validator.new
22
+ assert_raise(Errno::ENOENT) do
23
+ v.parse_and_validate("/tmp/some_file.framecurve.txt")
24
+ end
25
+ end
26
+
27
+ def test_should_record_filename_error_with_improper_extension
28
+ File.open("wrong.extension", "wb"){|f| f.write("# This might have been\r\n1\t123.45") }
29
+ begin
30
+ v = Framecurve::Validator.new
31
+ v.parse_and_validate("wrong.extension")
32
+ assert v.any_errors?
33
+ assert_equal ["The framecurve file has to have the .framecurve.txt double extension, but had \".extension\""], v.errors
34
+ ensure
35
+ File.unlink("wrong.extension")
36
+ end
37
+ end
38
+
39
+ def test_should_init_with_empty_errors_and_warnings
40
+ v = Framecurve::Validator.new
41
+ assert !v.any_errors?
42
+ assert !v.any_warnings?
43
+ assert_equal [], v.errors
44
+ assert_equal [], v.warnings
45
+ end
46
+
47
+ def test_should_error_out_with_empty
48
+ v = Framecurve::Validator.new
49
+ v.validate([])
50
+ assert v.any_errors?
51
+ assert_equal ["The framecurve did not contain any lines at all",
52
+ "The framecurve did not contain any frame correlation records"], v.errors
53
+ end
54
+
55
+ def test_should_error_out_without_actual_tuples
56
+ c = Framecurve::Curve.new( Framecurve::Comment.new("Only text") )
57
+ v = Framecurve::Validator.new
58
+ v.validate(c)
59
+ assert v.any_errors?
60
+ assert_equal ["The framecurve did not contain any frame correlation records"], v.errors
61
+ end
62
+
63
+ def test_should_error_out_with_dupe_frames
64
+ c = Framecurve::Curve.new( Framecurve::Tuple.new(10, 123.4), Framecurve::Tuple.new(10, 123.4) )
65
+ v = Framecurve::Validator.new
66
+ v.validate(c)
67
+ assert v.any_errors?
68
+ assert_equal ["The framecurve contains the same frame (10) twice or more (2 times)"], v.errors
69
+ end
70
+
71
+ def test_should_error_out_with_improper_sequencing
72
+ c = Framecurve::Curve.new( Framecurve::Tuple.new(10, 123.4), Framecurve::Tuple.new(1, 123.4) )
73
+ v = Framecurve::Validator.new
74
+ v.validate(c)
75
+ assert v.any_errors?
76
+ assert_equal ["The frame sequencing is out of order (expected [1, 10] but got [10, 1]). The framecurve spec mandates that frames are recorded sequentially"], v.errors
77
+ end
78
+ end
@@ -0,0 +1,51 @@
1
+ require 'helper'
2
+ require "fileutils"
3
+
4
+ class TestFramecurveValidatorBinary < Test::Unit::TestCase
5
+ BINARY = File.expand_path(File.dirname(__FILE__) + "/../bin/framecurve_validator")
6
+ GOOD_FC_PATH = "simple.framecurve.txt"
7
+ BAD_FC_PATH = "crap"
8
+
9
+ # Run the binary under test with passed options, and return [exit_code, stdout_content, stderr_content]
10
+ def cli(commandline_arguments)
11
+ CLITest.new(BINARY).run(commandline_arguments)
12
+ end
13
+
14
+ def setup
15
+ File.open(GOOD_FC_PATH, "wb") do | f |
16
+ c = Framecurve::Curve.new(Framecurve::Tuple.new(10, 123.45))
17
+ Framecurve::Serializer.new.serialize(f, c)
18
+ end
19
+
20
+ File.open(BAD_FC_PATH, "wb") do | f |
21
+ f.write("This is gibberish")
22
+ end
23
+ end
24
+
25
+ def teardown
26
+ File.unlink(GOOD_FC_PATH) if File.exist?(GOOD_FC_PATH)
27
+ File.unlink(BAD_FC_PATH) if File.exist?(BAD_FC_PATH)
28
+ File.unlink(GOOD_FC_PATH + ".tmp") if File.exist?(GOOD_FC_PATH + ".tmp")
29
+ end
30
+
31
+ def test_cli_with_valid_file
32
+ s, o, e = cli(File.expand_path(GOOD_FC_PATH))
33
+ assert_equal 0, s
34
+ assert o.include?("OK!")
35
+ assert_equal '', e
36
+ end
37
+
38
+ def test_cli_with_bad_file
39
+ s, o, e = cli(File.expand_path(BAD_FC_PATH))
40
+ assert_equal 10, s, "The exit status on fail should not be 0"
41
+ assert e.include?("ERRORS")
42
+ end
43
+
44
+ def test_cli_with_bad_filename_extension
45
+ FileUtils.cp(GOOD_FC_PATH, GOOD_FC_PATH + ".tmp")
46
+ s, o, e = cli(File.expand_path(GOOD_FC_PATH + ".tmp"))
47
+ assert_equal 10, s, "The exit status on fail should not be 0"
48
+ assert e.include?("but had \".tmp\"")
49
+ end
50
+
51
+ end
metadata ADDED
@@ -0,0 +1,114 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: framecurve
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Julik
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-12-30 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: term-ansicolor
16
+ requirement: &10799030 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *10799030
25
+ - !ruby/object:Gem::Dependency
26
+ name: jeweler
27
+ requirement: &10798700 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: 1.6.4
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *10798700
36
+ - !ruby/object:Gem::Dependency
37
+ name: rake
38
+ requirement: &10798440 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *10798440
47
+ - !ruby/object:Gem::Dependency
48
+ name: cli_test
49
+ requirement: &10798170 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *10798170
58
+ description: ! ' Parser, validation and interpolation'
59
+ email: me@julik.nl
60
+ executables:
61
+ - framecurve_validator
62
+ extensions: []
63
+ extra_rdoc_files:
64
+ - LICENSE.txt
65
+ - README.rdoc
66
+ files:
67
+ - .document
68
+ - Gemfile
69
+ - LICENSE.txt
70
+ - README.rdoc
71
+ - Rakefile
72
+ - bin/framecurve_validator
73
+ - framecurve.gemspec
74
+ - lib/framecurve.rb
75
+ - lib/framecurve/comment.rb
76
+ - lib/framecurve/curve.rb
77
+ - lib/framecurve/parser.rb
78
+ - lib/framecurve/serializer.rb
79
+ - lib/framecurve/tuple.rb
80
+ - lib/framecurve/validator.rb
81
+ - test/helper.rb
82
+ - test/test_framecurve_comment.rb
83
+ - test/test_framecurve_curve.rb
84
+ - test/test_framecurve_parser.rb
85
+ - test/test_framecurve_serializer.rb
86
+ - test/test_framecurve_tuple.rb
87
+ - test/test_framecurve_validator.rb
88
+ - test/test_framecurve_validator_binary.rb
89
+ homepage: http://github.com/guerilla-di/framecurve
90
+ licenses:
91
+ - MIT
92
+ post_install_message:
93
+ rdoc_options: []
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ none: false
104
+ requirements:
105
+ - - ! '>='
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ requirements: []
109
+ rubyforge_project:
110
+ rubygems_version: 1.8.11
111
+ signing_key:
112
+ specification_version: 3
113
+ summary: Handles Framecurve files
114
+ test_files: []