Permalink
Browse files

Add FontVariationDescription.parse for Ruby usage.

  • Loading branch information...
1 parent e8ffe56 commit cf4ac21537596e595927bb3952a1e555cb49ca37 Matt Colyer committed Mar 4, 2011
@@ -1,6 +1,7 @@
require File.dirname(__FILE__) + '/fontvariationdescription/item'
require File.dirname(__FILE__) + '/fontvariationdescription/compactor'
require File.dirname(__FILE__) + '/fontvariationdescription/expander'
+require File.dirname(__FILE__) + '/fontvariationdescription/parser'
module FontVariationDescription
VERSION = '0.9.0'
@@ -63,4 +64,19 @@ def self.expand(input)
@expander.parse(input)
end
-end
+ # Public: Transform a Font Variation Description into a List of its
+ # parts.
+ #
+ # input - A Font Variation Description String.
+ #
+ # Examples
+ #
+ # FontVariationDescription.parse("i7")
+ # # => ["italic", "700"]
+ #
+ # Returns the CSS @font-face descriptor String.
+ def self.parse(input)
+ @parser ||= Parser.new
+ @parser.parse(input)
+ end
+end
@@ -12,10 +12,14 @@ def compact(output, value)
output[@index] = match[0] if match
end
- def expand(output, value)
+ def expand(output, value, include_property=true)
match = @values.find { |v| value == v[0] }
- output[@index] = [@property, match[1]].join(':') if match
+ if include_property
+ output[@index] = [@property, match[1]].join(':') if match
+ else
+ output[@index] = match[1] if match
+ end
end
end
-end
+end
@@ -0,0 +1,25 @@
+module FontVariationDescription
+ class Parser
+
+ def initialize(properties=PROPERTIES, values=VALUES)
+ @properties = properties
+ @values = values
+ end
+
+ def parse(input)
+ return nil unless input && input.size == 2
+
+ result = [nil, nil]
+
+ @properties.each_with_index { |property, index|
+ key = input[index, 1]
+ values = @values[property]
+ item = Item.new(index, property, values)
+ item.expand(result, key, false)
+ }
+
+ result if result.all?
+ end
+
+ end
+end
@@ -18,5 +18,12 @@ def expand_tests
}
end
+ def parser_test
+ data = YAML.load_file(File.dirname(__FILE__) + "/../../parser.yml")
+ data['tests'].map { |test|
+ [test['output'], test['fvd'], test['description']]
+ }
+ end
+
end
-end
+end
View
@@ -0,0 +1,82 @@
+# This file describes a set of test cases for parsing a Font Variation
+# Description into a set of @font-face descriptors.
+
+tests:
+
+ # font-style
+
+ - description: "n4"
+ fvd: "n4"
+ output: ["normal", "400"]
+
+ - description: "i4"
+ fvd: "i4"
+ output: ["italic", "400"]
+
+ - description: "o4"
+ fvd: "o4"
+ output: ["oblique", "400"]
+
+ # font-weight
+
+ - description: "n1"
+ fvd: "n1"
+ output: ["normal", "100"]
+
+ - description: "n2"
+ fvd: "n2"
+ output: ["normal", "200"]
+
+ - description: "n3"
+ fvd: "n3"
+ output: ["normal", "300"]
+
+ - description: "n4"
+ fvd: "n4"
+ output: ["normal", "400"]
+
+ - description: "n5"
+ fvd: "n5"
+ output: ["normal", "500"]
+
+ - description: "n6"
+ fvd: "n6"
+ output: ["normal", "600"]
+
+ - description: "n7"
+ fvd: "n7"
+ output: ["normal", "700"]
+
+ - description: "n8"
+ fvd: "n8"
+ output: ["normal", "800"]
+
+ - description: "n9"
+ fvd: "n9"
+ output: ["normal", "900"]
+
+ # invalid description
+
+ - description: "'' is null because it's empty"
+ fvd: ""
+ output: ~
+
+ - description: "'x0' is null because the style is invalid"
+ fvd: "x0"
+ output: ~
+
+ - description: "'n0' is null because the weight is invalid"
+ fvd: "n0"
+ output: ~
+
+ - description: "'n' is null because there is only one input"
+ fvd: "n"
+ output: ~
+
+ - description: "'1' is null because there is only one input"
+ fvd: "1"
+ output: ~
+
+ - description: "'n1x' is null because there are more than two inputs"
+ fvd: "n1x"
+ output: ~
@@ -10,4 +10,8 @@ def test_expand
assert_equal 'font-style:italic;font-weight:700;', FontVariationDescription.expand('i7')
end
-end
+ def test_parse
+ assert_equal ['italic', '700'], FontVariationDescription.parse('i7')
+ end
+
+end
View
@@ -0,0 +1,16 @@
+require File.dirname(__FILE__) + '/test_helper'
+
+class ExpanderTest < Test::Unit::TestCase
+
+ def setup
+ @expander = FontVariationDescription::Parser.new
+ end
+
+ # See expand.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
+ end
+ end
+
+end

0 comments on commit cf4ac21

Please sign in to comment.