Browse files

Switch to returning hashes instead of a list.

  • Loading branch information...
1 parent 700e89a commit bf877c423d1a58fdf9be9e00a66baa49d7e12c5d Matt Colyer committed Mar 4, 2011
View
5 lib/fontvariationdescription.rb
@@ -72,10 +72,9 @@ def self.expand(input)
# Examples
#
# FontVariationDescription.parse("i7")
- # # => ["italic", "700"]
+ # # => { "font-style" => "italic", "font-weight" => "700"}
#
- # Returns the a List of Strings in the order defined by the Font
- # Variation Description String.
+ # Returns the a Hash of CSS properties and their values.
def self.parse(input)
@parser ||= Parser.new
@parser.parse(input)
View
6 lib/fontvariationdescription/item.rb
@@ -12,12 +12,12 @@ def compact(output, value)
output[@index] = match[0] if match
end
- def expand(output, value, include_property=true)
+ def expand(output, value, join_property=true)
match = @values.find { |v| value == v[0] }
- if include_property
+ if join_property
output[@index] = [@property, match[1]].join(':') if match
else
- output[@index] = match[1] if match
+ output[@index] = [@property, match[1]] if match
end
end
View
2 lib/fontvariationdescription/parser.rb
@@ -18,7 +18,7 @@ def parse(input)
item.expand(result, key, false)
}
- result if result.all?
+ Hash[result] if result.all?
end
end
View
51 parser.yml
@@ -1,59 +1,83 @@
# This file describes a set of test cases for parsing a Font Variation
-# Description into a set of @font-face descriptors.
+# Description into a Ruby hash.
tests:
# font-style
- description: "n4"
fvd: "n4"
- output: ["normal", "400"]
+ output:
+ font-style: normal
+ font-weight: "400"
- description: "i4"
fvd: "i4"
- output: ["italic", "400"]
+ output:
+ font-style: italic
+ font-weight: "400"
- description: "o4"
fvd: "o4"
- output: ["oblique", "400"]
+ output:
+ font-style: oblique
+ font-weight: "400"
# font-weight
- description: "n1"
fvd: "n1"
- output: ["normal", "100"]
+ output:
+ font-style: normal
+ font-weight: "100"
- description: "n2"
fvd: "n2"
- output: ["normal", "200"]
+ output:
+ font-style: normal
+ font-weight: "200"
- description: "n3"
fvd: "n3"
- output: ["normal", "300"]
+ output:
+ font-style: normal
+ font-weight: "300"
- description: "n4"
fvd: "n4"
- output: ["normal", "400"]
+ output:
+ font-style: normal
+ font-weight: "400"
- description: "n5"
fvd: "n5"
- output: ["normal", "500"]
+ output:
+ font-style: normal
+ font-weight: "500"
- description: "n6"
fvd: "n6"
- output: ["normal", "600"]
+ output:
+ font-style: normal
+ font-weight: "600"
- description: "n7"
fvd: "n7"
- output: ["normal", "700"]
+ output:
+ font-style: normal
+ font-weight: "700"
- description: "n8"
fvd: "n8"
- output: ["normal", "800"]
+ output:
+ font-style: normal
+ font-weight: "800"
- description: "n9"
fvd: "n9"
- output: ["normal", "900"]
+ output:
+ font-style: normal
+ font-weight: "900"
# invalid description
@@ -80,3 +104,4 @@ tests:
- description: "'n1x' is null because there are more than two inputs"
fvd: "n1x"
output: ~
+
View
2 test/font_variation_description_test.rb
@@ -11,7 +11,7 @@ def test_expand
end
def test_parse
- assert_equal ['italic', '700'], FontVariationDescription.parse('i7')
+ assert_equal({ "font-style" => 'italic', "font-weight" => '700' }, FontVariationDescription.parse('i7'))
end
end
View
8 test/parser_test.rb
@@ -1,15 +1,15 @@
require File.dirname(__FILE__) + '/test_helper'
-class ExpanderTest < Test::Unit::TestCase
+class ParserTest < Test::Unit::TestCase
def setup
- @expander = FontVariationDescription::Parser.new
+ @parser = FontVariationDescription::Parser.new
end
- # See expand.yml for test cases.
+ # See parser.yml for test cases.
FontVariationDescription::Test.parser_test.each_with_index do |(output, fvd, description), index|
define_method "test_#{index}_#{description.gsub(/\W/, '')}" do
- assert_equal @expander.parse(fvd), output, description
+ assert_equal @parser.parse(fvd), output, description
end
end

0 comments on commit bf877c4

Please sign in to comment.